react-resizable-panels 2.1.8 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (31) hide show
  1. package/LICENSE +21 -0
  2. package/dist/declarations/src/Panel.d.ts +9 -9
  3. package/dist/declarations/src/PanelGroup.d.ts +10 -10
  4. package/dist/declarations/src/PanelResizeHandle.d.ts +1 -1
  5. package/dist/declarations/src/constants.d.ts +15 -0
  6. package/dist/declarations/src/hooks/usePanelGroupContext.d.ts +4 -0
  7. package/dist/declarations/src/index.d.ts +4 -2
  8. package/dist/{react-resizable-panels.browser.development.esm.js → react-resizable-panels.browser.development.js} +148 -106
  9. package/dist/{react-resizable-panels.browser.esm.js → react-resizable-panels.browser.js} +148 -106
  10. package/dist/react-resizable-panels.d.ts +2 -0
  11. package/dist/{react-resizable-panels.development.esm.js → react-resizable-panels.development.js} +148 -106
  12. package/dist/{react-resizable-panels.development.node.esm.js → react-resizable-panels.development.node.js} +148 -106
  13. package/dist/{react-resizable-panels.esm.js → react-resizable-panels.js} +148 -106
  14. package/dist/{react-resizable-panels.node.esm.js → react-resizable-panels.node.js} +148 -106
  15. package/package.json +25 -47
  16. package/dist/react-resizable-panels.browser.cjs.js +0 -2453
  17. package/dist/react-resizable-panels.browser.cjs.mjs +0 -18
  18. package/dist/react-resizable-panels.browser.development.cjs.js +0 -2559
  19. package/dist/react-resizable-panels.browser.development.cjs.mjs +0 -18
  20. package/dist/react-resizable-panels.cjs.d.mts +0 -2
  21. package/dist/react-resizable-panels.cjs.d.mts.map +0 -1
  22. package/dist/react-resizable-panels.cjs.d.ts +0 -2
  23. package/dist/react-resizable-panels.cjs.d.ts.map +0 -1
  24. package/dist/react-resizable-panels.cjs.js +0 -2455
  25. package/dist/react-resizable-panels.cjs.mjs +0 -18
  26. package/dist/react-resizable-panels.development.cjs.js +0 -2566
  27. package/dist/react-resizable-panels.development.cjs.mjs +0 -18
  28. package/dist/react-resizable-panels.development.node.cjs.js +0 -2332
  29. package/dist/react-resizable-panels.development.node.cjs.mjs +0 -18
  30. package/dist/react-resizable-panels.node.cjs.js +0 -2231
  31. package/dist/react-resizable-panels.node.cjs.mjs +0 -18
@@ -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,47 @@ 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
+ ownerDocumentCounts.forEach((_, ownerDocument) => {
694
+ const {
695
+ body
696
+ } = ownerDocument;
697
+ body.addEventListener("pointerup", handlePointerUp, options);
698
+ body.addEventListener("pointercancel", handlePointerUp, options);
699
+ });
700
+ } else {
701
+ ownerDocumentCounts.forEach((count, ownerDocument) => {
702
+ const {
703
+ body
704
+ } = ownerDocument;
705
+ if (count > 0) {
706
+ body.addEventListener("pointerdown", handlePointerDown, options);
707
+ body.addEventListener("pointermove", handlePointerMove, options);
708
+ }
709
+ });
674
710
  }
675
711
  }
676
712
  function updateResizeHandlerStates(action, event) {
@@ -695,8 +731,6 @@ function assert(expectedCondition, message) {
695
731
  }
696
732
  }
697
733
 
