Skip to content

Commit 80d44e0

Browse files
committed
fix sidebar resize
1 parent 8fd1c42 commit 80d44e0

File tree

7 files changed

+42
-35
lines changed

7 files changed

+42
-35
lines changed

src/main/client/src/Router.jsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ import {
3434
StompContext,
3535
} from "src/util.js"
3636
import {
37-
useLayoutStore,
37+
useViewStateStore,
3838
} from "src/layout.js"
3939

4040
export const Router = createBrowserRouter(
@@ -63,7 +63,7 @@ export const Router = createBrowserRouter(
6363
)
6464

6565
function Frame() {
66-
let dragging = useLayoutStore(state => state.dragging)
66+
let dragging = useViewStateStore(state => state.dragging)
6767
return <>
6868
<div className={twJoin(
6969
"h-full",

src/main/client/src/component/Chat.jsx

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,17 @@ import {
1111
import {
1212
useAuthStore,
1313
} from "src/store.js"
14+
import {
15+
useViewStateStore,
16+
} from "src/layout.js"
1417
import {
1518
StompContext,
1619
tfetch,
1720
doTry,
1821
getRemInPixel,
1922
} from "src/util.js"
2023

21-
export const Chat = ({chatId}) => {
24+
export const Chat = ({chatId, className}) => {
2225
let [messages, setMessages] = useState([])
2326
let [users, setUsers] = useState([])
2427
let messageRef = useRef()
@@ -90,6 +93,7 @@ export const Chat = ({chatId}) => {
9093

9194
return <>
9295
<SplitPane
96+
className={className}
9397
messageRef={messageRef}
9498
topElement={<>
9599
{users.map(user => (
@@ -104,7 +108,7 @@ export const Chat = ({chatId}) => {
104108
/>
105109
<form className="flex-none mb-2" onSubmit={onSendMessage}>
106110
<input
107-
className="w-full px-1 py-2 border border-gray-500 bg-stone-800 text-stone-100 focus:outline-none"
111+
className="w-full px-1 py-1 border border-gray-500 bg-stone-800 text-stone-100 focus:outline-none"
108112
type="text"
109113
name="message"
110114
/>
@@ -123,12 +127,13 @@ function isLastChildVisible(container) {
123127
return lastChildTop < containerBottom
124128
}
125129

126-
function SplitPane({messageRef, topElement, bottomElement}) {
130+
function SplitPane({className, messageRef, topElement, bottomElement}) {
127131
let [dragOffset, setDragOffset] = useState(Number.NaN)
128132
let [splitPos, setSplitPos] = useState(200)
129133
let splitPosRef = useRef()
130134
splitPosRef.current = splitPos
131135
let containerRef = useRef()
136+
132137
useEffect(() => {
133138
let onMouseMove = (e) => {
134139
if (Number.isNaN(dragOffset)) {
@@ -166,48 +171,58 @@ function SplitPane({messageRef, topElement, bottomElement}) {
166171
containerRef.current = ref
167172
}}
168173
className={twJoin(
169-
"grow border-t border-x border-gray-500 bg-gray-900 flex flex-col overflow-y-hidden",
174+
"grow flex flex-col gap-y-2 overflow-y-hidden",
170175
!Number.isNaN(dragOffset) && "cursor-row-resize",
176+
className,
171177
)}>
172178
<div
173179
style={{height: topElementHeight + "px"}}
174-
className="px-1 pt-1 pb-2 flex-none overflow-y-scroll">
180+
className="p-1 bg-gray-900 border border-gray-500 flex-none overflow-y-scroll">
175181
{topElement}
176182
</div>
177183
<SplitBar
178184
container={containerRef.current}
179185
splitPos={splitPos}
180186
dragOffset={dragOffset}
181187
onMouseDown={onMouseDown} />
182-
<div ref={messageRef} className="px-1 pt-3 pb-1 overflow-y-scroll">
188+
<div ref={messageRef} className="p-1 bg-gray-900 border-t border-x border-gray-500 h-full overflow-y-scroll">
183189
{bottomElement}
184190
</div>
185191
</div>
186192
)
187193
}
188194

189195
function SplitBar({splitPos, dragOffset, onMouseDown, container}) {
196+
let dragging = useViewStateStore(state => state.dragging)
190197
if (!container) {
191198
return <div />
192199
}
193200
let innerHeight = Math.trunc(getRemInPixel() * 0.5)
194201
let rect = container.getBoundingClientRect()
195-
let parentRect = container.parentNode.getBoundingClientRect()
196-
let width = rect.width
202+
let parentRect = container.offsetParent.getBoundingClientRect()
203+
let width = dragging ? 2 * rect.width : rect.width
197204
let left = rect.left - parentRect.left
205+
let lineClass = twJoin(
206+
"absolute h-[1px] z-20 cursor-row-resize",
207+
"bg-transparent",
208+
)
209+
let barClass = twJoin(
210+
"absolute z-20 cursor-row-resize",
211+
"bg-transparent",
212+
)
198213
return <>
199214
<div
200215
onMouseDown={Number.isNaN(dragOffset) ? onMouseDown : undefined}
201-
style={{top: Math.trunc(splitPos - 1), height: 1, width: width, left: left}}
202-
className="absolute h-[1px] bg-gray-500 z-20 cursor-row-resize" />
216+
style={{top: Math.trunc(splitPos), height: 1, width: width, left: left}}
217+
className={lineClass} />
203218
<div
204219
onMouseDown={Number.isNaN(dragOffset) ? onMouseDown : undefined}
205-
style={{top: Math.trunc(splitPos), height: innerHeight, width: width, left: left}}
206-
className="absolute bg-slate-800 z-20 cursor-row-resize" />
220+
style={{top: Math.trunc(splitPos + 1), height: innerHeight, width: width, left: left}}
221+
className={barClass} />
207222
<div
208223
onMouseDown={Number.isNaN(dragOffset) ? onMouseDown : undefined}
209-
style={{top: Math.trunc(splitPos + innerHeight), height: 1, width: width, left: left}}
210-
className="absolute h-[1px] bg-gray-500 z-20 cursor-row-resize" />
224+
style={{top: Math.trunc(splitPos + innerHeight + 1), height: 1, width: width, left: left}}
225+
className={lineClass} />
211226
</>
212227
}
213228

src/main/client/src/component/SideBar.jsx

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import {
22
useRef,
33
useEffect,
4-
useState,
54
useCallback,
65
} from "react"
76
import {
@@ -18,28 +17,19 @@ export const SideBar = ({page, children}) => {
1817
let setDragging = useViewStateStore(state => state.setDragging)
1918
let sidebarWidth = useLayoutStore(state => state.sidebarWidth[page])
2019
let setSidebarWidth = useLayoutStore(state => state.setSidebarWidth)
21-
let [ghostWidth, setGhostWidth] = useState(sidebarWidth)
2220
let draggingRef = useRef()
2321
let ghostWidthRef = useRef()
2422
draggingRef.current = dragging
25-
ghostWidthRef.current = ghostWidth
23+
ghostWidthRef.current = sidebarWidth
2624
useEffect(() => {
2725
let mousemove = (e) => {
2826
if (!draggingRef.current) {
2927
return
3028
}
3129
let width = sanitizeSidebarWidth(vw() - e.clientX)
32-
setGhostWidth(width)
33-
}
34-
let mouseup = () => {
35-
if (!draggingRef.current) {
36-
return
37-
}
38-
let width = sanitizeSidebarWidth(ghostWidthRef.current)
39-
setSidebarWidth(page, Math.trunc(width))
40-
setGhostWidth(Math.trunc(width))
41-
setDragging(false)
30+
setSidebarWidth(page, width)
4231
}
32+
let mouseup = () => setDragging(false)
4333
window.document.addEventListener("mousemove", mousemove)
4434
window.document.addEventListener("mouseup", mouseup)
4535
return () => {
@@ -61,7 +51,7 @@ export const SideBar = ({page, children}) => {
6151
className="fixed top-0 w-[3px] h-full bg-slate-700 z-10 cursor-col-resize" />
6252
{dragging && (
6353
<div
64-
style={{right: ghostWidth + "px"}}
54+
style={{right: sidebarWidth + "px"}}
6555
className="fixed top-0 w-[3px] h-full bg-slate-600 z-20" />
6656
)}
6757
{children}

src/main/client/src/feature/game/GamePanel.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ function Panel() {
176176
{(result.w > result.b ? "W+" : "B+") + Math.abs(result.b - result.w)}
177177
</div>
178178
)}
179-
<Chat chatId={gameId}/>
179+
<Chat className="mt-1" chatId={gameId}/>
180180
</>
181181
)
182182
}

src/main/client/src/feature/lobby/LobbyPanel.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,6 @@ function Panel() {
3838
return undefined
3939
}, [auth])
4040
return <>
41-
<Chat chatId="Lobby"/>
41+
<Chat chatId="Lobby" />
4242
</>
4343
}

src/main/client/src/layout.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,9 @@ import {
99
} from "zustand/middleware"
1010
import {
1111
getRemInPixel,
12+
DELTA,
1213
} from "src/util.js"
1314

14-
const TINY = 0.0001220703125
15-
1615
export const useViewStateStore = create((set, get) => ({
1716
zoom: 0,
1817
dragging: false,
@@ -26,7 +25,7 @@ export const useViewStateStore = create((set, get) => ({
2625
if (Math.trunc(zoom)) {
2726
state.zoom = zoom
2827
} else {
29-
state.zoom = get().zoom ? 0 : TINY // force re-render
28+
state.zoom = get().zoom ? 0 : DELTA // force re-render
3029
}
3130
}))
3231
},

src/main/client/src/util.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ export const ANY_REMOVED = REMOVED_W | REMOVED_B
2323

2424
export const COLORS = BLACK | WHITE
2525

26+
// some number that is small, yet big enough for adding to typical pixel values like 100 or 1000
27+
export const DELTA = 0.0001220703125
28+
2629
export async function tfetch(url, options) {
2730
let response
2831
try {

0 commit comments

Comments
 (0)