11import * as Schema from "effect/Schema" ;
2- import { type PointerEvent as ReactPointerEvent , useLayoutEffect , useRef , useState } from "react" ;
2+ import {
3+ type KeyboardEvent as ReactKeyboardEvent ,
4+ type PointerEvent as ReactPointerEvent ,
5+ useLayoutEffect ,
6+ useRef ,
7+ useState ,
8+ } from "react" ;
39
410import { getLocalStorageItem , setLocalStorageItem } from "./useLocalStorage" ;
511import { useResizeDrag } from "./useResizeDrag" ;
@@ -20,19 +26,25 @@ export interface UseResizableWidthOptions {
2026}
2127
2228export interface ResizableWidthHandlers {
29+ readonly onKeyDown : ( event : ReactKeyboardEvent < HTMLElement > ) => void ;
2330 readonly onPointerDown : ( event : ReactPointerEvent < HTMLElement > ) => void ;
2431 readonly onPointerMove : ( event : ReactPointerEvent < HTMLElement > ) => void ;
2532 readonly onPointerUp : ( event : ReactPointerEvent < HTMLElement > ) => void ;
2633 readonly onPointerCancel : ( event : ReactPointerEvent < HTMLElement > ) => void ;
2734 readonly onLostPointerCapture : ( event : ReactPointerEvent < HTMLElement > ) => void ;
2835}
2936
37+ const KEYBOARD_RESIZE_STEP = 16 ;
38+ const KEYBOARD_RESIZE_LARGE_STEP = 64 ;
39+
3040/**
3141 * Width is read on mount or storage-key changes and persisted on drag-end (not on every rAF tick
3242 * — would otherwise be ~60 writes/sec).
3343 */
3444export function useResizableWidth ( options : UseResizableWidthOptions ) : {
3545 readonly width : number ;
46+ readonly minWidth : number ;
47+ readonly maxWidth : number ;
3648 readonly handlers : ResizableWidthHandlers ;
3749} {
3850 const { storageKey, defaultWidth, minWidth, maxWidth, edge } = options ;
@@ -98,5 +110,35 @@ export function useResizableWidth(options: UseResizableWidthOptions): {
98110 // rewrites the committed width, so re-apply the live pointer position.
99111 useLayoutEffect ( refresh , [ clamp , clampedWidth , refresh ] ) ;
100112
101- return { width : clampedWidth , handlers } ;
113+ const onKeyDown = ( event : ReactKeyboardEvent < HTMLElement > ) => {
114+ if ( event . nativeEvent . isComposing || event . ctrlKey || event . altKey || event . metaKey ) return ;
115+
116+ let nextWidth : number | undefined ;
117+ if ( event . key === "Home" ) {
118+ nextWidth = minWidth ;
119+ } else if ( event . key === "End" && Number . isFinite ( maxWidth ) ) {
120+ nextWidth = maxWidth ;
121+ } else if ( event . key === "ArrowLeft" || event . key === "ArrowRight" ) {
122+ const step = event . shiftKey ? KEYBOARD_RESIZE_LARGE_STEP : KEYBOARD_RESIZE_STEP ;
123+ const direction =
124+ event . key === "ArrowLeft" ? ( edge === "left" ? 1 : - 1 ) : edge === "left" ? - 1 : 1 ;
125+ nextWidth = clamp ( clampedWidth + direction * step ) ;
126+ }
127+ if ( nextWidth === undefined ) return ;
128+
129+ event . preventDefault ( ) ;
130+ if ( nextWidth === clampedWidth ) return ;
131+ event . currentTarget . parentElement ?. style . setProperty (
132+ RESIZABLE_WIDTH_PROPERTY ,
133+ `${ nextWidth } px` ,
134+ ) ;
135+ setWidthState ( { storageKey, width : nextWidth } ) ;
136+ try {
137+ setLocalStorageItem ( storageKey , nextWidth , WidthSchema ) ;
138+ } catch ( error ) {
139+ console . error ( "Could not persist panel width." , error ) ;
140+ }
141+ } ;
142+
143+ return { width : clampedWidth , minWidth, maxWidth, handlers : { ...handlers , onKeyDown } } ;
102144}
0 commit comments