diff --git a/src/App.tsx b/src/App.tsx index 672b69a..fe9ae6b 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,4 +1,4 @@ -import { type ReactElement, useMemo } from 'react'; +import { type CSSProperties, type ReactElement, useMemo } from 'react'; import './App.css'; import appShellStyles from './features/app-shell/AppShell.module.css'; import { PathCanvas } from './features/canvas/PathCanvas'; @@ -27,12 +27,18 @@ import { useWorkspaceFileCommands } from './features/workspace-file/useWorkspace import { usePwaController } from './pwa/usePwaController'; import { ThemePreferenceProvider } from './features/theme/ThemePreferenceContext'; import { BenchmarkProfiler } from './bench/reactPerf'; +import { useResizableSidebar } from './features/app-shell/useResizableSidebar'; + +type AppBodyStyle = CSSProperties & { + '--left-sidebar-width': string; +}; const EditorApp = (): ReactElement => { const { notification, setNotification, clearNotification } = useAppNotification(); const { appBodyRef, sidebarRef, rightPanelRef, layout } = useFloatingInspectorLayout(); + const resizableSidebar = useResizableSidebar(); const pwaController = usePwaController(); const workspacePersistence = useWorkspacePersistence({ setNotification, @@ -59,6 +65,12 @@ const EditorApp = (): ReactElement => { }), [csvExportCommand, pathSetExportCommand, workspaceCommands], ); + const appBodyStyle = useMemo( + () => ({ + '--left-sidebar-width': `${resizableSidebar.width}px`, + }), + [resizableSidebar.width], + ); return (
@@ -77,9 +89,13 @@ const EditorApp = (): ReactElement => { onDismiss={clearNotification} /> -
+
- + diff --git a/src/components/common/SidePanel.module.css b/src/components/common/SidePanel.module.css index 019e026..61f71c5 100644 --- a/src/components/common/SidePanel.module.css +++ b/src/components/common/SidePanel.module.css @@ -8,6 +8,7 @@ } .panelLeft { + position: relative; overflow: hidden; padding: 1rem 0.85rem; gap: 1rem; diff --git a/src/features/app-shell/AppShell.module.css b/src/features/app-shell/AppShell.module.css index c809375..718bd10 100644 --- a/src/features/app-shell/AppShell.module.css +++ b/src/features/app-shell/AppShell.module.css @@ -11,12 +11,12 @@ flex: 1; min-height: 0; display: grid; - grid-template-columns: 320px 1fr 320px; + grid-template-columns: var(--left-sidebar-width, 320px) minmax(0, 1fr) 320px; position: relative; } @media (max-width: 1400px) { .appBody { - grid-template-columns: 280px 1fr 280px; + grid-template-columns: var(--left-sidebar-width, 280px) minmax(0, 1fr) 280px; } } diff --git a/src/features/app-shell/useResizableSidebar.ts b/src/features/app-shell/useResizableSidebar.ts new file mode 100644 index 0000000..6ee8140 --- /dev/null +++ b/src/features/app-shell/useResizableSidebar.ts @@ -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) => void; + onResizeKeyDown: (event: KeyboardEvent) => 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): void => { + if (event.button !== 0) { + return; + } + + event.preventDefault(); + dragStartClientXRef.current = event.clientX; + dragStartWidthRef.current = width; + setIsResizing(true); + }, + [width], + ); + + const onResizeKeyDown = useCallback( + (event: KeyboardEvent): 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], + ); + + return { + width, + minWidth: LEFT_SIDEBAR_MIN_WIDTH, + maxWidth: LEFT_SIDEBAR_MAX_WIDTH, + isResizing, + onResizeStart, + onResizeKeyDown, + }; +}; diff --git a/src/features/sidebar/Sidebar.module.css b/src/features/sidebar/Sidebar.module.css index 83fb7a1..92d34d0 100644 --- a/src/features/sidebar/Sidebar.module.css +++ b/src/features/sidebar/Sidebar.module.css @@ -1,5 +1,6 @@ .pathsSection { - flex: 0 1 auto; + flex: 0 0 auto; + min-height: 120px; } .librarySection { @@ -9,13 +10,67 @@ .pathsList { display: flex; + flex: 1 1 0; flex-direction: column; gap: 0.45rem; - max-height: min(18rem, 35vh); + min-height: 0; overflow: auto; padding-right: 0.15rem; } +.sectionResizeHandle { + position: relative; + flex: 0 0 10px; + margin: -0.25rem -0.85rem; + cursor: row-resize; + outline: none; +} + +.sectionResizeHandle::before { + content: ''; + position: absolute; + top: 4px; + right: 0.85rem; + left: 0.85rem; + height: 2px; + background: transparent; + transition: background-color 0.12s ease; +} + +.sectionResizeHandle:hover::before, +.sectionResizeHandle:focus-visible::before, +.sectionResizeHandleActive::before { + background: var(--color-accent-fg); +} + +.resizeHandle { + position: absolute; + top: 0; + right: -4px; + bottom: 0; + z-index: 2; + width: 8px; + cursor: col-resize; + outline: none; +} + +.resizeHandle::before { + content: ''; + position: absolute; + top: 0; + right: 3px; + bottom: 0; + width: 2px; + background: transparent; + transition: background-color 0.12s ease; +} + +.resizeHandle:hover::before, +.resizeHandle:focus-visible::before, +.resizeHandleActive::before { + background: var(--color-accent-fg); +} + /* Sidebar cards use more rounded corners than the SidePanel default */ .pathCard { border-radius: var(--radius-xl); diff --git a/src/features/sidebar/Sidebar.tsx b/src/features/sidebar/Sidebar.tsx index c947558..1217268 100644 --- a/src/features/sidebar/Sidebar.tsx +++ b/src/features/sidebar/Sidebar.tsx @@ -1,15 +1,18 @@ import { memo, useMemo, type ReactElement, type Ref } from 'react'; import { useActivePathId, usePaths } from '../../store/workspaceSelectors'; import { PointLibraryPanel } from '../pointLibrary/PointLibraryPanel'; +import type { ResizableSidebarState } from '../app-shell/useResizableSidebar'; import { SidebarPresenter } from './SidebarPresenter'; import { sortPathsByDisplayName } from './sidebarPathList'; import { useSidebarActions } from './useSidebarActions'; +import { useResizableSidebarSections } from './useResizableSidebarSections'; type SidebarProps = { hostRef?: Ref | undefined; + resize: ResizableSidebarState; }; -const SidebarComponent = ({ hostRef }: SidebarProps): ReactElement => { +const SidebarComponent = ({ hostRef, resize }: SidebarProps): ReactElement => { const { addPath, deletePath, @@ -21,6 +24,7 @@ const SidebarComponent = ({ hostRef }: SidebarProps): ReactElement => { } = useSidebarActions(); const paths = usePaths(); const activePathId = useActivePathId(); + const sectionResize = useResizableSidebarSections(); const sortedPaths = useMemo(() => sortPathsByDisplayName(paths), [paths]); const libraryPanel = useMemo(() => , []); @@ -38,6 +42,8 @@ const SidebarComponent = ({ hostRef }: SidebarProps): ReactElement => { onSetActivePath={setActivePath} onTogglePathVisible={togglePathVisible} libraryPanel={libraryPanel} + resize={resize} + sectionResize={sectionResize} /> ); }; diff --git a/src/features/sidebar/SidebarPresenter.tsx b/src/features/sidebar/SidebarPresenter.tsx index 77fe3c6..4d65fa4 100644 --- a/src/features/sidebar/SidebarPresenter.tsx +++ b/src/features/sidebar/SidebarPresenter.tsx @@ -2,6 +2,8 @@ import { memo, useCallback, useMemo, type ReactElement, type Ref } from 'react'; import { Plus } from 'lucide-react'; import type { PathModel } from '../../domain/models'; import { SidePanel, SidePanelSection } from '../../components/common/SidePanel'; +import type { ResizableSidebarState } from '../app-shell/useResizableSidebar'; +import type { ResizableSidebarSectionsState } from './useResizableSidebarSections'; import styles from './Sidebar.module.css'; import { Button } from '../../components/common/Button'; import { InteractiveList } from '../../components/common/InteractiveList'; @@ -19,6 +21,8 @@ export type SidebarPresenterProps = { onSetActivePath: (pathId: string) => void; onTogglePathVisible: (pathId: string) => void; libraryPanel: ReactElement; + resize: ResizableSidebarState; + sectionResize: ResizableSidebarSectionsState; }; const SidebarPresenterComponent = ({ @@ -33,6 +37,8 @@ const SidebarPresenterComponent = ({ onSetActivePath, onTogglePathVisible, libraryPanel, + resize, + sectionResize, }: SidebarPresenterProps): ReactElement => { const getKey = useCallback((path: PathModel): string => path.id, []); const renderItem = useCallback( @@ -82,6 +88,7 @@ const SidebarPresenterComponent = ({ headerActions={headerActions} className={styles.pathsSection} aria-label="path management" + style={{ flexBasis: `${sectionResize.pathsHeight}px` }} > +
+ {libraryPanel} + +
); }; diff --git a/src/features/sidebar/useResizableSidebarSections.ts b/src/features/sidebar/useResizableSidebarSections.ts new file mode 100644 index 0000000..8c66190 --- /dev/null +++ b/src/features/sidebar/useResizableSidebarSections.ts @@ -0,0 +1,149 @@ +import { + useCallback, + useEffect, + useRef, + useState, + type KeyboardEvent, + type PointerEvent, +} from 'react'; + +export const SIDEBAR_PATHS_MIN_HEIGHT = 120; +export const SIDEBAR_PATHS_MAX_HEIGHT = 520; + +const SIDEBAR_PATHS_DEFAULT_HEIGHT = 220; +const SIDEBAR_PATHS_HEIGHT_STORAGE_KEY = 'path-editor.sidebar-paths-height'; +const SIDEBAR_PATHS_KEYBOARD_STEP = 16; + +const clampPathsHeight = (height: number): number => { + return Math.min( + Math.max(height, SIDEBAR_PATHS_MIN_HEIGHT), + SIDEBAR_PATHS_MAX_HEIGHT, + ); +}; + +const readStoredPathsHeight = (): number => { + try { + const storedHeight = globalThis.localStorage.getItem( + SIDEBAR_PATHS_HEIGHT_STORAGE_KEY, + ); + if (storedHeight === null) { + return SIDEBAR_PATHS_DEFAULT_HEIGHT; + } + + const parsedHeight = Number.parseInt(storedHeight, 10); + return Number.isFinite(parsedHeight) + ? clampPathsHeight(parsedHeight) + : SIDEBAR_PATHS_DEFAULT_HEIGHT; + } catch { + return SIDEBAR_PATHS_DEFAULT_HEIGHT; + } +}; + +export type ResizableSidebarSectionsState = { + pathsHeight: number; + minPathsHeight: number; + maxPathsHeight: number; + isResizing: boolean; + onResizeStart: (event: PointerEvent) => void; + onResizeKeyDown: (event: KeyboardEvent) => void; +}; + +export const useResizableSidebarSections = + (): ResizableSidebarSectionsState => { + const [pathsHeight, setPathsHeight] = useState(readStoredPathsHeight); + const [isResizing, setIsResizing] = useState(false); + const dragStartClientYRef = useRef(0); + const dragStartHeightRef = useRef(pathsHeight); + + useEffect(() => { + try { + globalThis.localStorage.setItem( + SIDEBAR_PATHS_HEIGHT_STORAGE_KEY, + String(pathsHeight), + ); + } catch { + // Ignore storage failures; resizing should still work for this session. + } + }, [pathsHeight]); + + useEffect(() => { + if (!isResizing) { + return undefined; + } + + const handlePointerMove = (event: globalThis.PointerEvent): void => { + const nextHeight = + dragStartHeightRef.current + + event.clientY - + dragStartClientYRef.current; + setPathsHeight(clampPathsHeight(nextHeight)); + }; + + 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 updatePathsHeight = useCallback((nextHeight: number): void => { + setPathsHeight(clampPathsHeight(nextHeight)); + }, []); + + const onResizeStart = useCallback( + (event: PointerEvent): void => { + if (event.button !== 0) { + return; + } + + event.preventDefault(); + dragStartClientYRef.current = event.clientY; + dragStartHeightRef.current = pathsHeight; + setIsResizing(true); + }, + [pathsHeight], + ); + + const onResizeKeyDown = useCallback( + (event: KeyboardEvent): void => { + if (event.key === 'ArrowUp') { + event.preventDefault(); + updatePathsHeight(pathsHeight - SIDEBAR_PATHS_KEYBOARD_STEP); + return; + } + + if (event.key === 'ArrowDown') { + event.preventDefault(); + updatePathsHeight(pathsHeight + SIDEBAR_PATHS_KEYBOARD_STEP); + return; + } + + if (event.key === 'Home') { + event.preventDefault(); + updatePathsHeight(SIDEBAR_PATHS_MIN_HEIGHT); + return; + } + + if (event.key === 'End') { + event.preventDefault(); + updatePathsHeight(SIDEBAR_PATHS_MAX_HEIGHT); + } + }, + [pathsHeight, updatePathsHeight], + ); + + return { + pathsHeight, + minPathsHeight: SIDEBAR_PATHS_MIN_HEIGHT, + maxPathsHeight: SIDEBAR_PATHS_MAX_HEIGHT, + isResizing, + onResizeStart, + onResizeKeyDown, + }; + }; diff --git a/src/tests/unit/SidebarPresenter.test.tsx b/src/tests/unit/SidebarPresenter.test.tsx index ac87d30..34629ad 100644 --- a/src/tests/unit/SidebarPresenter.test.tsx +++ b/src/tests/unit/SidebarPresenter.test.tsx @@ -2,6 +2,8 @@ import { fireEvent, render, screen } from '@testing-library/react'; import { describe, expect, it, vi } from 'vitest'; import type { PathModel } from '../../domain/models'; import { SidebarPresenter } from '../../features/sidebar/SidebarPresenter'; +import type { ResizableSidebarState } from '../../features/app-shell/useResizableSidebar'; +import type { ResizableSidebarSectionsState } from '../../features/sidebar/useResizableSidebarSections'; const makePath = (id: string, name: string): PathModel => ({ id, @@ -13,6 +15,24 @@ const makePath = (id: string, name: string): PathModel => ({ sectionRMin: [], }); +const makeResizeState = (): ResizableSidebarState => ({ + width: 320, + minWidth: 240, + maxWidth: 520, + isResizing: false, + onResizeStart: vi.fn(), + onResizeKeyDown: vi.fn(), +}); + +const makeSectionResizeState = (): ResizableSidebarSectionsState => ({ + pathsHeight: 220, + minPathsHeight: 120, + maxPathsHeight: 520, + isResizing: false, + onResizeStart: vi.fn(), + onResizeKeyDown: vi.fn(), +}); + const defaultProps = { paths: [makePath('p1', 'Path 1'), makePath('p2', 'Path 2')], activePathId: 'p1', @@ -24,6 +44,8 @@ const defaultProps = { onSetActivePath: vi.fn(), onTogglePathVisible: vi.fn(), libraryPanel:
, + resize: makeResizeState(), + sectionResize: makeSectionResizeState(), }; describe('SidebarPresenter', () => { @@ -125,4 +147,58 @@ describe('SidebarPresenter', () => { render(); expect(screen.getByTestId('library-panel')).toBeInTheDocument(); }); + + it('renders a resize handle with current width metadata', () => { + render(); + + const handle = screen.getByRole('slider', { + name: 'resize editor sidebar', + }); + expect(handle).toHaveAttribute('aria-orientation', 'vertical'); + expect(handle).toHaveAttribute('aria-valuemin', '240'); + expect(handle).toHaveAttribute('aria-valuemax', '520'); + expect(handle).toHaveAttribute('aria-valuenow', '320'); + }); + + it('forwards pointer and keyboard events from the resize handle', () => { + const resize = makeResizeState(); + render(); + + const handle = screen.getByRole('slider', { + name: 'resize editor sidebar', + }); + fireEvent.pointerDown(handle, { button: 0, clientX: 320 }); + fireEvent.keyDown(handle, { key: 'ArrowRight' }); + + expect(resize.onResizeStart).toHaveBeenCalledOnce(); + expect(resize.onResizeKeyDown).toHaveBeenCalledOnce(); + }); + + it('renders a section resize handle with current paths height metadata', () => { + render(); + + const handle = screen.getByRole('slider', { + name: 'resize paths and library panels', + }); + expect(handle).toHaveAttribute('aria-orientation', 'vertical'); + expect(handle).toHaveAttribute('aria-valuemin', '120'); + expect(handle).toHaveAttribute('aria-valuemax', '520'); + expect(handle).toHaveAttribute('aria-valuenow', '220'); + }); + + it('forwards pointer and keyboard events from the section resize handle', () => { + const sectionResize = makeSectionResizeState(); + render( + , + ); + + const handle = screen.getByRole('slider', { + name: 'resize paths and library panels', + }); + fireEvent.pointerDown(handle, { button: 0, clientY: 220 }); + fireEvent.keyDown(handle, { key: 'ArrowDown' }); + + expect(sectionResize.onResizeStart).toHaveBeenCalledOnce(); + expect(sectionResize.onResizeKeyDown).toHaveBeenCalledOnce(); + }); }); diff --git a/src/tests/unit/useResizableSidebar.test.ts b/src/tests/unit/useResizableSidebar.test.ts new file mode 100644 index 0000000..a2748c3 --- /dev/null +++ b/src/tests/unit/useResizableSidebar.test.ts @@ -0,0 +1,80 @@ +import { act, fireEvent, renderHook } from '@testing-library/react'; +import { beforeEach, describe, expect, it, vi } from 'vitest'; +import { + LEFT_SIDEBAR_MAX_WIDTH, + LEFT_SIDEBAR_MIN_WIDTH, + useResizableSidebar, +} from '../../features/app-shell/useResizableSidebar'; + +const STORAGE_KEY = 'path-editor.left-sidebar-width'; + +beforeEach(() => { + globalThis.localStorage.clear(); +}); + +describe('useResizableSidebar', () => { + it('restores a stored sidebar width', () => { + globalThis.localStorage.setItem(STORAGE_KEY, '360'); + + const { result } = renderHook(() => useResizableSidebar()); + + expect(result.current.width).toBe(360); + }); + + it('updates and persists width from keyboard controls', () => { + globalThis.localStorage.setItem(STORAGE_KEY, '300'); + const { result } = renderHook(() => useResizableSidebar()); + + act(() => { + result.current.onResizeKeyDown({ + key: 'ArrowRight', + preventDefault: vi.fn(), + } as unknown as Parameters[0]); + }); + + expect(result.current.width).toBe(316); + expect(globalThis.localStorage.getItem(STORAGE_KEY)).toBe('316'); + + act(() => { + result.current.onResizeKeyDown({ + key: 'Home', + preventDefault: vi.fn(), + } as unknown as Parameters[0]); + }); + + expect(result.current.width).toBe(LEFT_SIDEBAR_MIN_WIDTH); + }); + + it('clamps pointer drag resizing to the supported range', () => { + globalThis.localStorage.setItem(STORAGE_KEY, '300'); + const { result } = renderHook(() => useResizableSidebar()); + + act(() => { + result.current.onResizeStart({ + button: 0, + clientX: 100, + preventDefault: vi.fn(), + } as unknown as Parameters[0]); + }); + + expect(result.current.isResizing).toBe(true); + + act(() => { + fireEvent.pointerMove(window, { clientX: 1000 }); + }); + + expect(result.current.width).toBe(LEFT_SIDEBAR_MAX_WIDTH); + + act(() => { + fireEvent.pointerMove(window, { clientX: -1000 }); + }); + + expect(result.current.width).toBe(LEFT_SIDEBAR_MIN_WIDTH); + + act(() => { + fireEvent.pointerUp(window); + }); + + expect(result.current.isResizing).toBe(false); + }); +}); diff --git a/src/tests/unit/useResizableSidebarSections.test.ts b/src/tests/unit/useResizableSidebarSections.test.ts new file mode 100644 index 0000000..712fa10 --- /dev/null +++ b/src/tests/unit/useResizableSidebarSections.test.ts @@ -0,0 +1,80 @@ +import { act, fireEvent, renderHook } from '@testing-library/react'; +import { beforeEach, describe, expect, it, vi } from 'vitest'; +import { + SIDEBAR_PATHS_MAX_HEIGHT, + SIDEBAR_PATHS_MIN_HEIGHT, + useResizableSidebarSections, +} from '../../features/sidebar/useResizableSidebarSections'; + +const STORAGE_KEY = 'path-editor.sidebar-paths-height'; + +beforeEach(() => { + globalThis.localStorage.clear(); +}); + +describe('useResizableSidebarSections', () => { + it('restores a stored paths panel height', () => { + globalThis.localStorage.setItem(STORAGE_KEY, '300'); + + const { result } = renderHook(() => useResizableSidebarSections()); + + expect(result.current.pathsHeight).toBe(300); + }); + + it('updates and persists height from keyboard controls', () => { + globalThis.localStorage.setItem(STORAGE_KEY, '220'); + const { result } = renderHook(() => useResizableSidebarSections()); + + act(() => { + result.current.onResizeKeyDown({ + key: 'ArrowDown', + preventDefault: vi.fn(), + } as unknown as Parameters[0]); + }); + + expect(result.current.pathsHeight).toBe(236); + expect(globalThis.localStorage.getItem(STORAGE_KEY)).toBe('236'); + + act(() => { + result.current.onResizeKeyDown({ + key: 'Home', + preventDefault: vi.fn(), + } as unknown as Parameters[0]); + }); + + expect(result.current.pathsHeight).toBe(SIDEBAR_PATHS_MIN_HEIGHT); + }); + + it('clamps pointer drag resizing to the supported range', () => { + globalThis.localStorage.setItem(STORAGE_KEY, '220'); + const { result } = renderHook(() => useResizableSidebarSections()); + + act(() => { + result.current.onResizeStart({ + button: 0, + clientY: 100, + preventDefault: vi.fn(), + } as unknown as Parameters[0]); + }); + + expect(result.current.isResizing).toBe(true); + + act(() => { + fireEvent.pointerMove(window, { clientY: 1000 }); + }); + + expect(result.current.pathsHeight).toBe(SIDEBAR_PATHS_MAX_HEIGHT); + + act(() => { + fireEvent.pointerMove(window, { clientY: -1000 }); + }); + + expect(result.current.pathsHeight).toBe(SIDEBAR_PATHS_MIN_HEIGHT); + + act(() => { + fireEvent.pointerUp(window); + }); + + expect(result.current.isResizing).toBe(false); + }); +});