Repository navigation
add adjust sidebar width and section height features #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,6 +8,7 @@ | |
| } | ||
|
|
||
| .panelLeft { | ||
| position: relative; | ||
| overflow: hidden; | ||
| padding: 1rem 0.85rem; | ||
| gap: 1rem; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,154 @@ | ||
| import { | ||
| useCallback, | ||
| useEffect, | ||
| useRef, | ||
| useState, | ||
| type KeyboardEvent, | ||
| type PointerEvent, | ||
| } from 'react'; | ||
|
|
||
| export const LEFT_SIDEBAR_MIN_WIDTH = 240; | ||
| export const LEFT_SIDEBAR_MAX_WIDTH = 520; | ||
|
|
||
| const LEFT_SIDEBAR_DEFAULT_WIDE_WIDTH = 320; | ||
| const LEFT_SIDEBAR_DEFAULT_NARROW_WIDTH = 280; | ||
| const LEFT_SIDEBAR_WIDTH_STORAGE_KEY = 'path-editor.left-sidebar-width'; | ||
| const LEFT_SIDEBAR_KEYBOARD_STEP = 16; | ||
| const NARROW_VIEWPORT_BREAKPOINT = 1400; | ||
|
|
||
| const clampSidebarWidth = (width: number): number => { | ||
| return Math.min( | ||
| Math.max(width, LEFT_SIDEBAR_MIN_WIDTH), | ||
| LEFT_SIDEBAR_MAX_WIDTH, | ||
| ); | ||
| }; | ||
|
|
||
| const resolveDefaultSidebarWidth = (): number => { | ||
| return globalThis.innerWidth <= NARROW_VIEWPORT_BREAKPOINT | ||
| ? LEFT_SIDEBAR_DEFAULT_NARROW_WIDTH | ||
| : LEFT_SIDEBAR_DEFAULT_WIDE_WIDTH; | ||
| }; | ||
|
|
||
| const readStoredSidebarWidth = (): number => { | ||
| try { | ||
| const storedWidth = globalThis.localStorage.getItem( | ||
| LEFT_SIDEBAR_WIDTH_STORAGE_KEY, | ||
| ); | ||
| if (storedWidth === null) { | ||
| return resolveDefaultSidebarWidth(); | ||
| } | ||
|
|
||
| const parsedWidth = Number.parseInt(storedWidth, 10); | ||
| return Number.isFinite(parsedWidth) | ||
| ? clampSidebarWidth(parsedWidth) | ||
| : resolveDefaultSidebarWidth(); | ||
| } catch { | ||
| return resolveDefaultSidebarWidth(); | ||
| } | ||
| }; | ||
|
|
||
| export type ResizableSidebarState = { | ||
| width: number; | ||
| minWidth: number; | ||
| maxWidth: number; | ||
| isResizing: boolean; | ||
| onResizeStart: (event: PointerEvent<HTMLElement>) => void; | ||
| onResizeKeyDown: (event: KeyboardEvent<HTMLElement>) => void; | ||
| }; | ||
|
|
||
| export const useResizableSidebar = (): ResizableSidebarState => { | ||
| const [width, setWidth] = useState(readStoredSidebarWidth); | ||
| const [isResizing, setIsResizing] = useState(false); | ||
| const dragStartClientXRef = useRef(0); | ||
| const dragStartWidthRef = useRef(width); | ||
|
|
||
| useEffect(() => { | ||
| try { | ||
| globalThis.localStorage.setItem( | ||
| LEFT_SIDEBAR_WIDTH_STORAGE_KEY, | ||
| String(width), | ||
| ); | ||
| } catch { | ||
| // Ignore storage failures; resizing should still work for this session. | ||
| } | ||
| }, [width]); | ||
|
|
||
| useEffect(() => { | ||
| if (!isResizing) { | ||
| return undefined; | ||
| } | ||
|
|
||
| const handlePointerMove = (event: globalThis.PointerEvent): void => { | ||
| const nextWidth = | ||
| dragStartWidthRef.current + event.clientX - dragStartClientXRef.current; | ||
| setWidth(clampSidebarWidth(nextWidth)); | ||
| }; | ||
|
|
||
| const handlePointerUp = (): void => { | ||
| setIsResizing(false); | ||
| }; | ||
|
|
||
| globalThis.addEventListener('pointermove', handlePointerMove); | ||
| globalThis.addEventListener('pointerup', handlePointerUp, { once: true }); | ||
|
|
||
| return () => { | ||
| globalThis.removeEventListener('pointermove', handlePointerMove); | ||
| globalThis.removeEventListener('pointerup', handlePointerUp); | ||
| }; | ||
| }, [isResizing]); | ||
|
|
||
| const updateWidth = useCallback((nextWidth: number): void => { | ||
| setWidth(clampSidebarWidth(nextWidth)); | ||
| }, []); | ||
|
|
||
| const onResizeStart = useCallback( | ||
| (event: PointerEvent<HTMLElement>): void => { | ||
| if (event.button !== 0) { | ||
| return; | ||
| } | ||
|
|
||
| event.preventDefault(); | ||
| dragStartClientXRef.current = event.clientX; | ||
| dragStartWidthRef.current = width; | ||
| setIsResizing(true); | ||
| }, | ||
| [width], | ||
| ); | ||
|
|
||
| const onResizeKeyDown = useCallback( | ||
| (event: KeyboardEvent<HTMLElement>): void => { | ||
| if (event.key === 'ArrowLeft') { | ||
| event.preventDefault(); | ||
| updateWidth(width - LEFT_SIDEBAR_KEYBOARD_STEP); | ||
| return; | ||
| } | ||
|
|
||
| if (event.key === 'ArrowRight') { | ||
| event.preventDefault(); | ||
| updateWidth(width + LEFT_SIDEBAR_KEYBOARD_STEP); | ||
| return; | ||
| } | ||
|
|
||
| if (event.key === 'Home') { | ||
| event.preventDefault(); | ||
| updateWidth(LEFT_SIDEBAR_MIN_WIDTH); | ||
| return; | ||
| } | ||
|
|
||
| if (event.key === 'End') { | ||
| event.preventDefault(); | ||
| updateWidth(LEFT_SIDEBAR_MAX_WIDTH); | ||
| } | ||
| }, | ||
| [updateWidth, width], | ||
| ); | ||
|
Comment on lines
+59
to
+144
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The We can optimize this by using a mutable ref ( export const useResizableSidebar = (): ResizableSidebarState => {
const [width, setWidth] = useState(readStoredSidebarWidth);
const [isResizing, setIsResizing] = useState(false);
const dragStartClientXRef = useRef(0);
const dragStartWidthRef = useRef(width);
const widthRef = useRef(width);
widthRef.current = width;
useEffect(() => {
try {
globalThis.localStorage.setItem(
LEFT_SIDEBAR_WIDTH_STORAGE_KEY,
String(width),
);
} catch {
// Ignore storage failures; resizing should still work for this session.
}
}, [width]);
useEffect(() => {
if (!isResizing) {
return undefined;
}
const handlePointerMove = (event: globalThis.PointerEvent): void => {
const nextWidth =
dragStartWidthRef.current + event.clientX - dragStartClientXRef.current;
setWidth(clampSidebarWidth(nextWidth));
};
const handlePointerUp = (): void => {
setIsResizing(false);
};
globalThis.addEventListener('pointermove', handlePointerMove);
globalThis.addEventListener('pointerup', handlePointerUp, { once: true });
return () => {
globalThis.removeEventListener('pointermove', handlePointerMove);
globalThis.removeEventListener('pointerup', handlePointerUp);
};
}, [isResizing]);
const onResizeStart = useCallback(
(event: PointerEvent<HTMLElement>): void => {
if (event.button !== 0) {
return;
}
event.preventDefault();
dragStartClientXRef.current = event.clientX;
dragStartWidthRef.current = widthRef.current;
setIsResizing(true);
},
[],
);
const onResizeKeyDown = useCallback(
(event: KeyboardEvent<HTMLElement>): void => {
if (event.key === 'ArrowLeft') {
event.preventDefault();
setWidth((w) => clampSidebarWidth(w - LEFT_SIDEBAR_KEYBOARD_STEP));
return;
}
if (event.key === 'ArrowRight') {
event.preventDefault();
setWidth((w) => clampSidebarWidth(w + LEFT_SIDEBAR_KEYBOARD_STEP));
return;
}
if (event.key === 'Home') {
event.preventDefault();
setWidth(LEFT_SIDEBAR_MIN_WIDTH);
return;
}
if (event.key === 'End') {
event.preventDefault();
setWidth(LEFT_SIDEBAR_MAX_WIDTH);
}
},
[],
); |
||
|
|
||
| return { | ||
| width, | ||
| minWidth: LEFT_SIDEBAR_MIN_WIDTH, | ||
| maxWidth: LEFT_SIDEBAR_MAX_WIDTH, | ||
| isResizing, | ||
| onResizeStart, | ||
| onResizeKeyDown, | ||
| }; | ||
| }; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
.panelLeftcontainer hasoverflow: hiddenset. Since the.resizeHandleis positioned absolutely atright: -4px(with a width of8px), the right half of the handle (4px) will be clipped and rendered invisible/non-interactive. This reduces the grab area and cuts the visual indicator in half. Changingoverflowtovisibleon.panelLeftresolves this clipping issue safely, as the inner sections already manage their own scroll/overflow behavior.