Skip to content

Commit f80130b

Browse files
committed
improve sidebar drag
1 parent 1e9d52e commit f80130b

File tree

1 file changed

+26
-19
lines changed

1 file changed

+26
-19
lines changed

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

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
import {
77
vw,
88
sanitizeSidebarWidth,
9+
getRemInPixel,
910
} from "src/util.js"
1011
import {
1112
useLayoutStore,
@@ -15,45 +16,51 @@ import {
1516
export const SideBar = ({page, children}) => {
1617
let dragging = useViewStateStore(state => state.dragging)
1718
let setDragging = useViewStateStore(state => state.setDragging)
19+
let dragOffset = useRef(0)
1820
let sidebarWidth = useLayoutStore(state => state.sidebarWidth[page])
21+
let sidebarWidthRef = useRef()
22+
sidebarWidthRef.current = sidebarWidth
1923
let setSidebarWidth = useLayoutStore(state => state.setSidebarWidth)
2024
let draggingRef = useRef()
21-
let ghostWidthRef = useRef()
2225
draggingRef.current = dragging
23-
ghostWidthRef.current = sidebarWidth
26+
2427
useEffect(() => {
25-
let mousemove = (e) => {
28+
let onMouseMove = (e) => {
2629
if (!draggingRef.current) {
2730
return
2831
}
29-
let width = sanitizeSidebarWidth(vw() - e.clientX)
32+
let offset = dragOffset.current
33+
let width = sanitizeSidebarWidth(vw() - e.clientX + offset)
3034
setSidebarWidth(page, width)
3135
}
32-
let mouseup = () => setDragging(false)
33-
window.document.addEventListener("mousemove", mousemove)
34-
window.document.addEventListener("mouseup", mouseup)
36+
let onMouseUp = () => setDragging(false)
37+
window.document.addEventListener("mousemove", onMouseMove)
38+
window.document.addEventListener("mouseup", onMouseUp)
3539
return () => {
36-
window.document.removeEventListener("mousemove", mousemove)
37-
window.document.removeEventListener("mouseup", mouseup)
40+
window.document.removeEventListener("mousemove", onMouseMove)
41+
window.document.removeEventListener("mouseup", onMouseUp)
3842
}
39-
}, [page, draggingRef, setDragging, setSidebarWidth])
43+
}, [page, setDragging, setSidebarWidth])
44+
4045
let onMouseDown = useCallback((e) => {
4146
e.preventDefault()
4247
setDragging(true)
48+
let sidebarWidth = sidebarWidthRef.current
49+
dragOffset.current = sidebarWidth + e.clientX - vw()
4350
}, [setDragging])
51+
52+
let ghostWidth = Math.trunc(getRemInPixel() * 0.5) + 2
4453
return (
4554
<div
4655
style={{width: sidebarWidth + "px"}}
47-
className="fixed top-0 right-0 h-full bg-slate-800">
56+
className="absolute border-l border-gray-500 top-0 right-0 h-full bg-slate-800">
4857
<div
49-
onMouseDown={onMouseDown}
50-
style={{right: sidebarWidth + "px"}}
51-
className="fixed top-0 w-[3px] h-full bg-slate-700 z-10 cursor-col-resize" />
52-
{dragging && (
53-
<div
54-
style={{right: sidebarWidth + "px"}}
55-
className="fixed top-0 w-[3px] h-full bg-slate-600 z-20" />
56-
)}
58+
onMouseDown={!dragging ? onMouseDown : undefined}
59+
style={{
60+
right: (sidebarWidth - ghostWidth + 1),
61+
width: Math.trunc(getRemInPixel() * 0.5) + 2,
62+
}}
63+
className="absolute top-0 h-full bg-transparent z-20 cursor-col-resize" />
5764
{children}
5865
</div>
5966
)

0 commit comments

Comments
 (0)