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.
@@ -8,6 +8,22 @@ const isBrowser = typeof window !== "undefined";
8
8
  const PanelGroupContext = createContext(null);
9
9
  PanelGroupContext.displayName = "PanelGroupContext";
10
10
 
11
+ const DATA_ATTRIBUTES = {
12
+ group: "data-panel-group",
13
+ groupDirection: "data-panel-group-direction",
14
+ groupId: "data-panel-group-id",
15
+ panel: "data-panel",
16
+ panelCollapsible: "data-panel-collapsible",
17
+ panelId: "data-panel-id",
18
+ panelSize: "data-panel-size",
19
+ resizeHandle: "data-resize-handle",
20
+ resizeHandleActive: "data-resize-handle-active",
21
+ resizeHandleEnabled: "data-panel-resize-handle-enabled",
22
+ resizeHandleId: "data-panel-resize-handle-id",
23
+ resizeHandleState: "data-resize-handle-state"
24
+ };
25
+ const PRECISION = 10;
26
+
11
27
  const useIsomorphicLayoutEffect = isBrowser ? useLayoutEffect : () => {};
12
28
 
13
29
  const useId = React["useId".toString()];
@@ -155,11 +171,11 @@ function PanelWithForwardedRef({
155
171
  ...styleFromProps
156
172
  },
157
173
  // CSS selectors
158
- "data-panel": "",
159
- "data-panel-collapsible": collapsible || undefined,
160
- "data-panel-group-id": groupId,
161
- "data-panel-id": panelId,
162
- "data-panel-size": parseFloat("" + style.flexGrow).toFixed(1)
174
+ [DATA_ATTRIBUTES.groupId]: groupId,
175
+ [DATA_ATTRIBUTES.panel]: "",
176
+ [DATA_ATTRIBUTES.panelCollapsible]: collapsible || undefined,
177
+ [DATA_ATTRIBUTES.panelId]: panelId,
178
+ [DATA_ATTRIBUTES.panelSize]: parseFloat("" + style.flexGrow).toFixed(1)
163
179
  });
164
180
  }
