react-resizable-panels 2.1.9 → 3.0.1

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.
Files changed (28) hide show
  1. package/dist/declarations/src/Panel.d.ts +9 -9
  2. package/dist/declarations/src/PanelGroup.d.ts +9 -9
  3. package/dist/declarations/src/hooks/usePanelGroupContext.d.ts +4 -0
  4. package/dist/declarations/src/index.d.ts +2 -1
  5. package/dist/{react-resizable-panels.browser.development.esm.js → react-resizable-panels.browser.development.js} +16 -3
  6. package/dist/{react-resizable-panels.browser.esm.js → react-resizable-panels.browser.js} +16 -3
  7. package/dist/react-resizable-panels.d.ts +2 -0
  8. package/dist/{react-resizable-panels.development.node.esm.js → react-resizable-panels.development.edge-light.js} +16 -3
  9. package/dist/{react-resizable-panels.development.esm.js → react-resizable-panels.development.js} +16 -3
  10. package/dist/{react-resizable-panels.node.esm.js → react-resizable-panels.edge-light.js} +16 -3
  11. package/dist/{react-resizable-panels.esm.js → react-resizable-panels.js} +16 -3
  12. package/package.json +27 -41
  13. package/dist/react-resizable-panels.browser.cjs.js +0 -2483
  14. package/dist/react-resizable-panels.browser.cjs.mjs +0 -19
  15. package/dist/react-resizable-panels.browser.development.cjs.js +0 -2589
  16. package/dist/react-resizable-panels.browser.development.cjs.mjs +0 -19
  17. package/dist/react-resizable-panels.cjs.d.mts +0 -2
  18. package/dist/react-resizable-panels.cjs.d.mts.map +0 -1
  19. package/dist/react-resizable-panels.cjs.d.ts +0 -2
  20. package/dist/react-resizable-panels.cjs.d.ts.map +0 -1
  21. package/dist/react-resizable-panels.cjs.js +0 -2485
  22. package/dist/react-resizable-panels.cjs.mjs +0 -19
  23. package/dist/react-resizable-panels.development.cjs.js +0 -2596
  24. package/dist/react-resizable-panels.development.cjs.mjs +0 -19
  25. package/dist/react-resizable-panels.development.node.cjs.js +0 -2362
  26. package/dist/react-resizable-panels.development.node.cjs.mjs +0 -19
  27. package/dist/react-resizable-panels.node.cjs.js +0 -2261
  28. package/dist/react-resizable-panels.node.cjs.mjs +0 -19
