@octanejs/mantine-hooks 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +29 -0
- package/package.json +45 -0
- package/src/index.ts +259 -0
- package/src/internal.ts +6 -0
- package/src/use-click-outside/use-click-outside.ts +57 -0
- package/src/use-clipboard/use-clipboard.ts +69 -0
- package/src/use-collapse/use-collapse.ts +162 -0
- package/src/use-collapse/use-horizontal-collapse.ts +164 -0
- package/src/use-color-scheme/use-color-scheme.ts +13 -0
- package/src/use-counter/use-counter.ts +59 -0
- package/src/use-debounced-callback/use-debounced-callback.ts +159 -0
- package/src/use-debounced-state/use-debounced-state.ts +44 -0
- package/src/use-debounced-value/use-debounced-value.ts +71 -0
- package/src/use-did-update/use-did-update.ts +26 -0
- package/src/use-disclosure/use-disclosure.ts +57 -0
- package/src/use-document-title/use-document-title.ts +9 -0
- package/src/use-document-visibility/use-document-visibility.ts +14 -0
- package/src/use-drag/use-drag.ts +463 -0
- package/src/use-event-listener/use-event-listener.ts +47 -0
- package/src/use-eye-dropper/use-eye-dropper.ts +41 -0
- package/src/use-favicon/use-favicon.ts +41 -0
- package/src/use-fetch/use-fetch.ts +85 -0
- package/src/use-file-dialog/use-file-dialog.ts +140 -0
- package/src/use-floating-window/use-floating-window.ts +431 -0
- package/src/use-focus-return/use-focus-return.ts +65 -0
- package/src/use-focus-trap/scope-tab.ts +36 -0
- package/src/use-focus-trap/tabbable.ts +65 -0
- package/src/use-focus-trap/use-focus-trap.ts +84 -0
- package/src/use-focus-within/use-focus-within.ts +90 -0
- package/src/use-force-update/use-force-update.ts +8 -0
- package/src/use-fullscreen/use-fullscreen.ts +153 -0
- package/src/use-hash/use-hash.ts +42 -0
- package/src/use-headroom/use-headroom.ts +160 -0
- package/src/use-hotkeys/parse-hotkey.ts +130 -0
- package/src/use-hotkeys/use-hotkeys.ts +63 -0
- package/src/use-hover/use-hover.ts +57 -0
- package/src/use-id/use-id.ts +23 -0
- package/src/use-idle/use-idle.ts +54 -0
- package/src/use-in-viewport/use-in-viewport.ts +34 -0
- package/src/use-input-state/use-input-state.ts +41 -0
- package/src/use-intersection/use-intersection.ts +41 -0
- package/src/use-interval/use-interval.ts +84 -0
- package/src/use-is-first-render/use-is-first-render.ts +12 -0
- package/src/use-isomorphic-effect/use-isomorphic-effect.ts +5 -0
- package/src/use-list-state/use-list-state.ts +157 -0
- package/src/use-local-storage/create-storage.ts +200 -0
- package/src/use-local-storage/use-local-storage.ts +19 -0
- package/src/use-logger/use-logger.ts +16 -0
- package/src/use-long-press/use-long-press.ts +188 -0
- package/src/use-map/use-map.ts +28 -0
- package/src/use-mask/use-mask.ts +1049 -0
- package/src/use-media-query/use-media-query.ts +57 -0
- package/src/use-merged-ref/use-merged-ref.ts +53 -0
- package/src/use-mounted/use-mounted.ts +7 -0
- package/src/use-mouse/use-mouse.ts +91 -0
- package/src/use-move/use-move.ts +165 -0
- package/src/use-mutation-observer/use-mutation-observer.ts +61 -0
- package/src/use-network/use-network.ts +70 -0
- package/src/use-orientation/use-orientation.ts +87 -0
- package/src/use-os/use-os.ts +93 -0
- package/src/use-page-leave/use-page-leave.ts +10 -0
- package/src/use-pagination/use-pagination.ts +149 -0
- package/src/use-previous/use-previous.ts +11 -0
- package/src/use-queue/use-queue.ts +71 -0
- package/src/use-radial-move/use-radial-move.ts +161 -0
- package/src/use-reduced-motion/use-reduced-motion.ts +6 -0
- package/src/use-resize-observer/use-resize-observer.ts +95 -0
- package/src/use-roving-index/use-roving-index.ts +386 -0
- package/src/use-scroll-direction/use-scroll-direction.ts +41 -0
- package/src/use-scroll-into-view/use-scroll-into-view.ts +294 -0
- package/src/use-scroll-spy/use-scroll-spy.ts +155 -0
- package/src/use-scroller/use-scroller.ts +225 -0
- package/src/use-selection/use-selection.ts +120 -0
- package/src/use-session-storage/use-session-storage.ts +19 -0
- package/src/use-set/use-set.ts +88 -0
- package/src/use-set-state/use-set-state.ts +29 -0
- package/src/use-shallow-effect/use-shallow-effect.ts +40 -0
- package/src/use-splitter/use-splitter.ts +1242 -0
- package/src/use-state-history/use-state-history.ts +70 -0
- package/src/use-text-selection/use-text-selection.ts +20 -0
- package/src/use-throttled-callback/use-throttled-callback.ts +60 -0
- package/src/use-throttled-state/use-throttled-state.ts +12 -0
- package/src/use-throttled-value/use-throttled-value.ts +23 -0
- package/src/use-timeout/use-timeout.ts +56 -0
- package/src/use-toggle/use-toggle.ts +23 -0
- package/src/use-uncontrolled/use-uncontrolled.ts +53 -0
- package/src/use-validated-state/use-validated-state.ts +50 -0
- package/src/use-viewport-size/use-viewport-size.ts +23 -0
- package/src/use-window-event/use-window-event.ts +16 -0
- package/src/use-window-scroll/use-window-scroll.ts +49 -0
- package/src/utils/clamp/clamp.ts +15 -0
- package/src/utils/index.ts +7 -0
- package/src/utils/lower-first/lower-first.ts +3 -0
- package/src/utils/random-id/random-id.ts +3 -0
- package/src/utils/range/range.ts +10 -0
- package/src/utils/shallow-equal/shallow-equal.ts +34 -0
- package/src/utils/upper-first/upper-first.ts +3 -0
- package/src/utils/use-callback-ref/use-callback-ref.ts +11 -0
|
@@ -0,0 +1,1242 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-deprecated */
|
|
2
|
+
import { useCallback, useEffect, useRef, useState } from 'octane';
|
|
3
|
+
import { useUncontrolled } from '../use-uncontrolled/use-uncontrolled';
|
|
4
|
+
|
|
5
|
+
/** Pane size expressed in CSS units. A bare `number` or `%` string is a flexible size (shares
|
|
6
|
+
* the leftover space), `px`/`rem` strings are fixed sizes that keep their pixel size when the
|
|
7
|
+
* container is resized. */
|
|
8
|
+
export type SplitterPaneSize = number | `${number}%` | `${number}px` | `${number}rem`;
|
|
9
|
+
|
|
10
|
+
/** Keyboard step expressed in CSS units. A bare `number` or `%` string is a percentage of the
|
|
11
|
+
* container, `px`/`rem` strings are resolved to pixels. */
|
|
12
|
+
export type SplitterStep = number | `${number}%` | `${number}px` | `${number}rem`;
|
|
13
|
+
|
|
14
|
+
export interface UseSplitterPanel {
|
|
15
|
+
/** Initial size, a `number`/`%` is a flexible size, `px`/`rem` is a fixed size. A bare number is treated as a percentage. */
|
|
16
|
+
defaultSize: SplitterPaneSize;
|
|
17
|
+
/** Minimum size in the same units as `defaultSize`, `0` by default */
|
|
18
|
+
min?: SplitterPaneSize;
|
|
19
|
+
/** Maximum size in the same units as `defaultSize`, no limit by default */
|
|
20
|
+
max?: SplitterPaneSize;
|
|
21
|
+
/** Whether this panel can be collapsed, `false` by default */
|
|
22
|
+
collapsible?: boolean;
|
|
23
|
+
/** Size below which the panel snaps to collapsed, defaults to `min` */
|
|
24
|
+
collapseThreshold?: SplitterPaneSize;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** Panel configuration resolved to numeric units (percent or pixels) passed to redistribute functions */
|
|
28
|
+
export interface UseSplitterResolvedPanel {
|
|
29
|
+
/** Resolved default size in the same units as redistribute sizes */
|
|
30
|
+
defaultSize: number;
|
|
31
|
+
/** Resolved minimum size */
|
|
32
|
+
min?: number;
|
|
33
|
+
/** Resolved maximum size */
|
|
34
|
+
max?: number;
|
|
35
|
+
/** Whether this panel can be collapsed */
|
|
36
|
+
collapsible?: boolean;
|
|
37
|
+
/** Resolved collapse threshold */
|
|
38
|
+
collapseThreshold?: number;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export interface UseSplitterRedistributeInput {
|
|
42
|
+
/** Current sizes before applying delta, in resolved units (percent or pixels) */
|
|
43
|
+
sizes: number[];
|
|
44
|
+
/** Resolved panel configurations, in the same units as `sizes` */
|
|
45
|
+
panels: UseSplitterResolvedPanel[];
|
|
46
|
+
/** Index of the handle being dragged */
|
|
47
|
+
handleIndex: number;
|
|
48
|
+
/** Requested size change in resolved units (positive = grow before-panel) */
|
|
49
|
+
delta: number;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export type UseSplitterRedistributeFn = (input: UseSplitterRedistributeInput) => number[];
|
|
53
|
+
|
|
54
|
+
export interface UseSplitterOptions {
|
|
55
|
+
/** Panel configuration array (minimum 2 panels) */
|
|
56
|
+
panels: UseSplitterPanel[];
|
|
57
|
+
/** Layout direction, `'horizontal'` by default */
|
|
58
|
+
orientation?: 'horizontal' | 'vertical';
|
|
59
|
+
/** Controlled sizes, each value keeps the unit it was declared in */
|
|
60
|
+
sizes?: SplitterPaneSize[];
|
|
61
|
+
/** Called during resize with updated sizes, each value keeps its declared unit */
|
|
62
|
+
onSizeChange?: (sizes: SplitterPaneSize[]) => void;
|
|
63
|
+
/** Called when drag starts */
|
|
64
|
+
onResizeStart?: (handleIndex: number) => void;
|
|
65
|
+
/** Called when drag ends */
|
|
66
|
+
onResizeEnd?: (handleIndex: number, sizes: SplitterPaneSize[]) => void;
|
|
67
|
+
/** Called when a panel collapses or expands */
|
|
68
|
+
onCollapseChange?: (panelIndex: number, collapsed: boolean) => void;
|
|
69
|
+
/** How to borrow space from non-adjacent panels when the immediate neighbor is at its min/max.
|
|
70
|
+
* `'nearest'` takes from the nearest panel in the drag direction first.
|
|
71
|
+
* `'equal'` distributes equally among all panels in the drag direction.
|
|
72
|
+
* A function receives sizes, panels, handleIndex and delta, and returns new sizes.
|
|
73
|
+
* When not set, only the two adjacent panels are affected. */
|
|
74
|
+
redistribute?: 'nearest' | 'equal' | UseSplitterRedistributeFn;
|
|
75
|
+
/** Keyboard step size, a `number`/`%` is a percentage, `px`/`rem` is resolved to pixels, `1` by default */
|
|
76
|
+
step?: SplitterStep;
|
|
77
|
+
/** Shift+arrow step size, a `number`/`%` is a percentage, `px`/`rem` is resolved to pixels, `10` by default */
|
|
78
|
+
shiftStep?: SplitterStep;
|
|
79
|
+
/** Text direction for keyboard nav, `'ltr'` by default */
|
|
80
|
+
dir?: 'ltr' | 'rtl';
|
|
81
|
+
/** Restore the two panels adjacent to a handle to their default ratio (preserving their combined size) when the handle is double-clicked, `true` by default */
|
|
82
|
+
resetOnDoubleClick?: boolean;
|
|
83
|
+
/** Enable/disable the hook, `true` by default */
|
|
84
|
+
enabled?: boolean;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export interface UseSplitterReturnValue<T extends HTMLElement = any> {
|
|
88
|
+
/** Ref callback for the container element */
|
|
89
|
+
ref: React.RefCallback<T | null>;
|
|
90
|
+
/** Current panel sizes, each value keeps the unit it was declared in */
|
|
91
|
+
sizes: SplitterPaneSize[];
|
|
92
|
+
/** Whether sizes are tracked in pixels because any pane size, `min`, `max`, `step`, `shiftStep`
|
|
93
|
+
* or `collapseThreshold` uses a fixed `px`/`rem` unit */
|
|
94
|
+
pixelMode: boolean;
|
|
95
|
+
/** Which panels are currently collapsed */
|
|
96
|
+
collapsed: boolean[];
|
|
97
|
+
/** Index of handle being dragged, or -1 */
|
|
98
|
+
activeHandle: number;
|
|
99
|
+
/** Get props to spread on each resize handle */
|
|
100
|
+
getHandleProps: (input: { index: number }) => {
|
|
101
|
+
ref: React.RefCallback<HTMLElement>;
|
|
102
|
+
role: 'separator';
|
|
103
|
+
'aria-orientation': 'horizontal' | 'vertical';
|
|
104
|
+
'aria-valuenow': number;
|
|
105
|
+
'aria-valuemin': number;
|
|
106
|
+
'aria-valuemax': number;
|
|
107
|
+
tabIndex: number;
|
|
108
|
+
onKeyDown: React.KeyboardEventHandler;
|
|
109
|
+
onDoubleClick: React.MouseEventHandler;
|
|
110
|
+
'data-active': boolean | undefined;
|
|
111
|
+
'data-orientation': 'horizontal' | 'vertical';
|
|
112
|
+
};
|
|
113
|
+
/** Programmatically set sizes, each value keeps its declared unit */
|
|
114
|
+
setSizes: (sizes: SplitterPaneSize[]) => void;
|
|
115
|
+
/** Collapse a panel */
|
|
116
|
+
collapse: (panelIndex: number) => void;
|
|
117
|
+
/** Expand a collapsed panel */
|
|
118
|
+
expand: (panelIndex: number) => void;
|
|
119
|
+
/** Toggle collapse of a panel */
|
|
120
|
+
toggleCollapse: (panelIndex: number) => void;
|
|
121
|
+
/** Reset the two panels adjacent to a handle to their default ratio, preserving
|
|
122
|
+
* their combined size */
|
|
123
|
+
reset: (handleIndex: number) => void;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
const PX_RE = /^(-?[\d.]+)px$/;
|
|
127
|
+
const REM_RE = /^(-?[\d.]+)rem$/;
|
|
128
|
+
const PERCENT_RE = /^(-?[\d.]+)%$/;
|
|
129
|
+
|
|
130
|
+
function isFixedSize(size: SplitterPaneSize | undefined): boolean {
|
|
131
|
+
return typeof size === 'string' && (PX_RE.test(size) || REM_RE.test(size));
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
function sizeMagnitude(size: SplitterPaneSize): number {
|
|
135
|
+
return typeof size === 'number' ? size : parseFloat(size);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
function detectPixelMode(options: UseSplitterOptions): boolean {
|
|
139
|
+
return (
|
|
140
|
+
options.panels.some(
|
|
141
|
+
(panel) =>
|
|
142
|
+
isFixedSize(panel.defaultSize) ||
|
|
143
|
+
isFixedSize(panel.min) ||
|
|
144
|
+
isFixedSize(panel.max) ||
|
|
145
|
+
isFixedSize(panel.collapseThreshold),
|
|
146
|
+
) ||
|
|
147
|
+
isFixedSize(options.step) ||
|
|
148
|
+
isFixedSize(options.shiftStep) ||
|
|
149
|
+
(options.sizes?.some(isFixedSize) ?? false)
|
|
150
|
+
);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
function getRootFontSize(): number {
|
|
154
|
+
if (typeof window === 'undefined') {
|
|
155
|
+
return 16;
|
|
156
|
+
}
|
|
157
|
+
const fontSize = parseFloat(getComputedStyle(document.documentElement).fontSize);
|
|
158
|
+
return Number.isFinite(fontSize) && fontSize > 0 ? fontSize : 16;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
function resolveSize(
|
|
162
|
+
size: SplitterPaneSize,
|
|
163
|
+
pixelMode: boolean,
|
|
164
|
+
containerPx: number,
|
|
165
|
+
rootFontSize: number,
|
|
166
|
+
): number {
|
|
167
|
+
if (!pixelMode) {
|
|
168
|
+
return sizeMagnitude(size);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
if (typeof size === 'number') {
|
|
172
|
+
return (size / 100) * containerPx;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
const percent = PERCENT_RE.exec(size);
|
|
176
|
+
if (percent) {
|
|
177
|
+
return (parseFloat(percent[1]) / 100) * containerPx;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
const rem = REM_RE.exec(size);
|
|
181
|
+
if (rem) {
|
|
182
|
+
return parseFloat(rem[1]) * rootFontSize;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
const px = PX_RE.exec(size);
|
|
186
|
+
if (px) {
|
|
187
|
+
return parseFloat(px[1]);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
return 0;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/** Down-scaling factor applied to fixed panes when their combined pixel size overflows the
|
|
194
|
+
* container, so they shrink to fit (matching `resolveWorkingSizes`). Returns `1` when nothing
|
|
195
|
+
* overflows or the layout is not in pixel mode. Encoders divide by this to invert the scaling and
|
|
196
|
+
* persist the original absolute sizes instead of the shrunk-to-fit ones. */
|
|
197
|
+
function getFixedScale(
|
|
198
|
+
sizes: SplitterPaneSize[],
|
|
199
|
+
pixelMode: boolean,
|
|
200
|
+
containerPx: number,
|
|
201
|
+
rootFontSize: number,
|
|
202
|
+
): number {
|
|
203
|
+
if (!pixelMode) {
|
|
204
|
+
return 1;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
let fixedTotal = 0;
|
|
208
|
+
sizes.forEach((size) => {
|
|
209
|
+
if (isFixedSize(size)) {
|
|
210
|
+
fixedTotal += resolveSize(size, true, containerPx, rootFontSize);
|
|
211
|
+
}
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
return fixedTotal > containerPx && fixedTotal > 0 ? containerPx / fixedTotal : 1;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/** Resolves all sizes to pixels at once. Fixed panes get their absolute pixel size, flexible panes
|
|
218
|
+
* share the leftover space by their weight ratio – matching how the layout is rendered with
|
|
219
|
+
* `flex-grow`, so drag math operates on the same pixel sizes the user sees. */
|
|
220
|
+
function resolveWorkingSizes(
|
|
221
|
+
sizes: SplitterPaneSize[],
|
|
222
|
+
pixelMode: boolean,
|
|
223
|
+
containerPx: number,
|
|
224
|
+
rootFontSize: number,
|
|
225
|
+
): number[] {
|
|
226
|
+
if (!pixelMode) {
|
|
227
|
+
return sizes.map((size) => sizeMagnitude(size));
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
let fixedTotal = 0;
|
|
231
|
+
let flexibleWeight = 0;
|
|
232
|
+
sizes.forEach((size) => {
|
|
233
|
+
if (isFixedSize(size)) {
|
|
234
|
+
fixedTotal += resolveSize(size, true, containerPx, rootFontSize);
|
|
235
|
+
} else {
|
|
236
|
+
flexibleWeight += sizeMagnitude(size);
|
|
237
|
+
}
|
|
238
|
+
});
|
|
239
|
+
|
|
240
|
+
const leftover = Math.max(0, containerPx - fixedTotal);
|
|
241
|
+
const fixedScale = getFixedScale(sizes, pixelMode, containerPx, rootFontSize);
|
|
242
|
+
|
|
243
|
+
return sizes.map((size) => {
|
|
244
|
+
if (isFixedSize(size)) {
|
|
245
|
+
return resolveSize(size, true, containerPx, rootFontSize) * fixedScale;
|
|
246
|
+
}
|
|
247
|
+
return flexibleWeight > 0 ? (sizeMagnitude(size) / flexibleWeight) * leftover : 0;
|
|
248
|
+
});
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
function encodeSize(
|
|
252
|
+
value: number,
|
|
253
|
+
original: SplitterPaneSize,
|
|
254
|
+
pixelMode: boolean,
|
|
255
|
+
containerPx: number,
|
|
256
|
+
rootFontSize: number,
|
|
257
|
+
fixedScale: number = 1,
|
|
258
|
+
): SplitterPaneSize {
|
|
259
|
+
if (!pixelMode) {
|
|
260
|
+
return typeof original === 'string' && PERCENT_RE.test(original) ? `${value}%` : value;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
if (typeof original === 'number') {
|
|
264
|
+
return containerPx > 0 ? (value / containerPx) * 100 : original;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
if (PERCENT_RE.test(original)) {
|
|
268
|
+
return `${containerPx > 0 ? (value / containerPx) * 100 : parseFloat(original)}%`;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
const absolute = fixedScale > 0 ? value / fixedScale : value;
|
|
272
|
+
|
|
273
|
+
if (REM_RE.test(original)) {
|
|
274
|
+
return `${rootFontSize > 0 ? absolute / rootFontSize : 0}rem`;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
return `${absolute}px`;
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
/** Encodes working pixel sizes back to raw sizes after a resize, keeping the unit each pane was
|
|
281
|
+
* declared in. Panes whose working size did not change keep their original raw value. When fixed
|
|
282
|
+
* panes overflow the container they render down-scaled, so their working sizes are scaled back up to
|
|
283
|
+
* absolute sizes (preserving their declared sizes). If the resize instead hands space to a flexible
|
|
284
|
+
* pane the overflow clears and the layout leaves the down-scaled regime: every pane is then encoded
|
|
285
|
+
* from its current working size – including untouched fixed panes (so they do not jump back to their
|
|
286
|
+
* over-sized value) and untouched flexible panes (so a pane that was squeezed to `0` does not keep a
|
|
287
|
+
* stale weight and steal the freed space on the next render). */
|
|
288
|
+
function encodeWorkingSizes(
|
|
289
|
+
nextWorking: number[],
|
|
290
|
+
baseWorking: number[],
|
|
291
|
+
baseRaw: SplitterPaneSize[],
|
|
292
|
+
pixelMode: boolean,
|
|
293
|
+
containerPx: number,
|
|
294
|
+
rootFontSize: number,
|
|
295
|
+
): SplitterPaneSize[] {
|
|
296
|
+
const fixedScale = getFixedScale(baseRaw, pixelMode, containerPx, rootFontSize);
|
|
297
|
+
|
|
298
|
+
let fixedWorkingSum = 0;
|
|
299
|
+
nextWorking.forEach((value, i) => {
|
|
300
|
+
if (isFixedSize(baseRaw[i])) {
|
|
301
|
+
fixedWorkingSum += value;
|
|
302
|
+
}
|
|
303
|
+
});
|
|
304
|
+
const overflowCleared = fixedScale < 1 && fixedWorkingSum < containerPx - 1e-6;
|
|
305
|
+
const encodeScale = overflowCleared ? 1 : fixedScale;
|
|
306
|
+
|
|
307
|
+
return nextWorking.map((value, i) =>
|
|
308
|
+
overflowCleared || Math.abs(value - baseWorking[i]) > 1e-6
|
|
309
|
+
? encodeSize(value, baseRaw[i], pixelMode, containerPx, rootFontSize, encodeScale)
|
|
310
|
+
: baseRaw[i],
|
|
311
|
+
);
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
function resolvePanel(
|
|
315
|
+
panel: UseSplitterPanel,
|
|
316
|
+
pixelMode: boolean,
|
|
317
|
+
containerPx: number,
|
|
318
|
+
rootFontSize: number,
|
|
319
|
+
): UseSplitterResolvedPanel {
|
|
320
|
+
return {
|
|
321
|
+
defaultSize: resolveSize(panel.defaultSize, pixelMode, containerPx, rootFontSize),
|
|
322
|
+
min: panel.min != null ? resolveSize(panel.min, pixelMode, containerPx, rootFontSize) : 0,
|
|
323
|
+
max:
|
|
324
|
+
panel.max != null
|
|
325
|
+
? resolveSize(panel.max, pixelMode, containerPx, rootFontSize)
|
|
326
|
+
: pixelMode
|
|
327
|
+
? containerPx
|
|
328
|
+
: 100,
|
|
329
|
+
collapseThreshold:
|
|
330
|
+
panel.collapseThreshold != null
|
|
331
|
+
? resolveSize(panel.collapseThreshold, pixelMode, containerPx, rootFontSize)
|
|
332
|
+
: undefined,
|
|
333
|
+
collapsible: panel.collapsible,
|
|
334
|
+
};
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
function resolveStep(
|
|
338
|
+
step: SplitterStep,
|
|
339
|
+
pixelMode: boolean,
|
|
340
|
+
containerPx: number,
|
|
341
|
+
rootFontSize: number,
|
|
342
|
+
): number {
|
|
343
|
+
return resolveSize(step, pixelMode, containerPx, rootFontSize);
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
function clamp(value: number, min: number, max: number): number {
|
|
347
|
+
return Math.min(Math.max(value, min), max);
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
function getMin(panel: UseSplitterResolvedPanel): number {
|
|
351
|
+
return panel.min ?? 0;
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
function getMax(panel: UseSplitterResolvedPanel): number {
|
|
355
|
+
return panel.max ?? Infinity;
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
function getCollapseThreshold(panel: UseSplitterResolvedPanel): number {
|
|
359
|
+
return panel.collapseThreshold ?? getMin(panel);
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
interface SplitterInternalState {
|
|
363
|
+
isDragging: boolean;
|
|
364
|
+
handleIndex: number;
|
|
365
|
+
startPointer: number;
|
|
366
|
+
containerSize: number;
|
|
367
|
+
rootFontSize: number;
|
|
368
|
+
pixelMode: boolean;
|
|
369
|
+
startSizes: number[];
|
|
370
|
+
startRaw: SplitterPaneSize[];
|
|
371
|
+
preCollapseSizes: SplitterPaneSize[];
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
function createInitialInternalState(): SplitterInternalState {
|
|
375
|
+
return {
|
|
376
|
+
isDragging: false,
|
|
377
|
+
handleIndex: -1,
|
|
378
|
+
startPointer: 0,
|
|
379
|
+
containerSize: 0,
|
|
380
|
+
rootFontSize: 16,
|
|
381
|
+
pixelMode: false,
|
|
382
|
+
startSizes: [],
|
|
383
|
+
startRaw: [],
|
|
384
|
+
preCollapseSizes: [],
|
|
385
|
+
};
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
function checkCollapse(
|
|
389
|
+
sizes: number[],
|
|
390
|
+
panels: UseSplitterResolvedPanel[],
|
|
391
|
+
handleIndex: number,
|
|
392
|
+
delta: number,
|
|
393
|
+
): number[] | null {
|
|
394
|
+
const beforeIdx = handleIndex;
|
|
395
|
+
const afterIdx = handleIndex + 1;
|
|
396
|
+
const beforePanel = panels[beforeIdx];
|
|
397
|
+
const afterPanel = panels[afterIdx];
|
|
398
|
+
|
|
399
|
+
const rawBefore = sizes[beforeIdx] + delta;
|
|
400
|
+
const rawAfter = sizes[afterIdx] - delta;
|
|
401
|
+
|
|
402
|
+
if (
|
|
403
|
+
beforePanel.collapsible &&
|
|
404
|
+
rawBefore < getCollapseThreshold(beforePanel) &&
|
|
405
|
+
rawBefore < sizes[beforeIdx]
|
|
406
|
+
) {
|
|
407
|
+
const result = [...sizes];
|
|
408
|
+
result[afterIdx] += result[beforeIdx];
|
|
409
|
+
result[beforeIdx] = 0;
|
|
410
|
+
return result;
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
if (
|
|
414
|
+
afterPanel.collapsible &&
|
|
415
|
+
rawAfter < getCollapseThreshold(afterPanel) &&
|
|
416
|
+
rawAfter < sizes[afterIdx]
|
|
417
|
+
) {
|
|
418
|
+
const result = [...sizes];
|
|
419
|
+
result[beforeIdx] += result[afterIdx];
|
|
420
|
+
result[afterIdx] = 0;
|
|
421
|
+
return result;
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
return null;
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
function applyAdjacentOnly(
|
|
428
|
+
sizes: number[],
|
|
429
|
+
panels: UseSplitterResolvedPanel[],
|
|
430
|
+
handleIndex: number,
|
|
431
|
+
delta: number,
|
|
432
|
+
): number[] {
|
|
433
|
+
const result = [...sizes];
|
|
434
|
+
const beforeIdx = handleIndex;
|
|
435
|
+
const afterIdx = handleIndex + 1;
|
|
436
|
+
|
|
437
|
+
const total = result[beforeIdx] + result[afterIdx];
|
|
438
|
+
const effectiveBeforeMax = Math.min(getMax(panels[beforeIdx]), total - getMin(panels[afterIdx]));
|
|
439
|
+
const effectiveBeforeMin = Math.max(getMin(panels[beforeIdx]), total - getMax(panels[afterIdx]));
|
|
440
|
+
const newBefore = clamp(result[beforeIdx] + delta, effectiveBeforeMin, effectiveBeforeMax);
|
|
441
|
+
result[beforeIdx] = newBefore;
|
|
442
|
+
result[afterIdx] = total - newBefore;
|
|
443
|
+
return result;
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
function redistributeNearest(
|
|
447
|
+
sizes: number[],
|
|
448
|
+
panels: UseSplitterResolvedPanel[],
|
|
449
|
+
handleIndex: number,
|
|
450
|
+
delta: number,
|
|
451
|
+
): number[] {
|
|
452
|
+
const result = [...sizes];
|
|
453
|
+
|
|
454
|
+
if (delta > 0) {
|
|
455
|
+
const growIdx = handleIndex;
|
|
456
|
+
const maxGrow = getMax(panels[growIdx]) - result[growIdx];
|
|
457
|
+
const wantedGrow = Math.min(delta, maxGrow);
|
|
458
|
+
|
|
459
|
+
let taken = 0;
|
|
460
|
+
for (let i = handleIndex + 1; i < result.length && taken < wantedGrow; i += 1) {
|
|
461
|
+
const canGive = result[i] - getMin(panels[i]);
|
|
462
|
+
const take = Math.min(canGive, wantedGrow - taken);
|
|
463
|
+
result[i] -= take;
|
|
464
|
+
taken += take;
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
result[growIdx] += taken;
|
|
468
|
+
} else if (delta < 0) {
|
|
469
|
+
const growIdx = handleIndex + 1;
|
|
470
|
+
const maxGrow = getMax(panels[growIdx]) - result[growIdx];
|
|
471
|
+
const wantedGrow = Math.min(Math.abs(delta), maxGrow);
|
|
472
|
+
|
|
473
|
+
let taken = 0;
|
|
474
|
+
for (let i = handleIndex; i >= 0 && taken < wantedGrow; i -= 1) {
|
|
475
|
+
const canGive = result[i] - getMin(panels[i]);
|
|
476
|
+
const take = Math.min(canGive, wantedGrow - taken);
|
|
477
|
+
result[i] -= take;
|
|
478
|
+
taken += take;
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
result[growIdx] += taken;
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
return result;
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
function redistributeEqual(
|
|
488
|
+
sizes: number[],
|
|
489
|
+
panels: UseSplitterResolvedPanel[],
|
|
490
|
+
handleIndex: number,
|
|
491
|
+
delta: number,
|
|
492
|
+
): number[] {
|
|
493
|
+
const result = [...sizes];
|
|
494
|
+
|
|
495
|
+
if (delta > 0) {
|
|
496
|
+
const growIdx = handleIndex;
|
|
497
|
+
const maxGrow = getMax(panels[growIdx]) - result[growIdx];
|
|
498
|
+
const wantedGrow = Math.min(delta, maxGrow);
|
|
499
|
+
|
|
500
|
+
const donors: number[] = [];
|
|
501
|
+
for (let i = handleIndex + 1; i < result.length; i += 1) {
|
|
502
|
+
if (result[i] > getMin(panels[i])) {
|
|
503
|
+
donors.push(i);
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
let remaining = wantedGrow;
|
|
508
|
+
while (remaining > 0.001 && donors.length > 0) {
|
|
509
|
+
const perDonor = remaining / donors.length;
|
|
510
|
+
const exhausted: number[] = [];
|
|
511
|
+
|
|
512
|
+
for (let d = 0; d < donors.length; d += 1) {
|
|
513
|
+
const idx = donors[d];
|
|
514
|
+
const canGive = result[idx] - getMin(panels[idx]);
|
|
515
|
+
const take = Math.min(canGive, perDonor);
|
|
516
|
+
result[idx] -= take;
|
|
517
|
+
remaining -= take;
|
|
518
|
+
if (canGive <= perDonor + 0.001) {
|
|
519
|
+
exhausted.push(d);
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
for (let i = exhausted.length - 1; i >= 0; i -= 1) {
|
|
524
|
+
donors.splice(exhausted[i], 1);
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
if (exhausted.length === 0) {
|
|
528
|
+
break;
|
|
529
|
+
}
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
result[growIdx] += wantedGrow - remaining;
|
|
533
|
+
} else if (delta < 0) {
|
|
534
|
+
const growIdx = handleIndex + 1;
|
|
535
|
+
const maxGrow = getMax(panels[growIdx]) - result[growIdx];
|
|
536
|
+
const wantedGrow = Math.min(Math.abs(delta), maxGrow);
|
|
537
|
+
|
|
538
|
+
const donors: number[] = [];
|
|
539
|
+
for (let i = handleIndex; i >= 0; i -= 1) {
|
|
540
|
+
if (result[i] > getMin(panels[i])) {
|
|
541
|
+
donors.push(i);
|
|
542
|
+
}
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
let remaining = wantedGrow;
|
|
546
|
+
while (remaining > 0.001 && donors.length > 0) {
|
|
547
|
+
const perDonor = remaining / donors.length;
|
|
548
|
+
const exhausted: number[] = [];
|
|
549
|
+
|
|
550
|
+
for (let d = 0; d < donors.length; d += 1) {
|
|
551
|
+
const idx = donors[d];
|
|
552
|
+
const canGive = result[idx] - getMin(panels[idx]);
|
|
553
|
+
const take = Math.min(canGive, perDonor);
|
|
554
|
+
result[idx] -= take;
|
|
555
|
+
remaining -= take;
|
|
556
|
+
if (canGive <= perDonor + 0.001) {
|
|
557
|
+
exhausted.push(d);
|
|
558
|
+
}
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
for (let i = exhausted.length - 1; i >= 0; i -= 1) {
|
|
562
|
+
donors.splice(exhausted[i], 1);
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
if (exhausted.length === 0) {
|
|
566
|
+
break;
|
|
567
|
+
}
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
result[growIdx] += wantedGrow - remaining;
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
return result;
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
function applyConstraints(
|
|
577
|
+
sizes: number[],
|
|
578
|
+
panels: UseSplitterResolvedPanel[],
|
|
579
|
+
handleIndex: number,
|
|
580
|
+
delta: number,
|
|
581
|
+
redistribute?: 'nearest' | 'equal' | UseSplitterRedistributeFn,
|
|
582
|
+
): number[] {
|
|
583
|
+
if (typeof redistribute === 'function') {
|
|
584
|
+
return redistribute({ sizes: [...sizes], panels, handleIndex, delta });
|
|
585
|
+
}
|
|
586
|
+
|
|
587
|
+
if (redistribute === 'nearest' || redistribute === 'equal') {
|
|
588
|
+
const strategy = redistribute === 'nearest' ? redistributeNearest : redistributeEqual;
|
|
589
|
+
const result = strategy(sizes, panels, handleIndex, delta);
|
|
590
|
+
|
|
591
|
+
const beforeIdx = handleIndex;
|
|
592
|
+
const afterIdx = handleIndex + 1;
|
|
593
|
+
const beforePanel = panels[beforeIdx];
|
|
594
|
+
const afterPanel = panels[afterIdx];
|
|
595
|
+
|
|
596
|
+
if (
|
|
597
|
+
beforePanel.collapsible &&
|
|
598
|
+
result[beforeIdx] < getCollapseThreshold(beforePanel) &&
|
|
599
|
+
result[beforeIdx] < sizes[beforeIdx]
|
|
600
|
+
) {
|
|
601
|
+
const freed = result[beforeIdx];
|
|
602
|
+
result[afterIdx] += freed;
|
|
603
|
+
result[beforeIdx] = 0;
|
|
604
|
+
} else if (
|
|
605
|
+
afterPanel.collapsible &&
|
|
606
|
+
result[afterIdx] < getCollapseThreshold(afterPanel) &&
|
|
607
|
+
result[afterIdx] < sizes[afterIdx]
|
|
608
|
+
) {
|
|
609
|
+
const freed = result[afterIdx];
|
|
610
|
+
result[beforeIdx] += freed;
|
|
611
|
+
result[afterIdx] = 0;
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
return result;
|
|
615
|
+
}
|
|
616
|
+
|
|
617
|
+
const collapsed = checkCollapse(sizes, panels, handleIndex, delta);
|
|
618
|
+
if (collapsed) {
|
|
619
|
+
return collapsed;
|
|
620
|
+
}
|
|
621
|
+
|
|
622
|
+
return applyAdjacentOnly(sizes, panels, handleIndex, delta);
|
|
623
|
+
}
|
|
624
|
+
|
|
625
|
+
export function useSplitter<T extends HTMLElement = any>(
|
|
626
|
+
options: UseSplitterOptions,
|
|
627
|
+
): UseSplitterReturnValue<T> {
|
|
628
|
+
const {
|
|
629
|
+
panels,
|
|
630
|
+
orientation = 'horizontal',
|
|
631
|
+
sizes: controlledSizes,
|
|
632
|
+
onSizeChange,
|
|
633
|
+
onCollapseChange,
|
|
634
|
+
redistribute,
|
|
635
|
+
step = 1,
|
|
636
|
+
shiftStep = 10,
|
|
637
|
+
dir = 'ltr',
|
|
638
|
+
resetOnDoubleClick = true,
|
|
639
|
+
enabled = true,
|
|
640
|
+
} = options;
|
|
641
|
+
|
|
642
|
+
const pixelMode = detectPixelMode(options);
|
|
643
|
+
|
|
644
|
+
const defaultSizes = panels.map((panel) => panel.defaultSize);
|
|
645
|
+
|
|
646
|
+
const [currentSizes, setCurrentSizes] = useUncontrolled<SplitterPaneSize[]>({
|
|
647
|
+
value: controlledSizes,
|
|
648
|
+
defaultValue: defaultSizes,
|
|
649
|
+
finalValue: defaultSizes,
|
|
650
|
+
onChange: onSizeChange,
|
|
651
|
+
});
|
|
652
|
+
|
|
653
|
+
const [activeHandle, setActiveHandle] = useState(-1);
|
|
654
|
+
const [containerSize, setContainerSize] = useState(0);
|
|
655
|
+
|
|
656
|
+
const optionsRef = useRef(options);
|
|
657
|
+
optionsRef.current = options;
|
|
658
|
+
|
|
659
|
+
const internalStateRef = useRef<SplitterInternalState>(createInitialInternalState());
|
|
660
|
+
const containerRef = useRef<T | null>(null);
|
|
661
|
+
const containerSizeRef = useRef(0);
|
|
662
|
+
const rootFontSizeRef = useRef(16);
|
|
663
|
+
const documentControllerRef = useRef<AbortController | null>(null);
|
|
664
|
+
const frameRef = useRef(0);
|
|
665
|
+
const currentSizesRef = useRef(currentSizes);
|
|
666
|
+
currentSizesRef.current = currentSizes;
|
|
667
|
+
|
|
668
|
+
const preCollapseSizesRef = useRef<SplitterPaneSize[]>(defaultSizes);
|
|
669
|
+
|
|
670
|
+
const collapsed = currentSizes.map((size) => sizeMagnitude(size) === 0);
|
|
671
|
+
|
|
672
|
+
const measureContainer = useCallback(() => {
|
|
673
|
+
const node = containerRef.current;
|
|
674
|
+
if (!node) {
|
|
675
|
+
return 0;
|
|
676
|
+
}
|
|
677
|
+
const rect = node.getBoundingClientRect();
|
|
678
|
+
return (optionsRef.current.orientation ?? 'horizontal') === 'horizontal'
|
|
679
|
+
? rect.width
|
|
680
|
+
: rect.height;
|
|
681
|
+
}, []);
|
|
682
|
+
|
|
683
|
+
const updateSizes = useCallback(
|
|
684
|
+
(newSizes: SplitterPaneSize[]) => {
|
|
685
|
+
currentSizesRef.current = newSizes;
|
|
686
|
+
setCurrentSizes(newSizes);
|
|
687
|
+
},
|
|
688
|
+
[setCurrentSizes],
|
|
689
|
+
);
|
|
690
|
+
|
|
691
|
+
const collapsePanel = useCallback(
|
|
692
|
+
(panelIndex: number) => {
|
|
693
|
+
if (!panels[panelIndex]?.collapsible) {
|
|
694
|
+
return;
|
|
695
|
+
}
|
|
696
|
+
const raw = currentSizesRef.current;
|
|
697
|
+
if (sizeMagnitude(raw[panelIndex]) === 0) {
|
|
698
|
+
return;
|
|
699
|
+
}
|
|
700
|
+
|
|
701
|
+
const container = pixelMode ? containerSizeRef.current || measureContainer() : 0;
|
|
702
|
+
const rootFontSize = rootFontSizeRef.current;
|
|
703
|
+
const working = resolveWorkingSizes(raw, pixelMode, container, rootFontSize);
|
|
704
|
+
|
|
705
|
+
preCollapseSizesRef.current = [...raw];
|
|
706
|
+
const freedSize = working[panelIndex];
|
|
707
|
+
working[panelIndex] = 0;
|
|
708
|
+
|
|
709
|
+
const neighbor = panelIndex === 0 ? 1 : panelIndex - 1;
|
|
710
|
+
working[neighbor] += freedSize;
|
|
711
|
+
|
|
712
|
+
updateSizes(
|
|
713
|
+
working.map((value, i) => encodeSize(value, raw[i], pixelMode, container, rootFontSize)),
|
|
714
|
+
);
|
|
715
|
+
onCollapseChange?.(panelIndex, true);
|
|
716
|
+
},
|
|
717
|
+
[panels, pixelMode, measureContainer, updateSizes, onCollapseChange],
|
|
718
|
+
);
|
|
719
|
+
|
|
720
|
+
const expandPanel = useCallback(
|
|
721
|
+
(panelIndex: number) => {
|
|
722
|
+
if (!panels[panelIndex]?.collapsible) {
|
|
723
|
+
return;
|
|
724
|
+
}
|
|
725
|
+
const raw = currentSizesRef.current;
|
|
726
|
+
if (sizeMagnitude(raw[panelIndex]) !== 0) {
|
|
727
|
+
return;
|
|
728
|
+
}
|
|
729
|
+
|
|
730
|
+
const container = pixelMode ? containerSizeRef.current || measureContainer() : 0;
|
|
731
|
+
const rootFontSize = rootFontSizeRef.current;
|
|
732
|
+
const working = resolveWorkingSizes(raw, pixelMode, container, rootFontSize);
|
|
733
|
+
|
|
734
|
+
const preCollapse = preCollapseSizesRef.current;
|
|
735
|
+
const restoreSource =
|
|
736
|
+
preCollapse[panelIndex] != null && sizeMagnitude(preCollapse[panelIndex]) !== 0
|
|
737
|
+
? preCollapse[panelIndex]
|
|
738
|
+
: panels[panelIndex].defaultSize;
|
|
739
|
+
const restoreSize = resolveSize(restoreSource, pixelMode, container, rootFontSize);
|
|
740
|
+
|
|
741
|
+
const neighbor = panelIndex === 0 ? 1 : panelIndex - 1;
|
|
742
|
+
const neighborMin =
|
|
743
|
+
panels[neighbor].min != null
|
|
744
|
+
? resolveSize(panels[neighbor].min!, pixelMode, container, rootFontSize)
|
|
745
|
+
: 0;
|
|
746
|
+
const available = Math.max(0, working[neighbor] - neighborMin);
|
|
747
|
+
const actualRestore = Math.min(restoreSize, available);
|
|
748
|
+
|
|
749
|
+
if (actualRestore <= 0) {
|
|
750
|
+
return;
|
|
751
|
+
}
|
|
752
|
+
|
|
753
|
+
working[panelIndex] = actualRestore;
|
|
754
|
+
working[neighbor] -= actualRestore;
|
|
755
|
+
|
|
756
|
+
updateSizes(
|
|
757
|
+
working.map((value, i) => encodeSize(value, raw[i], pixelMode, container, rootFontSize)),
|
|
758
|
+
);
|
|
759
|
+
onCollapseChange?.(panelIndex, false);
|
|
760
|
+
},
|
|
761
|
+
[panels, pixelMode, measureContainer, updateSizes, onCollapseChange],
|
|
762
|
+
);
|
|
763
|
+
|
|
764
|
+
const toggleCollapsePanel = useCallback(
|
|
765
|
+
(panelIndex: number) => {
|
|
766
|
+
if (sizeMagnitude(currentSizesRef.current[panelIndex]) === 0) {
|
|
767
|
+
expandPanel(panelIndex);
|
|
768
|
+
} else {
|
|
769
|
+
collapsePanel(panelIndex);
|
|
770
|
+
}
|
|
771
|
+
},
|
|
772
|
+
[collapsePanel, expandPanel],
|
|
773
|
+
);
|
|
774
|
+
|
|
775
|
+
const emitCollapseTransitions = useCallback(
|
|
776
|
+
(
|
|
777
|
+
prev: SplitterPaneSize[],
|
|
778
|
+
next: number[],
|
|
779
|
+
indices: number[],
|
|
780
|
+
preCollapseSnapshot: SplitterPaneSize[],
|
|
781
|
+
) => {
|
|
782
|
+
const onChange = optionsRef.current.onCollapseChange;
|
|
783
|
+
for (const idx of indices) {
|
|
784
|
+
const wasCollapsed = sizeMagnitude(prev[idx]) === 0;
|
|
785
|
+
const nowCollapsed = next[idx] === 0;
|
|
786
|
+
if (!wasCollapsed && nowCollapsed) {
|
|
787
|
+
preCollapseSizesRef.current = [...preCollapseSnapshot];
|
|
788
|
+
onChange?.(idx, true);
|
|
789
|
+
} else if (wasCollapsed && !nowCollapsed) {
|
|
790
|
+
onChange?.(idx, false);
|
|
791
|
+
}
|
|
792
|
+
}
|
|
793
|
+
},
|
|
794
|
+
[],
|
|
795
|
+
);
|
|
796
|
+
|
|
797
|
+
const reset = useCallback(
|
|
798
|
+
(handleIndex: number) => {
|
|
799
|
+
const raw = currentSizesRef.current;
|
|
800
|
+
|
|
801
|
+
const beforeIdx = handleIndex;
|
|
802
|
+
const afterIdx = handleIndex + 1;
|
|
803
|
+
if (beforeIdx < 0 || afterIdx >= raw.length) {
|
|
804
|
+
return;
|
|
805
|
+
}
|
|
806
|
+
|
|
807
|
+
const container = pixelMode ? containerSizeRef.current || measureContainer() : 0;
|
|
808
|
+
const rootFontSize = rootFontSizeRef.current;
|
|
809
|
+
const working = resolveWorkingSizes(raw, pixelMode, container, rootFontSize);
|
|
810
|
+
const resolvedPanels = optionsRef.current.panels.map((panel) =>
|
|
811
|
+
resolvePanel(panel, pixelMode, container, rootFontSize),
|
|
812
|
+
);
|
|
813
|
+
|
|
814
|
+
const total = working[beforeIdx] + working[afterIdx];
|
|
815
|
+
const defBefore = resolvedPanels[beforeIdx].defaultSize;
|
|
816
|
+
const defAfter = resolvedPanels[afterIdx].defaultSize;
|
|
817
|
+
const defTotal = defBefore + defAfter;
|
|
818
|
+
const targetBefore = defTotal === 0 ? total / 2 : total * (defBefore / defTotal);
|
|
819
|
+
|
|
820
|
+
const next = applyAdjacentOnly(
|
|
821
|
+
working,
|
|
822
|
+
resolvedPanels,
|
|
823
|
+
beforeIdx,
|
|
824
|
+
targetBefore - working[beforeIdx],
|
|
825
|
+
);
|
|
826
|
+
emitCollapseTransitions(raw, next, [beforeIdx, afterIdx], raw);
|
|
827
|
+
updateSizes(encodeWorkingSizes(next, working, raw, pixelMode, container, rootFontSize));
|
|
828
|
+
},
|
|
829
|
+
[emitCollapseTransitions, updateSizes, pixelMode, measureContainer],
|
|
830
|
+
);
|
|
831
|
+
|
|
832
|
+
const containerRefCallback: React.RefCallback<T | null> = useCallback((node) => {
|
|
833
|
+
containerRef.current = node;
|
|
834
|
+
}, []);
|
|
835
|
+
|
|
836
|
+
useEffect(() => {
|
|
837
|
+
if (!pixelMode || typeof ResizeObserver === 'undefined') {
|
|
838
|
+
return undefined;
|
|
839
|
+
}
|
|
840
|
+
|
|
841
|
+
const node = containerRef.current;
|
|
842
|
+
if (!node) {
|
|
843
|
+
return undefined;
|
|
844
|
+
}
|
|
845
|
+
|
|
846
|
+
let frame = 0;
|
|
847
|
+
const update = () => {
|
|
848
|
+
const rect = node.getBoundingClientRect();
|
|
849
|
+
const size =
|
|
850
|
+
(optionsRef.current.orientation ?? 'horizontal') === 'horizontal'
|
|
851
|
+
? rect.width
|
|
852
|
+
: rect.height;
|
|
853
|
+
rootFontSizeRef.current = getRootFontSize();
|
|
854
|
+
containerSizeRef.current = size;
|
|
855
|
+
setContainerSize((prev) => (prev !== size ? size : prev));
|
|
856
|
+
};
|
|
857
|
+
|
|
858
|
+
const observer = new ResizeObserver(() => {
|
|
859
|
+
cancelAnimationFrame(frame);
|
|
860
|
+
frame = requestAnimationFrame(update);
|
|
861
|
+
});
|
|
862
|
+
|
|
863
|
+
observer.observe(node);
|
|
864
|
+
update();
|
|
865
|
+
|
|
866
|
+
return () => {
|
|
867
|
+
cancelAnimationFrame(frame);
|
|
868
|
+
observer.disconnect();
|
|
869
|
+
};
|
|
870
|
+
}, [pixelMode, orientation]);
|
|
871
|
+
|
|
872
|
+
const handleRefCallbacks = useRef<Map<number, (node: HTMLElement | null) => void>>(new Map());
|
|
873
|
+
const handleElementControllers = useRef<Map<number, AbortController>>(new Map());
|
|
874
|
+
|
|
875
|
+
const getHandleRefCallback = useCallback(
|
|
876
|
+
(handleIndex: number): React.RefCallback<HTMLElement> => {
|
|
877
|
+
if (handleRefCallbacks.current.has(handleIndex)) {
|
|
878
|
+
return handleRefCallbacks.current.get(handleIndex)!;
|
|
879
|
+
}
|
|
880
|
+
|
|
881
|
+
const callback = (node: HTMLElement | null) => {
|
|
882
|
+
const existingController = handleElementControllers.current.get(handleIndex);
|
|
883
|
+
if (existingController) {
|
|
884
|
+
existingController.abort();
|
|
885
|
+
handleElementControllers.current.delete(handleIndex);
|
|
886
|
+
}
|
|
887
|
+
|
|
888
|
+
if (!node) {
|
|
889
|
+
return;
|
|
890
|
+
}
|
|
891
|
+
|
|
892
|
+
const elementController = new AbortController();
|
|
893
|
+
handleElementControllers.current.set(handleIndex, elementController);
|
|
894
|
+
|
|
895
|
+
const onPointerDown = (event: PointerEvent) => {
|
|
896
|
+
if (optionsRef.current.enabled === false) {
|
|
897
|
+
return;
|
|
898
|
+
}
|
|
899
|
+
if (event.button !== 0) {
|
|
900
|
+
return;
|
|
901
|
+
}
|
|
902
|
+
|
|
903
|
+
const container = containerRef.current;
|
|
904
|
+
if (!container) {
|
|
905
|
+
return;
|
|
906
|
+
}
|
|
907
|
+
|
|
908
|
+
const opts = optionsRef.current;
|
|
909
|
+
const isHorizontal = (opts.orientation ?? 'horizontal') === 'horizontal';
|
|
910
|
+
const rect = container.getBoundingClientRect();
|
|
911
|
+
const containerSizePx = isHorizontal ? rect.width : rect.height;
|
|
912
|
+
const pointerPos = isHorizontal ? event.clientX : event.clientY;
|
|
913
|
+
const isPixelMode = detectPixelMode(opts);
|
|
914
|
+
const rootFontSize = getRootFontSize();
|
|
915
|
+
|
|
916
|
+
const s = internalStateRef.current;
|
|
917
|
+
s.isDragging = true;
|
|
918
|
+
s.handleIndex = handleIndex;
|
|
919
|
+
s.startPointer = pointerPos;
|
|
920
|
+
s.containerSize = containerSizePx;
|
|
921
|
+
s.rootFontSize = rootFontSize;
|
|
922
|
+
s.pixelMode = isPixelMode;
|
|
923
|
+
s.startRaw = [...currentSizesRef.current];
|
|
924
|
+
s.startSizes = resolveWorkingSizes(
|
|
925
|
+
s.startRaw,
|
|
926
|
+
isPixelMode,
|
|
927
|
+
containerSizePx,
|
|
928
|
+
rootFontSize,
|
|
929
|
+
);
|
|
930
|
+
s.preCollapseSizes = [...preCollapseSizesRef.current];
|
|
931
|
+
|
|
932
|
+
setActiveHandle(handleIndex);
|
|
933
|
+
document.body.style.userSelect = 'none';
|
|
934
|
+
document.body.style.webkitUserSelect = 'none';
|
|
935
|
+
document.body.style.cursor = isHorizontal ? 'col-resize' : 'row-resize';
|
|
936
|
+
|
|
937
|
+
opts.onResizeStart?.(handleIndex);
|
|
938
|
+
|
|
939
|
+
documentControllerRef.current?.abort();
|
|
940
|
+
documentControllerRef.current = new AbortController();
|
|
941
|
+
const sig = documentControllerRef.current.signal;
|
|
942
|
+
|
|
943
|
+
document.addEventListener('pointermove', onPointerMove, { signal: sig });
|
|
944
|
+
document.addEventListener('pointerup', onPointerUp, { signal: sig });
|
|
945
|
+
document.addEventListener('pointercancel', onPointerUp, { signal: sig });
|
|
946
|
+
};
|
|
947
|
+
|
|
948
|
+
const flushResize = (pointerEvent: PointerEvent) => {
|
|
949
|
+
const s = internalStateRef.current;
|
|
950
|
+
if (!s.containerSize) {
|
|
951
|
+
return;
|
|
952
|
+
}
|
|
953
|
+
const opts = optionsRef.current;
|
|
954
|
+
const isHorizontal = (opts.orientation ?? 'horizontal') === 'horizontal';
|
|
955
|
+
const isRtl = isHorizontal && opts.dir === 'rtl';
|
|
956
|
+
const pointerPos = isHorizontal ? pointerEvent.clientX : pointerEvent.clientY;
|
|
957
|
+
const pixelDelta = (isRtl ? -1 : 1) * (pointerPos - s.startPointer);
|
|
958
|
+
const delta = s.pixelMode ? pixelDelta : (pixelDelta / s.containerSize) * 100;
|
|
959
|
+
|
|
960
|
+
const resolvedPanels = opts.panels.map((panel) =>
|
|
961
|
+
resolvePanel(panel, s.pixelMode, s.containerSize, s.rootFontSize),
|
|
962
|
+
);
|
|
963
|
+
|
|
964
|
+
const newSizes = applyConstraints(
|
|
965
|
+
s.startSizes,
|
|
966
|
+
resolvedPanels,
|
|
967
|
+
s.handleIndex,
|
|
968
|
+
delta,
|
|
969
|
+
opts.redistribute,
|
|
970
|
+
);
|
|
971
|
+
|
|
972
|
+
const prevSizes = currentSizesRef.current;
|
|
973
|
+
emitCollapseTransitions(
|
|
974
|
+
prevSizes,
|
|
975
|
+
newSizes,
|
|
976
|
+
[s.handleIndex, s.handleIndex + 1],
|
|
977
|
+
s.startRaw,
|
|
978
|
+
);
|
|
979
|
+
|
|
980
|
+
const encoded = encodeWorkingSizes(
|
|
981
|
+
newSizes,
|
|
982
|
+
s.startSizes,
|
|
983
|
+
s.startRaw,
|
|
984
|
+
s.pixelMode,
|
|
985
|
+
s.containerSize,
|
|
986
|
+
s.rootFontSize,
|
|
987
|
+
);
|
|
988
|
+
currentSizesRef.current = encoded;
|
|
989
|
+
setCurrentSizes(encoded);
|
|
990
|
+
};
|
|
991
|
+
|
|
992
|
+
const onPointerMove = (event: PointerEvent) => {
|
|
993
|
+
const s = internalStateRef.current;
|
|
994
|
+
if (!s.isDragging) {
|
|
995
|
+
return;
|
|
996
|
+
}
|
|
997
|
+
|
|
998
|
+
cancelAnimationFrame(frameRef.current);
|
|
999
|
+
frameRef.current = requestAnimationFrame(() => {
|
|
1000
|
+
flushResize(event);
|
|
1001
|
+
});
|
|
1002
|
+
};
|
|
1003
|
+
|
|
1004
|
+
const onPointerUp = (event: PointerEvent) => {
|
|
1005
|
+
const s = internalStateRef.current;
|
|
1006
|
+
if (!s.isDragging) {
|
|
1007
|
+
return;
|
|
1008
|
+
}
|
|
1009
|
+
|
|
1010
|
+
cancelAnimationFrame(frameRef.current);
|
|
1011
|
+
flushResize(event);
|
|
1012
|
+
|
|
1013
|
+
s.isDragging = false;
|
|
1014
|
+
const finishedHandle = s.handleIndex;
|
|
1015
|
+
s.handleIndex = -1;
|
|
1016
|
+
|
|
1017
|
+
setActiveHandle(-1);
|
|
1018
|
+
document.body.style.userSelect = '';
|
|
1019
|
+
document.body.style.webkitUserSelect = '';
|
|
1020
|
+
document.body.style.cursor = '';
|
|
1021
|
+
|
|
1022
|
+
documentControllerRef.current?.abort();
|
|
1023
|
+
documentControllerRef.current = null;
|
|
1024
|
+
|
|
1025
|
+
optionsRef.current.onResizeEnd?.(finishedHandle, [...currentSizesRef.current]);
|
|
1026
|
+
};
|
|
1027
|
+
|
|
1028
|
+
node.addEventListener('pointerdown', onPointerDown, {
|
|
1029
|
+
signal: elementController.signal,
|
|
1030
|
+
});
|
|
1031
|
+
};
|
|
1032
|
+
|
|
1033
|
+
handleRefCallbacks.current.set(handleIndex, callback);
|
|
1034
|
+
return callback;
|
|
1035
|
+
},
|
|
1036
|
+
[setCurrentSizes],
|
|
1037
|
+
);
|
|
1038
|
+
|
|
1039
|
+
const getHandleProps = useCallback(
|
|
1040
|
+
(input: { index: number }) => {
|
|
1041
|
+
const { index } = input;
|
|
1042
|
+
const orient = orientation;
|
|
1043
|
+
const rootFontSize = rootFontSizeRef.current;
|
|
1044
|
+
const working = resolveWorkingSizes(currentSizes, pixelMode, containerSize, rootFontSize);
|
|
1045
|
+
const resolvedPanels = panels.map((panel) =>
|
|
1046
|
+
resolvePanel(panel, pixelMode, containerSize, rootFontSize),
|
|
1047
|
+
);
|
|
1048
|
+
const beforeSize = working[index] ?? 0;
|
|
1049
|
+
const beforePanel = resolvedPanels[index];
|
|
1050
|
+
|
|
1051
|
+
return {
|
|
1052
|
+
ref: getHandleRefCallback(index),
|
|
1053
|
+
role: 'separator' as const,
|
|
1054
|
+
'aria-orientation': orient,
|
|
1055
|
+
'aria-valuenow': Math.round(beforeSize),
|
|
1056
|
+
'aria-valuemin': Math.round(getMin(beforePanel)),
|
|
1057
|
+
'aria-valuemax': Math.round(getMax(beforePanel)),
|
|
1058
|
+
tabIndex: 0,
|
|
1059
|
+
onKeyDown: (event: React.KeyboardEvent) => {
|
|
1060
|
+
if (!enabled) {
|
|
1061
|
+
return;
|
|
1062
|
+
}
|
|
1063
|
+
|
|
1064
|
+
const isHorizontal = orient === 'horizontal';
|
|
1065
|
+
const isRtl = dir === 'rtl';
|
|
1066
|
+
|
|
1067
|
+
const container = pixelMode ? containerSizeRef.current || measureContainer() : 0;
|
|
1068
|
+
const liveRootFontSize = rootFontSizeRef.current;
|
|
1069
|
+
const liveWorking = resolveWorkingSizes(
|
|
1070
|
+
currentSizes,
|
|
1071
|
+
pixelMode,
|
|
1072
|
+
container,
|
|
1073
|
+
liveRootFontSize,
|
|
1074
|
+
);
|
|
1075
|
+
const livePanels = panels.map((panel) =>
|
|
1076
|
+
resolvePanel(panel, pixelMode, container, liveRootFontSize),
|
|
1077
|
+
);
|
|
1078
|
+
const liveBeforePanel = livePanels[index];
|
|
1079
|
+
const liveAfterPanel = livePanels[index + 1];
|
|
1080
|
+
|
|
1081
|
+
let delta = 0;
|
|
1082
|
+
const currentStep = resolveStep(
|
|
1083
|
+
event.shiftKey ? shiftStep : step,
|
|
1084
|
+
pixelMode,
|
|
1085
|
+
container,
|
|
1086
|
+
liveRootFontSize,
|
|
1087
|
+
);
|
|
1088
|
+
|
|
1089
|
+
switch (event.key) {
|
|
1090
|
+
case 'ArrowLeft': {
|
|
1091
|
+
if (!isHorizontal) {
|
|
1092
|
+
return;
|
|
1093
|
+
}
|
|
1094
|
+
delta = isRtl ? currentStep : -currentStep;
|
|
1095
|
+
break;
|
|
1096
|
+
}
|
|
1097
|
+
case 'ArrowRight': {
|
|
1098
|
+
if (!isHorizontal) {
|
|
1099
|
+
return;
|
|
1100
|
+
}
|
|
1101
|
+
delta = isRtl ? -currentStep : currentStep;
|
|
1102
|
+
break;
|
|
1103
|
+
}
|
|
1104
|
+
case 'ArrowUp': {
|
|
1105
|
+
if (isHorizontal) {
|
|
1106
|
+
return;
|
|
1107
|
+
}
|
|
1108
|
+
delta = -currentStep;
|
|
1109
|
+
break;
|
|
1110
|
+
}
|
|
1111
|
+
case 'ArrowDown': {
|
|
1112
|
+
if (isHorizontal) {
|
|
1113
|
+
return;
|
|
1114
|
+
}
|
|
1115
|
+
delta = currentStep;
|
|
1116
|
+
break;
|
|
1117
|
+
}
|
|
1118
|
+
case 'Home': {
|
|
1119
|
+
delta = -(liveWorking[index] - getMin(liveBeforePanel));
|
|
1120
|
+
break;
|
|
1121
|
+
}
|
|
1122
|
+
case 'End': {
|
|
1123
|
+
delta = getMax(liveBeforePanel) - liveWorking[index];
|
|
1124
|
+
break;
|
|
1125
|
+
}
|
|
1126
|
+
case 'Enter': {
|
|
1127
|
+
const beforeCollapsible = liveBeforePanel?.collapsible;
|
|
1128
|
+
const afterCollapsible = liveAfterPanel?.collapsible;
|
|
1129
|
+
|
|
1130
|
+
if (beforeCollapsible && liveWorking[index] <= liveWorking[index + 1]) {
|
|
1131
|
+
toggleCollapsePanel(index);
|
|
1132
|
+
event.preventDefault();
|
|
1133
|
+
return;
|
|
1134
|
+
}
|
|
1135
|
+
if (afterCollapsible) {
|
|
1136
|
+
toggleCollapsePanel(index + 1);
|
|
1137
|
+
event.preventDefault();
|
|
1138
|
+
return;
|
|
1139
|
+
}
|
|
1140
|
+
if (beforeCollapsible) {
|
|
1141
|
+
toggleCollapsePanel(index);
|
|
1142
|
+
event.preventDefault();
|
|
1143
|
+
return;
|
|
1144
|
+
}
|
|
1145
|
+
return;
|
|
1146
|
+
}
|
|
1147
|
+
default:
|
|
1148
|
+
return;
|
|
1149
|
+
}
|
|
1150
|
+
|
|
1151
|
+
event.preventDefault();
|
|
1152
|
+
|
|
1153
|
+
if (delta !== 0) {
|
|
1154
|
+
const newSizes = applyConstraints(liveWorking, livePanels, index, delta, redistribute);
|
|
1155
|
+
emitCollapseTransitions(currentSizes, newSizes, [index, index + 1], currentSizes);
|
|
1156
|
+
updateSizes(
|
|
1157
|
+
encodeWorkingSizes(
|
|
1158
|
+
newSizes,
|
|
1159
|
+
liveWorking,
|
|
1160
|
+
currentSizes,
|
|
1161
|
+
pixelMode,
|
|
1162
|
+
container,
|
|
1163
|
+
liveRootFontSize,
|
|
1164
|
+
),
|
|
1165
|
+
);
|
|
1166
|
+
}
|
|
1167
|
+
},
|
|
1168
|
+
onDoubleClick: () => {
|
|
1169
|
+
if (!enabled || !resetOnDoubleClick) {
|
|
1170
|
+
return;
|
|
1171
|
+
}
|
|
1172
|
+
reset(index);
|
|
1173
|
+
},
|
|
1174
|
+
'data-active': activeHandle === index || undefined,
|
|
1175
|
+
'data-orientation': orient,
|
|
1176
|
+
};
|
|
1177
|
+
},
|
|
1178
|
+
[
|
|
1179
|
+
orientation,
|
|
1180
|
+
currentSizes,
|
|
1181
|
+
panels,
|
|
1182
|
+
pixelMode,
|
|
1183
|
+
containerSize,
|
|
1184
|
+
enabled,
|
|
1185
|
+
dir,
|
|
1186
|
+
step,
|
|
1187
|
+
shiftStep,
|
|
1188
|
+
resetOnDoubleClick,
|
|
1189
|
+
activeHandle,
|
|
1190
|
+
redistribute,
|
|
1191
|
+
measureContainer,
|
|
1192
|
+
getHandleRefCallback,
|
|
1193
|
+
toggleCollapsePanel,
|
|
1194
|
+
updateSizes,
|
|
1195
|
+
emitCollapseTransitions,
|
|
1196
|
+
reset,
|
|
1197
|
+
],
|
|
1198
|
+
);
|
|
1199
|
+
|
|
1200
|
+
useEffect(
|
|
1201
|
+
() => () => {
|
|
1202
|
+
documentControllerRef.current?.abort();
|
|
1203
|
+
documentControllerRef.current = null;
|
|
1204
|
+
handleElementControllers.current.forEach((controller) => controller.abort());
|
|
1205
|
+
handleElementControllers.current.clear();
|
|
1206
|
+
cancelAnimationFrame(frameRef.current);
|
|
1207
|
+
|
|
1208
|
+
if (internalStateRef.current.isDragging) {
|
|
1209
|
+
internalStateRef.current.isDragging = false;
|
|
1210
|
+
document.body.style.userSelect = '';
|
|
1211
|
+
document.body.style.webkitUserSelect = '';
|
|
1212
|
+
document.body.style.cursor = '';
|
|
1213
|
+
}
|
|
1214
|
+
},
|
|
1215
|
+
[],
|
|
1216
|
+
);
|
|
1217
|
+
|
|
1218
|
+
return {
|
|
1219
|
+
ref: containerRefCallback,
|
|
1220
|
+
sizes: currentSizes,
|
|
1221
|
+
pixelMode,
|
|
1222
|
+
collapsed,
|
|
1223
|
+
activeHandle,
|
|
1224
|
+
getHandleProps,
|
|
1225
|
+
setSizes: updateSizes,
|
|
1226
|
+
collapse: collapsePanel,
|
|
1227
|
+
expand: expandPanel,
|
|
1228
|
+
toggleCollapse: toggleCollapsePanel,
|
|
1229
|
+
reset,
|
|
1230
|
+
};
|
|
1231
|
+
}
|
|
1232
|
+
|
|
1233
|
+
export namespace useSplitter {
|
|
1234
|
+
export type Panel = UseSplitterPanel;
|
|
1235
|
+
export type Options = UseSplitterOptions;
|
|
1236
|
+
export type RedistributeInput = UseSplitterRedistributeInput;
|
|
1237
|
+
export type RedistributeFn = UseSplitterRedistributeFn;
|
|
1238
|
+
export type ResolvedPanel = UseSplitterResolvedPanel;
|
|
1239
|
+
export type ReturnValue<T extends HTMLElement = any> = UseSplitterReturnValue<T>;
|
|
1240
|
+
export type PaneSize = SplitterPaneSize;
|
|
1241
|
+
export type Step = SplitterStep;
|
|
1242
|
+
}
|