MCPcopy
hub / github.com/tomkp/react-split-pane / useKeyboardResize

Function useKeyboardResize

src/hooks/useKeyboardResize.ts:39–216  ·  view source on GitHub ↗
(options: UseKeyboardResizeOptions)

Source from the content-addressed store, hash-verified

37 * @returns Handler for keyboard events
38 */
39export function useKeyboardResize(options: UseKeyboardResizeOptions) {
40 const {
41 direction,
42 sizes,
43 minSizes,
44 maxSizes,
45 step = DEFAULT_STEP,
46 largeStep = DEFAULT_LARGE_STEP,
47 onResize,
48 onResizeEnd,
49 } = options;
50
51 // Track sizes at start of keyboard interaction for Escape to restore
52 const initialSizesRef = useRef<number[] | null>(null);
53
54 const handleKeyDown = useCallback(
55 (dividerIndex: number) => (e: React.KeyboardEvent) => {
56 const isHorizontal = direction === 'horizontal';
57 const moveKeys = isHorizontal
58 ? ['ArrowLeft', 'ArrowRight']
59 : ['ArrowUp', 'ArrowDown'];
60
61 if (
62 !moveKeys.includes(e.key) &&
63 e.key !== 'Home' &&
64 e.key !== 'End' &&
65 e.key !== 'Escape'
66 ) {
67 return;
68 }
69
70 e.preventDefault();
71
72 // Handle Escape - restore to initial sizes
73 if (e.key === 'Escape') {
74 if (initialSizesRef.current) {
75 const restoredSizes = initialSizesRef.current;
76 initialSizesRef.current = null;
77
78 if (onResize) {
79 onResize(restoredSizes, {
80 sizes: restoredSizes,
81 source: 'keyboard',
82 originalEvent: e.nativeEvent,
83 });
84 }
85
86 if (onResizeEnd) {
87 onResizeEnd(restoredSizes, {
88 sizes: restoredSizes,
89 source: 'keyboard',
90 originalEvent: e.nativeEvent,
91 });
92 }
93
94 announce('Pane sizes restored');
95 }
96 return;

Callers 2

SplitPaneFunction · 0.90

Calls 4

announceFunction · 0.90
calculateDraggedSizesFunction · 0.90
clampFunction · 0.90

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…