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()];
@@ -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,42 @@ 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
+ window.addEventListener("pointerup", handlePointerUp, options);
683
+ window.addEventListener("pointercancel", handlePointerUp, options);
684
+ } else {
685
+ ownerDocumentCounts.forEach((count, ownerDocument) => {
686
+ const {
687
+ body
688
+ } = ownerDocument;
689
+ if (count > 0) {
690
+ body.addEventListener("pointerdown", handlePointerDown, options);
691
+ body.addEventListener("pointermove", handlePointerMove, options);
692
+ }
693
+ });
663
694
  }
664
695
  }
665
696
  function updateResizeHandlerStates(action, event) {
@@ -684,8 +715,6 @@ function assert(expectedCondition, message) {
684
715
  }
685
716
  }
686
717
 
687
- const PRECISION = 10;
688
-
689
718
  function fuzzyCompareNumbers(actual, expected, fractionDigits = PRECISION) {
690
719
  if (actual.toFixed(fractionDigits) === expected.toFixed(fractionDigits)) {
691
720
  return 0;
@@ -1029,12 +1058,12 @@ function calculateAriaValues({
1029
1058
  }
1030
1059
 
1031
1060
  function getResizeHandleElementsForGroup(groupId, scope = document) {
1032
- return Array.from(scope.querySelectorAll(`[data-panel-resize-handle-id][data-panel-group-id="${groupId}"]`));
1061
+ return Array.from(scope.querySelectorAll(`[${DATA_ATTRIBUTES.resizeHandleId}][data-panel-group-id="${groupId}"]`));
1033
1062
  }
1034
1063
 
1035
1064
  function getResizeHandleElementIndex(groupId, id, scope = document) {
1036
1065
  const handles = getResizeHandleElementsForGroup(groupId, scope);
1037
- const index = handles.findIndex(handle => handle.getAttribute("data-panel-resize-handle-id") === id);
1066
+ const index = handles.findIndex(handle => handle.getAttribute(DATA_ATTRIBUTES.resizeHandleId) === id);
1038
1067
  return index !== null && index !== void 0 ? index : null;
1039
1068
  }
1040
1069
 
@@ -1059,7 +1088,7 @@ function getPanelGroupElement(id, rootElement = document) {
1059
1088
  }
1060
1089
 
1061
1090
  function getResizeHandleElement(id, scope = document) {
1062
- const element = scope.querySelector(`[data-panel-resize-handle-id="${id}"]`);
1091
+ const element = scope.querySelector(`[${DATA_ATTRIBUTES.resizeHandleId}="${id}"]`);
1063
1092
  if (element) {
1064
1093
  return element;
1065
1094
  }
@@ -1138,7 +1167,7 @@ function useWindowSplitterPanelGroupBehavior({
1138
1167
  const handles = getResizeHandleElementsForGroup(groupId, panelGroupElement);
1139
1168
  assert(handles, `No resize handles found for group id "${groupId}"`);
1140
1169
  const cleanupFunctions = handles.map(handle => {
1141
- const handleId = handle.getAttribute("data-panel-resize-handle-id");
1170
+ const handleId = handle.getAttribute(DATA_ATTRIBUTES.resizeHandleId);
1142
1171
  assert(handleId, `Resize handle element has no handle id attribute`);
1143
1172
  const [idBefore, idAfter] = getResizeHandlePanelIds(groupId, handleId, panelDataArray, panelGroupElement);
1144
1173
  if (idBefore == null || idAfter == null) {
@@ -1216,7 +1245,7 @@ function calculateDragOffsetPercentage(event, dragHandleId, direction, initialDr
1216
1245
  const isHorizontal = direction === "horizontal";
1217
1246
  const handleElement = getResizeHandleElement(dragHandleId, panelGroupElement);
1218
1247
  assert(handleElement, `No resize handle element found for id "${dragHandleId}"`);
1219
- const groupId = handleElement.getAttribute("data-panel-group-id");
1248
+ const groupId = handleElement.getAttribute(DATA_ATTRIBUTES.groupId);
1220
1249
  assert(groupId, `Resize handle element has no group id attribute`);
1221
1250
  let {
1222
1251
  initialCursorPosition
@@ -2132,9 +2161,9 @@ function PanelGroupWithForwardedRef({
2132
2161
  ...styleFromProps
2133
2162
  },
2134
2163
  // CSS selectors
2135
- "data-panel-group": "",
2136
- "data-panel-group-direction": direction,
2137
- "data-panel-group-id": groupId
2164
+ [DATA_ATTRIBUTES.group]: "",
2165
+ [DATA_ATTRIBUTES.groupDirection]: direction,
2166
+ [DATA_ATTRIBUTES.groupId]: groupId
2138
2167
  }));
2139
2168
  }
2140
2169
  const PanelGroup = forwardRef((props, ref) => createElement(PanelGroupWithForwardedRef, {
@@ -2193,7 +2222,7 @@ function useWindowSplitterResizeHandlerBehavior({
2193
2222
  case "F6":
2194
2223
  {
2195
2224
  event.preventDefault();
2196
- const groupId = handleElement.getAttribute("data-panel-group-id");
2225
+ const groupId = handleElement.getAttribute(DATA_ATTRIBUTES.groupId);
2197
2226
  assert(groupId, `No group element found for id "${groupId}"`);
2198
2227
  const handles = getResizeHandleElementsForGroup(groupId, panelGroupElement);
2199
2228
  const index = getResizeHandleElementIndex(groupId, handleId, panelGroupElement);
@@ -2288,54 +2317,54 @@ function PanelResizeHandle({
2288
2317
  assert(element, "Element ref not attached");
2289
2318
  let didMove = false;
2290
2319
  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
- {
2320
+ if (!isActive) {
2321
+ setState("inactive");
2322
+ return;
2323
+ }
2324
+ switch (action) {
2325
+ case "down":
2326
+ {
2327
+ setState("drag");
2328
+ didMove = false;
2329
+ assert(event, 'Expected event to be defined for "down" action');
2330
+ startDragging(resizeHandleId, event);
2331
+ const {
2332
+ onDragging,
2333
+ onPointerDown
2334
+ } = callbacksRef.current;
2335
+ onDragging === null || onDragging === void 0 ? void 0 : onDragging(true);
2336
+ onPointerDown === null || onPointerDown === void 0 ? void 0 : onPointerDown();
2337
+ break;
2338
+ }
2339
+ case "move":
2340
+ {
2341
+ const {
2342
+ state
2343
+ } = committedValuesRef.current;
2344
+ didMove = true;
2345
+ if (state !== "drag") {
2322
2346
  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
2347
  }
2336
- }
2337
- } else {
2338
- setState("inactive");
2348
+ assert(event, 'Expected event to be defined for "move" action');
2349
+ resizeHandler(event);
2350
+ break;
2351
+ }
2352
+ case "up":
2353
+ {
2354
+ setState("hover");
2355
+ stopDragging();
2356
+ const {
2357
+ onClick,
2358
+ onDragging,
2359
+ onPointerUp
2360
+ } = callbacksRef.current;
2361
+ onDragging === null || onDragging === void 0 ? void 0 : onDragging(false);
2362
+ onPointerUp === null || onPointerUp === void 0 ? void 0 : onPointerUp();
2363
+ if (!didMove) {
2364
+ onClick === null || onClick === void 0 ? void 0 : onClick();
2365
+ }
2366
+ break;
2367
+ }
2339
2368
  }
2340
2369
  };
2341
2370
  return registerResizeHandle(resizeHandleId, element, direction, {
@@ -2374,13 +2403,13 @@ function PanelResizeHandle({
2374
2403
  },
2375
2404
  tabIndex,
2376
2405
  // 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
2406
+ [DATA_ATTRIBUTES.groupDirection]: direction,
2407
+ [DATA_ATTRIBUTES.groupId]: groupId,
2408
+ [DATA_ATTRIBUTES.resizeHandle]: "",
2409
+ [DATA_ATTRIBUTES.resizeHandleActive]: state === "drag" ? "pointer" : isFocused ? "keyboard" : undefined,
2410
+ [DATA_ATTRIBUTES.resizeHandleEnabled]: !disabled,
2411
+ [DATA_ATTRIBUTES.resizeHandleId]: resizeHandleId,
2412
+ [DATA_ATTRIBUTES.resizeHandleState]: state
2384
2413
  });
2385
2414
  }
2386
2415
  PanelResizeHandle.displayName = "PanelResizeHandle";
@@ -2414,4 +2443,4 @@ function getIntersectingRectangle(rectOne, rectTwo, strict) {
2414
2443
  };
2415
2444
  }
2416
2445
 
2417
- export { Panel, PanelGroup, PanelResizeHandle, assert, disableGlobalCursorStyles, enableGlobalCursorStyles, getIntersectingRectangle, getPanelElement, getPanelElementsForGroup, getPanelGroupElement, getResizeHandleElement, getResizeHandleElementIndex, getResizeHandleElementsForGroup, getResizeHandlePanelIds, intersects, setNonce };
2446
+ export { DATA_ATTRIBUTES, Panel, PanelGroup, PanelResizeHandle, assert, disableGlobalCursorStyles, enableGlobalCursorStyles, getIntersectingRectangle, getPanelElement, getPanelElementsForGroup, getPanelGroupElement, getResizeHandleElement, getResizeHandleElementIndex, getResizeHandleElementsForGroup, getResizeHandlePanelIds, intersects, setNonce };