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()];
@@ -144,11 +160,11 @@ function PanelWithForwardedRef({
144
160
  ...styleFromProps
145
161
  },
146
162
  // CSS selectors
147
- "data-panel": "",
148
- "data-panel-collapsible": collapsible || undefined,
149
- "data-panel-group-id": groupId,
150
- "data-panel-id": panelId,
151
- "data-panel-size": parseFloat("" + style.flexGrow).toFixed(1)
163
+ [DATA_ATTRIBUTES.groupId]: groupId,
164
+ [DATA_ATTRIBUTES.panel]: "",
165
+ [DATA_ATTRIBUTES.panelCollapsible]: collapsible || undefined,
166
+ [DATA_ATTRIBUTES.panelId]: panelId,
167
+ [DATA_ATTRIBUTES.panelSize]: parseFloat("" + style.flexGrow).toFixed(1)
152
168
  });
153
169
  }
154
170
  const Panel = forwardRef((props, ref) => createElement(PanelWithForwardedRef, {
@@ -168,6 +184,7 @@ function setNonce(value) {
168
184
 
169
185
  let currentCursorStyle = null;
170
186
  let enabled = true;
187
+ let prevRuleIndex = -1;
171
188
  let styleElement = null;
172
189
  function disableGlobalCursorStyles() {
173
190
  enabled = false;
@@ -217,9 +234,11 @@ function resetGlobalCursorStyle() {
217
234
  document.head.removeChild(styleElement);
218
235
  currentCursorStyle = null;
219
236
  styleElement = null;
237
+ prevRuleIndex = -1;
220
238
  }
221
239
  }
222
240
  function setGlobalCursorStyle(state, constraintFlags) {
241
+ var _styleElement$sheet$i, _styleElement$sheet2;
223
242
  if (!enabled) {
224
243
  return;
225
244
  }
@@ -236,7 +255,11 @@ function setGlobalCursorStyle(state, constraintFlags) {
236
255
  }
237
256
  document.head.appendChild(styleElement);
238
257
  }
239
- styleElement.innerHTML = `*{cursor: ${style}!important;}`;
258
+ if (prevRuleIndex >= 0) {
259
+ var _styleElement$sheet;
260
+ (_styleElement$sheet = styleElement.sheet) === null || _styleElement$sheet === void 0 ? void 0 : _styleElement$sheet.removeRule(prevRuleIndex);
261
+ }
262
+ 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;
240
263
  }
241
264
 
242
265
  function isKeyDown(event) {
@@ -467,7 +490,9 @@ function handlePointerDown(event) {
467
490
  if (intersectingHandles.length > 0) {
468
491
  updateResizeHandlerStates("down", event);
469
492
  event.preventDefault();
470
- event.stopPropagation();
493
+ if (!isWithinResizeHandle(target)) {
494
+ event.stopImmediatePropagation();
495
+ }
471
496
  }
472
497
  }
473
498
  function handlePointerMove(event) {
@@ -516,6 +541,9 @@ function handlePointerUp(event) {
516
541
  isPointerDown = false;
517
542
  if (intersectingHandles.length > 0) {
518
543
  event.preventDefault();
544
+ if (!isWithinResizeHandle(target)) {
545
+ event.stopImmediatePropagation();
546
+ }
519
547
  }
520
548
  updateResizeHandlerStates("up", event);
521
549
  recalculateIntersectingHandles({
@@ -526,6 +554,16 @@ function handlePointerUp(event) {
526
554
  updateCursor();
527
555
  updateListeners();
528
556
  }
557
+ function isWithinResizeHandle(element) {
558
+ let currentElement = element;
559
+ while (currentElement) {
560
+ if (currentElement.hasAttribute(DATA_ATTRIBUTES.resizeHandle)) {
561
+ return true;
562
+ }
563
+ currentElement = currentElement.parentElement;
564
+ }
565
+ return false;
566
+ }
529
567
  function recalculateIntersectingHandles({
530
568
  target,
531
569
  x,
@@ -617,49 +655,47 @@ function updateCursor() {
617
655
  resetGlobalCursorStyle();
618
656
  }
619
657
  }
658
+ let listenersAbortController = new AbortController();
620
659
  function updateListeners() {
621
- ownerDocumentCounts.forEach((_, ownerDocument) => {
622
- const {
623
- body
624
- } = ownerDocument;
625
- body.removeEventListener("contextmenu", handlePointerUp);
626
- body.removeEventListener("pointerdown", handlePointerDown, {
627
- capture: true
628
- });
629
- body.removeEventListener("pointerleave", handlePointerMove);
630
- body.removeEventListener("pointermove", handlePointerMove);
631
- });
632
- window.removeEventListener("pointerup", handlePointerUp);
633
- window.removeEventListener("pointercancel", handlePointerUp);
634
- if (registeredResizeHandlers.size > 0) {
635
- if (isPointerDown) {
636
- if (intersectingHandles.length > 0) {
637
- ownerDocumentCounts.forEach((count, ownerDocument) => {
638
- const {
639
- body
640
- } = ownerDocument;
641
- if (count > 0) {
642
- body.addEventListener("contextmenu", handlePointerUp);
643
- body.addEventListener("pointerleave", handlePointerMove);
644
- body.addEventListener("pointermove", handlePointerMove);
645
- }
646
- });
647
- }
648
- window.addEventListener("pointerup", handlePointerUp);
649
- window.addEventListener("pointercancel", handlePointerUp);
650
- } else {
660
+ listenersAbortController.abort();
661
+ listenersAbortController = new AbortController();
662
+ const options = {
663
+ capture: true,
664
+ signal: listenersAbortController.signal
665
+ };
666
+ if (!registeredResizeHandlers.size) {
667
+ return;
668
+ }
669
+ if (isPointerDown) {
670
+ if (intersectingHandles.length > 0) {
651
671
  ownerDocumentCounts.forEach((count, ownerDocument) => {
652
672
  const {
653
673
  body
654
674
  } = ownerDocument;
655
675
  if (count > 0) {
656
- body.addEventListener("pointerdown", handlePointerDown, {
657
- capture: true
658
- });
659
- body.addEventListener("pointermove", handlePointerMove);
676
+ body.addEventListener("contextmenu", handlePointerUp, options);
677
+ body.addEventListener("pointerleave", handlePointerMove, options);
678
+ body.addEventListener("pointermove", handlePointerMove, options);
660
679
  }
661
680
  });
662
681
  }
682
+ ownerDocumentCounts.forEach((_, ownerDocument) => {
683
+ const {
684
+ body
685
+ } = ownerDocument;
686
+ body.addEventListener("pointerup", handlePointerUp, options);
687
+ body.addEventListener("pointercancel", handlePointerUp, options);
688
+ });
689
+ } else {
690
+ ownerDocumentCounts.forEach((count, ownerDocument) => {
691
+ const {
692
+ body
693
+ } = ownerDocument;
694
+ if (count > 0) {
695
+ body.addEventListener("pointerdown", handlePointerDown, options);
696
+ body.addEventListener("pointermove", handlePointerMove, options);
697
+ }
698
+ });
663
699
  }
664
700
  }
665
701
  function updateResizeHandlerStates(action, event) {
@@ -684,8 +720,6 @@ function assert(expectedCondition, message) {
684
720
  }
685
721
  }
686
722
 
687
- const PRECISION = 10;
688
-
689
723
  function fuzzyCompareNumbers(actual, expected, fractionDigits = PRECISION) {
690
724
  if (actual.toFixed(fractionDigits) === expected.toFixed(fractionDigits)) {
691
725
  return 0;
@@ -1029,12 +1063,12 @@ function calculateAriaValues({
1029
1063
  }
1030
1064
 
1031
1065
  function getResizeHandleElementsForGroup(groupId, scope = document) {
1032
- return Array.from(scope.querySelectorAll(`[data-panel-resize-handle-id][data-panel-group-id="${groupId}"]`));
1066
+ return Array.from(scope.querySelectorAll(`[${DATA_ATTRIBUTES.resizeHandleId}][data-panel-group-id="${groupId}"]`));
1033
1067
  }
1034
1068
 
1035
1069
  function getResizeHandleElementIndex(groupId, id, scope = document) {
1036
1070
  const handles = getResizeHandleElementsForGroup(groupId, scope);
1037
- const index = handles.findIndex(handle => handle.getAttribute("data-panel-resize-handle-id") === id);
1071
+ const index = handles.findIndex(handle => handle.getAttribute(DATA_ATTRIBUTES.resizeHandleId) === id);
1038
1072
  return index !== null && index !== void 0 ? index : null;
1039
1073
  }
1040
1074
 
@@ -1059,7 +1093,7 @@ function getPanelGroupElement(id, rootElement = document) {
1059
1093
  }
1060
1094
 
1061
1095
  function getResizeHandleElement(id, scope = document) {
1062
- const element = scope.querySelector(`[data-panel-resize-handle-id="${id}"]`);
1096
+ const element = scope.querySelector(`[${DATA_ATTRIBUTES.resizeHandleId}="${id}"]`);
1063
1097
  if (element) {
1064
1098
  return element;
1065
1099
  }
@@ -1138,7 +1172,7 @@ function useWindowSplitterPanelGroupBehavior({
1138
1172
  const handles = getResizeHandleElementsForGroup(groupId, panelGroupElement);
1139
1173
  assert(handles, `No resize handles found for group id "${groupId}"`);
1140
1174
  const cleanupFunctions = handles.map(handle => {
1141
- const handleId = handle.getAttribute("data-panel-resize-handle-id");
1175
+ const handleId = handle.getAttribute(DATA_ATTRIBUTES.resizeHandleId);
1142
1176
  assert(handleId, `Resize handle element has no handle id attribute`);
1143
1177
  const [idBefore, idAfter] = getResizeHandlePanelIds(groupId, handleId, panelDataArray, panelGroupElement);
1144
1178
  if (idBefore == null || idAfter == null) {
@@ -1216,7 +1250,7 @@ function calculateDragOffsetPercentage(event, dragHandleId, direction, initialDr
1216
1250
  const isHorizontal = direction === "horizontal";
1217
1251
  const handleElement = getResizeHandleElement(dragHandleId, panelGroupElement);
1218
1252
  assert(handleElement, `No resize handle element found for id "${dragHandleId}"`);
1219
- const groupId = handleElement.getAttribute("data-panel-group-id");
1253
+ const groupId = handleElement.getAttribute(DATA_ATTRIBUTES.groupId);
1220
1254
  assert(groupId, `Resize handle element has no group id attribute`);
1221
1255
  let {
1222
1256
  initialCursorPosition
@@ -2132,9 +2166,9 @@ function PanelGroupWithForwardedRef({
2132
2166
  ...styleFromProps
2133
2167
  },
2134
2168
  // CSS selectors
2135
- "data-panel-group": "",
2136
- "data-panel-group-direction": direction,
2137
- "data-panel-group-id": groupId
2169
+ [DATA_ATTRIBUTES.group]: "",
2170
+ [DATA_ATTRIBUTES.groupDirection]: direction,
2171
+ [DATA_ATTRIBUTES.groupId]: groupId
2138
2172
  }));
2139
2173
  }
2140
2174
  const PanelGroup = forwardRef((props, ref) => createElement(PanelGroupWithForwardedRef, {
@@ -2193,7 +2227,7 @@ function useWindowSplitterResizeHandlerBehavior({
2193
2227
  case "F6":
2194
2228
  {
2195
2229
  event.preventDefault();
2196
- const groupId = handleElement.getAttribute("data-panel-group-id");
2230
+ const groupId = handleElement.getAttribute(DATA_ATTRIBUTES.groupId);
2197
2231
  assert(groupId, `No group element found for id "${groupId}"`);
2198
2232
  const handles = getResizeHandleElementsForGroup(groupId, panelGroupElement);
2199
2233
  const index = getResizeHandleElementIndex(groupId, handleId, panelGroupElement);
@@ -2288,54 +2322,54 @@ function PanelResizeHandle({
2288
2322
  assert(element, "Element ref not attached");
2289
2323
  let didMove = false;
2290
2324
  const setResizeHandlerState = (action, isActive, event) => {
2291
- if (isActive) {
2292
- switch (action) {
2293
- case "down":
2294
- {
2295
- setState("drag");
2296
- didMove = false;
2297
- assert(event, 'Expected event to be defined for "down" action');
2298
- startDragging(resizeHandleId, event);
2299
- const {
2300
- onDragging,
2301
- onPointerDown
2302
- } = callbacksRef.current;
2303
- onDragging === null || onDragging === void 0 ? void 0 : onDragging(true);
2304
- onPointerDown === null || onPointerDown === void 0 ? void 0 : onPointerDown();
2305
- break;
2306
- }
2307
- case "move":
2308
- {
2309
- const {
2310
- state
2311
- } = committedValuesRef.current;
2312
- didMove = true;
2313
- if (state !== "drag") {
2314
- setState("hover");
2315
- }
2316
- assert(event, 'Expected event to be defined for "move" action');
2317
- resizeHandler(event);
2318
- break;
2319
- }
2320
- case "up":
2321
- {
2325
+ if (!isActive) {
2326
+ setState("inactive");
2327
+ return;
2328
+ }
2329
+ switch (action) {
2330
+ case "down":
2331
+ {
2332
+ setState("drag");
2333
+ didMove = false;
2334
+ assert(event, 'Expected event to be defined for "down" action');
2335
+ startDragging(resizeHandleId, event);
2336
+ const {
2337
+ onDragging,
2338
+ onPointerDown
2339
+ } = callbacksRef.current;
2340
+ onDragging === null || onDragging === void 0 ? void 0 : onDragging(true);
2341
+ onPointerDown === null || onPointerDown === void 0 ? void 0 : onPointerDown();
2342
+ break;
2343
+ }
2344
+ case "move":
2345
+ {
2346
+ const {
2347
+ state
2348
+ } = committedValuesRef.current;
2349
+ didMove = true;
2350
+ if (state !== "drag") {
2322
2351
  setState("hover");
2323
- stopDragging();
2324
- const {
2325
- onClick,
2326
- onDragging,
2327
- onPointerUp
2328
- } = callbacksRef.current;
2329
- onDragging === null || onDragging === void 0 ? void 0 : onDragging(false);
2330
- onPointerUp === null || onPointerUp === void 0 ? void 0 : onPointerUp();
2331
- if (!didMove) {
2332
- onClick === null || onClick === void 0 ? void 0 : onClick();
2333
- }
2334
- break;
2335
2352
  }
2336
- }
2337
- } else {
2338
- setState("inactive");
2353
+ assert(event, 'Expected event to be defined for "move" action');
2354
+ resizeHandler(event);
2355
+ break;
2356
+ }
2357
+ case "up":
2358
+ {
2359
+ setState("hover");
2360
+ stopDragging();
2361
+ const {
2362
+ onClick,
2363
+ onDragging,
2364
+ onPointerUp
2365
+ } = callbacksRef.current;
2366
+ onDragging === null || onDragging === void 0 ? void 0 : onDragging(false);
2367
+ onPointerUp === null || onPointerUp === void 0 ? void 0 : onPointerUp();
2368
+ if (!didMove) {
2369
+ onClick === null || onClick === void 0 ? void 0 : onClick();
2370
+ }
2371
+ break;
2372
+ }
2339
2373
  }
2340
2374
  };
2341
2375
  return registerResizeHandle(resizeHandleId, element, direction, {
@@ -2374,17 +2408,25 @@ function PanelResizeHandle({
2374
2408
  },
2375
2409
  tabIndex,
2376
2410
  // CSS selectors
2377
- "data-panel-group-direction": direction,
2378
- "data-panel-group-id": groupId,
2379
- "data-resize-handle": "",
2380
- "data-resize-handle-active": state === "drag" ? "pointer" : isFocused ? "keyboard" : undefined,
2381
- "data-resize-handle-state": state,
2382
- "data-panel-resize-handle-enabled": !disabled,
2383
- "data-panel-resize-handle-id": resizeHandleId
2411
+ [DATA_ATTRIBUTES.groupDirection]: direction,
2412
+ [DATA_ATTRIBUTES.groupId]: groupId,
2413
+ [DATA_ATTRIBUTES.resizeHandle]: "",
2414
+ [DATA_ATTRIBUTES.resizeHandleActive]: state === "drag" ? "pointer" : isFocused ? "keyboard" : undefined,
2415
+ [DATA_ATTRIBUTES.resizeHandleEnabled]: !disabled,
2416
+ [DATA_ATTRIBUTES.resizeHandleId]: resizeHandleId,
2417
+ [DATA_ATTRIBUTES.resizeHandleState]: state
2384
2418
  });
2385
2419
  }
2386
2420
  PanelResizeHandle.displayName = "PanelResizeHandle";
2387
2421
 
2422
+ function usePanelGroupContext() {
2423
+ const context = useContext(PanelGroupContext);
2424
+ return {
2425
+ direction: context === null || context === void 0 ? void 0 : context.direction,
2426
+ groupId: context === null || context === void 0 ? void 0 : context.groupId
2427
+ };
2428
+ }
2429
+
2388
2430
  function getPanelElement(id, scope = document) {
2389
2431
  const element = scope.querySelector(`[data-panel-id="${id}"]`);
2390
2432
  if (element) {
@@ -2414,4 +2456,4 @@ function getIntersectingRectangle(rectOne, rectTwo, strict) {
2414
2456
  };
2415
2457
  }
2416
2458
 
2417
- export { Panel, PanelGroup, PanelResizeHandle, assert, disableGlobalCursorStyles, enableGlobalCursorStyles, getIntersectingRectangle, getPanelElement, getPanelElementsForGroup, getPanelGroupElement, getResizeHandleElement, getResizeHandleElementIndex, getResizeHandleElementsForGroup, getResizeHandlePanelIds, intersects, setNonce };
2459
+ export { DATA_ATTRIBUTES, Panel, PanelGroup, PanelResizeHandle, assert, disableGlobalCursorStyles, enableGlobalCursorStyles, getIntersectingRectangle, getPanelElement, getPanelElementsForGroup, getPanelGroupElement, getResizeHandleElement, getResizeHandleElementIndex, getResizeHandleElementsForGroup, getResizeHandlePanelIds, intersects, setNonce, usePanelGroupContext };