react-resizable-panels 2.1.8 → 2.1.9

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.
@@ -31,6 +31,22 @@ const isBrowser = typeof window !== "undefined";
31
31
  const PanelGroupContext = React.createContext(null);
32
32
  PanelGroupContext.displayName = "PanelGroupContext";
33
33
 
34
+ const DATA_ATTRIBUTES = {
35
+ group: "data-panel-group",
36
+ groupDirection: "data-panel-group-direction",
37
+ groupId: "data-panel-group-id",
38
+ panel: "data-panel",
39
+ panelCollapsible: "data-panel-collapsible",
40
+ panelId: "data-panel-id",
41
+ panelSize: "data-panel-size",
42
+ resizeHandle: "data-resize-handle",
43
+ resizeHandleActive: "data-resize-handle-active",
44
+ resizeHandleEnabled: "data-panel-resize-handle-enabled",
45
+ resizeHandleId: "data-panel-resize-handle-id",
46
+ resizeHandleState: "data-resize-handle-state"
47
+ };
48
+ const PRECISION = 10;
49
+
34
50
  const useIsomorphicLayoutEffect = isBrowser ? React.useLayoutEffect : () => {};
35
51
 
36
52
  const useId = React__namespace["useId".toString()];
@@ -178,11 +194,11 @@ function PanelWithForwardedRef({
178
194
  ...styleFromProps
179
195
  },
180
196
  // CSS selectors
181
- "data-panel": "",
182
- "data-panel-collapsible": collapsible || undefined,
183
- "data-panel-group-id": groupId,
184
- "data-panel-id": panelId,
185
- "data-panel-size": parseFloat("" + style.flexGrow).toFixed(1)
197
+ [DATA_ATTRIBUTES.groupId]: groupId,
198
+ [DATA_ATTRIBUTES.panel]: "",
199
+ [DATA_ATTRIBUTES.panelCollapsible]: collapsible || undefined,
200
+ [DATA_ATTRIBUTES.panelId]: panelId,
201
+ [DATA_ATTRIBUTES.panelSize]: parseFloat("" + style.flexGrow).toFixed(1)
186
202
  });
187
203
  }
