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.
@@ -29,6 +29,22 @@ var React__namespace = /*#__PURE__*/_interopNamespace(React);
29
29
  const PanelGroupContext = React.createContext(null);
30
30
  PanelGroupContext.displayName = "PanelGroupContext";
31
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
+
32
48
  const useIsomorphicLayoutEffect = React.useLayoutEffect ;
33
49
 
34
50
  const useId = React__namespace["useId".toString()];
@@ -171,11 +187,11 @@ function PanelWithForwardedRef({
171
187
  ...styleFromProps
172
188
  },
173
189
  // CSS selectors
174
- "data-panel": "",
175
- "data-panel-collapsible": collapsible || undefined,
176
- "data-panel-group-id": groupId,
177
- "data-panel-id": panelId,
178
- "data-panel-size": parseFloat("" + style.flexGrow).toFixed(1)
190
+ [DATA_ATTRIBUTES.groupId]: groupId,
191
+ [DATA_ATTRIBUTES.panel]: "",
192
+ [DATA_ATTRIBUTES.panelCollapsible]: collapsible || undefined,
193
+ [DATA_ATTRIBUTES.panelId]: panelId,
194
+ [DATA_ATTRIBUTES.panelSize]: parseFloat("" + style.flexGrow).toFixed(1)
179
195
  });
180
196
  }