@@ -1,2362 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
- var React = require('react');
6
-
7
- function _interopNamespace(e) {
8
- if (e && e.__esModule) return e;
9
- var n = Object.create(null);
10
- if (e) {
11
- Object.keys(e).forEach(function (k) {
12
- if (k !== 'default') {
13
- var d = Object.getOwnPropertyDescriptor(e, k);
14
- Object.defineProperty(n, k, d.get ? d : {
15
- enumerable: true,
16
- get: function () { return e[k]; }
17
- });
18
- }
19
- });
20
- }
21
- n["default"] = e;
22
- return Object.freeze(n);
23
- }
24
-
25
- var React__namespace = /*#__PURE__*/_interopNamespace(React);
26
-
27
- // The "contextmenu" event is not supported as a PointerEvent in all browsers yet, so MouseEvent still need to be handled
28
-
29
- const PanelGroupContext = React.createContext(null);
30
- PanelGroupContext.displayName = "PanelGroupContext";
31
-
32
- const DATA_ATTRIBUTES = {
33
- group: "data-panel-group",
34
- groupDirection: "data-panel-group-direction",
35
- groupId: "data-panel-group-id",
36
- panel: "data-panel",
37
- panelCollapsible: "data-panel-collapsible",
38
- panelId: "data-panel-id",
39
- panelSize: "data-panel-size",
40
- resizeHandle: "data-resize-handle",
41
- resizeHandleActive: "data-resize-handle-active",
42
- resizeHandleEnabled: "data-panel-resize-handle-enabled",
43
- resizeHandleId: "data-panel-resize-handle-id",
44
- resizeHandleState: "data-resize-handle-state"
45
- };
46
- const PRECISION = 10;
47
-
48
- const useId = React__namespace["useId".toString()];
49
- const wrappedUseId = typeof useId === "function" ? useId : () => null;
50
- let counter = 0;
51
- function useUniqueId(idFromParams = null) {
52
- const idFromUseId = wrappedUseId();
53
- const idRef = React.useRef(idFromParams || idFromUseId || null);
54
- if (idRef.current === null) {
55
- idRef.current = "" + counter++;
56
- }
57
- return idFromParams !== null && idFromParams !== void 0 ? idFromParams : idRef.current;
58
- }
59
-
60
- function PanelWithForwardedRef({
61
- children,
62
- className: classNameFromProps = "",
63
- collapsedSize,
64
- collapsible,
65
- defaultSize,
66
- forwardedRef,
67
- id: idFromProps,
68
- maxSize,
69
- minSize,
70
- onCollapse,
71
- onExpand,
72
- onResize,
73
- order,
74
- style: styleFromProps,
75
- tagName: Type = "div",
76
- ...rest
77
- }) {
78
- const context = React.useContext(PanelGroupContext);
79
- if (context === null) {
80
- throw Error(`Panel components must be rendered within a PanelGroup container`);
81
- }
82
- const {
83
- collapsePanel,
84
- expandPanel,
85
- getPanelSize,
86
- getPanelStyle,
87
- groupId,
88
- isPanelCollapsed,
89
- reevaluatePanelConstraints,
90
- registerPanel,
91
- resizePanel,
92
- unregisterPanel
93
- } = context;
94
- const panelId = useUniqueId(idFromProps);
95
- const panelDataRef = React.useRef({
96
- callbacks: {
97
- onCollapse,
98
- onExpand,
99
- onResize
100
- },
101
- constraints: {
102
- collapsedSize,
103
- collapsible,
104
- defaultSize,
105
- maxSize,
106
- minSize
107
- },
108
- id: panelId,
109
- idIsFromProps: idFromProps !== undefined,
110
- order
111
- });
112
- const devWarningsRef = React.useRef({
113
- didLogMissingDefaultSizeWarning: false
114
- });
115
-
116
- // Normally we wouldn't log a warning during render,
117
- // but effects don't run on the server, so we can't do it there
118
- {
119
- if (!devWarningsRef.current.didLogMissingDefaultSizeWarning) {
120
- if (defaultSize == null) {
121
- devWarningsRef.current.didLogMissingDefaultSizeWarning = true;
122
- console.warn(`WARNING: Panel defaultSize prop recommended to avoid layout shift after server rendering`);
123
- }
124
- }
125
- }
126
- React.useImperativeHandle(forwardedRef, () => ({
127
- collapse: () => {
128
- collapsePanel(panelDataRef.current);
129
- },
130
- expand: minSize => {
131
- expandPanel(panelDataRef.current, minSize);
132
- },
133
- getId() {
134
- return panelId;
135
- },
136
- getSize() {
137
- return getPanelSize(panelDataRef.current);
138
- },
139
- isCollapsed() {
140
- return isPanelCollapsed(panelDataRef.current);
141
- },
142
- isExpanded() {
143
- return !isPanelCollapsed(panelDataRef.current);
144
- },
145
- resize: size => {
146
- resizePanel(panelDataRef.current, size);
147
- }
148
- }), [collapsePanel, expandPanel, getPanelSize, isPanelCollapsed, panelId, resizePanel]);
149
- const style = getPanelStyle(panelDataRef.current, defaultSize);
150
- return React.createElement(Type, {
151
- ...rest,
152
- children,
153
- className: classNameFromProps,
154
- id: panelId,
155
- style: {
156
- ...style,
157
- ...styleFromProps
158
- },
159
- // CSS selectors
160
- [DATA_ATTRIBUTES.groupId]: groupId,
161
- [DATA_ATTRIBUTES.panel]: "",
162
- [DATA_ATTRIBUTES.panelCollapsible]: collapsible || undefined,
163
- [DATA_ATTRIBUTES.panelId]: panelId,
164
- [DATA_ATTRIBUTES.panelSize]: parseFloat("" + style.flexGrow).toFixed(1)
165
- });
166
- }
167
- const Panel = React.forwardRef((props, ref) => React.createElement(PanelWithForwardedRef, {
168
- ...props,
169
- forwardedRef: ref
170
- }));
171
- PanelWithForwardedRef.displayName = "Panel";
172
- Panel.displayName = "forwardRef(Panel)";
173
-
174
- let nonce;
175
- function getNonce() {
176
- return nonce;
177
- }
178
- function setNonce(value) {
179
- nonce = value;
180
- }
181
-
182
- let currentCursorStyle = null;
183
- let enabled = true;
184
- let prevRuleIndex = -1;
185
- let styleElement = null;
186
- function disableGlobalCursorStyles() {
187
- enabled = false;
188
- }
189
- function enableGlobalCursorStyles() {
190
- enabled = true;
191
- }
192
- function getCursorStyle(state, constraintFlags) {
193
- if (constraintFlags) {
194
- const horizontalMin = (constraintFlags & EXCEEDED_HORIZONTAL_MIN) !== 0;
195
- const horizontalMax = (constraintFlags & EXCEEDED_HORIZONTAL_MAX) !== 0;
196
- const verticalMin = (constraintFlags & EXCEEDED_VERTICAL_MIN) !== 0;
197
- const verticalMax = (constraintFlags & EXCEEDED_VERTICAL_MAX) !== 0;
198
- if (horizontalMin) {
199
- if (verticalMin) {
200
- return "se-resize";
201
- } else if (verticalMax) {
202
- return "ne-resize";
203
- } else {
204
- return "e-resize";
205
- }
206
- } else if (horizontalMax) {
207
- if (verticalMin) {
208
- return "sw-resize";
209
- } else if (verticalMax) {
210
- return "nw-resize";
211
- } else {
212
- return "w-resize";
213
- }
214
- } else if (verticalMin) {
215
- return "s-resize";
216
- } else if (verticalMax) {
217
- return "n-resize";
218
- }
219
- }
220
- switch (state) {
221
- case "horizontal":
222
- return "ew-resize";
223
- case "intersection":
224
- return "move";
225
- case "vertical":
226
- return "ns-resize";
227
- }
228
- }
229
- function resetGlobalCursorStyle() {
230
- if (styleElement !== null) {
231
- document.head.removeChild(styleElement);
232
- currentCursorStyle = null;
233
- styleElement = null;
234
- prevRuleIndex = -1;
235
- }
236
- }
237
- function setGlobalCursorStyle(state, constraintFlags) {
238
- var _styleElement$sheet$i, _styleElement$sheet2;
239
- if (!enabled) {
240
- return;
241
- }
242
- const style = getCursorStyle(state, constraintFlags);
243
- if (currentCursorStyle === style) {
244
- return;
245
- }
246
- currentCursorStyle = style;
247
- if (styleElement === null) {
248
- styleElement = document.createElement("style");
249
- const nonce = getNonce();
250
- if (nonce) {
251
- styleElement.setAttribute("nonce", nonce);
252
- }
253
- document.head.appendChild(styleElement);
254
- }
255
- if (prevRuleIndex >= 0) {
256
- var _styleElement$sheet;
257
- (_styleElement$sheet = styleElement.sheet) === null || _styleElement$sheet === void 0 ? void 0 : _styleElement$sheet.removeRule(prevRuleIndex);
258
- }
259
- prevRuleIndex = (_styleElement$sheet$i = (_styleElement$sheet2 = styleElement.sheet) === null || _styleElement$sheet2 === void 0 ? void 0 : _styleElement$sheet2.insertRule(`*{cursor: ${style} !important;}`)) !== null && _styleElement$sheet$i !== void 0 ? _styleElement$sheet$i : -1;
260
- }
261
-
262
- function isKeyDown(event) {
263
- return event.type === "keydown";
264
- }
265
- function isPointerEvent(event) {
266
- return event.type.startsWith("pointer");
267
- }
268
- function isMouseEvent(event) {
269
- return event.type.startsWith("mouse");
270
- }
271
-
272
- function getResizeEventCoordinates(event) {
273
- if (isPointerEvent(event)) {
274
- if (event.isPrimary) {
275
- return {
276
- x: event.clientX,
277
- y: event.clientY
278
- };
279
- }
280
- } else if (isMouseEvent(event)) {
281
- return {
282
- x: event.clientX,
283
- y: event.clientY
284
- };
285
- }
286
- return {
287
- x: Infinity,
288
- y: Infinity
289
- };
290
- }
291
-
292
- function getInputType() {
293
- if (typeof matchMedia === "function") {
294
- return matchMedia("(pointer:coarse)").matches ? "coarse" : "fine";
295
- }
296
- }
297
-
298
- function intersects(rectOne, rectTwo, strict) {
299
- if (strict) {
300
- return rectOne.x < rectTwo.x + rectTwo.width && rectOne.x + rectOne.width > rectTwo.x && rectOne.y < rectTwo.y + rectTwo.height && rectOne.y + rectOne.height > rectTwo.y;
301
- } else {
302
- return rectOne.x <= rectTwo.x + rectTwo.width && rectOne.x + rectOne.width >= rectTwo.x && rectOne.y <= rectTwo.y + rectTwo.height && rectOne.y + rectOne.height >= rectTwo.y;
303
- }
304
- }
305
-
306
- // Forked from NPM stacking-order@2.0.0
307
-
308
- /**
309
- * Determine which of two nodes appears in front of the other —
310
- * if `a` is in front, returns 1, otherwise returns -1
311
- * @param {HTMLElement | SVGElement} a
312
- * @param {HTMLElement | SVGElement} b
313
- */
314
- function compare(a, b) {
315
- if (a === b) throw new Error("Cannot compare node with itself");
316
- const ancestors = {
317
- a: get_ancestors(a),
318
- b: get_ancestors(b)
319
- };
320
- let common_ancestor;
321
-
322
- // remove shared ancestors
323
- while (ancestors.a.at(-1) === ancestors.b.at(-1)) {
324
- a = ancestors.a.pop();
325
- b = ancestors.b.pop();
326
- common_ancestor = a;
327
- }
328
- assert(common_ancestor, "Stacking order can only be calculated for elements with a common ancestor");
329
- const z_indexes = {
330
- a: get_z_index(find_stacking_context(ancestors.a)),
331
- b: get_z_index(find_stacking_context(ancestors.b))
332
- };
333
- if (z_indexes.a === z_indexes.b) {
334
- const children = common_ancestor.childNodes;
335
- const furthest_ancestors = {
336
- a: ancestors.a.at(-1),
337
- b: ancestors.b.at(-1)
338
- };
339
- let i = children.length;
340
- while (i--) {
341
- const child = children[i];
342
- if (child === furthest_ancestors.a) return 1;
343
- if (child === furthest_ancestors.b) return -1;
344
- }
345
- }
346
- return Math.sign(z_indexes.a - z_indexes.b);
347
- }
348
- const props = /\b(?:position|zIndex|opacity|transform|webkitTransform|mixBlendMode|filter|webkitFilter|isolation)\b/;
349
-
350
- /** @param {HTMLElement | SVGElement} node */
351
- function is_flex_item(node) {
352
- var _get_parent;
353
- // @ts-ignore
354
- const display = getComputedStyle((_get_parent = get_parent(node)) !== null && _get_parent !== void 0 ? _get_parent : node).display;
355
- return display === "flex" || display === "inline-flex";
356
- }
357
-
358
- /** @param {HTMLElement | SVGElement} node */
359
- function creates_stacking_context(node) {
360
- const style = getComputedStyle(node);
361
-
362
- // https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Positioning/Understanding_z_index/The_stacking_context
363
- if (style.position === "fixed") return true;
364
- // Forked to fix upstream bug https://github.com/Rich-Harris/stacking-order/issues/3
365
- // if (
366
- // (style.zIndex !== "auto" && style.position !== "static") ||
367
- // is_flex_item(node)
368
- // )
369
- if (style.zIndex !== "auto" && (style.position !== "static" || is_flex_item(node))) return true;
370
- if (+style.opacity < 1) return true;
371
- if ("transform" in style && style.transform !== "none") return true;
372
- if ("webkitTransform" in style && style.webkitTransform !== "none") return true;
373
- if ("mixBlendMode" in style && style.mixBlendMode !== "normal") return true;
374
- if ("filter" in style && style.filter !== "none") return true;
375
- if ("webkitFilter" in style && style.webkitFilter !== "none") return true;
376
- if ("isolation" in style && style.isolation === "isolate") return true;
377
- if (props.test(style.willChange)) return true;
378
- // @ts-expect-error
379
- if (style.webkitOverflowScrolling === "touch") return true;
380
- return false;
381
- }
382
-
383
- /** @param {(HTMLElement| SVGElement)[]} nodes */
384
- function find_stacking_context(nodes) {
385
- let i = nodes.length;
386
- while (i--) {
387
- const node = nodes[i];
388
- assert(node, "Missing node");
389
- if (creates_stacking_context(node)) return node;
390
- }
391
- return null;
392
- }
393
-
394
- /** @param {HTMLElement | SVGElement} node */
395
- function get_z_index(node) {
396
- return node && Number(getComputedStyle(node).zIndex) || 0;
397
- }
398
-
399
- /** @param {HTMLElement} node */
400
- function get_ancestors(node) {
401
- const ancestors = [];
402
- while (node) {
403
- ancestors.push(node);
404
- // @ts-ignore
405
- node = get_parent(node);
406
- }
407
- return ancestors; // [ node, ... <body>, <html>, document ]
408
- }
409
-
410
- /** @param {HTMLElement} node */
411
- function get_parent(node) {
412
- const {
413
- parentNode
414
- } = node;
415
- if (parentNode && parentNode instanceof ShadowRoot) {
416
- return parentNode.host;
417
- }
418
- return parentNode;
419
- }
420
-
421
- const EXCEEDED_HORIZONTAL_MIN = 0b0001;
422
- const EXCEEDED_HORIZONTAL_MAX = 0b0010;
423
- const EXCEEDED_VERTICAL_MIN = 0b0100;
424
- const EXCEEDED_VERTICAL_MAX = 0b1000;
425
- const isCoarsePointer = getInputType() === "coarse";
426
- let intersectingHandles = [];
427
- let isPointerDown = false;
428
- let ownerDocumentCounts = new Map();
429
- let panelConstraintFlags = new Map();
430
- const registeredResizeHandlers = new Set();
431
- function registerResizeHandle(resizeHandleId, element, direction, hitAreaMargins, setResizeHandlerState) {
432
- var _ownerDocumentCounts$;
433
- const {
434
- ownerDocument
435
- } = element;
436
- const data = {
437
- direction,
438
- element,
439
- hitAreaMargins,
440
- setResizeHandlerState
441
- };
442
- const count = (_ownerDocumentCounts$ = ownerDocumentCounts.get(ownerDocument)) !== null && _ownerDocumentCounts$ !== void 0 ? _ownerDocumentCounts$ : 0;
443
- ownerDocumentCounts.set(ownerDocument, count + 1);
444
- registeredResizeHandlers.add(data);
445
- updateListeners();
446
- return function unregisterResizeHandle() {
447
- var _ownerDocumentCounts$2;
448
- panelConstraintFlags.delete(resizeHandleId);
449
- registeredResizeHandlers.delete(data);
450
- const count = (_ownerDocumentCounts$2 = ownerDocumentCounts.get(ownerDocument)) !== null && _ownerDocumentCounts$2 !== void 0 ? _ownerDocumentCounts$2 : 1;
451
- ownerDocumentCounts.set(ownerDocument, count - 1);
452
- updateListeners();
453
- if (count === 1) {
454
- ownerDocumentCounts.delete(ownerDocument);
455
- }
456
-
457
- // If the resize handle that is currently unmounting is intersecting with the pointer,
458
- // update the global pointer to account for the change
459
- if (intersectingHandles.includes(data)) {
460
- const index = intersectingHandles.indexOf(data);
461
- if (index >= 0) {
462
- intersectingHandles.splice(index, 1);
463
- }
464
- updateCursor();
465
-
466
- // Also instruct the handle to stop dragging; this prevents the parent group from being left in an inconsistent state
467
- // See github.com/bvaughn/react-resizable-panels/issues/402
468
- setResizeHandlerState("up", true, null);
469
- }
470
- };
471
- }
472
- function handlePointerDown(event) {
473
- const {
474
- target
475
- } = event;
476
- const {
477
- x,
478
- y
479
- } = getResizeEventCoordinates(event);
480
- isPointerDown = true;
481
- recalculateIntersectingHandles({
482
- target,
483
- x,
484
- y
485
- });
486
- updateListeners();
487
- if (intersectingHandles.length > 0) {
488
- updateResizeHandlerStates("down", event);
489
- event.preventDefault();
490
- if (!isWithinResizeHandle(target)) {
491
- event.stopImmediatePropagation();
492
- }
493
- }
494
- }
495
- function handlePointerMove(event) {
496
- const {
497
- x,
498
- y
499
- } = getResizeEventCoordinates(event);
500
-
501
- // Edge case (see #340)
502
- // Detect when the pointer has been released outside an iframe on a different domain
503
- if (isPointerDown && event.buttons === 0) {
504
- isPointerDown = false;
505
- updateResizeHandlerStates("up", event);
506
- }
507
- if (!isPointerDown) {
508
- const {
509
- target
510
- } = event;
511
-
512
- // Recalculate intersecting handles whenever the pointer moves, except if it has already been pressed
513
- // at that point, the handles may not move with the pointer (depending on constraints)
514
- // but the same set of active handles should be locked until the pointer is released
515
- recalculateIntersectingHandles({
516
- target,
517
- x,
518
- y
519
- });
520
- }
521
- updateResizeHandlerStates("move", event);
522
-
523
- // Update cursor based on return value(s) from active handles
524
- updateCursor();
525
- if (intersectingHandles.length > 0) {
526
- event.preventDefault();
527
- }
528
- }
529
- function handlePointerUp(event) {
530
- const {
531
- target
532
- } = event;
533
- const {
534
- x,
535
- y
536
- } = getResizeEventCoordinates(event);
537
- panelConstraintFlags.clear();
538
- isPointerDown = false;
539
- if (intersectingHandles.length > 0) {
540
- event.preventDefault();
541
- if (!isWithinResizeHandle(target)) {
542
- event.stopImmediatePropagation();
543
- }
544
- }
545
- updateResizeHandlerStates("up", event);
546
- recalculateIntersectingHandles({
547
- target,
548
- x,
549
- y
550
- });
551
- updateCursor();
552
- updateListeners();
553
- }
554
- function isWithinResizeHandle(element) {
555
- let currentElement = element;
556
- while (currentElement) {
557
- if (currentElement.hasAttribute(DATA_ATTRIBUTES.resizeHandle)) {
558
- return true;
559
- }
560
- currentElement = currentElement.parentElement;
561
- }
562
- return false;
563
- }
564
- function recalculateIntersectingHandles({
565
- target,
566
- x,
567
- y
568
- }) {
569
- intersectingHandles.splice(0);
570
- let targetElement = null;
571
- if (target instanceof HTMLElement || target instanceof SVGElement) {
572
- targetElement = target;
573
- }
574
- registeredResizeHandlers.forEach(data => {
575
- const {
576
- element: dragHandleElement,
577
- hitAreaMargins
578
- } = data;
579
- const dragHandleRect = dragHandleElement.getBoundingClientRect();
580
- const {
581
- bottom,
582
- left,
583
- right,
584
- top
585
- } = dragHandleRect;
586
- const margin = isCoarsePointer ? hitAreaMargins.coarse : hitAreaMargins.fine;
587
- const eventIntersects = x >= left - margin && x <= right + margin && y >= top - margin && y <= bottom + margin;
588
- if (eventIntersects) {
589
- // TRICKY
590
- // We listen for pointers events at the root in order to support hit area margins
591
- // (determining when the pointer is close enough to an element to be considered a "hit")
592
- // Clicking on an element "above" a handle (e.g. a modal) should prevent a hit though
593
- // so at this point we need to compare stacking order of a potentially intersecting drag handle,
594
- // and the element that was actually clicked/touched
595
- if (targetElement !== null && document.contains(targetElement) && dragHandleElement !== targetElement && !dragHandleElement.contains(targetElement) && !targetElement.contains(dragHandleElement) &&
596
- // Calculating stacking order has a cost, so we should avoid it if possible
597
- // That is why we only check potentially intersecting handles,
598
- // and why we skip if the event target is within the handle's DOM
599
- compare(targetElement, dragHandleElement) > 0) {
600
- // If the target is above the drag handle, then we also need to confirm they overlap
601
- // If they are beside each other (e.g. a panel and its drag handle) then the handle is still interactive
602
- //
603
- // It's not enough to compare only the target
604
- // The target might be a small element inside of a larger container
605
- // (For example, a SPAN or a DIV inside of a larger modal dialog)
606
- let currentElement = targetElement;
607
- let didIntersect = false;
608
- while (currentElement) {
609
- if (currentElement.contains(dragHandleElement)) {
610
- break;
611
- } else if (intersects(currentElement.getBoundingClientRect(), dragHandleRect, true)) {
612
- didIntersect = true;
613
- break;
614
- }
615
- currentElement = currentElement.parentElement;
616
- }
617
- if (didIntersect) {
618
- return;
619
- }
620
- }
621
- intersectingHandles.push(data);
622
- }
623
- });
624
- }
625
- function reportConstraintsViolation(resizeHandleId, flag) {
626
- panelConstraintFlags.set(resizeHandleId, flag);
627
- }
628
- function updateCursor() {
629
- let intersectsHorizontal = false;
630
- let intersectsVertical = false;
631
- intersectingHandles.forEach(data => {
632
- const {
633
- direction
634
- } = data;
635
- if (direction === "horizontal") {
636
- intersectsHorizontal = true;
637
- } else {
638
- intersectsVertical = true;
639
- }
640
- });
641
- let constraintFlags = 0;
642
- panelConstraintFlags.forEach(flag => {
643
- constraintFlags |= flag;
644
- });
645
- if (intersectsHorizontal && intersectsVertical) {
646
- setGlobalCursorStyle("intersection", constraintFlags);
647
- } else if (intersectsHorizontal) {
648
- setGlobalCursorStyle("horizontal", constraintFlags);
649
- } else if (intersectsVertical) {
650
- setGlobalCursorStyle("vertical", constraintFlags);
651
- } else {
652
- resetGlobalCursorStyle();
653
- }
654
- }
655
- let listenersAbortController = new AbortController();
656
- function updateListeners() {
657
- listenersAbortController.abort();
658
- listenersAbortController = new AbortController();
659
- const options = {
660
- capture: true,
661
- signal: listenersAbortController.signal
662
- };
663
- if (!registeredResizeHandlers.size) {
664
- return;
665
- }
666
- if (isPointerDown) {
667
- if (intersectingHandles.length > 0) {
668
- ownerDocumentCounts.forEach((count, ownerDocument) => {
669
- const {
670
- body
671
- } = ownerDocument;
672
- if (count > 0) {
673
- body.addEventListener("contextmenu", handlePointerUp, options);
674
- body.addEventListener("pointerleave", handlePointerMove, options);
675
- body.addEventListener("pointermove", handlePointerMove, options);
676
- }
677
- });
678
- }
679
- window.addEventListener("pointerup", handlePointerUp, options);
680
- window.addEventListener("pointercancel", handlePointerUp, options);
681
- } else {
682
- ownerDocumentCounts.forEach((count, ownerDocument) => {
683
- const {
684
- body
685
- } = ownerDocument;
686
- if (count > 0) {
687
- body.addEventListener("pointerdown", handlePointerDown, options);
688
- body.addEventListener("pointermove", handlePointerMove, options);
689
- }
690
- });
691
- }
692
- }
693
- function updateResizeHandlerStates(action, event) {
694
- registeredResizeHandlers.forEach(data => {
695
- const {
696
- setResizeHandlerState
697
- } = data;
698
- const isActive = intersectingHandles.includes(data);
699
- setResizeHandlerState(action, isActive, event);
700
- });
701
- }
702
-
703
- function useForceUpdate() {
704
- const [_, setCount] = React.useState(0);
705
- return React.useCallback(() => setCount(prevCount => prevCount + 1), []);
706
- }
707
-
708
- function assert(expectedCondition, message) {
709
- if (!expectedCondition) {
710
- console.error(message);
711
- throw Error(message);
712
- }
713
- }
714
-
715
- function fuzzyCompareNumbers(actual, expected, fractionDigits = PRECISION) {
716
- if (actual.toFixed(fractionDigits) === expected.toFixed(fractionDigits)) {
717
- return 0;
718
- } else {
719
- return actual > expected ? 1 : -1;
720
- }
721
- }
722
- function fuzzyNumbersEqual$1(actual, expected, fractionDigits = PRECISION) {
723
- return fuzzyCompareNumbers(actual, expected, fractionDigits) === 0;
724
- }
725
-
726
- function fuzzyNumbersEqual(actual, expected, fractionDigits) {
727
- return fuzzyCompareNumbers(actual, expected, fractionDigits) === 0;
728
- }
729
-
730
- function fuzzyLayoutsEqual(actual, expected, fractionDigits) {
731
- if (actual.length !== expected.length) {
732
- return false;
733
- }
734
- for (let index = 0; index < actual.length; index++) {
735
- const actualSize = actual[index];
736
- const expectedSize = expected[index];
737
- if (!fuzzyNumbersEqual(actualSize, expectedSize, fractionDigits)) {
738
- return false;
739
- }
740
- }
741
- return true;
742
- }
743
-
744
- // Panel size must be in percentages; pixel values should be pre-converted
745
- function resizePanel({
746
- panelConstraints: panelConstraintsArray,
747
- panelIndex,
748
- size
749
- }) {
750
- const panelConstraints = panelConstraintsArray[panelIndex];
751
- assert(panelConstraints != null, `Panel constraints not found for index ${panelIndex}`);
752
- let {
753
- collapsedSize = 0,
754
- collapsible,
755
- maxSize = 100,
756
- minSize = 0
757
- } = panelConstraints;
758
- if (fuzzyCompareNumbers(size, minSize) < 0) {
759
- if (collapsible) {
760
- // Collapsible panels should snap closed or open only once they cross the halfway point between collapsed and min size.
761
- const halfwayPoint = (collapsedSize + minSize) / 2;
762
- if (fuzzyCompareNumbers(size, halfwayPoint) < 0) {
763
- size = collapsedSize;
764
- } else {
765
- size = minSize;
766
- }
767
- } else {
768
- size = minSize;
769
- }
770
- }
771
- size = Math.min(maxSize, size);
772
- size = parseFloat(size.toFixed(PRECISION));
773
- return size;
774
- }
775
-
776
- // All units must be in percentages; pixel values should be pre-converted
777
- function adjustLayoutByDelta({
778
- delta,
779
- initialLayout,
780
- panelConstraints: panelConstraintsArray,
781
- pivotIndices,
782
- prevLayout,
783
- trigger
784
- }) {
785
- if (fuzzyNumbersEqual(delta, 0)) {
786
- return initialLayout;
787
- }
788
- const nextLayout = [...initialLayout];
789
- const [firstPivotIndex, secondPivotIndex] = pivotIndices;
790
- assert(firstPivotIndex != null, "Invalid first pivot index");
791
- assert(secondPivotIndex != null, "Invalid second pivot index");
792
- let deltaApplied = 0;
793
-
794
- // const DEBUG = [];
795
- // DEBUG.push(`adjustLayoutByDelta()`);
796
- // DEBUG.push(` initialLayout: ${initialLayout.join(", ")}`);
797
- // DEBUG.push(` prevLayout: ${prevLayout.join(", ")}`);
798
- // DEBUG.push(` delta: ${delta}`);
799
- // DEBUG.push(` pivotIndices: ${pivotIndices.join(", ")}`);
800
- // DEBUG.push(` trigger: ${trigger}`);
801
- // DEBUG.push("");
802
-
803
- // A resizing panel affects the panels before or after it.
804
- //
805
- // A negative delta means the panel(s) immediately after the resize handle should grow/expand by decreasing its offset.
806
- // Other panels may also need to shrink/contract (and shift) to make room, depending on the min weights.
807
- //
808
- // A positive delta means the panel(s) immediately before the resize handle should "expand".
809
- // This is accomplished by shrinking/contracting (and shifting) one or more of the panels after the resize handle.
810
-
811
- {
812
- // If this is a resize triggered by a keyboard event, our logic for expanding/collapsing is different.
813
- // We no longer check the halfway threshold because this may prevent the panel from expanding at all.
814
- if (trigger === "keyboard") {
815
- {
816
- // Check if we should expand a collapsed panel
817
- const index = delta < 0 ? secondPivotIndex : firstPivotIndex;
818
- const panelConstraints = panelConstraintsArray[index];
819
- assert(panelConstraints, `Panel constraints not found for index ${index}`);
820
- const {
821
- collapsedSize = 0,
822
- collapsible,
823
- minSize = 0
824
- } = panelConstraints;
825
-
826
- // DEBUG.push(`edge case check 1: ${index}`);
827
- // DEBUG.push(` -> collapsible? ${collapsible}`);
828
- if (collapsible) {
829
- const prevSize = initialLayout[index];
830
- assert(prevSize != null, `Previous layout not found for panel index ${index}`);
831
- if (fuzzyNumbersEqual(prevSize, collapsedSize)) {
832
- const localDelta = minSize - prevSize;
833
- // DEBUG.push(` -> expand delta: ${localDelta}`);
834
-
835
- if (fuzzyCompareNumbers(localDelta, Math.abs(delta)) > 0) {
836
- delta = delta < 0 ? 0 - localDelta : localDelta;
837
- // DEBUG.push(` -> delta: ${delta}`);
838
- }
839
- }
840
- }
841
- }
842
-
843
- {
844
- // Check if we should collapse a panel at its minimum size
845
- const index = delta < 0 ? firstPivotIndex : secondPivotIndex;
846
- const panelConstraints = panelConstraintsArray[index];
847
- assert(panelConstraints, `No panel constraints found for index ${index}`);
848
- const {
849
- collapsedSize = 0,
850
- collapsible,
851
- minSize = 0
852
- } = panelConstraints;
853
-
854
- // DEBUG.push(`edge case check 2: ${index}`);
855
- // DEBUG.push(` -> collapsible? ${collapsible}`);
856
- if (collapsible) {
857
- const prevSize = initialLayout[index];
858
- assert(prevSize != null, `Previous layout not found for panel index ${index}`);
859
- if (fuzzyNumbersEqual(prevSize, minSize)) {
860
- const localDelta = prevSize - collapsedSize;
861
- // DEBUG.push(` -> expand delta: ${localDelta}`);
862
-
863
- if (fuzzyCompareNumbers(localDelta, Math.abs(delta)) > 0) {
864
- delta = delta < 0 ? 0 - localDelta : localDelta;
865
- // DEBUG.push(` -> delta: ${delta}`);
866
- }
867
- }
868
- }
869
- }
870
- }
871
- // DEBUG.push("");
872
- }
873
-
874
- {
875
- // Pre-calculate max available delta in the opposite direction of our pivot.
876
- // This will be the maximum amount we're allowed to expand/contract the panels in the primary direction.
877
- // If this amount is less than the requested delta, adjust the requested delta.
878
- // If this amount is greater than the requested delta, that's useful information too–
879
- // as an expanding panel might change from collapsed to min size.
880
-
881
- const increment = delta < 0 ? 1 : -1;
882
- let index = delta < 0 ? secondPivotIndex : firstPivotIndex;
883
- let maxAvailableDelta = 0;
884
-
885
- // DEBUG.push("pre calc...");
886
- while (true) {
887
- const prevSize = initialLayout[index];
888
- assert(prevSize != null, `Previous layout not found for panel index ${index}`);
889
- const maxSafeSize = resizePanel({
890
- panelConstraints: panelConstraintsArray,
891
- panelIndex: index,
892
- size: 100
893
- });
894
- const delta = maxSafeSize - prevSize;
895
- // DEBUG.push(` ${index}: ${prevSize} -> ${maxSafeSize}`);
896
-
897
- maxAvailableDelta += delta;
898
- index += increment;
899
- if (index < 0 || index >= panelConstraintsArray.length) {
900
- break;
901
- }
902
- }
903
-
904
- // DEBUG.push(` -> max available delta: ${maxAvailableDelta}`);
905
- const minAbsDelta = Math.min(Math.abs(delta), Math.abs(maxAvailableDelta));
906
- delta = delta < 0 ? 0 - minAbsDelta : minAbsDelta;
907
- // DEBUG.push(` -> adjusted delta: ${delta}`);
908
- // DEBUG.push("");
909
- }
910
-
911
- {
912
- // Delta added to a panel needs to be subtracted from other panels (within the constraints that those panels allow).
913
-
914
- const pivotIndex = delta < 0 ? firstPivotIndex : secondPivotIndex;
915
- let index = pivotIndex;
916
- while (index >= 0 && index < panelConstraintsArray.length) {
917
- const deltaRemaining = Math.abs(delta) - Math.abs(deltaApplied);
918
- const prevSize = initialLayout[index];
919
- assert(prevSize != null, `Previous layout not found for panel index ${index}`);
920
- const unsafeSize = prevSize - deltaRemaining;
921
- const safeSize = resizePanel({
922
- panelConstraints: panelConstraintsArray,
923
- panelIndex: index,
924
- size: unsafeSize
925
- });
926
- if (!fuzzyNumbersEqual(prevSize, safeSize)) {
927
- deltaApplied += prevSize - safeSize;
928
- nextLayout[index] = safeSize;
929
- if (deltaApplied.toPrecision(3).localeCompare(Math.abs(delta).toPrecision(3), undefined, {
930
- numeric: true
931
- }) >= 0) {
932
- break;
933
- }
934
- }
935
- if (delta < 0) {
936
- index--;
937
- } else {
938
- index++;
939
- }
940
- }
941
- }
942
- // DEBUG.push(`after 1: ${nextLayout.join(", ")}`);
943
- // DEBUG.push(` deltaApplied: ${deltaApplied}`);
944
- // DEBUG.push("");
945
-
946
- // If we were unable to resize any of the panels panels, return the previous state.
947
- // This will essentially bailout and ignore e.g. drags past a panel's boundaries
948
- if (fuzzyLayoutsEqual(prevLayout, nextLayout)) {
949
- // DEBUG.push(`bailout to previous layout: ${prevLayout.join(", ")}`);
950
- // console.log(DEBUG.join("\n"));
951
-
952
- return prevLayout;
953
- }
954
- {
955
- // Now distribute the applied delta to the panels in the other direction
956
- const pivotIndex = delta < 0 ? secondPivotIndex : firstPivotIndex;
957
- const prevSize = initialLayout[pivotIndex];
958
- assert(prevSize != null, `Previous layout not found for panel index ${pivotIndex}`);
959
- const unsafeSize = prevSize + deltaApplied;
960
- const safeSize = resizePanel({
961
- panelConstraints: panelConstraintsArray,
962
- panelIndex: pivotIndex,
963
- size: unsafeSize
964
- });
965
-
966
- // Adjust the pivot panel before, but only by the amount that surrounding panels were able to shrink/contract.
967
- nextLayout[pivotIndex] = safeSize;
968
-
969
- // Edge case where expanding or contracting one panel caused another one to change collapsed state
970
- if (!fuzzyNumbersEqual(safeSize, unsafeSize)) {
971
- let deltaRemaining = unsafeSize - safeSize;
972
- const pivotIndex = delta < 0 ? secondPivotIndex : firstPivotIndex;
973
- let index = pivotIndex;
974
- while (index >= 0 && index < panelConstraintsArray.length) {
975
- const prevSize = nextLayout[index];
976
- assert(prevSize != null, `Previous layout not found for panel index ${index}`);
977
- const unsafeSize = prevSize + deltaRemaining;
978
- const safeSize = resizePanel({
979
- panelConstraints: panelConstraintsArray,
980
- panelIndex: index,
981
- size: unsafeSize
982
- });
983
- if (!fuzzyNumbersEqual(prevSize, safeSize)) {
984
- deltaRemaining -= safeSize - prevSize;
985
- nextLayout[index] = safeSize;
986
- }
987
- if (fuzzyNumbersEqual(deltaRemaining, 0)) {
988
- break;
989
- }
990
- if (delta > 0) {
991
- index--;
992
- } else {
993
- index++;
994
- }
995
- }
996
- }
997
- }
998
- // DEBUG.push(`after 2: ${nextLayout.join(", ")}`);
999
- // DEBUG.push(` deltaApplied: ${deltaApplied}`);
1000
- // DEBUG.push("");
1001
-
1002
- const totalSize = nextLayout.reduce((total, size) => size + total, 0);
1003
- // DEBUG.push(`total size: ${totalSize}`);
1004
-
1005
- // If our new layout doesn't add up to 100%, that means the requested delta can't be applied
1006
- // In that case, fall back to our most recent valid layout
1007
- if (!fuzzyNumbersEqual(totalSize, 100)) {
1008
- // DEBUG.push(`bailout to previous layout: ${prevLayout.join(", ")}`);
1009
- // console.log(DEBUG.join("\n"));
1010
-
1011
- return prevLayout;
1012
- }
1013
-
1014
- // console.log(DEBUG.join("\n"));
1015
- return nextLayout;
1016
- }
1017
-
1018
- function getResizeHandleElementsForGroup(groupId, scope = document) {
1019
- return Array.from(scope.querySelectorAll(`[${DATA_ATTRIBUTES.resizeHandleId}][data-panel-group-id="${groupId}"]`));
1020
- }
1021
-
1022
- function getResizeHandleElementIndex(groupId, id, scope = document) {
1023
- const handles = getResizeHandleElementsForGroup(groupId, scope);
1024
- const index = handles.findIndex(handle => handle.getAttribute(DATA_ATTRIBUTES.resizeHandleId) === id);
1025
- return index !== null && index !== void 0 ? index : null;
1026
- }
1027
-
1028
- function determinePivotIndices(groupId, dragHandleId, panelGroupElement) {
1029
- const index = getResizeHandleElementIndex(groupId, dragHandleId, panelGroupElement);
1030
- return index != null ? [index, index + 1] : [-1, -1];
1031
- }
1032
-
1033
- function getPanelGroupElement(id, rootElement = document) {
1034
- var _dataset;
1035
- //If the root element is the PanelGroup
1036
- if (rootElement instanceof HTMLElement && (rootElement === null || rootElement === void 0 ? void 0 : (_dataset = rootElement.dataset) === null || _dataset === void 0 ? void 0 : _dataset.panelGroupId) == id) {
1037
- return rootElement;
1038
- }
1039
-
1040
- //Else query children
1041
- const element = rootElement.querySelector(`[data-panel-group][data-panel-group-id="${id}"]`);
1042
- if (element) {
1043
- return element;
1044
- }
1045
- return null;
1046
- }
1047
-
1048
- function getResizeHandleElement(id, scope = document) {
1049
- const element = scope.querySelector(`[${DATA_ATTRIBUTES.resizeHandleId}="${id}"]`);
1050
- if (element) {
1051
- return element;
1052
- }
1053
- return null;
1054
- }
1055
-
1056
- function getResizeHandlePanelIds(groupId, handleId, panelsArray, scope = document) {
1057
- var _panelsArray$index$id, _panelsArray$index, _panelsArray$id, _panelsArray;
1058
- const handle = getResizeHandleElement(handleId, scope);
1059
- const handles = getResizeHandleElementsForGroup(groupId, scope);
1060
- const index = handle ? handles.indexOf(handle) : -1;
1061
- const idBefore = (_panelsArray$index$id = (_panelsArray$index = panelsArray[index]) === null || _panelsArray$index === void 0 ? void 0 : _panelsArray$index.id) !== null && _panelsArray$index$id !== void 0 ? _panelsArray$index$id : null;
1062
- const idAfter = (_panelsArray$id = (_panelsArray = panelsArray[index + 1]) === null || _panelsArray === void 0 ? void 0 : _panelsArray.id) !== null && _panelsArray$id !== void 0 ? _panelsArray$id : null;
1063
- return [idBefore, idAfter];
1064
- }
1065
-
1066
- // https://www.w3.org/WAI/ARIA/apg/patterns/windowsplitter/
1067
-
1068
- function useWindowSplitterPanelGroupBehavior({
1069
- committedValuesRef,
1070
- eagerValuesRef,
1071
- groupId,
1072
- layout,
1073
- panelDataArray,
1074
- panelGroupElement,
1075
- setLayout
1076
- }) {
1077
- React.useRef({
1078
- didWarnAboutMissingResizeHandle: false
1079
- });
1080
- React.useEffect(() => {
1081
- if (!panelGroupElement) {
1082
- return;
1083
- }
1084
- const eagerValues = eagerValuesRef.current;
1085
- assert(eagerValues, `Eager values not found`);
1086
- const {
1087
- panelDataArray
1088
- } = eagerValues;
1089
- const groupElement = getPanelGroupElement(groupId, panelGroupElement);
1090
- assert(groupElement != null, `No group found for id "${groupId}"`);
1091
- const handles = getResizeHandleElementsForGroup(groupId, panelGroupElement);
1092
- assert(handles, `No resize handles found for group id "${groupId}"`);
1093
- const cleanupFunctions = handles.map(handle => {
1094
- const handleId = handle.getAttribute(DATA_ATTRIBUTES.resizeHandleId);
1095
- assert(handleId, `Resize handle element has no handle id attribute`);
1096
- const [idBefore, idAfter] = getResizeHandlePanelIds(groupId, handleId, panelDataArray, panelGroupElement);
1097
- if (idBefore == null || idAfter == null) {
1098
- return () => {};
1099
- }
1100
- const onKeyDown = event => {
1101
- if (event.defaultPrevented) {
1102
- return;
1103
- }
1104
- switch (event.key) {
1105
- case "Enter":
1106
- {
1107
- event.preventDefault();
1108
- const index = panelDataArray.findIndex(panelData => panelData.id === idBefore);
1109
- if (index >= 0) {
1110
- const panelData = panelDataArray[index];
1111
- assert(panelData, `No panel data found for index ${index}`);
1112
- const size = layout[index];
1113
- const {
1114
- collapsedSize = 0,
1115
- collapsible,
1116
- minSize = 0
1117
- } = panelData.constraints;
1118
- if (size != null && collapsible) {
1119
- const nextLayout = adjustLayoutByDelta({
1120
- delta: fuzzyNumbersEqual(size, collapsedSize) ? minSize - collapsedSize : collapsedSize - size,
1121
- initialLayout: layout,
1122
- panelConstraints: panelDataArray.map(panelData => panelData.constraints),
1123
- pivotIndices: determinePivotIndices(groupId, handleId, panelGroupElement),
1124
- prevLayout: layout,
1125
- trigger: "keyboard"
1126
- });
1127
- if (layout !== nextLayout) {
1128
- setLayout(nextLayout);
1129
- }
1130
- }
1131
- }
1132
- break;
1133
- }
1134
- }
1135
- };
1136
- handle.addEventListener("keydown", onKeyDown);
1137
- return () => {
1138
- handle.removeEventListener("keydown", onKeyDown);
1139
- };
1140
- });
1141
- return () => {
1142
- cleanupFunctions.forEach(cleanupFunction => cleanupFunction());
1143
- };
1144
- }, [panelGroupElement, committedValuesRef, eagerValuesRef, groupId, layout, panelDataArray, setLayout]);
1145
- }
1146
-
1147
- function areEqual(arrayA, arrayB) {
1148
- if (arrayA.length !== arrayB.length) {
1149
- return false;
1150
- }
1151
- for (let index = 0; index < arrayA.length; index++) {
1152
- if (arrayA[index] !== arrayB[index]) {
1153
- return false;
1154
- }
1155
- }
1156
- return true;
1157
- }
1158
-
1159
- function getResizeEventCursorPosition(direction, event) {
1160
- const isHorizontal = direction === "horizontal";
1161
- const {
1162
- x,
1163
- y
1164
- } = getResizeEventCoordinates(event);
1165
- return isHorizontal ? x : y;
1166
- }
1167
-
1168
- function calculateDragOffsetPercentage(event, dragHandleId, direction, initialDragState, panelGroupElement) {
1169
- const isHorizontal = direction === "horizontal";
1170
- const handleElement = getResizeHandleElement(dragHandleId, panelGroupElement);
1171
- assert(handleElement, `No resize handle element found for id "${dragHandleId}"`);
1172
- const groupId = handleElement.getAttribute(DATA_ATTRIBUTES.groupId);
1173
- assert(groupId, `Resize handle element has no group id attribute`);
1174
- let {
1175
- initialCursorPosition
1176
- } = initialDragState;
1177
- const cursorPosition = getResizeEventCursorPosition(direction, event);
1178
- const groupElement = getPanelGroupElement(groupId, panelGroupElement);
1179
- assert(groupElement, `No group element found for id "${groupId}"`);
1180
- const groupRect = groupElement.getBoundingClientRect();
1181
- const groupSizeInPixels = isHorizontal ? groupRect.width : groupRect.height;
1182
- const offsetPixels = cursorPosition - initialCursorPosition;
1183
- const offsetPercentage = offsetPixels / groupSizeInPixels * 100;
1184
- return offsetPercentage;
1185
- }
1186
-
1187
- // https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/movementX
1188
- function calculateDeltaPercentage(event, dragHandleId, direction, initialDragState, keyboardResizeBy, panelGroupElement) {
1189
- if (isKeyDown(event)) {
1190
- const isHorizontal = direction === "horizontal";
1191
- let delta = 0;
1192
- if (event.shiftKey) {
1193
- delta = 100;
1194
- } else if (keyboardResizeBy != null) {
1195
- delta = keyboardResizeBy;
1196
- } else {
1197
- delta = 10;
1198
- }
1199
- let movement = 0;
1200
- switch (event.key) {
1201
- case "ArrowDown":
1202
- movement = isHorizontal ? 0 : delta;
1203
- break;
1204
- case "ArrowLeft":
1205
- movement = isHorizontal ? -delta : 0;
1206
- break;
1207
- case "ArrowRight":
1208
- movement = isHorizontal ? delta : 0;
1209
- break;
1210
- case "ArrowUp":
1211
- movement = isHorizontal ? 0 : -delta;
1212
- break;
1213
- case "End":
1214
- movement = 100;
1215
- break;
1216
- case "Home":
1217
- movement = -100;
1218
- break;
1219
- }
1220
- return movement;
1221
- } else {
1222
- if (initialDragState == null) {
1223
- return 0;
1224
- }
1225
- return calculateDragOffsetPercentage(event, dragHandleId, direction, initialDragState, panelGroupElement);
1226
- }
1227
- }
1228
-
1229
- // Layout should be pre-converted into percentages
1230
- function callPanelCallbacks(panelsArray, layout, panelIdToLastNotifiedSizeMap) {
1231
- layout.forEach((size, index) => {
1232
- const panelData = panelsArray[index];
1233
- assert(panelData, `Panel data not found for index ${index}`);
1234
- const {
1235
- callbacks,
1236
- constraints,
1237
- id: panelId
1238
- } = panelData;
1239
- const {
1240
- collapsedSize = 0,
1241
- collapsible
1242
- } = constraints;
1243
- const lastNotifiedSize = panelIdToLastNotifiedSizeMap[panelId];
1244
- if (lastNotifiedSize == null || size !== lastNotifiedSize) {
1245
- panelIdToLastNotifiedSizeMap[panelId] = size;
1246
- const {
1247
- onCollapse,
1248
- onExpand,
1249
- onResize
1250
- } = callbacks;
1251
- if (onResize) {
1252
- onResize(size, lastNotifiedSize);
1253
- }
1254
- if (collapsible && (onCollapse || onExpand)) {
1255
- if (onExpand && (lastNotifiedSize == null || fuzzyNumbersEqual$1(lastNotifiedSize, collapsedSize)) && !fuzzyNumbersEqual$1(size, collapsedSize)) {
1256
- onExpand();
1257
- }
1258
- if (onCollapse && (lastNotifiedSize == null || !fuzzyNumbersEqual$1(lastNotifiedSize, collapsedSize)) && fuzzyNumbersEqual$1(size, collapsedSize)) {
1259
- onCollapse();
1260
- }
1261
- }
1262
- }
1263
- });
1264
- }
1265
-
1266
- function compareLayouts(a, b) {
1267
- if (a.length !== b.length) {
1268
- return false;
1269
- } else {
1270
- for (let index = 0; index < a.length; index++) {
1271
- if (a[index] != b[index]) {
1272
- return false;
1273
- }
1274
- }
1275
- }
1276
- return true;
1277
- }
1278
-
1279
- // This method returns a number between 1 and 100 representing
1280
-
1281
- // the % of the group's overall space this panel should occupy.
1282
- function computePanelFlexBoxStyle({
1283
- defaultSize,
1284
- dragState,
1285
- layout,
1286
- panelData,
1287
- panelIndex,
1288
- precision = 3
1289
- }) {
1290
- const size = layout[panelIndex];
1291
- let flexGrow;
1292
- if (size == null) {
1293
- // Initial render (before panels have registered themselves)
1294
- // In order to support server rendering, fall back to default size if provided
1295
- flexGrow = defaultSize != undefined ? defaultSize.toPrecision(precision) : "1";
1296
- } else if (panelData.length === 1) {
1297
- // Special case: Single panel group should always fill full width/height
1298
- flexGrow = "1";
1299
- } else {
1300
- flexGrow = size.toPrecision(precision);
1301
- }
1302
- return {
1303
- flexBasis: 0,
1304
- flexGrow,
1305
- flexShrink: 1,
1306
- // Without this, Panel sizes may be unintentionally overridden by their content
1307
- overflow: "hidden",
1308
- // Disable pointer events inside of a panel during resize
1309
- // This avoid edge cases like nested iframes
1310
- pointerEvents: dragState !== null ? "none" : undefined
1311
- };
1312
- }
1313
-
1314
- function debounce(callback, durationMs = 10) {
1315
- let timeoutId = null;
1316
- let callable = (...args) => {
1317
- if (timeoutId !== null) {
1318
- clearTimeout(timeoutId);
1319
- }
1320
- timeoutId = setTimeout(() => {
1321
- callback(...args);
1322
- }, durationMs);
1323
- };
1324
- return callable;
1325
- }
1326
-
1327
- // PanelGroup might be rendering in a server-side environment where localStorage is not available
1328
- // or on a browser with cookies/storage disabled.
1329
- // In either case, this function avoids accessing localStorage until needed,
1330
- // and avoids throwing user-visible errors.
1331
- function initializeDefaultStorage(storageObject) {
1332
- try {
1333
- if (typeof localStorage !== "undefined") {
1334
- // Bypass this check for future calls
1335
- storageObject.getItem = name => {
1336
- return localStorage.getItem(name);
1337
- };
1338
- storageObject.setItem = (name, value) => {
1339
- localStorage.setItem(name, value);
1340
- };
1341
- } else {
1342
- throw new Error("localStorage not supported in this environment");
1343
- }
1344
- } catch (error) {
1345
- console.error(error);
1346
- storageObject.getItem = () => null;
1347
- storageObject.setItem = () => {};
1348
- }
1349
- }
1350
-
1351
- function getPanelGroupKey(autoSaveId) {
1352
- return `react-resizable-panels:${autoSaveId}`;
1353
- }
1354
-
1355
- // Note that Panel ids might be user-provided (stable) or useId generated (non-deterministic)
1356
- // so they should not be used as part of the serialization key.
1357
- // Using the min/max size attributes should work well enough as a backup.
1358
- // Pre-sorting by minSize allows remembering layouts even if panels are re-ordered/dragged.
1359
- function getPanelKey(panels) {
1360
- return panels.map(panel => {
1361
- const {
1362
- constraints,
1363
- id,
1364
- idIsFromProps,
1365
- order
1366
- } = panel;
1367
- if (idIsFromProps) {
1368
- return id;
1369
- } else {
1370
- return order ? `${order}:${JSON.stringify(constraints)}` : JSON.stringify(constraints);
1371
- }
1372
- }).sort((a, b) => a.localeCompare(b)).join(",");
1373
- }
1374
- function loadSerializedPanelGroupState(autoSaveId, storage) {
1375
- try {
1376
- const panelGroupKey = getPanelGroupKey(autoSaveId);
1377
- const serialized = storage.getItem(panelGroupKey);
1378
- if (serialized) {
1379
- const parsed = JSON.parse(serialized);
1380
- if (typeof parsed === "object" && parsed != null) {
1381
- return parsed;
1382
- }
1383
- }
1384
- } catch (error) {}
1385
- return null;
1386
- }
1387
- function savePanelGroupState(autoSaveId, panels, panelSizesBeforeCollapse, sizes, storage) {
1388
- var _loadSerializedPanelG2;
1389
- const panelGroupKey = getPanelGroupKey(autoSaveId);
1390
- const panelKey = getPanelKey(panels);
1391
- const state = (_loadSerializedPanelG2 = loadSerializedPanelGroupState(autoSaveId, storage)) !== null && _loadSerializedPanelG2 !== void 0 ? _loadSerializedPanelG2 : {};
1392
- state[panelKey] = {
1393
- expandToSizes: Object.fromEntries(panelSizesBeforeCollapse.entries()),
1394
- layout: sizes
1395
- };
1396
- try {
1397
- storage.setItem(panelGroupKey, JSON.stringify(state));
1398
- } catch (error) {
1399
- console.error(error);
1400
- }
1401
- }
1402
-
1403
- function validatePanelConstraints({
1404
- panelConstraints: panelConstraintsArray,
1405
- panelId,
1406
- panelIndex
1407
- }) {
1408
- {
1409
- const warnings = [];
1410
- const panelConstraints = panelConstraintsArray[panelIndex];
1411
- assert(panelConstraints, `No panel constraints found for index ${panelIndex}`);
1412
- const {
1413
- collapsedSize = 0,
1414
- collapsible = false,
1415
- defaultSize,
1416
- maxSize = 100,
1417
- minSize = 0
1418
- } = panelConstraints;
1419
- if (minSize > maxSize) {
1420
- warnings.push(`min size (${minSize}%) should not be greater than max size (${maxSize}%)`);
1421
- }
1422
- if (defaultSize != null) {
1423
- if (defaultSize < 0) {
1424
- warnings.push("default size should not be less than 0");
1425
- } else if (defaultSize < minSize && (!collapsible || defaultSize !== collapsedSize)) {
1426
- warnings.push("default size should not be less than min size");
1427
- }
1428
- if (defaultSize > 100) {
1429
- warnings.push("default size should not be greater than 100");
1430
- } else if (defaultSize > maxSize) {
1431
- warnings.push("default size should not be greater than max size");
1432
- }
1433
- }
1434
- if (collapsedSize > minSize) {
1435
- warnings.push("collapsed size should not be greater than min size");
1436
- }
1437
- if (warnings.length > 0) {
1438
- const name = panelId != null ? `Panel "${panelId}"` : "Panel";
1439
- console.warn(`${name} has an invalid configuration:\n\n${warnings.join("\n")}`);
1440
- return false;
1441
- }
1442
- }
1443
- return true;
1444
- }
1445
-
1446
- // All units must be in percentages; pixel values should be pre-converted
1447
- function validatePanelGroupLayout({
1448
- layout: prevLayout,
1449
- panelConstraints
1450
- }) {
1451
- const nextLayout = [...prevLayout];
1452
- const nextLayoutTotalSize = nextLayout.reduce((accumulated, current) => accumulated + current, 0);
1453
-
1454
- // Validate layout expectations
1455
- if (nextLayout.length !== panelConstraints.length) {
1456
- throw Error(`Invalid ${panelConstraints.length} panel layout: ${nextLayout.map(size => `${size}%`).join(", ")}`);
1457
- } else if (!fuzzyNumbersEqual(nextLayoutTotalSize, 100) && nextLayout.length > 0) {
1458
- // This is not ideal so we should warn about it, but it may be recoverable in some cases
1459
- // (especially if the amount is small)
1460
- {
1461
- console.warn(`WARNING: Invalid layout total size: ${nextLayout.map(size => `${size}%`).join(", ")}. Layout normalization will be applied.`);
1462
- }
1463
- for (let index = 0; index < panelConstraints.length; index++) {
1464
- const unsafeSize = nextLayout[index];
1465
- assert(unsafeSize != null, `No layout data found for index ${index}`);
1466
- const safeSize = 100 / nextLayoutTotalSize * unsafeSize;
1467
- nextLayout[index] = safeSize;
1468
- }
1469
- }
1470
- let remainingSize = 0;
1471
-
1472
- // First pass: Validate the proposed layout given each panel's constraints
1473
- for (let index = 0; index < panelConstraints.length; index++) {
1474
- const unsafeSize = nextLayout[index];
1475
- assert(unsafeSize != null, `No layout data found for index ${index}`);
1476
- const safeSize = resizePanel({
1477
- panelConstraints,
1478
- panelIndex: index,
1479
- size: unsafeSize
1480
- });
1481
- if (unsafeSize != safeSize) {
1482
- remainingSize += unsafeSize - safeSize;
1483
- nextLayout[index] = safeSize;
1484
- }
1485
- }
1486
-
1487
- // If there is additional, left over space, assign it to any panel(s) that permits it
1488
- // (It's not worth taking multiple additional passes to evenly distribute)
1489
- if (!fuzzyNumbersEqual(remainingSize, 0)) {
1490
- for (let index = 0; index < panelConstraints.length; index++) {
1491
- const prevSize = nextLayout[index];
1492
- assert(prevSize != null, `No layout data found for index ${index}`);
1493
- const unsafeSize = prevSize + remainingSize;
1494
- const safeSize = resizePanel({
1495
- panelConstraints,
1496
- panelIndex: index,
1497
- size: unsafeSize
1498
- });
1499
- if (prevSize !== safeSize) {
1500
- remainingSize -= safeSize - prevSize;
1501
- nextLayout[index] = safeSize;
1502
-
1503
- // Once we've used up the remainder, bail
1504
- if (fuzzyNumbersEqual(remainingSize, 0)) {
1505
- break;
1506
- }
1507
- }
1508
- }
1509
- }
1510
- return nextLayout;
1511
- }
1512
-
1513
- const LOCAL_STORAGE_DEBOUNCE_INTERVAL = 100;
1514
- const defaultStorage = {
1515
- getItem: name => {
1516
- initializeDefaultStorage(defaultStorage);
1517
- return defaultStorage.getItem(name);
1518
- },
1519
- setItem: (name, value) => {
1520
- initializeDefaultStorage(defaultStorage);
1521
- defaultStorage.setItem(name, value);
1522
- }
1523
- };
1524
- const debounceMap = {};
1525
- function PanelGroupWithForwardedRef({
1526
- autoSaveId = null,
1527
- children,
1528
- className: classNameFromProps = "",
1529
- direction,
1530
- forwardedRef,
1531
- id: idFromProps = null,
1532
- onLayout = null,
1533
- keyboardResizeBy = null,
1534
- storage = defaultStorage,
1535
- style: styleFromProps,
1536
- tagName: Type = "div",
1537
- ...rest
1538
- }) {
1539
- const groupId = useUniqueId(idFromProps);
1540
- const panelGroupElementRef = React.useRef(null);
1541
- const [dragState, setDragState] = React.useState(null);
1542
- const [layout, setLayout] = React.useState([]);
1543
- const forceUpdate = useForceUpdate();
1544
- const panelIdToLastNotifiedSizeMapRef = React.useRef({});
1545
- const panelSizeBeforeCollapseRef = React.useRef(new Map());
1546
- const prevDeltaRef = React.useRef(0);
1547
- const committedValuesRef = React.useRef({
1548
- autoSaveId,
1549
- direction,
1550
- dragState,
1551
- id: groupId,
1552
- keyboardResizeBy,
1553
- onLayout,
1554
- storage
1555
- });
1556
- const eagerValuesRef = React.useRef({
1557
- layout,
1558
- panelDataArray: [],
1559
- panelDataArrayChanged: false
1560
- });
1561
- const devWarningsRef = React.useRef({
1562
- didLogIdAndOrderWarning: false,
1563
- didLogPanelConstraintsWarning: false,
1564
- prevPanelIds: []
1565
- });
1566
- React.useImperativeHandle(forwardedRef, () => ({
1567
- getId: () => committedValuesRef.current.id,
1568
- getLayout: () => {
1569
- const {
1570
- layout
1571
- } = eagerValuesRef.current;
1572
- return layout;
1573
- },
1574
- setLayout: unsafeLayout => {
1575
- const {
1576
- onLayout
1577
- } = committedValuesRef.current;
1578
- const {
1579
- layout: prevLayout,
1580
- panelDataArray
1581
- } = eagerValuesRef.current;
1582
- const safeLayout = validatePanelGroupLayout({
1583
- layout: unsafeLayout,
1584
- panelConstraints: panelDataArray.map(panelData => panelData.constraints)
1585
- });
1586
- if (!areEqual(prevLayout, safeLayout)) {
1587
- setLayout(safeLayout);
1588
- eagerValuesRef.current.layout = safeLayout;
1589
- if (onLayout) {
1590
- onLayout(safeLayout);
1591
- }
1592
- callPanelCallbacks(panelDataArray, safeLayout, panelIdToLastNotifiedSizeMapRef.current);
1593
- }
1594
- }
1595
- }), []);
1596
- useWindowSplitterPanelGroupBehavior({
1597
- committedValuesRef,
1598
- eagerValuesRef,
1599
- groupId,
1600
- layout,
1601
- panelDataArray: eagerValuesRef.current.panelDataArray,
1602
- setLayout,
1603
- panelGroupElement: panelGroupElementRef.current
1604
- });
1605
- React.useEffect(() => {
1606
- const {
1607
- panelDataArray
1608
- } = eagerValuesRef.current;
1609
-
1610
- // If this panel has been configured to persist sizing information, save sizes to local storage.
1611
- if (autoSaveId) {
1612
- if (layout.length === 0 || layout.length !== panelDataArray.length) {
1613
- return;
1614
- }
1615
- let debouncedSave = debounceMap[autoSaveId];
1616
-
1617
- // Limit the frequency of localStorage updates.
1618
- if (debouncedSave == null) {
1619
- debouncedSave = debounce(savePanelGroupState, LOCAL_STORAGE_DEBOUNCE_INTERVAL);
1620
- debounceMap[autoSaveId] = debouncedSave;
1621
- }
1622
-
1623
- // Clone mutable data before passing to the debounced function,
1624
- // else we run the risk of saving an incorrect combination of mutable and immutable values to state.
1625
- const clonedPanelDataArray = [...panelDataArray];
1626
- const clonedPanelSizesBeforeCollapse = new Map(panelSizeBeforeCollapseRef.current);
1627
- debouncedSave(autoSaveId, clonedPanelDataArray, clonedPanelSizesBeforeCollapse, layout, storage);
1628
- }
1629
- }, [autoSaveId, layout, storage]);
1630
-
1631
- // DEV warnings
1632
- React.useEffect(() => {
1633
- {
1634
- const {
1635
- panelDataArray
1636
- } = eagerValuesRef.current;
1637
- const {
1638
- didLogIdAndOrderWarning,
1639
- didLogPanelConstraintsWarning,
1640
- prevPanelIds
1641
- } = devWarningsRef.current;
1642
- if (!didLogIdAndOrderWarning) {
1643
- const panelIds = panelDataArray.map(({
1644
- id
1645
- }) => id);
1646
- devWarningsRef.current.prevPanelIds = panelIds;
1647
- const panelsHaveChanged = prevPanelIds.length > 0 && !areEqual(prevPanelIds, panelIds);
1648
- if (panelsHaveChanged) {
1649
- if (panelDataArray.find(({
1650
- idIsFromProps,
1651
- order
1652
- }) => !idIsFromProps || order == null)) {
1653
- devWarningsRef.current.didLogIdAndOrderWarning = true;
1654
- console.warn(`WARNING: Panel id and order props recommended when panels are dynamically rendered`);
1655
- }
1656
- }
1657
- }
1658
- if (!didLogPanelConstraintsWarning) {
1659
- const panelConstraints = panelDataArray.map(panelData => panelData.constraints);
1660
- for (let panelIndex = 0; panelIndex < panelConstraints.length; panelIndex++) {
1661
- const panelData = panelDataArray[panelIndex];
1662
- assert(panelData, `Panel data not found for index ${panelIndex}`);
1663
- const isValid = validatePanelConstraints({
1664
- panelConstraints,
1665
- panelId: panelData.id,
1666
- panelIndex
1667
- });
1668
- if (!isValid) {
1669
- devWarningsRef.current.didLogPanelConstraintsWarning = true;
1670
- break;
1671
- }
1672
- }
1673
- }
1674
- }
1675
- });
1676
-
1677
- // External APIs are safe to memoize via committed values ref
1678
- const collapsePanel = React.useCallback(panelData => {
1679
- const {
1680
- onLayout
1681
- } = committedValuesRef.current;
1682
- const {
1683
- layout: prevLayout,
1684
- panelDataArray
1685
- } = eagerValuesRef.current;
1686
- if (panelData.constraints.collapsible) {
1687
- const panelConstraintsArray = panelDataArray.map(panelData => panelData.constraints);
1688
- const {
1689
- collapsedSize = 0,
1690
- panelSize,
1691
- pivotIndices
1692
- } = panelDataHelper(panelDataArray, panelData, prevLayout);
1693
- assert(panelSize != null, `Panel size not found for panel "${panelData.id}"`);
1694
- if (!fuzzyNumbersEqual$1(panelSize, collapsedSize)) {
1695
- // Store size before collapse;
1696
- // This is the size that gets restored if the expand() API is used.
1697
- panelSizeBeforeCollapseRef.current.set(panelData.id, panelSize);
1698
- const isLastPanel = findPanelDataIndex(panelDataArray, panelData) === panelDataArray.length - 1;
1699
- const delta = isLastPanel ? panelSize - collapsedSize : collapsedSize - panelSize;
1700
- const nextLayout = adjustLayoutByDelta({
1701
- delta,
1702
- initialLayout: prevLayout,
1703
- panelConstraints: panelConstraintsArray,
1704
- pivotIndices,
1705
- prevLayout,
1706
- trigger: "imperative-api"
1707
- });
1708
- if (!compareLayouts(prevLayout, nextLayout)) {
1709
- setLayout(nextLayout);
1710
- eagerValuesRef.current.layout = nextLayout;
1711
- if (onLayout) {
1712
- onLayout(nextLayout);
1713
- }
1714
- callPanelCallbacks(panelDataArray, nextLayout, panelIdToLastNotifiedSizeMapRef.current);
1715
- }
1716
- }
1717
- }
1718
- }, []);
1719
-
1720
- // External APIs are safe to memoize via committed values ref
1721
- const expandPanel = React.useCallback((panelData, minSizeOverride) => {
1722
- const {
1723
- onLayout
1724
- } = committedValuesRef.current;
1725
- const {
1726
- layout: prevLayout,
1727
- panelDataArray
1728
- } = eagerValuesRef.current;
1729
- if (panelData.constraints.collapsible) {
1730
- const panelConstraintsArray = panelDataArray.map(panelData => panelData.constraints);
1731
- const {
1732
- collapsedSize = 0,
1733
- panelSize = 0,
1734
- minSize: minSizeFromProps = 0,
1735
- pivotIndices
1736
- } = panelDataHelper(panelDataArray, panelData, prevLayout);
1737
- const minSize = minSizeOverride !== null && minSizeOverride !== void 0 ? minSizeOverride : minSizeFromProps;
1738
- if (fuzzyNumbersEqual$1(panelSize, collapsedSize)) {
1739
- // Restore this panel to the size it was before it was collapsed, if possible.
1740
- const prevPanelSize = panelSizeBeforeCollapseRef.current.get(panelData.id);
1741
- const baseSize = prevPanelSize != null && prevPanelSize >= minSize ? prevPanelSize : minSize;
1742
- const isLastPanel = findPanelDataIndex(panelDataArray, panelData) === panelDataArray.length - 1;
1743
- const delta = isLastPanel ? panelSize - baseSize : baseSize - panelSize;
1744
- const nextLayout = adjustLayoutByDelta({
1745
- delta,
1746
- initialLayout: prevLayout,
1747
- panelConstraints: panelConstraintsArray,
1748
- pivotIndices,
1749
- prevLayout,
1750
- trigger: "imperative-api"
1751
- });
1752
- if (!compareLayouts(prevLayout, nextLayout)) {
1753
- setLayout(nextLayout);
1754
- eagerValuesRef.current.layout = nextLayout;
1755
- if (onLayout) {
1756
- onLayout(nextLayout);
1757
- }
1758
- callPanelCallbacks(panelDataArray, nextLayout, panelIdToLastNotifiedSizeMapRef.current);
1759
- }
1760
- }
1761
- }
1762
- }, []);
1763
-
1764
- // External APIs are safe to memoize via committed values ref
1765
- const getPanelSize = React.useCallback(panelData => {
1766
- const {
1767
- layout,
1768
- panelDataArray
1769
- } = eagerValuesRef.current;
1770
- const {
1771
- panelSize
1772
- } = panelDataHelper(panelDataArray, panelData, layout);
1773
- assert(panelSize != null, `Panel size not found for panel "${panelData.id}"`);
1774
- return panelSize;
1775
- }, []);
1776
-
1777
- // This API should never read from committedValuesRef
1778
- const getPanelStyle = React.useCallback((panelData, defaultSize) => {
1779
- const {
1780
- panelDataArray
1781
- } = eagerValuesRef.current;
1782
- const panelIndex = findPanelDataIndex(panelDataArray, panelData);
1783
- return computePanelFlexBoxStyle({
1784
- defaultSize,
1785
- dragState,
1786
- layout,
1787
- panelData: panelDataArray,
1788
- panelIndex
1789
- });
1790
- }, [dragState, layout]);
1791
-
1792
- // External APIs are safe to memoize via committed values ref
1793
- const isPanelCollapsed = React.useCallback(panelData => {
1794
- const {
1795
- layout,
1796
- panelDataArray
1797
- } = eagerValuesRef.current;
1798
- const {
1799
- collapsedSize = 0,
1800
- collapsible,
1801
- panelSize
1802
- } = panelDataHelper(panelDataArray, panelData, layout);
1803
- assert(panelSize != null, `Panel size not found for panel "${panelData.id}"`);
1804
- return collapsible === true && fuzzyNumbersEqual$1(panelSize, collapsedSize);
1805
- }, []);
1806
-
1807
- // External APIs are safe to memoize via committed values ref
1808
- const isPanelExpanded = React.useCallback(panelData => {
1809
- const {
1810
- layout,
1811
- panelDataArray
1812
- } = eagerValuesRef.current;
1813
- const {
1814
- collapsedSize = 0,
1815
- collapsible,
1816
- panelSize
1817
- } = panelDataHelper(panelDataArray, panelData, layout);
1818
- assert(panelSize != null, `Panel size not found for panel "${panelData.id}"`);
1819
- return !collapsible || fuzzyCompareNumbers(panelSize, collapsedSize) > 0;
1820
- }, []);
1821
- const registerPanel = React.useCallback(panelData => {
1822
- const {
1823
- panelDataArray
1824
- } = eagerValuesRef.current;
1825
- panelDataArray.push(panelData);
1826
- panelDataArray.sort((panelA, panelB) => {
1827
- const orderA = panelA.order;
1828
- const orderB = panelB.order;
1829
- if (orderA == null && orderB == null) {
1830
- return 0;
1831
- } else if (orderA == null) {
1832
- return -1;
1833
- } else if (orderB == null) {
1834
- return 1;
1835
- } else {
1836
- return orderA - orderB;
1837
- }
1838
- });
1839
- eagerValuesRef.current.panelDataArrayChanged = true;
1840
- forceUpdate();
1841
- }, [forceUpdate]);
1842
- const registerResizeHandle = React.useCallback(dragHandleId => {
1843
- let isRTL = false;
1844
- const panelGroupElement = panelGroupElementRef.current;
1845
- if (panelGroupElement) {
1846
- const style = window.getComputedStyle(panelGroupElement, null);
1847
- if (style.getPropertyValue("direction") === "rtl") {
1848
- isRTL = true;
1849
- }
1850
- }
1851
- return function resizeHandler(event) {
1852
- event.preventDefault();
1853
- const panelGroupElement = panelGroupElementRef.current;
1854
- if (!panelGroupElement) {
1855
- return () => null;
1856
- }
1857
- const {
1858
- direction,
1859
- dragState,
1860
- id: groupId,
1861
- keyboardResizeBy,
1862
- onLayout
1863
- } = committedValuesRef.current;
1864
- const {
1865
- layout: prevLayout,
1866
- panelDataArray
1867
- } = eagerValuesRef.current;
1868
- const {
1869
- initialLayout
1870
- } = dragState !== null && dragState !== void 0 ? dragState : {};
1871
- const pivotIndices = determinePivotIndices(groupId, dragHandleId, panelGroupElement);
1872
- let delta = calculateDeltaPercentage(event, dragHandleId, direction, dragState, keyboardResizeBy, panelGroupElement);
1873
- const isHorizontal = direction === "horizontal";
1874
- if (isHorizontal && isRTL) {
1875
- delta = -delta;
1876
- }
1877
- const panelConstraints = panelDataArray.map(panelData => panelData.constraints);
1878
- const nextLayout = adjustLayoutByDelta({
1879
- delta,
1880
- initialLayout: initialLayout !== null && initialLayout !== void 0 ? initialLayout : prevLayout,
1881
- panelConstraints,
1882
- pivotIndices,
1883
- prevLayout,
1884
- trigger: isKeyDown(event) ? "keyboard" : "mouse-or-touch"
1885
- });
1886
- const layoutChanged = !compareLayouts(prevLayout, nextLayout);
1887
-
1888
- // Only update the cursor for layout changes triggered by touch/mouse events (not keyboard)
1889
- // Update the cursor even if the layout hasn't changed (we may need to show an invalid cursor state)
1890
- if (isPointerEvent(event) || isMouseEvent(event)) {
1891
- // Watch for multiple subsequent deltas; this might occur for tiny cursor movements.
1892
- // In this case, Panel sizes might not change–
1893
- // but updating cursor in this scenario would cause a flicker.
1894
- if (prevDeltaRef.current != delta) {
1895
- prevDeltaRef.current = delta;
1896
- if (!layoutChanged && delta !== 0) {
1897
- // If the pointer has moved too far to resize the panel any further, note this so we can update the cursor.
1898
- // This mimics VS Code behavior.
1899
- if (isHorizontal) {
1900
- reportConstraintsViolation(dragHandleId, delta < 0 ? EXCEEDED_HORIZONTAL_MIN : EXCEEDED_HORIZONTAL_MAX);
1901
- } else {
1902
- reportConstraintsViolation(dragHandleId, delta < 0 ? EXCEEDED_VERTICAL_MIN : EXCEEDED_VERTICAL_MAX);
1903
- }
1904
- } else {
1905
- reportConstraintsViolation(dragHandleId, 0);
1906
- }
1907
- }
1908
- }
1909
- if (layoutChanged) {
1910
- setLayout(nextLayout);
1911
- eagerValuesRef.current.layout = nextLayout;
1912
- if (onLayout) {
1913
- onLayout(nextLayout);
1914
- }
1915
- callPanelCallbacks(panelDataArray, nextLayout, panelIdToLastNotifiedSizeMapRef.current);
1916
- }
1917
- };
1918
- }, []);
1919
-
1920
- // External APIs are safe to memoize via committed values ref
1921
- const resizePanel = React.useCallback((panelData, unsafePanelSize) => {
1922
- const {
1923
- onLayout
1924
- } = committedValuesRef.current;
1925
- const {
1926
- layout: prevLayout,
1927
- panelDataArray
1928
- } = eagerValuesRef.current;
1929
- const panelConstraintsArray = panelDataArray.map(panelData => panelData.constraints);
1930
- const {
1931
- panelSize,
1932
- pivotIndices
1933
- } = panelDataHelper(panelDataArray, panelData, prevLayout);
1934
- assert(panelSize != null, `Panel size not found for panel "${panelData.id}"`);
1935
- const isLastPanel = findPanelDataIndex(panelDataArray, panelData) === panelDataArray.length - 1;
1936
- const delta = isLastPanel ? panelSize - unsafePanelSize : unsafePanelSize - panelSize;
1937
- const nextLayout = adjustLayoutByDelta({
1938
- delta,
1939
- initialLayout: prevLayout,
1940
- panelConstraints: panelConstraintsArray,
1941
- pivotIndices,
1942
- prevLayout,
1943
- trigger: "imperative-api"
1944
- });
1945
- if (!compareLayouts(prevLayout, nextLayout)) {
1946
- setLayout(nextLayout);
1947
- eagerValuesRef.current.layout = nextLayout;
1948
- if (onLayout) {
1949
- onLayout(nextLayout);
1950
- }
1951
- callPanelCallbacks(panelDataArray, nextLayout, panelIdToLastNotifiedSizeMapRef.current);
1952
- }
1953
- }, []);
1954
- const reevaluatePanelConstraints = React.useCallback((panelData, prevConstraints) => {
1955
- const {
1956
- layout,
1957
- panelDataArray
1958
- } = eagerValuesRef.current;
1959
- const {
1960
- collapsedSize: prevCollapsedSize = 0,
1961
- collapsible: prevCollapsible
1962
- } = prevConstraints;
1963
- const {
1964
- collapsedSize: nextCollapsedSize = 0,
1965
- collapsible: nextCollapsible,
1966
- maxSize: nextMaxSize = 100,
1967
- minSize: nextMinSize = 0
1968
- } = panelData.constraints;
1969
- const {
1970
- panelSize: prevPanelSize
1971
- } = panelDataHelper(panelDataArray, panelData, layout);
1972
- if (prevPanelSize == null) {
1973
- // It's possible that the panels in this group have changed since the last render
1974
- return;
1975
- }
1976
- if (prevCollapsible && nextCollapsible && fuzzyNumbersEqual$1(prevPanelSize, prevCollapsedSize)) {
1977
- if (!fuzzyNumbersEqual$1(prevCollapsedSize, nextCollapsedSize)) {
1978
- resizePanel(panelData, nextCollapsedSize);
1979
- }
1980
- } else if (prevPanelSize < nextMinSize) {
1981
- resizePanel(panelData, nextMinSize);
1982
- } else if (prevPanelSize > nextMaxSize) {
1983
- resizePanel(panelData, nextMaxSize);
1984
- }
1985
- }, [resizePanel]);
1986
-
1987
- // TODO Multiple drag handles can be active at the same time so this API is a bit awkward now
1988
- const startDragging = React.useCallback((dragHandleId, event) => {
1989
- const {
1990
- direction
1991
- } = committedValuesRef.current;
1992
- const {
1993
- layout
1994
- } = eagerValuesRef.current;
1995
- if (!panelGroupElementRef.current) {
1996
- return;
1997
- }
1998
- const handleElement = getResizeHandleElement(dragHandleId, panelGroupElementRef.current);
1999
- assert(handleElement, `Drag handle element not found for id "${dragHandleId}"`);
2000
- const initialCursorPosition = getResizeEventCursorPosition(direction, event);
2001
- setDragState({
2002
- dragHandleId,
2003
- dragHandleRect: handleElement.getBoundingClientRect(),
2004
- initialCursorPosition,
2005
- initialLayout: layout
2006
- });
2007
- }, []);
2008
- const stopDragging = React.useCallback(() => {
2009
- setDragState(null);
2010
- }, []);
2011
- const unregisterPanel = React.useCallback(panelData => {
2012
- const {
2013
- panelDataArray
2014
- } = eagerValuesRef.current;
2015
- const index = findPanelDataIndex(panelDataArray, panelData);
2016
- if (index >= 0) {
2017
- panelDataArray.splice(index, 1);
2018
-
2019
- // TRICKY
2020
- // When a panel is removed from the group, we should delete the most recent prev-size entry for it.
2021
- // If we don't do this, then a conditionally rendered panel might not call onResize when it's re-mounted.
2022
- // Strict effects mode makes this tricky though because all panels will be registered, unregistered, then re-registered on mount.
2023
- delete panelIdToLastNotifiedSizeMapRef.current[panelData.id];
2024
- eagerValuesRef.current.panelDataArrayChanged = true;
2025
- forceUpdate();
2026
- }
2027
- }, [forceUpdate]);
2028
- const context = React.useMemo(() => ({
2029
- collapsePanel,
2030
- direction,
2031
- dragState,
2032
- expandPanel,
2033
- getPanelSize,
2034
- getPanelStyle,
2035
- groupId,
2036
- isPanelCollapsed,
2037
- isPanelExpanded,
2038
- reevaluatePanelConstraints,
2039
- registerPanel,
2040
- registerResizeHandle,
2041
- resizePanel,
2042
- startDragging,
2043
- stopDragging,
2044
- unregisterPanel,
2045
- panelGroupElement: panelGroupElementRef.current
2046
- }), [collapsePanel, dragState, direction, expandPanel, getPanelSize, getPanelStyle, groupId, isPanelCollapsed, isPanelExpanded, reevaluatePanelConstraints, registerPanel, registerResizeHandle, resizePanel, startDragging, stopDragging, unregisterPanel]);
2047
- const style = {
2048
- display: "flex",
2049
- flexDirection: direction === "horizontal" ? "row" : "column",
2050
- height: "100%",
2051
- overflow: "hidden",
2052
- width: "100%"
2053
- };
2054
- return React.createElement(PanelGroupContext.Provider, {
2055
- value: context
2056
- }, React.createElement(Type, {
2057
- ...rest,
2058
- children,
2059
- className: classNameFromProps,
2060
- id: idFromProps,
2061
- ref: panelGroupElementRef,
2062
- style: {
2063
- ...style,
2064
- ...styleFromProps
2065
- },
2066
- // CSS selectors
2067
- [DATA_ATTRIBUTES.group]: "",
2068
- [DATA_ATTRIBUTES.groupDirection]: direction,
2069
- [DATA_ATTRIBUTES.groupId]: groupId
2070
- }));
2071
- }
2072
- const PanelGroup = React.forwardRef((props, ref) => React.createElement(PanelGroupWithForwardedRef, {
2073
- ...props,
2074
- forwardedRef: ref
2075
- }));
2076
- PanelGroupWithForwardedRef.displayName = "PanelGroup";
2077
- PanelGroup.displayName = "forwardRef(PanelGroup)";
2078
- function findPanelDataIndex(panelDataArray, panelData) {
2079
- return panelDataArray.findIndex(prevPanelData => prevPanelData === panelData || prevPanelData.id === panelData.id);
2080
- }
2081
- function panelDataHelper(panelDataArray, panelData, layout) {
2082
- const panelIndex = findPanelDataIndex(panelDataArray, panelData);
2083
- const isLastPanel = panelIndex === panelDataArray.length - 1;
2084
- const pivotIndices = isLastPanel ? [panelIndex - 1, panelIndex] : [panelIndex, panelIndex + 1];
2085
- const panelSize = layout[panelIndex];
2086
- return {
2087
- ...panelData.constraints,
2088
- panelSize,
2089
- pivotIndices
2090
- };
2091
- }
2092
-
2093
- // https://www.w3.org/WAI/ARIA/apg/patterns/windowsplitter/
2094
-
2095
- function useWindowSplitterResizeHandlerBehavior({
2096
- disabled,
2097
- handleId,
2098
- resizeHandler,
2099
- panelGroupElement
2100
- }) {
2101
- React.useEffect(() => {
2102
- if (disabled || resizeHandler == null || panelGroupElement == null) {
2103
- return;
2104
- }
2105
- const handleElement = getResizeHandleElement(handleId, panelGroupElement);
2106
- if (handleElement == null) {
2107
- return;
2108
- }
2109
- const onKeyDown = event => {
2110
- if (event.defaultPrevented) {
2111
- return;
2112
- }
2113
- switch (event.key) {
2114
- case "ArrowDown":
2115
- case "ArrowLeft":
2116
- case "ArrowRight":
2117
- case "ArrowUp":
2118
- case "End":
2119
- case "Home":
2120
- {
2121
- event.preventDefault();
2122
- resizeHandler(event);
2123
- break;
2124
- }
2125
- case "F6":
2126
- {
2127
- event.preventDefault();
2128
- const groupId = handleElement.getAttribute(DATA_ATTRIBUTES.groupId);
2129
- assert(groupId, `No group element found for id "${groupId}"`);
2130
- const handles = getResizeHandleElementsForGroup(groupId, panelGroupElement);
2131
- const index = getResizeHandleElementIndex(groupId, handleId, panelGroupElement);
2132
- assert(index !== null, `No resize element found for id "${handleId}"`);
2133
- const nextIndex = event.shiftKey ? index > 0 ? index - 1 : handles.length - 1 : index + 1 < handles.length ? index + 1 : 0;
2134
- const nextHandle = handles[nextIndex];
2135
- nextHandle.focus();
2136
- break;
2137
- }
2138
- }
2139
- };
2140
- handleElement.addEventListener("keydown", onKeyDown);
2141
- return () => {
2142
- handleElement.removeEventListener("keydown", onKeyDown);
2143
- };
2144
- }, [panelGroupElement, disabled, handleId, resizeHandler]);
2145
- }
2146
-
2147
- function PanelResizeHandle({
2148
- children = null,
2149
- className: classNameFromProps = "",
2150
- disabled = false,
2151
- hitAreaMargins,
2152
- id: idFromProps,
2153
- onBlur,
2154
- onClick,
2155
- onDragging,
2156
- onFocus,
2157
- onPointerDown,
2158
- onPointerUp,
2159
- style: styleFromProps = {},
2160
- tabIndex = 0,
2161
- tagName: Type = "div",
2162
- ...rest
2163
- }) {
2164
- var _hitAreaMargins$coars, _hitAreaMargins$fine;
2165
- const elementRef = React.useRef(null);
2166
-
2167
- // Use a ref to guard against users passing inline props
2168
- const callbacksRef = React.useRef({
2169
- onClick,
2170
- onDragging,
2171
- onPointerDown,
2172
- onPointerUp
2173
- });
2174
- React.useEffect(() => {
2175
- callbacksRef.current.onClick = onClick;
2176
- callbacksRef.current.onDragging = onDragging;
2177
- callbacksRef.current.onPointerDown = onPointerDown;
2178
- callbacksRef.current.onPointerUp = onPointerUp;
2179
- });
2180
- const panelGroupContext = React.useContext(PanelGroupContext);
2181
- if (panelGroupContext === null) {
2182
- throw Error(`PanelResizeHandle components must be rendered within a PanelGroup container`);
2183
- }
2184
- const {
2185
- direction,
2186
- groupId,
2187
- registerResizeHandle: registerResizeHandleWithParentGroup,
2188
- startDragging,
2189
- stopDragging,
2190
- panelGroupElement
2191
- } = panelGroupContext;
2192
- const resizeHandleId = useUniqueId(idFromProps);
2193
- const [state, setState] = React.useState("inactive");
2194
- const [isFocused, setIsFocused] = React.useState(false);
2195
- const [resizeHandler, setResizeHandler] = React.useState(null);
2196
- const committedValuesRef = React.useRef({
2197
- state
2198
- });
2199
- React.useEffect(() => {
2200
- if (disabled) {
2201
- setResizeHandler(null);
2202
- } else {
2203
- const resizeHandler = registerResizeHandleWithParentGroup(resizeHandleId);
2204
- setResizeHandler(() => resizeHandler);
2205
- }
2206
- }, [disabled, resizeHandleId, registerResizeHandleWithParentGroup]);
2207
-
2208
- // Extract hit area margins before passing them to the effect's dependency array
2209
- // so that inline object values won't trigger re-renders
2210
- const coarseHitAreaMargins = (_hitAreaMargins$coars = hitAreaMargins === null || hitAreaMargins === void 0 ? void 0 : hitAreaMargins.coarse) !== null && _hitAreaMargins$coars !== void 0 ? _hitAreaMargins$coars : 15;
2211
- const fineHitAreaMargins = (_hitAreaMargins$fine = hitAreaMargins === null || hitAreaMargins === void 0 ? void 0 : hitAreaMargins.fine) !== null && _hitAreaMargins$fine !== void 0 ? _hitAreaMargins$fine : 5;
2212
- React.useEffect(() => {
2213
- if (disabled || resizeHandler == null) {
2214
- return;
2215
- }
2216
- const element = elementRef.current;
2217
- assert(element, "Element ref not attached");
2218
- let didMove = false;
2219
- const setResizeHandlerState = (action, isActive, event) => {
2220
- if (!isActive) {
2221
- setState("inactive");
2222
- return;
2223
- }
2224
- switch (action) {
2225
- case "down":
2226
- {
2227
- setState("drag");
2228
- didMove = false;
2229
- assert(event, 'Expected event to be defined for "down" action');
2230
- startDragging(resizeHandleId, event);
2231
- const {
2232
- onDragging,
2233
- onPointerDown
2234
- } = callbacksRef.current;
2235
- onDragging === null || onDragging === void 0 ? void 0 : onDragging(true);
2236
- onPointerDown === null || onPointerDown === void 0 ? void 0 : onPointerDown();
2237
- break;
2238
- }
2239
- case "move":
2240
- {
2241
- const {
2242
- state
2243
- } = committedValuesRef.current;
2244
- didMove = true;
2245
- if (state !== "drag") {
2246
- setState("hover");
2247
- }
2248
- assert(event, 'Expected event to be defined for "move" action');
2249
- resizeHandler(event);
2250
- break;
2251
- }
2252
- case "up":
2253
- {
2254
- setState("hover");
2255
- stopDragging();
2256
- const {
2257
- onClick,
2258
- onDragging,
2259
- onPointerUp
2260
- } = callbacksRef.current;
2261
- onDragging === null || onDragging === void 0 ? void 0 : onDragging(false);
2262
- onPointerUp === null || onPointerUp === void 0 ? void 0 : onPointerUp();
2263
- if (!didMove) {
2264
- onClick === null || onClick === void 0 ? void 0 : onClick();
2265
- }
2266
- break;
2267
- }
2268
- }
2269
- };
2270
- return registerResizeHandle(resizeHandleId, element, direction, {
2271
- coarse: coarseHitAreaMargins,
2272
- fine: fineHitAreaMargins
2273
- }, setResizeHandlerState);
2274
- }, [coarseHitAreaMargins, direction, disabled, fineHitAreaMargins, registerResizeHandleWithParentGroup, resizeHandleId, resizeHandler, startDragging, stopDragging]);
2275
- useWindowSplitterResizeHandlerBehavior({
2276
- disabled,
2277
- handleId: resizeHandleId,
2278
- resizeHandler,
2279
- panelGroupElement
2280
- });
2281
- const style = {
2282
- touchAction: "none",
2283
- userSelect: "none"
2284
- };
2285
- return React.createElement(Type, {
2286
- ...rest,
2287
- children,
2288
- className: classNameFromProps,
2289
- id: idFromProps,
2290
- onBlur: () => {
2291
- setIsFocused(false);
2292
- onBlur === null || onBlur === void 0 ? void 0 : onBlur();
2293
- },
2294
- onFocus: () => {
2295
- setIsFocused(true);
2296
- onFocus === null || onFocus === void 0 ? void 0 : onFocus();
2297
- },
2298
- ref: elementRef,
2299
- role: "separator",
2300
- style: {
2301
- ...style,
2302
- ...styleFromProps
2303
- },
2304
- tabIndex,
2305
- // CSS selectors
2306
- [DATA_ATTRIBUTES.groupDirection]: direction,
2307
- [DATA_ATTRIBUTES.groupId]: groupId,
2308
- [DATA_ATTRIBUTES.resizeHandle]: "",
2309
- [DATA_ATTRIBUTES.resizeHandleActive]: state === "drag" ? "pointer" : isFocused ? "keyboard" : undefined,
2310
- [DATA_ATTRIBUTES.resizeHandleEnabled]: !disabled,
2311
- [DATA_ATTRIBUTES.resizeHandleId]: resizeHandleId,
2312
- [DATA_ATTRIBUTES.resizeHandleState]: state
2313
- });
2314
- }
2315
- PanelResizeHandle.displayName = "PanelResizeHandle";
2316
-
2317
- function getPanelElement(id, scope = document) {
2318
- const element = scope.querySelector(`[data-panel-id="${id}"]`);
2319
- if (element) {
2320
- return element;
2321
- }
2322
- return null;
2323
- }
2324
-
2325
- function getPanelElementsForGroup(groupId, scope = document) {
2326
- return Array.from(scope.querySelectorAll(`[data-panel][data-panel-group-id="${groupId}"]`));
2327
- }
2328
-
2329
- function getIntersectingRectangle(rectOne, rectTwo, strict) {
2330
- if (!intersects(rectOne, rectTwo, strict)) {
2331
- return {
2332
- x: 0,
2333
- y: 0,
2334
- width: 0,
2335
- height: 0
2336
- };
2337
- }
2338
- return {
2339
- x: Math.max(rectOne.x, rectTwo.x),
2340
- y: Math.max(rectOne.y, rectTwo.y),
2341
- width: Math.min(rectOne.x + rectOne.width, rectTwo.x + rectTwo.width) - Math.max(rectOne.x, rectTwo.x),
2342
- height: Math.min(rectOne.y + rectOne.height, rectTwo.y + rectTwo.height) - Math.max(rectOne.y, rectTwo.y)
2343
- };
2344
- }
2345
-
2346
- exports.DATA_ATTRIBUTES = DATA_ATTRIBUTES;
2347
- exports.Panel = Panel;
2348
- exports.PanelGroup = PanelGroup;
2349
- exports.PanelResizeHandle = PanelResizeHandle;
2350
- exports.assert = assert;
2351
- exports.disableGlobalCursorStyles = disableGlobalCursorStyles;
2352
- exports.enableGlobalCursorStyles = enableGlobalCursorStyles;
2353
- exports.getIntersectingRectangle = getIntersectingRectangle;
2354
- exports.getPanelElement = getPanelElement;
2355
- exports.getPanelElementsForGroup = getPanelElementsForGroup;
2356
- exports.getPanelGroupElement = getPanelGroupElement;
2357
- exports.getResizeHandleElement = getResizeHandleElement;
2358
- exports.getResizeHandleElementIndex = getResizeHandleElementIndex;
2359
- exports.getResizeHandleElementsForGroup = getResizeHandleElementsForGroup;
2360
- exports.getResizeHandlePanelIds = getResizeHandlePanelIds;
2361
- exports.intersects = intersects;
2362
- exports.setNonce = setNonce;