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
26 changes: 24 additions & 2 deletions apps/web/src/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<HTMLDivElement>(null);

Expand All @@ -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);
}
Expand Down Expand Up @@ -139,6 +156,7 @@ export function App() {
{...stylex.props(
styles.desktopNavigation,
collapsed && styles.navigationCollapsed,
resizingSidebar && styles.navigationResizing,
)}
style={
{
Expand All @@ -158,6 +176,8 @@ export function App() {
min={sidebarMinWidth}
max={maxSidebarWidth}
onResize={resizeSidebar}
onDragStart={() => setResizingSidebar(true)}
onDrag={dragSidebar}
/>
</div>
{collapsed && (
Expand Down Expand Up @@ -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,
Expand Down
35 changes: 33 additions & 2 deletions apps/web/src/components/ui/resize-handle.tsx
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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. */
Expand All @@ -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.
<div
Expand All @@ -45,19 +62,33 @@ export function ResizeHandle({
event.preventDefault();
event.currentTarget.focus();
event.currentTarget.setPointerCapture(event.pointerId);
drag.current = { frame: 0, width: null };
onDragStart?.();
}}
onPointerMove={(event) => {
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;
Expand Down
Loading