181
197
  const Panel = React.forwardRef((props, ref) => React.createElement(PanelWithForwardedRef, {
@@ -195,6 +211,7 @@ function setNonce(value) {
195
211
 
196
212
  let currentCursorStyle = null;
197
213
  let enabled = true;
214
+ let prevRuleIndex = -1;
198
215
  let styleElement = null;
199
216
  function disableGlobalCursorStyles() {
200
217
  enabled = false;
@@ -244,9 +261,11 @@ function resetGlobalCursorStyle() {
244
261
  document.head.removeChild(styleElement);
245
262
  currentCursorStyle = null;
246
263
  styleElement = null;
264
+ prevRuleIndex = -1;
247
265
  }
248
266
  }
249
267
  function setGlobalCursorStyle(state, constraintFlags) {
268
+ var _styleElement$sheet$i, _styleElement$sheet2;
250
269
  if (!enabled) {
251
270
  return;
252
271
  }
@@ -263,7 +282,11 @@ function setGlobalCursorStyle(state, constraintFlags) {
263
282
  }
264
283
  document.head.appendChild(styleElement);
265
284
  }
266
- styleElement.innerHTML = `*{cursor: ${style}!important;}`;
285
+ if (prevRuleIndex >= 0) {
286
+ var _styleElement$sheet;
287
+ (_styleElement$sheet = styleElement.sheet) === null || _styleElement$sheet === void 0 ? void 0 : _styleElement$sheet.removeRule(prevRuleIndex);
288
+ }
289
+ 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;
267
290
  }
268
291
 
269
292
  function isKeyDown(event) {
@@ -494,7 +517,9 @@ function handlePointerDown(event) {
494
517
  if (intersectingHandles.length > 0) {
495
518
  updateResizeHandlerStates("down", event);
496
519
  event.preventDefault();
497
- event.stopPropagation();
520
+ if (!isWithinResizeHandle(target)) {
521
+ event.stopImmediatePropagation();
522
+ }
498
523
  }
499
524
  }
500
525
  function handlePointerMove(event) {
@@ -543,6 +568,9 @@ function handlePointerUp(event) {
543
568
  isPointerDown = false;
544
569
  if (intersectingHandles.length > 0) {
545
570
  event.preventDefault();
571
+ if (!isWithinResizeHandle(target)) {
572
+ event.stopImmediatePropagation();
573
+ }
546
574
  }
547
575
  updateResizeHandlerStates("up", event);
548
576
  recalculateIntersectingHandles({
@@ -553,6 +581,16 @@ function handlePointerUp(event) {
553
581
  updateCursor();
554
582
  updateListeners();
555
583
  }
584
+ function isWithinResizeHandle(element) {
585
+ let currentElement = element;
586
+ while (currentElement) {
587
+ if (currentElement.hasAttribute(DATA_ATTRIBUTES.resizeHandle)) {
588
+ return true;
589
+ }
590
+ currentElement = currentElement.parentElement;
591
+ }
592
+ return false;
593
+ }
556
594
  function recalculateIntersectingHandles({
557
595
  target,
558
596
  x,
@@ -644,49 +682,42 @@ function updateCursor() {
644
682
  resetGlobalCursorStyle();
645
683
  }
646
684
  }
685
+ let listenersAbortController = new AbortController();
647
686
  function updateListeners() {
648
- ownerDocumentCounts.forEach((_, ownerDocument) => {
649
- const {
650
- body
651
- } = ownerDocument;
652
- body.removeEventListener("contextmenu", handlePointerUp);
653
- body.removeEventListener("pointerdown", handlePointerDown, {
654
- capture: true
655
- });
656
- body.removeEventListener("pointerleave", handlePointerMove);
657
- body.removeEventListener("pointermove", handlePointerMove);
658
- });
659
- window.removeEventListener("pointerup", handlePointerUp);
660
- window.removeEventListener("pointercancel", handlePointerUp);
661
- if (registeredResizeHandlers.size > 0) {
662
- if (isPointerDown) {
663
- if (intersectingHandles.length > 0) {
664
- ownerDocumentCounts.forEach((count, ownerDocument) => {
665
- const {
666
- body
667
- } = ownerDocument;
668
- if (count > 0) {
669
- body.addEventListener("contextmenu", handlePointerUp);
670
- body.addEventListener("pointerleave", handlePointerMove);
671
- body.addEventListener("pointermove", handlePointerMove);
672
- }
673
- });
674
- }
675
- window.addEventListener("pointerup", handlePointerUp);
676
- window.addEventListener("pointercancel", handlePointerUp);
677
- } else {
687
+ listenersAbortController.abort();
688
+ listenersAbortController = new AbortController();
689
+ const options = {
690
+ capture: true,
691
+ signal: listenersAbortController.signal
692
+ };
693
+ if (!registeredResizeHandlers.size) {
694
+ return;
695
+ }
696
+ if (isPointerDown) {
697
+ if (intersectingHandles.length > 0) {
678
698
  ownerDocumentCounts.forEach((count, ownerDocument) => {
679
699
  const {
680
700
  body
681
701
  } = ownerDocument;
682
702
  if (count > 0) {
683
- body.addEventListener("pointerdown", handlePointerDown, {
684
- capture: true
685
- });
686
- body.addEventListener("pointermove", handlePointerMove);
703
+ body.addEventListener("contextmenu", handlePointerUp, options);
704
+ body.addEventListener("pointerleave", handlePointerMove, options);
705
+ body.addEventListener("pointermove", handlePointerMove, options);
687
706
  }
688
707
  });
689
708
  }
709
+ window.addEventListener("pointerup", handlePointerUp, options);
710
+ window.addEventListener("pointercancel", handlePointerUp, options);
711
+ } else {
712
+ ownerDocumentCounts.forEach((count, ownerDocument) => {
713
+ const {
714
+ body
715
+ } = ownerDocument;
716
+ if (count > 0) {
717
+ body.addEventListener("pointerdown", handlePointerDown, options);
718
+ body.addEventListener("pointermove", handlePointerMove, options);
719
+ }
720
+ });
690
721
  }
691
722
  }
692
723
  function updateResizeHandlerStates(action, event) {
@@ -711,8 +742,6 @@ function assert(expectedCondition, message) {
711
742
  }
712
743
  }
713
744
 
714
- const PRECISION = 10;
715
-
716
745
  function fuzzyCompareNumbers(actual, expected, fractionDigits = PRECISION) {
717
746
  if (actual.toFixed(fractionDigits) === expected.toFixed(fractionDigits)) {
718
747
  return 0;
@@ -1056,12 +1085,12 @@ function calculateAriaValues({
1056
1085
  }
1057
1086
 
1058
1087
  function getResizeHandleElementsForGroup(groupId, scope = document) {
1059
- return Array.from(scope.querySelectorAll(`[data-panel-resize-handle-id][data-panel-group-id="${groupId}"]`));
1088
+ return Array.from(scope.querySelectorAll(`[${DATA_ATTRIBUTES.resizeHandleId}][data-panel-group-id="${groupId}"]`));
1060
1089
  }
1061
1090
 
1062
1091
  function getResizeHandleElementIndex(groupId, id, scope = document) {
1063
1092
  const handles = getResizeHandleElementsForGroup(groupId, scope);
1064
- const index = handles.findIndex(handle => handle.getAttribute("data-panel-resize-handle-id") === id);
1093
+ const index = handles.findIndex(handle => handle.getAttribute(DATA_ATTRIBUTES.resizeHandleId) === id);
1065
1094
  return index !== null && index !== void 0 ? index : null;
1066
1095
  }
1067
1096
 
@@ -1086,7 +1115,7 @@ function getPanelGroupElement(id, rootElement = document) {
1086
1115
  }
1087
1116
 
1088
1117
  function getResizeHandleElement(id, scope = document) {
1089
- const element = scope.querySelector(`[data-panel-resize-handle-id="${id}"]`);
1118
+ const element = scope.querySelector(`[${DATA_ATTRIBUTES.resizeHandleId}="${id}"]`);
1090
1119
  if (element) {
1091
1120
  return element;
1092
1121
  }
@@ -1175,7 +1204,7 @@ function useWindowSplitterPanelGroupBehavior({
1175
1204
  const handles = getResizeHandleElementsForGroup(groupId, panelGroupElement);
1176
1205
  assert(handles, `No resize handles found for group id "${groupId}"`);
1177
1206
  const cleanupFunctions = handles.map(handle => {
1178
- const handleId = handle.getAttribute("data-panel-resize-handle-id");
1207
+ const handleId = handle.getAttribute(DATA_ATTRIBUTES.resizeHandleId);
1179
1208
  assert(handleId, `Resize handle element has no handle id attribute`);
1180
1209
  const [idBefore, idAfter] = getResizeHandlePanelIds(groupId, handleId, panelDataArray, panelGroupElement);
1181
1210
  if (idBefore == null || idAfter == null) {
@@ -1253,7 +1282,7 @@ function calculateDragOffsetPercentage(event, dragHandleId, direction, initialDr
1253
1282
  const isHorizontal = direction === "horizontal";
1254
1283
  const handleElement = getResizeHandleElement(dragHandleId, panelGroupElement);
1255
1284
  assert(handleElement, `No resize handle element found for id "${dragHandleId}"`);
1256
- const groupId = handleElement.getAttribute("data-panel-group-id");
1285
+ const groupId = handleElement.getAttribute(DATA_ATTRIBUTES.groupId);
1257
1286
  assert(groupId, `Resize handle element has no group id attribute`);
1258
1287
  let {
1259
1288
  initialCursorPosition
@@ -2259,9 +2288,9 @@ function PanelGroupWithForwardedRef({
2259
2288
  ...styleFromProps
2260
2289
  },
2261
2290
  // CSS selectors
2262
- "data-panel-group": "",
2263
- "data-panel-group-direction": direction,
2264
- "data-panel-group-id": groupId
2291
+ [DATA_ATTRIBUTES.group]: "",
2292
+ [DATA_ATTRIBUTES.groupDirection]: direction,
2293
+ [DATA_ATTRIBUTES.groupId]: groupId
2265
2294
  }));
2266
2295
  }
2267
2296
  const PanelGroup = React.forwardRef((props, ref) => React.createElement(PanelGroupWithForwardedRef, {
@@ -2320,7 +2349,7 @@ function useWindowSplitterResizeHandlerBehavior({
2320
2349
  case "F6":
2321
2350
  {
2322
2351
  event.preventDefault();
2323
- const groupId = handleElement.getAttribute("data-panel-group-id");
2352
+ const groupId = handleElement.getAttribute(DATA_ATTRIBUTES.groupId);
2324
2353
  assert(groupId, `No group element found for id "${groupId}"`);
2325
2354
  const handles = getResizeHandleElementsForGroup(groupId, panelGroupElement);
2326
2355
  const index = getResizeHandleElementIndex(groupId, handleId, panelGroupElement);
@@ -2415,54 +2444,54 @@ function PanelResizeHandle({
2415
2444
  assert(element, "Element ref not attached");
2416
2445
  let didMove = false;
2417
2446
  const setResizeHandlerState = (action, isActive, event) => {
2418
- if (isActive) {
2419
- switch (action) {
2420
- case "down":
2421
- {
2422
- setState("drag");
2423
- didMove = false;
2424
- assert(event, 'Expected event to be defined for "down" action');
2425
- startDragging(resizeHandleId, event);
2426
- const {
2427
- onDragging,
2428
- onPointerDown
2429
- } = callbacksRef.current;
2430
- onDragging === null || onDragging === void 0 ? void 0 : onDragging(true);
2431
- onPointerDown === null || onPointerDown === void 0 ? void 0 : onPointerDown();
2432
- break;
2433
- }
2434
- case "move":
2435
- {
2436
- const {
2437
- state
2438
- } = committedValuesRef.current;
2439
- didMove = true;
2440
- if (state !== "drag") {
2441
- setState("hover");
2442
- }
2443
- assert(event, 'Expected event to be defined for "move" action');
2444
- resizeHandler(event);
2445
- break;
2446
- }
2447
- case "up":
2448
- {
2447
+ if (!isActive) {
2448
+ setState("inactive");
2449
+ return;
2450
+ }
2451
+ switch (action) {
2452
+ case "down":
2453
+ {
2454
+ setState("drag");
2455
+ didMove = false;
2456
+ assert(event, 'Expected event to be defined for "down" action');
2457
+ startDragging(resizeHandleId, event);
2458
+ const {
2459
+ onDragging,
2460
+ onPointerDown
2461
+ } = callbacksRef.current;
2462
+ onDragging === null || onDragging === void 0 ? void 0 : onDragging(true);
2463
+ onPointerDown === null || onPointerDown === void 0 ? void 0 : onPointerDown();
2464
+ break;
2465
+ }
2466
+ case "move":
2467
+ {
2468
+ const {
2469
+ state
2470
+ } = committedValuesRef.current;
2471
+ didMove = true;
2472
+ if (state !== "drag") {
2449
2473
  setState("hover");
2450
- stopDragging();
2451
- const {
2452
- onClick,
2453
- onDragging,
2454
- onPointerUp
2455
- } = callbacksRef.current;
2456
- onDragging === null || onDragging === void 0 ? void 0 : onDragging(false);
2457
- onPointerUp === null || onPointerUp === void 0 ? void 0 : onPointerUp();
2458
- if (!didMove) {
2459
- onClick === null || onClick === void 0 ? void 0 : onClick();
2460
- }
2461
- break;
2462
2474
  }
2463
- }
2464
- } else {
2465
- setState("inactive");
2475
+ assert(event, 'Expected event to be defined for "move" action');
2476
+ resizeHandler(event);
2477
+ break;
2478
+ }
2479
+ case "up":
2480
+ {
2481
+ setState("hover");
2482
+ stopDragging();
2483
+ const {
2484
+ onClick,
2485
+ onDragging,
2486
+ onPointerUp
2487
+ } = callbacksRef.current;
2488
+ onDragging === null || onDragging === void 0 ? void 0 : onDragging(false);
2489
+ onPointerUp === null || onPointerUp === void 0 ? void 0 : onPointerUp();
2490
+ if (!didMove) {
2491
+ onClick === null || onClick === void 0 ? void 0 : onClick();
2492
+ }
2493
+ break;
2494
+ }
2466
2495
  }
2467
2496
  };
2468
2497
  return registerResizeHandle(resizeHandleId, element, direction, {
@@ -2501,13 +2530,13 @@ function PanelResizeHandle({
2501
2530
  },
2502
2531
  tabIndex,
2503
2532
  // CSS selectors
2504
- "data-panel-group-direction": direction,
2505
- "data-panel-group-id": groupId,
2506
- "data-resize-handle": "",
2507
- "data-resize-handle-active": state === "drag" ? "pointer" : isFocused ? "keyboard" : undefined,
2508
- "data-resize-handle-state": state,
2509
- "data-panel-resize-handle-enabled": !disabled,
2510
- "data-panel-resize-handle-id": resizeHandleId
2533
+ [DATA_ATTRIBUTES.groupDirection]: direction,
2534
+ [DATA_ATTRIBUTES.groupId]: groupId,
2535
+ [DATA_ATTRIBUTES.resizeHandle]: "",
2536
+ [DATA_ATTRIBUTES.resizeHandleActive]: state === "drag" ? "pointer" : isFocused ? "keyboard" : undefined,
2537
+ [DATA_ATTRIBUTES.resizeHandleEnabled]: !disabled,
2538
+ [DATA_ATTRIBUTES.resizeHandleId]: resizeHandleId,
2539
+ [DATA_ATTRIBUTES.resizeHandleState]: state
2511
2540
  });
2512
2541
  }
2513
2542
  PanelResizeHandle.displayName = "PanelResizeHandle";
@@ -2541,6 +2570,7 @@ function getIntersectingRectangle(rectOne, rectTwo, strict) {
2541
2570
  };
2542
2571
  }
2543
2572
 
2573
+ exports.DATA_ATTRIBUTES = DATA_ATTRIBUTES;
2544
2574
  exports.Panel = Panel;
2545
2575
  exports.PanelGroup = PanelGroup;
2546
2576
  exports.PanelResizeHandle = PanelResizeHandle;
@@ -1,4 +1,5 @@
1
1
  export {
2
+ DATA_ATTRIBUTES,
2
3
  Panel,
3
4
  PanelGroup,
4
5
  PanelResizeHandle,