-
Notifications
You must be signed in to change notification settings - Fork 411
Expand file tree
/
Copy pathSplitPane.tsx
More file actions
332 lines (292 loc) · 9.03 KB
/
SplitPane.tsx
File metadata and controls
332 lines (292 loc) · 9.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
import type { CSSProperties, ReactElement } from 'react';
import {
Children,
useCallback,
useEffect,
useMemo,
useRef,
useState,
} from 'react';
import type { SplitPaneProps, PaneProps, ResizeEvent } from '../types';
import { Pane } from './Pane';
import { Divider } from './Divider';
import { useResizer } from '../hooks/useResizer';
import { useKeyboardResize } from '../hooks/useKeyboardResize';
import { convertToPixels, distributeSizes } from '../utils/calculations';
import { cn } from '../utils/classNames';
const DEFAULT_CLASSNAME = 'split-pane';
const MIN_PANES = 2;
/**
* A flexible split pane component that allows resizable pane layouts.
*
* Supports horizontal (side-by-side) and vertical (stacked) layouts with
* mouse, touch, and keyboard interactions. Fully accessible with ARIA attributes.
*
* @example
* ```tsx
* // Basic horizontal split
* <SplitPane direction="horizontal">
* <Pane minSize="200px" defaultSize="300px">
* <Sidebar />
* </Pane>
* <Pane>
* <MainContent />
* </Pane>
* </SplitPane>
*
* // Controlled mode with state
* const [sizes, setSizes] = useState([300, 500]);
* <SplitPane onResize={setSizes}>
* <Pane size={sizes[0]}>Left</Pane>
* <Pane size={sizes[1]}>Right</Pane>
* </SplitPane>
* ```
*/
export function SplitPane(props: SplitPaneProps) {
const {
direction = 'horizontal',
resizable = true,
snapPoints,
snapTolerance = 10,
step,
onResizeStart,
onResize,
onResizeEnd,
className,
style,
divider: CustomDivider,
dividerStyle,
dividerClassName,
children,
} = props;
const containerRef = useRef<HTMLDivElement>(null);
const [containerSize, setContainerSize] = useState(0);
const prevContainerSizeRef = useRef(0);
// Extract pane configuration from children - memoized to avoid recreating on every render
const paneConfigs = useMemo(() => {
const paneElements = Children.toArray(children).filter(
(child): child is ReactElement<PaneProps> =>
typeof child === 'object' && child !== null && 'props' in child
);
return paneElements.map((pane) => ({
props: pane.props,
size: pane.props.size,
defaultSize: pane.props.defaultSize,
minSize: pane.props.minSize ?? 0,
maxSize: pane.props.maxSize ?? Infinity,
}));
}, [children]);
const paneCount = paneConfigs.length;
const warnedRef = useRef(false);
// Warn once if fewer than 2 panes
if (paneCount < MIN_PANES && !warnedRef.current) {
warnedRef.current = true;
console.warn(
`SplitPane requires at least ${MIN_PANES} Pane children. Received ${paneCount}.`
);
}
// Calculate min/max sizes from pane configs
const { minSizes, maxSizes } = useMemo(() => {
if (containerSize === 0) {
return {
minSizes: new Array(paneCount).fill(0),
maxSizes: new Array(paneCount).fill(Infinity),
};
}
const mins: number[] = [];
const maxs: number[] = [];
paneConfigs.forEach((config) => {
mins.push(convertToPixels(config.minSize, containerSize));
maxs.push(
config.maxSize === Infinity
? Infinity
: convertToPixels(config.maxSize, containerSize)
);
});
return { minSizes: mins, maxSizes: maxs };
}, [containerSize, paneCount, paneConfigs]);
// Calculate initial sizes from pane configs
const calculateInitialSizes = useCallback(
(containerSz: number): number[] => {
if (containerSz === 0) {
return new Array(paneCount).fill(0);
}
// First pass: calculate sizes for panes with explicit sizes
const sizes: (number | null)[] = paneConfigs.map((config) => {
const paneSize = config.size ?? config.defaultSize;
if (paneSize !== undefined) {
return convertToPixels(paneSize, containerSz);
}
return null; // Mark as needing auto-size
});
// Calculate remaining space and distribute to auto-sized panes
const explicitTotal = sizes.reduce<number>(
(sum, size) => sum + (size ?? 0),
0
);
const autoSizedCount = sizes.filter((s) => s === null).length;
const remainingSpace = containerSz - explicitTotal;
const autoSize = autoSizedCount > 0 ? remainingSpace / autoSizedCount : 0;
// Second pass: fill in auto-sized panes
return sizes.map((size) => (size === null ? autoSize : size));
},
[paneCount, paneConfigs]
);
const [paneSizes, setPaneSizes] = useState<number[]>(() =>
calculateInitialSizes(containerSize)
);
// Handle container size changes - update sizes proportionally
// Using a ref comparison to avoid effect dependency issues
const handleContainerSizeChange = useCallback(
(newContainerSize: number) => {
const prevSize = prevContainerSizeRef.current;
prevContainerSizeRef.current = newContainerSize;
if (newContainerSize === 0) return;
setPaneSizes((currentSizes) => {
// If sizes are uninitialized or pane count changed
if (
currentSizes.every((s) => s === 0) ||
currentSizes.length !== paneCount
) {
return calculateInitialSizes(newContainerSize);
}
// If container size changed, distribute proportionally
if (prevSize > 0 && prevSize !== newContainerSize) {
return distributeSizes(currentSizes, newContainerSize);
}
// First measurement - use initial sizes
if (prevSize === 0) {
return calculateInitialSizes(newContainerSize);
}
return currentSizes;
});
},
[paneCount, calculateInitialSizes]
);
// Measure container size with ResizeObserver
useEffect(() => {
const container = containerRef.current;
if (!container) return;
const updateSizeFromRect = (rect: { width: number; height: number }) => {
const size = direction === 'horizontal' ? rect.width : rect.height;
if (size > 0) {
setContainerSize(size);
handleContainerSizeChange(size);
}
};
const resizeObserver = new ResizeObserver((entries) => {
const entry = entries[0];
if (entry) {
updateSizeFromRect(entry.contentRect);
}
});
resizeObserver.observe(container);
return () => {
resizeObserver.disconnect();
};
}, [direction, handleContainerSizeChange]);
// Handle resize callback
const handleResize = useCallback(
(newSizes: number[], event: ResizeEvent) => {
setPaneSizes(newSizes);
onResize?.(newSizes, event);
},
[onResize]
);
// Resizer hook
const {
isDragging,
currentSizes,
handleMouseDown,
handleTouchStart,
handleTouchEnd,
} = useResizer({
direction,
sizes: paneSizes,
minSizes,
maxSizes,
snapPoints,
snapTolerance,
step,
onResizeStart,
onResize: handleResize,
onResizeEnd,
});
// Keyboard resize hook
const { handleKeyDown } = useKeyboardResize({
direction,
sizes: currentSizes,
minSizes,
maxSizes,
step,
onResize: handleResize,
onResizeEnd,
});
// Container styles
const containerStyle: CSSProperties = {
display: 'flex',
flexDirection: direction === 'horizontal' ? 'row' : 'column',
height: '100%',
width: '100%',
overflow: 'hidden',
position: 'relative',
...style,
};
const containerClassName = cn(DEFAULT_CLASSNAME, direction, className);
// Render panes and dividers
const renderChildren = () => {
if (paneCount < MIN_PANES) {
return null;
}
const elements: JSX.Element[] = [];
paneConfigs.forEach((config, index) => {
const paneSize = currentSizes[index] ?? 0;
const paneStyle: CSSProperties = {
...(direction === 'horizontal'
? { width: `${paneSize}px`, height: '100%' }
: { height: `${paneSize}px`, width: '100%' }),
...config.props.style,
};
// Render pane
elements.push(
<Pane key={`pane-${index}`} {...config.props} style={paneStyle}>
{config.props.children}
</Pane>
);
// Render divider (except after last pane)
if (index < paneCount - 1) {
const DividerComponent = CustomDivider ?? Divider;
const dividerMinSize = minSizes[index];
const dividerMaxSize = maxSizes[index];
elements.push(
<DividerComponent
key={`divider-${index}`}
direction={direction}
index={index}
isDragging={isDragging}
disabled={!resizable}
onMouseDown={handleMouseDown(index)}
onTouchStart={handleTouchStart(index)}
onTouchEnd={handleTouchEnd}
onKeyDown={handleKeyDown(index)}
className={dividerClassName}
style={dividerStyle}
currentSize={paneSize}
minSize={dividerMinSize}
maxSize={dividerMaxSize === Infinity ? undefined : dividerMaxSize}
/>
);
}
});
return elements;
};
return (
<div
ref={containerRef}
className={containerClassName}
style={containerStyle}
>
{containerSize > 0 && renderChildren()}
</div>
);
}