698
- const PRECISION = 10;
699
-
700
734
  function fuzzyCompareNumbers(actual, expected, fractionDigits = PRECISION) {
701
735
  if (actual.toFixed(fractionDigits) === expected.toFixed(fractionDigits)) {
702
736
  return 0;
@@ -1040,12 +1074,12 @@ function calculateAriaValues({
1040
1074
  }
1041
1075
 
1042
1076
  function getResizeHandleElementsForGroup(groupId, scope = document) {
1043
- return Array.from(scope.querySelectorAll(`[data-panel-resize-handle-id][data-panel-group-id="${groupId}"]`));
1077
+ return Array.from(scope.querySelectorAll(`[${DATA_ATTRIBUTES.resizeHandleId}][data-panel-group-id="${groupId}"]`));
1044
1078
  }
1045
1079
 
1046
1080
  function getResizeHandleElementIndex(groupId, id, scope = document) {
1047
1081
  const handles = getResizeHandleElementsForGroup(groupId, scope);
1048
- const index = handles.findIndex(handle => handle.getAttribute("data-panel-resize-handle-id") === id);
1082
+ const index = handles.findIndex(handle => handle.getAttribute(DATA_ATTRIBUTES.resizeHandleId) === id);
1049
1083
  return index !== null && index !== void 0 ? index : null;
1050
1084
  }
1051
1085
 
@@ -1070,7 +1104,7 @@ function getPanelGroupElement(id, rootElement = document) {
1070
1104
  }
1071
1105
 
1072
1106
  function getResizeHandleElement(id, scope = document) {
1073
- const element = scope.querySelector(`[data-panel-resize-handle-id="${id}"]`);
1107
+ const element = scope.querySelector(`[${DATA_ATTRIBUTES.resizeHandleId}="${id}"]`);
1074
1108
  if (element) {
1075
1109
  return element;
1076
1110
  }
@@ -1159,7 +1193,7 @@ function useWindowSplitterPanelGroupBehavior({
1159
1193
  const handles = getResizeHandleElementsForGroup(groupId, panelGroupElement);
1160
1194
  assert(handles, `No resize handles found for group id "${groupId}"`);
1161
1195
  const cleanupFunctions = handles.map(handle => {
1162
- const handleId = handle.getAttribute("data-panel-resize-handle-id");
1196
+ const handleId = handle.getAttribute(DATA_ATTRIBUTES.resizeHandleId);
1163
1197
  assert(handleId, `Resize handle element has no handle id attribute`);
1164
1198
  const [idBefore, idAfter] = getResizeHandlePanelIds(groupId, handleId, panelDataArray, panelGroupElement);
1165
1199
  if (idBefore == null || idAfter == null) {
@@ -1237,7 +1271,7 @@ function calculateDragOffsetPercentage(event, dragHandleId, direction, initialDr
1237
1271
  const isHorizontal = direction === "horizontal";
1238
1272
  const handleElement = getResizeHandleElement(dragHandleId, panelGroupElement);
1239
1273
  assert(handleElement, `No resize handle element found for id "${dragHandleId}"`);
1240
- const groupId = handleElement.getAttribute("data-panel-group-id");
1274
+ const groupId = handleElement.getAttribute(DATA_ATTRIBUTES.groupId);
1241
1275
  assert(groupId, `Resize handle element has no group id attribute`);
1242
1276
  let {
1243
1277
  initialCursorPosition
@@ -2243,9 +2277,9 @@ function PanelGroupWithForwardedRef({
2243
2277
  ...styleFromProps
2244
2278
  },
2245
2279
  // CSS selectors
2246
- "data-panel-group": "",
2247
- "data-panel-group-direction": direction,
2248
- "data-panel-group-id": groupId
2280
+ [DATA_ATTRIBUTES.group]: "",
2281
+ [DATA_ATTRIBUTES.groupDirection]: direction,
2282
+ [DATA_ATTRIBUTES.groupId]: groupId
2249
2283
  }));
2250
2284
  }
2251
2285
  const PanelGroup = forwardRef((props, ref) => createElement(PanelGroupWithForwardedRef, {
@@ -2304,7 +2338,7 @@ function useWindowSplitterResizeHandlerBehavior({
2304
2338
  case "F6":
2305
2339
  {
2306
2340
  event.preventDefault();
2307
- const groupId = handleElement.getAttribute("data-panel-group-id");
2341
+ const groupId = handleElement.getAttribute(DATA_ATTRIBUTES.groupId);
2308
2342
  assert(groupId, `No group element found for id "${groupId}"`);
2309
2343
  const handles = getResizeHandleElementsForGroup(groupId, panelGroupElement);
2310
2344
  const index = getResizeHandleElementIndex(groupId, handleId, panelGroupElement);
@@ -2399,54 +2433,54 @@ function PanelResizeHandle({
2399
2433
  assert(element, "Element ref not attached");
2400
2434
  let didMove = false;
2401
2435
  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
- {
2436
+ if (!isActive) {
2437
+ setState("inactive");
2438
+ return;
2439
+ }
2440
+ switch (action) {
2441
+ case "down":
2442
+ {
2443
+ setState("drag");
2444
+ didMove = false;
2445
+ assert(event, 'Expected event to be defined for "down" action');
2446
+ startDragging(resizeHandleId, event);
2447
+ const {
2448
+ onDragging,
2449
+ onPointerDown
2450
+ } = callbacksRef.current;
2451
+ onDragging === null || onDragging === void 0 ? void 0 : onDragging(true);
2452
+ onPointerDown === null || onPointerDown === void 0 ? void 0 : onPointerDown();
2453
+ break;
2454
+ }
2455
+ case "move":
2456
+ {
2457
+ const {
2458
+ state
2459
+ } = committedValuesRef.current;
2460
+ didMove = true;
2461
+ if (state !== "drag") {
2433
2462
  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
2463
  }
2447
- }
2448
- } else {
2449
- setState("inactive");
2464
+ assert(event, 'Expected event to be defined for "move" action');
2465
+ resizeHandler(event);
2466
+ break;
2467
+ }
2468
+ case "up":
2469
+ {
2470
+ setState("hover");
2471
+ stopDragging();
2472
+ const {
2473
+ onClick,
2474
+ onDragging,
2475
+ onPointerUp
2476
+ } = callbacksRef.current;
2477
+ onDragging === null || onDragging === void 0 ? void 0 : onDragging(false);
2478
+ onPointerUp === null || onPointerUp === void 0 ? void 0 : onPointerUp();
2479
+ if (!didMove) {
2480
+ onClick === null || onClick === void 0 ? void 0 : onClick();
2481
+ }
2482
+ break;
2483
+ }
2450
2484
  }
2451
2485
  };
2452
2486
  return registerResizeHandle(resizeHandleId, element, direction, {
@@ -2485,17 +2519,25 @@ function PanelResizeHandle({
2485
2519
  },
2486
2520
  tabIndex,
2487
2521
  // 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
2522
+ [DATA_ATTRIBUTES.groupDirection]: direction,
2523
+ [DATA_ATTRIBUTES.groupId]: groupId,
2524
+ [DATA_ATTRIBUTES.resizeHandle]: "",
2525
+ [DATA_ATTRIBUTES.resizeHandleActive]: state === "drag" ? "pointer" : isFocused ? "keyboard" : undefined,
2526
+ [DATA_ATTRIBUTES.resizeHandleEnabled]: !disabled,
2527
+ [DATA_ATTRIBUTES.resizeHandleId]: resizeHandleId,
2528
+ [DATA_ATTRIBUTES.resizeHandleState]: state
2495
2529
  });
2496
2530
  }
2497
2531
  PanelResizeHandle.displayName = "PanelResizeHandle";
2498
2532
 
2533
+ function usePanelGroupContext() {
2534
+ const context = useContext(PanelGroupContext);
2535
+ return {
2536
+ direction: context === null || context === void 0 ? void 0 : context.direction,
2537
+ groupId: context === null || context === void 0 ? void 0 : context.groupId
2538
+ };
2539
+ }
2540
+
2499
2541
  function getPanelElement(id, scope = document) {
2500
2542
  const element = scope.querySelector(`[data-panel-id="${id}"]`);
2501
2543
  if (element) {
@@ -2525,4 +2567,4 @@ function getIntersectingRectangle(rectOne, rectTwo, strict) {
2525
2567
  };
2526
2568
  }
2527
2569
 
2528
- export { Panel, PanelGroup, PanelResizeHandle, assert, disableGlobalCursorStyles, enableGlobalCursorStyles, getIntersectingRectangle, getPanelElement, getPanelElementsForGroup, getPanelGroupElement, getResizeHandleElement, getResizeHandleElementIndex, getResizeHandleElementsForGroup, getResizeHandlePanelIds, intersects, setNonce };
2570
+ export { DATA_ATTRIBUTES, Panel, PanelGroup, PanelResizeHandle, assert, disableGlobalCursorStyles, enableGlobalCursorStyles, getIntersectingRectangle, getPanelElement, getPanelElementsForGroup, getPanelGroupElement, getResizeHandleElement, getResizeHandleElementIndex, getResizeHandleElementsForGroup, getResizeHandlePanelIds, intersects, setNonce, usePanelGroupContext };