Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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,
Expand All @@ -59,6 +65,12 @@ const EditorApp = (): ReactElement => {
}),
[csvExportCommand, pathSetExportCommand, workspaceCommands],
);
const appBodyStyle = useMemo<AppBodyStyle>(
() => ({
'--left-sidebar-width': `${resizableSidebar.width}px`,
}),
[resizableSidebar.width],
);

return (
<div className={appShellStyles.appShell}>
Expand All @@ -77,9 +89,13 @@ const EditorApp = (): ReactElement => {
onDismiss={clearNotification}
/>

<div className={appShellStyles.appBody} ref={appBodyRef}>
<div
className={appShellStyles.appBody}
ref={appBodyRef}
style={appBodyStyle}
>
<BenchmarkProfiler id="sidebar">
<Sidebar hostRef={sidebarRef} />
<Sidebar hostRef={sidebarRef} resize={resizableSidebar} />
</BenchmarkProfiler>

<WorkspaceEditorProvider>
Expand Down
1 change: 1 addition & 0 deletions src/components/common/SidePanel.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
}

.panelLeft {
position: relative;
overflow: hidden;
Comment on lines +11 to 12

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The .panelLeft container has overflow: hidden set. Since the .resizeHandle is positioned absolutely at right: -4px (with a width of 8px), 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. Changing overflow to visible on .panelLeft resolves this clipping issue safely, as the inner sections already manage their own scroll/overflow behavior.

Suggested change
position: relative;
overflow: hidden;
position: relative;
overflow: visible;

padding: 1rem 0.85rem;
gap: 1rem;
Expand Down
4 changes: 2 additions & 2 deletions src/features/app-shell/AppShell.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
154 changes: 154 additions & 0 deletions src/features/app-shell/useResizableSidebar.ts
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

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The onResizeStart and onResizeKeyDown callbacks currently depend on the width state. This causes these callbacks to be recreated on every single pixel of dragging, which in turn forces the memoized Sidebar and SidebarPresenter components to re-render continuously during resize operations.

We can optimize this by using a mutable ref (widthRef) to track the current width, and using functional state updates (setWidth(w => ...)) in the keyboard handler. This completely removes width from the dependency arrays, keeping the callback references stable and preventing unnecessary re-renders of the sidebar components during active dragging.

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,
};
};
59 changes: 57 additions & 2 deletions src/features/sidebar/Sidebar.module.css
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
.pathsSection {
flex: 0 1 auto;
flex: 0 0 auto;
min-height: 120px;
}

.librarySection {
Expand All @@ -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);
Expand Down
8 changes: 7 additions & 1 deletion src/features/sidebar/Sidebar.tsx
Original file line number Diff line number Diff line change
@@ -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<HTMLElement> | undefined;
resize: ResizableSidebarState;
};

const SidebarComponent = ({ hostRef }: SidebarProps): ReactElement => {
const SidebarComponent = ({ hostRef, resize }: SidebarProps): ReactElement => {
const {
addPath,
deletePath,
Expand All @@ -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(() => <PointLibraryPanel />, []);
Expand All @@ -38,6 +42,8 @@ const SidebarComponent = ({ hostRef }: SidebarProps): ReactElement => {
onSetActivePath={setActivePath}
onTogglePathVisible={togglePathVisible}
libraryPanel={libraryPanel}
resize={resize}
sectionResize={sectionResize}
/>
);
};
Expand Down
Loading
Loading