165
181
  const Panel = forwardRef((props, ref) => createElement(PanelWithForwardedRef, {
@@ -179,6 +195,7 @@ function setNonce(value) {
179
195
 
180
196
  let currentCursorStyle = null;
181
197
  let enabled = true;
198
+ let prevRuleIndex = -1;
182
199
  let styleElement = null;
183
200
  function disableGlobalCursorStyles() {
184
201
  enabled = false;
@@ -228,9 +245,11 @@ function resetGlobalCursorStyle() {
228
245
  document.head.removeChild(styleElement);
229
246
  currentCursorStyle = null;
230
247
  styleElement = null;
248
+ prevRuleIndex = -1;
231
249
  }
232
250
  }
233
251
  function setGlobalCursorStyle(state, constraintFlags) {
252
+ var _styleElement$sheet$i, _styleElement$sheet2;
234
253
  if (!enabled) {
235
254
  return;
236
255
  }
@@ -247,7 +266,11 @@ function setGlobalCursorStyle(state, constraintFlags) {
247
266
  }
248
267
  document.head.appendChild(styleElement);
249
268
  }
250
- styleElement.innerHTML = `*{cursor: ${style}!important;}`;
269
+ if (prevRuleIndex >= 0) {
270
+ var _styleElement$sheet;
271
+ (_styleElement$sheet = styleElement.sheet) === null || _styleElement$sheet === void 0 ? void 0 : _styleElement$sheet.removeRule(prevRuleIndex);
272
+ }
273
+ 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;
251
274
  }
252
275
 
253
276
  function isKeyDown(event) {
@@ -478,7 +501,9 @@ function handlePointerDown(event) {
478
501
  if (intersectingHandles.length > 0) {
479
502
  updateResizeHandlerStates("down", event);
480
503
  event.preventDefault();
481
- event.stopPropagation();
504
+ if (!isWithinResizeHandle(target)) {
505
+ event.stopImmediatePropagation();
506
+ }
482
507
  }
483
508
  }
484
509
  function handlePointerMove(event) {
@@ -527,6 +552,9 @@ function handlePointerUp(event) {
527
552
  isPointerDown = false;
528
553
  if (intersectingHandles.length > 0) {
529
554
  event.preventDefault();
555
+ if (!isWithinResizeHandle(target)) {
556
+ event.stopImmediatePropagation();
557
+ }
530
558
  }
531
559
  updateResizeHandlerStates("up", event);
532
560
  recalculateIntersectingHandles({
@@ -537,6 +565,16 @@ function handlePointerUp(event) {
537
565
  updateCursor();
538
566
  updateListeners();
539
567
  }
568
+ function isWithinResizeHandle(element) {
569
+ let currentElement = element;
570
+ while (currentElement) {
571
+ if (currentElement.hasAttribute(DATA_ATTRIBUTES.resizeHandle)) {
572
+ return true;
573
+ }
574
+ currentElement = currentElement.parentElement;
575
+ }
576
+ return false;
577
+ }
540
578
  function recalculateIntersectingHandles({
541
579
  target,
542
580
  x,
@@ -628,49 +666,42 @@ function updateCursor() {
628
666
  resetGlobalCursorStyle();
629
667
  }
630
668
  }
669
+ let listenersAbortController = new AbortController();
631
670
  function updateListeners() {
632
- ownerDocumentCounts.forEach((_, ownerDocument) => {
633
- const {
634
- body
635
- } = ownerDocument;
636
- body.removeEventListener("contextmenu", handlePointerUp);
637
- body.removeEventListener("pointerdown", handlePointerDown, {
638
- capture: true
639
- });
640
- body.removeEventListener("pointerleave", handlePointerMove);
641
- body.removeEventListener("pointermove", handlePointerMove);
642
- });
643
- window.removeEventListener("pointerup", handlePointerUp);
644
- window.removeEventListener("pointercancel", handlePointerUp);
645
- if (registeredResizeHandlers.size > 0) {
646
- if (isPointerDown) {
647
- if (intersectingHandles.length > 0) {
648
- ownerDocumentCounts.forEach((count, ownerDocument) => {
649
- const {
650
- body
651
- } = ownerDocument;
652
- if (count > 0) {
653
- body.addEventListener("contextmenu", handlePointerUp);
654
- body.addEventListener("pointerleave", handlePointerMove);
655
- body.addEventListener("pointermove", handlePointerMove);
656
- }
657
- });
658
- }
659
- window.addEventListener("pointerup", handlePointerUp);
660
- window.addEventListener("pointercancel", handlePointerUp);
661
- } else {
671
+ listenersAbortController.abort();
672
+ listenersAbortController = new AbortController();
673
+ const options = {
674
+ capture: true,
675
+ signal: listenersAbortController.signal
676
+ };
677
+ if (!registeredResizeHandlers.size) {
678
+ return;
679
+ }
680
+ if (isPointerDown) {
681
+ if (intersectingHandles.length > 0) {
662
682
  ownerDocumentCounts.forEach((count, ownerDocument) => {
663
683
  const {
664
684
  body
665
685
  } = ownerDocument;
666
686
  if (count > 0) {
667
- body.addEventListener("pointerdown", handlePointerDown, {
668
- capture: true
669
- });
670
- body.addEventListener("pointermove", handlePointerMove);
687
+ body.addEventListener("contextmenu", handlePointerUp, options);
688
+ body.addEventListener("pointerleave", handlePointerMove, options);
689
+ body.addEventListener("pointermove", handlePointerMove, options);
671
690
  }
672
691
  });
673
692
  }
693
+ window.addEventListener("pointerup", handlePointerUp, options);
694
+ window.addEventListener("pointercancel", handlePointerUp, options);
695
+ } else {
696
+ ownerDocumentCounts.forEach((count, ownerDocument) => {
697
+ const {
698
+ body
699
+ } = ownerDocument;
700
+ if (count > 0) {
701
+ body.addEventListener("pointerdown", handlePointerDown, options);
702
+ body.addEventListener("pointermove", handlePointerMove, options);
703
+ }
704
+ });
674
705
  }
675
706
  }
676
707
  function updateResizeHandlerStates(action, event) {
@@ -695,8 +726,6 @@ function assert(expectedCondition, message) {
695
726
  }
696
727
  }
697
728
 
698
- const PRECISION = 10;
699
-
700
729
  function fuzzyCompareNumbers(actual, expected, fractionDigits = PRECISION) {
701
730
  if (actual.toFixed(fractionDigits) === expected.toFixed(fractionDigits)) {
702
731
  return 0;
@@ -1040,12 +1069,12 @@ function calculateAriaValues({
1040
1069
  }
1041
1070
 
1042
1071
  function getResizeHandleElementsForGroup(groupId, scope = document) {
1043
- return Array.from(scope.querySelectorAll(`[data-panel-resize-handle-id][data-panel-group-id="${groupId}"]`));
1072
+ return Array.from(scope.querySelectorAll(`[${DATA_ATTRIBUTES.resizeHandleId}][data-panel-group-id="${groupId}"]`));
1044
1073
  }
1045
1074
 
1046
1075
  function getResizeHandleElementIndex(groupId, id, scope = document) {
1047
1076
  const handles = getResizeHandleElementsForGroup(groupId, scope);
1048
- const index = handles.findIndex(handle => handle.getAttribute("data-panel-resize-handle-id") === id);
1077
+ const index = handles.findIndex(handle => handle.getAttribute(DATA_ATTRIBUTES.resizeHandleId) === id);
1049
1078
  return index !== null && index !== void 0 ? index : null;
1050
1079
  }
1051
1080
 
@@ -1070,7 +1099,7 @@ function getPanelGroupElement(id, rootElement = document) {
1070
1099
  }
1071
1100
 
1072
1101
  function getResizeHandleElement(id, scope = document) {
1073
- const element = scope.querySelector(`[data-panel-resize-handle-id="${id}"]`);
1102
+ const element = scope.querySelector(`[${DATA_ATTRIBUTES.resizeHandleId}="${id}"]`);
1074
1103
  if (element) {
1075
1104
  return element;
1076
1105
  }
@@ -1159,7 +1188,7 @@ function useWindowSplitterPanelGroupBehavior({
1159
1188
  const handles = getResizeHandleElementsForGroup(groupId, panelGroupElement);
1160
1189
  assert(handles, `No resize handles found for group id "${groupId}"`);
1161
1190
  const cleanupFunctions = handles.map(handle => {
1162
- const handleId = handle.getAttribute("data-panel-resize-handle-id");
1191
+ const handleId = handle.getAttribute(DATA_ATTRIBUTES.resizeHandleId);
1163
1192
  assert(handleId, `Resize handle element has no handle id attribute`);
1164
1193
  const [idBefore, idAfter] = getResizeHandlePanelIds(groupId, handleId, panelDataArray, panelGroupElement);
1165
1194
  if (idBefore == null || idAfter == null) {
@@ -1237,7 +1266,7 @@ function calculateDragOffsetPercentage(event, dragHandleId, direction, initialDr
1237
1266
  const isHorizontal = direction === "horizontal";
1238
1267
  const handleElement = getResizeHandleElement(dragHandleId, panelGroupElement);
1239
1268
  assert(handleElement, `No resize handle element found for id "${dragHandleId}"`);
1240
- const groupId = handleElement.getAttribute("data-panel-group-id");
1269
+ const groupId = handleElement.getAttribute(DATA_ATTRIBUTES.groupId);
1241
1270
  assert(groupId, `Resize handle element has no group id attribute`);
1242
1271
  let {
1243
1272
  initialCursorPosition
@@ -2243,9 +2272,9 @@ function PanelGroupWithForwardedRef({
2243
2272
  ...styleFromProps
2244
2273
  },
2245
2274
  // CSS selectors
2246
- "data-panel-group": "",
2247
- "data-panel-group-direction": direction,
2248
- "data-panel-group-id": groupId
2275
+ [DATA_ATTRIBUTES.group]: "",
2276
+ [DATA_ATTRIBUTES.groupDirection]: direction,
2277
+ [DATA_ATTRIBUTES.groupId]: groupId
2249
2278
  }));
2250
2279
  }
2251
2280
  const PanelGroup = forwardRef((props, ref) => createElement(PanelGroupWithForwardedRef, {
@@ -2304,7 +2333,7 @@ function useWindowSplitterResizeHandlerBehavior({
2304
2333
  case "F6":
2305
2334
  {
2306
2335
  event.preventDefault();
2307
- const groupId = handleElement.getAttribute("data-panel-group-id");
2336
+ const groupId = handleElement.getAttribute(DATA_ATTRIBUTES.groupId);
2308
2337
  assert(groupId, `No group element found for id "${groupId}"`);
2309
2338
  const handles = getResizeHandleElementsForGroup(groupId, panelGroupElement);
2310
2339
  const index = getResizeHandleElementIndex(groupId, handleId, panelGroupElement);
@@ -2399,54 +2428,54 @@ function PanelResizeHandle({
2399
2428
  assert(element, "Element ref not attached");
2400
2429
  let didMove = false;
2401
2430
  const setResizeHandlerState = (action, isActive, event) => {
2402
- if (isActive) {
2403
- switch (action) {
2404
- case "down":
2405
- {
2406
- setState("drag");
2407
- didMove = false;
2408
- assert(event, 'Expected event to be defined for "down" action');
2409
- startDragging(resizeHandleId, event);
2410
- const {
2411
- onDragging,
2412
- onPointerDown
2413
- } = callbacksRef.current;
2414
- onDragging === null || onDragging === void 0 ? void 0 : onDragging(true);
2415
- onPointerDown === null || onPointerDown === void 0 ? void 0 : onPointerDown();
2416
- break;
2417
- }
2418
- case "move":
2419
- {
2420
- const {
2421
- state
2422
- } = committedValuesRef.current;
2423
- didMove = true;
2424
- if (state !== "drag") {
2425
- setState("hover");
2426
- }
2427
- assert(event, 'Expected event to be defined for "move" action');
2428
- resizeHandler(event);
2429
- break;
2430
- }
2431
- case "up":
2432
- {
2431
+ if (!isActive) {
2432
+ setState("inactive");
2433
+ return;
2434
+ }
2435
+ switch (action) {
2436
+ case "down":
2437
+ {
2438
+ setState("drag");
2439
+ didMove = false;
2440
+ assert(event, 'Expected event to be defined for "down" action');
2441
+ startDragging(resizeHandleId, event);
2442
+ const {
2443
+ onDragging,
2444
+ onPointerDown
2445
+ } = callbacksRef.current;
2446
+ onDragging === null || onDragging === void 0 ? void 0 : onDragging(true);
2447
+ onPointerDown === null || onPointerDown === void 0 ? void 0 : onPointerDown();
2448
+ break;
2449
+ }
2450
+ case "move":
2451
+ {
2452
+ const {
2453
+ state
2454
+ } = committedValuesRef.current;
2455
+ didMove = true;
2456
+ if (state !== "drag") {
2433
2457
  setState("hover");
2434
- stopDragging();
2435
- const {
2436
- onClick,
2437
- onDragging,
2438
- onPointerUp
2439
- } = callbacksRef.current;
2440
- onDragging === null || onDragging === void 0 ? void 0 : onDragging(false);
2441
- onPointerUp === null || onPointerUp === void 0 ? void 0 : onPointerUp();
2442
- if (!didMove) {
2443
- onClick === null || onClick === void 0 ? void 0 : onClick();
2444
- }
2445
- break;
2446
2458
  }
2447
- }
2448
- } else {
2449
- setState("inactive");
2459
+ assert(event, 'Expected event to be defined for "move" action');
2460
+ resizeHandler(event);
2461
+ break;
2462
+ }
2463
+ case "up":
2464
+ {
2465
+ setState("hover");
2466
+ stopDragging();
2467
+ const {
2468
+ onClick,
2469
+ onDragging,
2470
+ onPointerUp
2471
+ } = callbacksRef.current;
2472
+ onDragging === null || onDragging === void 0 ? void 0 : onDragging(false);
2473
+ onPointerUp === null || onPointerUp === void 0 ? void 0 : onPointerUp();
2474
+ if (!didMove) {
2475
+ onClick === null || onClick === void 0 ? void 0 : onClick();
2476
+ }
2477
+ break;
2478
+ }
2450
2479
  }
2451
2480
  };
2452
2481
  return registerResizeHandle(resizeHandleId, element, direction, {
@@ -2485,13 +2514,13 @@ function PanelResizeHandle({
2485
2514
  },
2486
2515
  tabIndex,
2487
2516
  // CSS selectors
2488
- "data-panel-group-direction": direction,
2489
- "data-panel-group-id": groupId,
2490
- "data-resize-handle": "",
2491
- "data-resize-handle-active": state === "drag" ? "pointer" : isFocused ? "keyboard" : undefined,
2492
- "data-resize-handle-state": state,
2493
- "data-panel-resize-handle-enabled": !disabled,
2494
- "data-panel-resize-handle-id": resizeHandleId
2517
+ [DATA_ATTRIBUTES.groupDirection]: direction,
2518
+ [DATA_ATTRIBUTES.groupId]: groupId,
2519
+ [DATA_ATTRIBUTES.resizeHandle]: "",
2520
+ [DATA_ATTRIBUTES.resizeHandleActive]: state === "drag" ? "pointer" : isFocused ? "keyboard" : undefined,
2521
+ [DATA_ATTRIBUTES.resizeHandleEnabled]: !disabled,
2522
+ [DATA_ATTRIBUTES.resizeHandleId]: resizeHandleId,
2523
+ [DATA_ATTRIBUTES.resizeHandleState]: state
2495
2524
  });
2496
2525
  }
2497
2526
  PanelResizeHandle.displayName = "PanelResizeHandle";
@@ -2525,4 +2554,4 @@ function getIntersectingRectangle(rectOne, rectTwo, strict) {
2525
2554
  };
2526
2555
  }
2527
2556
 
2528
- export { Panel, PanelGroup, PanelResizeHandle, assert, disableGlobalCursorStyles, enableGlobalCursorStyles, getIntersectingRectangle, getPanelElement, getPanelElementsForGroup, getPanelGroupElement, getResizeHandleElement, getResizeHandleElementIndex, getResizeHandleElementsForGroup, getResizeHandlePanelIds, intersects, setNonce };
2557
+ export { DATA_ATTRIBUTES, Panel, PanelGroup, PanelResizeHandle, assert, disableGlobalCursorStyles, enableGlobalCursorStyles, getIntersectingRectangle, getPanelElement, getPanelElementsForGroup, getPanelGroupElement, getResizeHandleElement, getResizeHandleElementIndex, getResizeHandleElementsForGroup, getResizeHandlePanelIds, intersects, setNonce };