Skip to content

Commit 7ed9174

Browse files
andyon2claude
andcommitted
feat(web): make the right-panel resize handle keyboard operable
The handle was pointer-only, so panel width was the one part of the layout a keyboard user could not reach. - Arrow keys nudge it by 8px, Shift makes the step 48px, Home/End park it at either extreme. Keys are spatial: ArrowLeft always moves the handle left, which widens or narrows depending on which edge it is anchored to. - The handle is focusable and announces itself as a window splitter (aria-label / valuenow / valuemin / valuemax) instead of a bare separator. - The 1px line lights up on keyboard focus too — otherwise focus would sit on something invisible. The direction and step rules are a pure function (`nextWidthForKey`) with unit tests; the new props are optional so existing call sites keep compiling. Co-Authored-By: Claude Opus 5 <[email protected]>
1 parent 5719e8a commit 7ed9174

4 files changed

Lines changed: 185 additions & 14 deletions

File tree

‎apps/web/src/components/preview/PreviewPanelShell.tsx‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,15 @@ export function PreviewPanelShell(props: {
5353
data-preview-panel-mode={props.mode}
5454
data-preview-panel-maximized={props.maximized ? "true" : "false"}
5555
>
56-
{isInline && !props.maximized ? <RightPanelResizeHandle handlers={handlers} /> : null}
56+
{isInline && !props.maximized ? (
57+
<RightPanelResizeHandle
58+
handlers={handlers}
59+
label="Resize preview panel"
60+
maxWidth={maxWidth}
61+
minWidth={PREVIEW_PANEL_MIN_WIDTH}
62+
width={width}
63+
/>
64+
) : null}
5765
{useDragRegion ? <div className="electron-drag-region h-0 w-full" aria-hidden /> : null}
5866
{props.children}
5967
</div>

‎apps/web/src/components/preview/RightPanelResizeHandle.tsx‎

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,16 @@ import { cn } from "~/lib/utils";
44
interface Props {
55
handlers: ResizableWidthHandlers;
66
className?: string;
7+
/**
8+
* Current and allowed width in px. Optional so existing call sites keep
9+
* compiling, but pass them: reachable by keyboard while announcing nothing
10+
* is worse than not reachable at all.
11+
*/
12+
width?: number;
13+
minWidth?: number;
14+
maxWidth?: number;
15+
/** Spoken name, so the handle is never announced as a bare separator. */
16+
label?: string;
717
}
818

919
/**
@@ -13,21 +23,36 @@ interface Props {
1323
* user can grab a few pixels off the edge without aiming.
1424
* - Visual indicator is a 1px line that lights up on hover/active to mirror
1525
* VS Code / Cursor.
26+
* - Focusable window splitter: arrow keys move it, Shift makes the step
27+
* coarse, Home/End park it at either extreme. The line lights up on keyboard
28+
* focus too — otherwise focus would sit on something invisible.
1629
*/
17-
export function RightPanelResizeHandle({ handlers, className }: Props) {
30+
export function RightPanelResizeHandle({
31+
handlers,
32+
className,
33+
width,
34+
minWidth,
35+
maxWidth,
36+
label,
37+
}: Props) {
1838
return (
1939
<div
2040
role="separator"
2141
aria-orientation="vertical"
42+
aria-label={label ?? "Resize panel"}
43+
aria-valuenow={width}
44+
aria-valuemin={minWidth}
45+
aria-valuemax={maxWidth}
46+
tabIndex={0}
2247
className={cn(
23-
"group absolute inset-y-0 -left-1 z-20 w-2 cursor-col-resize select-none",
48+
"group absolute inset-y-0 -left-1 z-20 w-2 cursor-col-resize select-none outline-hidden",
2449
className,
2550
)}
2651
{...handlers}
2752
>
2853
<span
2954
aria-hidden
30-
className="pointer-events-none absolute inset-y-0 left-1/2 w-px -translate-x-1/2 bg-transparent transition-colors duration-150 group-hover:bg-border group-active:bg-primary/60"
55+
className="pointer-events-none absolute inset-y-0 left-1/2 w-px -translate-x-1/2 bg-transparent transition-colors duration-150 group-hover:bg-border group-focus-visible:bg-primary group-active:bg-primary/60"
3156
/>
3257
</div>
3358
);
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { describe, expect, it } from "vite-plus/test";
2+
3+
import { nextWidthForKey } from "./useResizableWidth";
4+
5+
const BOUNDS = { minWidth: 220, maxWidth: 520 } as const;
6+
7+
function press(
8+
key: string,
9+
edge: "left" | "right",
10+
extra: { width?: number; shiftKey?: boolean } = {},
11+
) {
12+
return nextWidthForKey({
13+
key,
14+
shiftKey: extra.shiftKey ?? false,
15+
width: extra.width ?? 300,
16+
edge,
17+
...BOUNDS,
18+
});
19+
}
20+
21+
describe("nextWidthForKey", () => {
22+
it("moves the handle left on ArrowLeft — whichever edge it sits on", () => {
23+
// Right-anchored panel: handle on its left edge, so left = wider.
24+
expect(press("ArrowLeft", "left")).toBe(308);
25+
// Left-anchored panel: handle on its right edge, so left = narrower.
26+
expect(press("ArrowLeft", "right")).toBe(292);
27+
});
28+
29+
it("moves the handle right on ArrowRight", () => {
30+
expect(press("ArrowRight", "left")).toBe(292);
31+
expect(press("ArrowRight", "right")).toBe(308);
32+
});
33+
34+
it("takes a coarse step with Shift", () => {
35+
expect(press("ArrowLeft", "left", { shiftKey: true })).toBe(348);
36+
});
37+
38+
it("parks the handle at either extreme with Home and End", () => {
39+
// Home = handle all the way left, which for a right-anchored panel is
40+
// its widest — not its smallest number.
41+
expect(press("Home", "left")).toBe(520);
42+
expect(press("End", "left")).toBe(220);
43+
expect(press("Home", "right")).toBe(220);
44+
expect(press("End", "right")).toBe(520);
45+
});
46+
47+
it("leaves keys it does not own alone", () => {
48+
expect(press("ArrowUp", "left")).toBeNull();
49+
expect(press("Enter", "left")).toBeNull();
50+
expect(press("a", "left")).toBeNull();
51+
});
52+
53+
it("may step past the bounds — the caller clamps", () => {
54+
// Deliberate: this function answers "where does the key point", the hook
55+
// owns the allowed range. Splitting it the other way would duplicate the
56+
// clamp that already guards the stored and dragged width.
57+
expect(press("ArrowLeft", "left", { width: 518 })).toBe(526);
58+
});
59+
});

‎apps/web/src/hooks/useResizableWidth.ts‎

Lines changed: 89 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
11
import * as Schema from "effect/Schema";
2-
import { type PointerEvent as ReactPointerEvent, useCallback, useRef, useState } from "react";
2+
import {
3+
type KeyboardEvent as ReactKeyboardEvent,
4+
type PointerEvent as ReactPointerEvent,
5+
useCallback,
6+
useRef,
7+
useState,
8+
} from "react";
39

410
import { getLocalStorageItem, setLocalStorageItem } from "./useLocalStorage";
511

612
const WidthSchema = Schema.Finite;
713

14+
/** Arrow-key step, and the coarse step held Shift gives you. */
15+
const KEYBOARD_STEP = 8;
16+
const KEYBOARD_COARSE_STEP = 48;
17+
818
export interface UseResizableWidthOptions {
919
/** localStorage key the persisted width is stored under. */
1020
readonly storageKey: string;
@@ -24,6 +34,47 @@ export interface ResizableWidthHandlers {
2434
readonly onPointerMove: (event: ReactPointerEvent<HTMLElement>) => void;
2535
readonly onPointerUp: (event: ReactPointerEvent<HTMLElement>) => void;
2636
readonly onPointerCancel: (event: ReactPointerEvent<HTMLElement>) => void;
37+
/**
38+
* Arrow keys nudge the handle, Shift makes the step coarse, Home/End park it
39+
* at either extreme. Without this the handle is mouse-only — the panel width
40+
* would be the one part of the layout a keyboard user cannot reach.
41+
*/
42+
readonly onKeyDown: (event: ReactKeyboardEvent<HTMLElement>) => void;
43+
}
44+
45+
/**
46+
* Width a key press asks for, or `null` if the key is not ours to handle.
47+
*
48+
* Keys are spatial: ArrowLeft always moves the handle left. Whether that
49+
* widens or narrows the panel depends on which edge the handle sits on — so
50+
* the same key does opposite things on a left- and a right-anchored panel,
51+
* and that is correct.
52+
*
53+
* Pure and exported so the direction/step rules can be tested without a DOM.
54+
*/
55+
export function nextWidthForKey(input: {
56+
readonly key: string;
57+
readonly shiftKey: boolean;
58+
readonly width: number;
59+
readonly minWidth: number;
60+
readonly maxWidth: number;
61+
readonly edge: "left" | "right";
62+
}): number | null {
63+
const { key, shiftKey, width, minWidth, maxWidth, edge } = input;
64+
const widerToTheLeft = edge === "left";
65+
const step = shiftKey ? KEYBOARD_COARSE_STEP : KEYBOARD_STEP;
66+
switch (key) {
67+
case "ArrowLeft":
68+
return widerToTheLeft ? width + step : width - step;
69+
case "ArrowRight":
70+
return widerToTheLeft ? width - step : width + step;
71+
case "Home":
72+
return widerToTheLeft ? maxWidth : minWidth;
73+
case "End":
74+
return widerToTheLeft ? minWidth : maxWidth;
75+
default:
76+
return null;
77+
}
2778
}
2879

2980
/**
@@ -133,21 +184,30 @@ export function useResizableWidth(options: UseResizableWidthOptions): {
133184
[clamp, edge],
134185
);
135186

136-
const onPointerUp = useCallback(
137-
(event: ReactPointerEvent<HTMLElement>) => {
138-
const state = dragStateRef.current;
139-
if (!state || state.pointerId !== event.pointerId) return;
140-
const finalWidth = clamp(state.pending);
141-
releasePointer(event.pointerId);
142-
// Commit once at drag-end to avoid 60Hz localStorage writes.
187+
/** Persist and apply in one go — the end of a drag, or a single key press. */
188+
const commit = useCallback(
189+
(value: number) => {
190+
const finalWidth = clamp(value);
143191
try {
144192
setLocalStorageItem(storageKey, finalWidth, WidthSchema);
145193
} catch (error) {
146194
console.error("Could not persist panel width.", error);
147195
}
148196
setWidth(finalWidth);
149197
},
150-
[clamp, releasePointer, storageKey],
198+
[clamp, storageKey],
199+
);
200+
201+
const onPointerUp = useCallback(
202+
(event: ReactPointerEvent<HTMLElement>) => {
203+
const state = dragStateRef.current;
204+
if (!state || state.pointerId !== event.pointerId) return;
205+
const pending = state.pending;
206+
releasePointer(event.pointerId);
207+
// Commit once at drag-end to avoid 60Hz localStorage writes.
208+
commit(pending);
209+
},
210+
[commit, releasePointer],
151211
);
152212

153213
const onPointerCancel = useCallback(
@@ -161,8 +221,27 @@ export function useResizableWidth(options: UseResizableWidthOptions): {
161221
[releasePointer],
162222
);
163223

224+
const onKeyDown = useCallback(
225+
(event: ReactKeyboardEvent<HTMLElement>) => {
226+
// Modified presses belong to the browser or the app, not to the handle.
227+
if (event.altKey || event.ctrlKey || event.metaKey) return;
228+
const next = nextWidthForKey({
229+
key: event.key,
230+
shiftKey: event.shiftKey,
231+
width: clampedWidth,
232+
minWidth,
233+
maxWidth,
234+
edge,
235+
});
236+
if (next === null) return;
237+
event.preventDefault();
238+
commit(next);
239+
},
240+
[clampedWidth, commit, edge, maxWidth, minWidth],
241+
);
242+
164243
return {
165244
width: clampedWidth,
166-
handlers: { onPointerDown, onPointerMove, onPointerUp, onPointerCancel },
245+
handlers: { onPointerDown, onPointerMove, onPointerUp, onPointerCancel, onKeyDown },
167246
};
168247
}

0 commit comments

Comments
 (0)