188
204
  const Panel = React.forwardRef((props, ref) => React.createElement(PanelWithForwardedRef, {
@@ -202,6 +218,7 @@ function setNonce(value) {
202
218
 
203
219
  let currentCursorStyle = null;
204
220
  let enabled = true;
221
+ let prevRuleIndex = -1;
205
222
  let styleElement = null;
206
223
  function disableGlobalCursorStyles() {
207
224
  enabled = false;
@@ -251,9 +268,11 @@ function resetGlobalCursorStyle() {
251
268
  document.head.removeChild(styleElement);
252
269
  currentCursorStyle = null;
253
270
  styleElement = null;
271
+ prevRuleIndex = -1;
254
272
  }
255
273
  }
256
274
  function setGlobalCursorStyle(state, constraintFlags) {
275
+ var _styleElement$sheet$i, _styleElement$sheet2;
257
276
  if (!enabled) {
258
277
  return;
259
278
  }
@@ -270,7 +289,11 @@ function setGlobalCursorStyle(state, constraintFlags) {
270
289
  }
271
290
  document.head.appendChild(styleElement);
272
291
  }
273
- styleElement.innerHTML = `*{cursor: ${style}!important;}`;
292
+ if (prevRuleIndex >= 0) {
293
+ var _styleElement$sheet;
294
+ (_styleElement$sheet = styleElement.sheet) === null || _styleElement$sheet === void 0 ? void 0 : _styleElement$sheet.removeRule(prevRuleIndex);
295
+ }
296
+ 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;
274
297
  }
275
298
 
276
299
  function isKeyDown(event) {
@@ -501,7 +524,9 @@ function handlePointerDown(event) {
501
524
  if (intersectingHandles.length > 0) {
502
525
  updateResizeHandlerStates("down", event);
503
526
  event.preventDefault();
504
- event.stopPropagation();
527
+ if (!isWithinResizeHandle(target)) {
528
+ event.stopImmediatePropagation();
529
+ }
505
530
  }
506
531
  }
507
532
  function handlePointerMove(event) {
@@ -550,6 +575,9 @@ function handlePointerUp(event) {
550
575
  isPointerDown = false;
551
576
  if (intersectingHandles.length > 0) {
552
577
  event.preventDefault();
578
+ if (!isWithinResizeHandle(target)) {
579
+ event.stopImmediatePropagation();
580
+ }
553
581
  }
554
582
  updateResizeHandlerStates("up", event);
555
583
  recalculateIntersectingHandles({
@@ -560,6 +588,16 @@ function handlePointerUp(event) {
560
588
  updateCursor();
561
589
  updateListeners();
562
590
  }
591
+ function isWithinResizeHandle(element) {
592
+ let currentElement = element;
593
+ while (currentElement) {
594
+ if (currentElement.hasAttribute(DATA_ATTRIBUTES.resizeHandle)) {
595
+ return true;
596
+ }
597
+ currentElement = currentElement.parentElement;
598
+ }
599
+ return false;
600
+ }
563
601
  function recalculateIntersectingHandles({
564
602
  target,
565
603
  x,
@@ -651,49 +689,42 @@ function updateCursor() {
651
689
  resetGlobalCursorStyle();
652
690
  }
653
691
  }
692
+ let listenersAbortController = new AbortController();
654
693
  function updateListeners() {
655
- ownerDocumentCounts.forEach((_, ownerDocument) => {
656
- const {
657
- body
658
- } = ownerDocument;
659
- body.removeEventListener("contextmenu", handlePointerUp);
660
- body.removeEventListener("pointerdown", handlePointerDown, {
661
- capture: true
662
- });
663
- body.removeEventListener("pointerleave", handlePointerMove);
664
- body.removeEventListener("pointermove", handlePointerMove);
665
- });
666
- window.removeEventListener("pointerup", handlePointerUp);
667
- window.removeEventListener("pointercancel", handlePointerUp);
668
- if (registeredResizeHandlers.size > 0) {
669
- if (isPointerDown) {
670
- if (intersectingHandles.length > 0) {
671
- ownerDocumentCounts.forEach((count, ownerDocument) => {
672
- const {
673
- body
674
- } = ownerDocument;
675
- if (count > 0) {
676
- body.addEventListener("contextmenu", handlePointerUp);
677
- body.addEventListener("pointerleave", handlePointerMove);
678
- body.addEventListener("pointermove", handlePointerMove);
679
- }
680
- });
681
- }
682
- window.addEventListener("pointerup", handlePointerUp);
683
- window.addEventListener("pointercancel", handlePointerUp);
684
- } else {
694
+ listenersAbortController.abort();
695
+ listenersAbortController = new AbortController();
696
+ const options = {
697
+ capture: true,
698
+ signal: listenersAbortController.signal
699
+ };
700
+ if (!registeredResizeHandlers.size) {
701
+ return;
702
+ }
703
+ if (isPointerDown) {
704
+ if (intersectingHandles.length > 0) {
685
705
  ownerDocumentCounts.forEach((count, ownerDocument) => {
686
706
  const {
687
707
  body
688
708
  } = ownerDocument;
689
709
  if (count > 0) {
690
- body.addEventListener("pointerdown", handlePointerDown, {
691
- capture: true
692
- });
693
- body.addEventListener("pointermove", handlePointerMove);
710
+ body.addEventListener("contextmenu", handlePointerUp, options);
711
+ body.addEventListener("pointerleave", handlePointerMove, options);
712
+ body.addEventListener("pointermove", handlePointerMove, options);
694
713
  }
695
714
  });
696
715
  }
716
+ window.addEventListener("pointerup", handlePointerUp, options);
717
+ window.addEventListener("pointercancel", handlePointerUp, options);
718
+ } else {
719
+ ownerDocumentCounts.forEach((count, ownerDocument) => {
720
+ const {
721
+ body
722
+ } = ownerDocument;
723
+ if (count > 0) {
724
+ body.addEventListener("pointerdown", handlePointerDown, options);
725
+ body.addEventListener("pointermove", handlePointerMove, options);
726
+ }
727
+ });
697
728
  }
698
729
  }
699
730
  function updateResizeHandlerStates(action, event) {
@@ -718,8 +749,6 @@ function assert(expectedCondition, message) {
718
749
  }
719
750
  }
720
751
 
721
- const PRECISION = 10;
722
-
723
752
  function fuzzyCompareNumbers(actual, expected, fractionDigits = PRECISION) {
724
753
  if (actual.toFixed(fractionDigits) === expected.toFixed(fractionDigits)) {
725
754
  return 0;
@@ -1063,12 +1092,12 @@ function calculateAriaValues({
1063
1092
  }
1064
1093
 
1065
1094
  function getResizeHandleElementsForGroup(groupId, scope = document) {
1066
- return Array.from(scope.querySelectorAll(`[data-panel-resize-handle-id][data-panel-group-id="${groupId}"]`));
1095
+ return Array.from(scope.querySelectorAll(`[${DATA_ATTRIBUTES.resizeHandleId}][data-panel-group-id="${groupId}"]`));
1067
1096
  }
1068
1097
 
1069
1098
  function getResizeHandleElementIndex(groupId, id, scope = document) {
1070
1099
  const handles = getResizeHandleElementsForGroup(groupId, scope);
1071
- const index = handles.findIndex(handle => handle.getAttribute("data-panel-resize-handle-id") === id);
1100
+ const index = handles.findIndex(handle => handle.getAttribute(DATA_ATTRIBUTES.resizeHandleId) === id);
1072
1101
  return index !== null && index !== void 0 ? index : null;
1073
1102
  }
1074
1103
 
@@ -1093,7 +1122,7 @@ function getPanelGroupElement(id, rootElement = document) {
1093
1122
  }
1094
1123
 
1095
1124
  function getResizeHandleElement(id, scope = document) {
1096
- const element = scope.querySelector(`[data-panel-resize-handle-id="${id}"]`);
1125
+ const element = scope.querySelector(`[${DATA_ATTRIBUTES.resizeHandleId}="${id}"]`);
1097
1126
  if (element) {
1098
1127
  return element;
1099
1128
  }
@@ -1182,7 +1211,7 @@ function useWindowSplitterPanelGroupBehavior({
1182
1211
  const handles = getResizeHandleElementsForGroup(groupId, panelGroupElement);
1183
1212
  assert(handles, `No resize handles found for group id "${groupId}"`);
1184
1213
  const cleanupFunctions = handles.map(handle => {
1185
- const handleId = handle.getAttribute("data-panel-resize-handle-id");
1214
+ const handleId = handle.getAttribute(DATA_ATTRIBUTES.resizeHandleId);
1186
1215
  assert(handleId, `Resize handle element has no handle id attribute`);
1187
1216
  const [idBefore, idAfter] = getResizeHandlePanelIds(groupId, handleId, panelDataArray, panelGroupElement);
1188
1217
  if (idBefore == null || idAfter == null) {
@@ -1260,7 +1289,7 @@ function calculateDragOffsetPercentage(event, dragHandleId, direction, initialDr
1260
1289
  const isHorizontal = direction === "horizontal";
1261
1290
  const handleElement = getResizeHandleElement(dragHandleId, panelGroupElement);
1262
1291
  assert(handleElement, `No resize handle element found for id "${dragHandleId}"`);
1263
- const groupId = handleElement.getAttribute("data-panel-group-id");
1292
+ const groupId = handleElement.getAttribute(DATA_ATTRIBUTES.groupId);
1264
1293
  assert(groupId, `Resize handle element has no group id attribute`);
1265
1294
  let {
1266
1295
  initialCursorPosition
@@ -2266,9 +2295,9 @@ function PanelGroupWithForwardedRef({
2266
2295
  ...styleFromProps
2267
2296
  },
2268
2297
  // CSS selectors
2269
- "data-panel-group": "",
2270
- "data-panel-group-direction": direction,
2271
- "data-panel-group-id": groupId
2298
+ [DATA_ATTRIBUTES.group]: "",
2299
+ [DATA_ATTRIBUTES.groupDirection]: direction,
2300
+ [DATA_ATTRIBUTES.groupId]: groupId
2272
2301
  }));
2273
2302
  }
2274
2303
  const PanelGroup = React.forwardRef((props, ref) => React.createElement(PanelGroupWithForwardedRef, {
@@ -2327,7 +2356,7 @@ function useWindowSplitterResizeHandlerBehavior({
2327
2356
  case "F6":
2328
2357
  {
2329
2358
  event.preventDefault();
2330
- const groupId = handleElement.getAttribute("data-panel-group-id");
2359
+ const groupId = handleElement.getAttribute(DATA_ATTRIBUTES.groupId);
2331
2360
  assert(groupId, `No group element found for id "${groupId}"`);
2332
2361
  const handles = getResizeHandleElementsForGroup(groupId, panelGroupElement);
2333
2362
  const index = getResizeHandleElementIndex(groupId, handleId, panelGroupElement);
@@ -2422,54 +2451,54 @@ function PanelResizeHandle({
2422
2451
  assert(element, "Element ref not attached");
2423
2452
  let didMove = false;
2424
2453
  const setResizeHandlerState = (action, isActive, event) => {
2425
- if (isActive) {
2426
- switch (action) {
2427
- case "down":
2428
- {
2429
- setState("drag");
2430
- didMove = false;
2431
- assert(event, 'Expected event to be defined for "down" action');
2432
- startDragging(resizeHandleId, event);
2433
- const {
2434
- onDragging,
2435
- onPointerDown
2436
- } = callbacksRef.current;
2437
- onDragging === null || onDragging === void 0 ? void 0 : onDragging(true);
2438
- onPointerDown === null || onPointerDown === void 0 ? void 0 : onPointerDown();
2439
- break;
2440
- }
2441
- case "move":
2442
- {
2443
- const {
2444
- state
2445
- } = committedValuesRef.current;
2446
- didMove = true;
2447
- if (state !== "drag") {
2448
- setState("hover");
2449
- }
2450
- assert(event, 'Expected event to be defined for "move" action');
2451
- resizeHandler(event);
2452
- break;
2453
- }
2454
- case "up":
2455
- {
2454
+ if (!isActive) {
2455
+ setState("inactive");
2456
+ return;
2457
+ }
2458
+ switch (action) {
2459
+ case "down":
2460
+ {
2461
+ setState("drag");
2462
+ didMove = false;
2463
+ assert(event, 'Expected event to be defined for "down" action');
2464
+ startDragging(resizeHandleId, event);
2465
+ const {
2466
+ onDragging,
2467
+ onPointerDown
2468
+ } = callbacksRef.current;
2469
+ onDragging === null || onDragging === void 0 ? void 0 : onDragging(true);
2470
+ onPointerDown === null || onPointerDown === void 0 ? void 0 : onPointerDown();
2471
+ break;
2472
+ }
2473
+ case "move":
2474
+ {
2475
+ const {
2476
+ state
2477
+ } = committedValuesRef.current;
2478
+ didMove = true;
2479
+ if (state !== "drag") {
2456
2480
  setState("hover");
2457
- stopDragging();
2458
- const {
2459
- onClick,
2460
- onDragging,
2461
- onPointerUp
2462
- } = callbacksRef.current;
2463
- onDragging === null || onDragging === void 0 ? void 0 : onDragging(false);
2464
- onPointerUp === null || onPointerUp === void 0 ? void 0 : onPointerUp();
2465
- if (!didMove) {
2466
- onClick === null || onClick === void 0 ? void 0 : onClick();
2467
- }
2468
- break;
2469
2481
  }
2470
- }
2471
- } else {
2472
- setState("inactive");
2482
+ assert(event, 'Expected event to be defined for "move" action');
2483
+ resizeHandler(event);
2484
+ break;
2485
+ }
2486
+ case "up":
2487
+ {
2488
+ setState("hover");
2489
+ stopDragging();
2490
+ const {
2491
+ onClick,
2492
+ onDragging,
2493
+ onPointerUp
2494
+ } = callbacksRef.current;
2495
+ onDragging === null || onDragging === void 0 ? void 0 : onDragging(false);
2496
+ onPointerUp === null || onPointerUp === void 0 ? void 0 : onPointerUp();
2497
+ if (!didMove) {
2498
+ onClick === null || onClick === void 0 ? void 0 : onClick();
2499
+ }
2500
+ break;
2501
+ }
2473
2502
  }
2474
2503
  };
2475
2504
  return registerResizeHandle(resizeHandleId, element, direction, {
@@ -2508,13 +2537,13 @@ function PanelResizeHandle({
2508
2537
  },
2509
2538
  tabIndex,
2510
2539
  // CSS selectors
2511
- "data-panel-group-direction": direction,
2512
- "data-panel-group-id": groupId,
2513
- "data-resize-handle": "",
2514
- "data-resize-handle-active": state === "drag" ? "pointer" : isFocused ? "keyboard" : undefined,
2515
- "data-resize-handle-state": state,
2516
- "data-panel-resize-handle-enabled": !disabled,
2517
- "data-panel-resize-handle-id": resizeHandleId
2540
+ [DATA_ATTRIBUTES.groupDirection]: direction,
2541
+ [DATA_ATTRIBUTES.groupId]: groupId,
2542
+ [DATA_ATTRIBUTES.resizeHandle]: "",
2543
+ [DATA_ATTRIBUTES.resizeHandleActive]: state === "drag" ? "pointer" : isFocused ? "keyboard" : undefined,
2544
+ [DATA_ATTRIBUTES.resizeHandleEnabled]: !disabled,
2545
+ [DATA_ATTRIBUTES.resizeHandleId]: resizeHandleId,
2546
+ [DATA_ATTRIBUTES.resizeHandleState]: state
2518
2547
  });
2519
2548
  }
2520
2549
  PanelResizeHandle.displayName = "PanelResizeHandle";
@@ -2548,6 +2577,7 @@ function getIntersectingRectangle(rectOne, rectTwo, strict) {
2548
2577
  };
2549
2578
  }
2550
2579
 
2580
+ exports.DATA_ATTRIBUTES = DATA_ATTRIBUTES;
2551
2581
  exports.Panel = Panel;
2552
2582
  exports.PanelGroup = PanelGroup;
2553
2583
  exports.PanelResizeHandle = PanelResizeHandle;
@@ -1,4 +1,5 @@
1
1
  export {
2
+ DATA_ATTRIBUTES,
2
3
  Panel,
3
4
  PanelGroup,
4
5
  PanelResizeHandle,