diff --git a/apps/web/src/app.tsx b/apps/web/src/app.tsx index 9f56126..7eccfc4 100644 --- a/apps/web/src/app.tsx +++ b/apps/web/src/app.tsx @@ -31,6 +31,7 @@ export function App() { agents.sidebarPreferences.width, ); const [maxSidebarWidth, setMaxSidebarWidth] = useState(sidebarMaxWidth); + const [resizingSidebar, setResizingSidebar] = useState(false); const sidebarWidth = Math.min(preferredSidebarWidth, maxSidebarWidth); const navigation = useRef(null); @@ -39,10 +40,26 @@ export function App() { saveSidebarPreference(sidebarCollapsedKey, value); } - function resizeSidebar(width: number) { - const next = Math.round( + function clampSidebarWidth(width: number) { + return Math.round( Math.max(sidebarMinWidth, Math.min(maxSidebarWidth, width)), ); + } + + // Dragging writes the width straight to the pane, so the app does not + // re-render on every pointer move. React state and the cookie catch up + // once, when the drag ends. + function dragSidebar(width: number) { + navigation.current?.style.setProperty( + "--sidebar-width", + `${clampSidebarWidth(width)}px`, + ); + } + + function resizeSidebar(width: number) { + const next = clampSidebarWidth(width); + setResizingSidebar(false); + navigation.current?.style.setProperty("--sidebar-width", `${next}px`); setPreferredSidebarWidth(next); saveSidebarPreference(sidebarWidthKey, next); } @@ -139,6 +156,7 @@ export function App() { {...stylex.props( styles.desktopNavigation, collapsed && styles.navigationCollapsed, + resizingSidebar && styles.navigationResizing, )} style={ { @@ -158,6 +176,8 @@ export function App() { min={sidebarMinWidth} max={maxSidebarWidth} onResize={resizeSidebar} + onDragStart={() => setResizingSidebar(true)} + onDrag={dragSidebar} /> {collapsed && ( @@ -211,6 +231,8 @@ const styles = stylex.create({ transitionTimingFunction: motion.easeInOut, }, navigationCollapsed: { width: 0 }, + // The collapse animation would make the pane trail the pointer while dragging. + navigationResizing: { transitionDuration: "0s" }, expand: { position: "sticky", top: 0, diff --git a/apps/web/src/components/ui/resize-handle.tsx b/apps/web/src/components/ui/resize-handle.tsx index c7124ab..8aa4d21 100644 --- a/apps/web/src/components/ui/resize-handle.tsx +++ b/apps/web/src/components/ui/resize-handle.tsx @@ -1,10 +1,12 @@ import * as stylex from "@stylexjs/stylex"; -import type { RefObject } from "react"; +import { type RefObject, useRef } from "react"; import { colors } from "../../styles/tokens.stylex"; /** * A keyboard- and pointer-operable splitter that sits on the right edge of a * pane. It reports the pane's new width in pixels; the owner clamps and stores. + * While dragging, widths go to `onDrag` at most once per frame so the owner can + * update the pane directly; `onResize` runs once when the drag ends. */ export function ResizeHandle({ label, @@ -15,6 +17,8 @@ export function ResizeHandle({ max, step = 24, onResize, + onDragStart, + onDrag, }: { label: string; /** Id of the pane the handle resizes, for assistive technology. */ @@ -27,7 +31,20 @@ export function ResizeHandle({ /** Width change for each arrow key press. */ step?: number; onResize: (width: number) => void; + onDragStart?: () => void; + /** Live width during a pointer drag. Without it, `onResize` runs per frame. */ + onDrag?: (width: number) => void; }) { + const drag = useRef<{ frame: number; width: number | null } | null>(null); + + function endDrag() { + const current = drag.current; + if (!current) return; + drag.current = null; + cancelAnimationFrame(current.frame); + onResize(current.width ?? value); + } + return ( // biome-ignore lint/a11y/useSemanticElements: This is an interactive pane splitter, not a thematic break.
{ + const current = drag.current; if ( + !current || !event.currentTarget.hasPointerCapture(event.pointerId) || !pane.current ) return; - onResize(event.clientX - pane.current.getBoundingClientRect().left); + current.width = + event.clientX - pane.current.getBoundingClientRect().left; + if (current.frame) return; + // Coalesce pointer events so layout runs at most once per frame. + current.frame = requestAnimationFrame(() => { + current.frame = 0; + if (drag.current !== current || current.width === null) return; + (onDrag ?? onResize)(current.width); + }); }} onPointerUp={(event) => { if (event.currentTarget.hasPointerCapture(event.pointerId)) event.currentTarget.releasePointerCapture(event.pointerId); + endDrag(); }} + onLostPointerCapture={endDrag} onKeyDown={(event) => { if (!["ArrowLeft", "ArrowRight", "Home", "End"].includes(event.key)) return;