Merge pull request #459 from msabramo/resizable-left-sidebar
Resizable left sidebar
This commit is contained in:
commit
a2d2cf6f3d
1 changed files with 114 additions and 28 deletions
|
|
@ -51,3 +51,55 @@ export function useDraggablePane(initialHeight: number) {
|
|||
handleDragStart,
|
||||
};
|
||||
}
|
||||
|
||||
export function useDraggableSidebar(initialWidth: number) {
|
||||
const [width, setWidth] = useState(initialWidth);
|
||||
const [isDragging, setIsDragging] = useState(false);
|
||||
const dragStartX = useRef<number>(0);
|
||||
const dragStartWidth = useRef<number>(0);
|
||||
|
||||
const handleDragStart = useCallback(
|
||||
(e: React.MouseEvent) => {
|
||||
setIsDragging(true);
|
||||
dragStartX.current = e.clientX;
|
||||
dragStartWidth.current = width;
|
||||
document.body.style.userSelect = "none";
|
||||
},
|
||||
[width],
|
||||
);
|
||||
|
||||
const handleDragMove = useCallback(
|
||||
(e: MouseEvent) => {
|
||||
if (!isDragging) return;
|
||||
const deltaX = e.clientX - dragStartX.current;
|
||||
const newWidth = Math.max(
|
||||
200,
|
||||
Math.min(600, dragStartWidth.current + deltaX),
|
||||
);
|
||||
setWidth(newWidth);
|
||||
},
|
||||
[isDragging],
|
||||
);
|
||||
|
||||
const handleDragEnd = useCallback(() => {
|
||||
setIsDragging(false);
|
||||
document.body.style.userSelect = "";
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
if (isDragging) {
|
||||
window.addEventListener("mousemove", handleDragMove);
|
||||
window.addEventListener("mouseup", handleDragEnd);
|
||||
return () => {
|
||||
window.removeEventListener("mousemove", handleDragMove);
|
||||
window.removeEventListener("mouseup", handleDragEnd);
|
||||
};
|
||||
}
|
||||
}, [isDragging, handleDragMove, handleDragEnd]);
|
||||
|
||||
return {
|
||||
width,
|
||||
isDragging,
|
||||
handleDragStart,
|
||||
};
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue