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 useId = React__namespace["useId".toString()];
33
49
  const wrappedUseId = typeof useId === "function" ? useId : () => null;
34
50
  let counter = 0;
@@ -141,11 +157,11 @@ function PanelWithForwardedRef({
141
157
  ...styleFromProps
142
158
  },
143
159
  // CSS selectors
144
- "data-panel": "",
145
- "data-panel-collapsible": collapsible || undefined,
146
- "data-panel-group-id": groupId,
147
- "data-panel-id": panelId,
148
- "data-panel-size": parseFloat("" + style.flexGrow).toFixed(1)
160
+ [DATA_ATTRIBUTES.groupId]: groupId,
161
+ [DATA_ATTRIBUTES.panel]: "",
162
+ [DATA_ATTRIBUTES.panelCollapsible]: collapsible || undefined,
163
+ [DATA_ATTRIBUTES.panelId]: panelId,
164
+ [DATA_ATTRIBUTES.panelSize]: parseFloat("" + style.flexGrow).toFixed(1)
149
165
  });
150
166
  }
151
167
  const Panel = React.forwardRef((props, ref) => React.createElement(PanelWithForwardedRef, {
@@ -165,6 +181,7 @@ function setNonce(value) {
165
181
 
166
182
  let currentCursorStyle = null;
167
183
  let enabled = true;
184
+ let prevRuleIndex = -1;
168
185
  let styleElement = null;
169
186
  function disableGlobalCursorStyles() {
170
187
  enabled = false;
@@ -214,9 +231,11 @@ function resetGlobalCursorStyle() {
214
231
  document.head.removeChild(styleElement);
215
232
  currentCursorStyle = null;
216
233
  styleElement = null;
234
+ prevRuleIndex = -1;
217
235
  }
218
236
  }
219
237
  function setGlobalCursorStyle(state, constraintFlags) {
238
+ var _styleElement$sheet$i, _styleElement$sheet2;
220
239
  if (!enabled) {
221
240
  return;
222
241
  }
@@ -233,7 +252,11 @@ function setGlobalCursorStyle(state, constraintFlags) {
233
252
  }
234
253
  document.head.appendChild(styleElement);
235
254
  }
236
- styleElement.innerHTML = `*{cursor: ${style}!important;}`;
255
+ if (prevRuleIndex >= 0) {
256
+ var _styleElement$sheet;
257
+ (_styleElement$sheet = styleElement.sheet) === null || _styleElement$sheet === void 0 ? void 0 : _styleElement$sheet.removeRule(prevRuleIndex);
258
+ }
259
+ 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;
237
260
  }
238
261
 
239
262
  function isKeyDown(event) {
@@ -464,7 +487,9 @@ function handlePointerDown(event) {
464
487
  if (intersectingHandles.length > 0) {
465
488
  updateResizeHandlerStates("down", event);
466
489
  event.preventDefault();
467
- event.stopPropagation();
490
+ if (!isWithinResizeHandle(target)) {
491
+ event.stopImmediatePropagation();
492
+ }
468
493
  }
469
494
  }
470
495
  function handlePointerMove(event) {
@@ -513,6 +538,9 @@ function handlePointerUp(event) {
513
538
  isPointerDown = false;
514
539
  if (intersectingHandles.length > 0) {
515
540
  event.preventDefault();
541
+ if (!isWithinResizeHandle(target)) {
542
+ event.stopImmediatePropagation();
543
+ }
516
544
  }
517
545
  updateResizeHandlerStates("up", event);
518
546
  recalculateIntersectingHandles({
@@ -523,6 +551,16 @@ function handlePointerUp(event) {
523
551
  updateCursor();
524
552
  updateListeners();
525
553
  }
554
+ function isWithinResizeHandle(element) {
555
+ let currentElement = element;
556
+ while (currentElement) {
557
+ if (currentElement.hasAttribute(DATA_ATTRIBUTES.resizeHandle)) {
558
+ return true;
559
+ }
560
+ currentElement = currentElement.parentElement;
561
+ }
562
+ return false;
563
+ }
526
564
  function recalculateIntersectingHandles({
527
565
  target,
528
566
  x,
@@ -614,49 +652,42 @@ function updateCursor() {
614
652
  resetGlobalCursorStyle();
615
653
  }
616
654
  }
655
+ let listenersAbortController = new AbortController();
617
656
  function updateListeners() {
618
- ownerDocumentCounts.forEach((_, ownerDocument) => {
619
- const {
620
- body
621
- } = ownerDocument;
622
- body.removeEventListener("contextmenu", handlePointerUp);
623
- body.removeEventListener("pointerdown", handlePointerDown, {
624
- capture: true
625
- });
626
- body.removeEventListener("pointerleave", handlePointerMove);
627
- body.removeEventListener("pointermove", handlePointerMove);
628
- });
629
- window.removeEventListener("pointerup", handlePointerUp);
630
- window.removeEventListener("pointercancel", handlePointerUp);
631
- if (registeredResizeHandlers.size > 0) {
632
- if (isPointerDown) {
633
- if (intersectingHandles.length > 0) {
634
- ownerDocumentCounts.forEach((count, ownerDocument) => {
635
- const {
636
- body
637
- } = ownerDocument;
638
- if (count > 0) {
639
- body.addEventListener("contextmenu", handlePointerUp);
640
- body.addEventListener("pointerleave", handlePointerMove);
641
- body.addEventListener("pointermove", handlePointerMove);
642
- }
643
- });
644
- }
645
- window.addEventListener("pointerup", handlePointerUp);
646
- window.addEventListener("pointercancel", handlePointerUp);
647
- } else {
657
+ listenersAbortController.abort();
658
+ listenersAbortController = new AbortController();
659
+ const options = {
660
+ capture: true,
661
+ signal: listenersAbortController.signal
662
+ };
663
+ if (!registeredResizeHandlers.size) {
664
+ return;
665
+ }
666
+ if (isPointerDown) {
667
+ if (intersectingHandles.length > 0) {
648
668
  ownerDocumentCounts.forEach((count, ownerDocument) => {
649
669
  const {
650
670
  body
651
671
  } = ownerDocument;
652
672
  if (count > 0) {
653
- body.addEventListener("pointerdown", handlePointerDown, {
654
- capture: true
655
- });
656
- body.addEventListener("pointermove", handlePointerMove);
673
+ body.addEventListener("contextmenu", handlePointerUp, options);
674
+ body.addEventListener("pointerleave", handlePointerMove, options);
675
+ body.addEventListener("pointermove", handlePointerMove, options);
657
676
  }
658
677
  });
659
678
  }
679
+ window.addEventListener("pointerup", handlePointerUp, options);
680
+ window.addEventListener("pointercancel", handlePointerUp, options);
681
+ } else {
682
+ ownerDocumentCounts.forEach((count, ownerDocument) => {
683
+ const {
684
+ body
685
+ } = ownerDocument;
686
+ if (count > 0) {
687
+ body.addEventListener("pointerdown", handlePointerDown, options);
688
+ body.addEventListener("pointermove", handlePointerMove, options);
689
+ }
690
+ });
660
691
  }
661
692
  }
662
693
  function updateResizeHandlerStates(action, event) {
@@ -681,8 +712,6 @@ function assert(expectedCondition, message) {
681
712
  }
682
713
  }
683
714
 
684
- const PRECISION = 10;
685
-
686
715
  function fuzzyCompareNumbers(actual, expected, fractionDigits = PRECISION) {
687
716
  if (actual.toFixed(fractionDigits) === expected.toFixed(fractionDigits)) {
688
717
  return 0;
@@ -987,12 +1016,12 @@ function adjustLayoutByDelta({
987
1016
  }
988
1017
 
989
1018
  function getResizeHandleElementsForGroup(groupId, scope = document) {
990
- return Array.from(scope.querySelectorAll(`[data-panel-resize-handle-id][data-panel-group-id="${groupId}"]`));
1019
+ return Array.from(scope.querySelectorAll(`[${DATA_ATTRIBUTES.resizeHandleId}][data-panel-group-id="${groupId}"]`));
991
1020
  }
992
1021
 
993
1022
  function getResizeHandleElementIndex(groupId, id, scope = document) {
994
1023
  const handles = getResizeHandleElementsForGroup(groupId, scope);
995
- const index = handles.findIndex(handle => handle.getAttribute("data-panel-resize-handle-id") === id);
1024
+ const index = handles.findIndex(handle => handle.getAttribute(DATA_ATTRIBUTES.resizeHandleId) === id);
996
1025
  return index !== null && index !== void 0 ? index : null;
997
1026
  }
998
1027
 
@@ -1017,7 +1046,7 @@ function getPanelGroupElement(id, rootElement = document) {
1017
1046
  }
1018
1047
 
1019
1048
  function getResizeHandleElement(id, scope = document) {
1020
- const element = scope.querySelector(`[data-panel-resize-handle-id="${id}"]`);
1049
+ const element = scope.querySelector(`[${DATA_ATTRIBUTES.resizeHandleId}="${id}"]`);
1021
1050
  if (element) {
1022
1051
  return element;
1023
1052
  }
@@ -1062,7 +1091,7 @@ function useWindowSplitterPanelGroupBehavior({
1062
1091
  const handles = getResizeHandleElementsForGroup(groupId, panelGroupElement);
1063
1092
  assert(handles, `No resize handles found for group id "${groupId}"`);
1064
1093
  const cleanupFunctions = handles.map(handle => {
1065
- const handleId = handle.getAttribute("data-panel-resize-handle-id");
1094
+ const handleId = handle.getAttribute(DATA_ATTRIBUTES.resizeHandleId);
1066
1095
  assert(handleId, `Resize handle element has no handle id attribute`);
1067
1096
  const [idBefore, idAfter] = getResizeHandlePanelIds(groupId, handleId, panelDataArray, panelGroupElement);
1068
1097
  if (idBefore == null || idAfter == null) {
@@ -1140,7 +1169,7 @@ function calculateDragOffsetPercentage(event, dragHandleId, direction, initialDr
1140
1169
  const isHorizontal = direction === "horizontal";
1141
1170
  const handleElement = getResizeHandleElement(dragHandleId, panelGroupElement);
1142
1171
  assert(handleElement, `No resize handle element found for id "${dragHandleId}"`);
1143
- const groupId = handleElement.getAttribute("data-panel-group-id");
1172
+ const groupId = handleElement.getAttribute(DATA_ATTRIBUTES.groupId);
1144
1173
  assert(groupId, `Resize handle element has no group id attribute`);
1145
1174
  let {
1146
1175
  initialCursorPosition
@@ -2035,9 +2064,9 @@ function PanelGroupWithForwardedRef({
2035
2064
  ...styleFromProps
2036
2065
  },
2037
2066
  // CSS selectors
2038
- "data-panel-group": "",
2039
- "data-panel-group-direction": direction,
2040
- "data-panel-group-id": groupId
2067
+ [DATA_ATTRIBUTES.group]: "",
2068
+ [DATA_ATTRIBUTES.groupDirection]: direction,
2069
+ [DATA_ATTRIBUTES.groupId]: groupId
2041
2070
  }));
2042
2071
  }
2043
2072
  const PanelGroup = React.forwardRef((props, ref) => React.createElement(PanelGroupWithForwardedRef, {
@@ -2096,7 +2125,7 @@ function useWindowSplitterResizeHandlerBehavior({
2096
2125
  case "F6":
2097
2126
  {
2098
2127
  event.preventDefault();
2099
- const groupId = handleElement.getAttribute("data-panel-group-id");
2128
+ const groupId = handleElement.getAttribute(DATA_ATTRIBUTES.groupId);
2100
2129
  assert(groupId, `No group element found for id "${groupId}"`);
2101
2130
  const handles = getResizeHandleElementsForGroup(groupId, panelGroupElement);
2102
2131
  const index = getResizeHandleElementIndex(groupId, handleId, panelGroupElement);
@@ -2188,54 +2217,54 @@ function PanelResizeHandle({
2188
2217
  assert(element, "Element ref not attached");
2189
2218
  let didMove = false;
2190
2219
  const setResizeHandlerState = (action, isActive, event) => {
2191
- if (isActive) {
2192
- switch (action) {
2193
- case "down":
2194
- {
2195
- setState("drag");
2196
- didMove = false;
2197
- assert(event, 'Expected event to be defined for "down" action');
2198
- startDragging(resizeHandleId, event);
2199
- const {
2200
- onDragging,
2201
- onPointerDown
2202
- } = callbacksRef.current;
2203
- onDragging === null || onDragging === void 0 ? void 0 : onDragging(true);
2204
- onPointerDown === null || onPointerDown === void 0 ? void 0 : onPointerDown();
2205
- break;
2206
- }
2207
- case "move":
2208
- {
2209
- const {
2210
- state
2211
- } = committedValuesRef.current;
2212
- didMove = true;
2213
- if (state !== "drag") {
2214
- setState("hover");
2215
- }
2216
- assert(event, 'Expected event to be defined for "move" action');
2217
- resizeHandler(event);
2218
- break;
2219
- }
2220
- case "up":
2221
- {
2220
+ if (!isActive) {
2221
+ setState("inactive");
2222
+ return;
2223
+ }
2224
+ switch (action) {
2225
+ case "down":
2226
+ {
2227
+ setState("drag");
2228
+ didMove = false;
2229
+ assert(event, 'Expected event to be defined for "down" action');
2230
+ startDragging(resizeHandleId, event);
2231
+ const {
2232
+ onDragging,
2233
+ onPointerDown
2234
+ } = callbacksRef.current;
2235
+ onDragging === null || onDragging === void 0 ? void 0 : onDragging(true);
2236
+ onPointerDown === null || onPointerDown === void 0 ? void 0 : onPointerDown();
2237
+ break;
2238
+ }
2239
+ case "move":
2240
+ {
2241
+ const {
2242
+ state
2243
+ } = committedValuesRef.current;
2244
+ didMove = true;
2245
+ if (state !== "drag") {
2222
2246
  setState("hover");
2223
- stopDragging();
2224
- const {
2225
- onClick,
2226
- onDragging,
2227
- onPointerUp
2228
- } = callbacksRef.current;
2229
- onDragging === null || onDragging === void 0 ? void 0 : onDragging(false);
2230
- onPointerUp === null || onPointerUp === void 0 ? void 0 : onPointerUp();
2231
- if (!didMove) {
2232
- onClick === null || onClick === void 0 ? void 0 : onClick();
2233
- }
2234
- break;
2235
2247
  }
2236
- }
2237
- } else {
2238
- setState("inactive");
2248
+ assert(event, 'Expected event to be defined for "move" action');
2249
+ resizeHandler(event);
2250
+ break;
2251
+ }
2252
+ case "up":
2253
+ {
2254
+ setState("hover");
2255
+ stopDragging();
2256
+ const {
2257
+ onClick,
2258
+ onDragging,
2259
+ onPointerUp
2260
+ } = callbacksRef.current;
2261
+ onDragging === null || onDragging === void 0 ? void 0 : onDragging(false);
2262
+ onPointerUp === null || onPointerUp === void 0 ? void 0 : onPointerUp();
2263
+ if (!didMove) {
2264
+ onClick === null || onClick === void 0 ? void 0 : onClick();
2265
+ }
2266
+ break;
2267
+ }
2239
2268
  }
2240
2269
  };
2241
2270
  return registerResizeHandle(resizeHandleId, element, direction, {
@@ -2274,13 +2303,13 @@ function PanelResizeHandle({
2274
2303
  },
2275
2304
  tabIndex,
2276
2305
  // CSS selectors
2277
- "data-panel-group-direction": direction,
2278
- "data-panel-group-id": groupId,
2279
- "data-resize-handle": "",
2280
- "data-resize-handle-active": state === "drag" ? "pointer" : isFocused ? "keyboard" : undefined,
2281
- "data-resize-handle-state": state,
2282
- "data-panel-resize-handle-enabled": !disabled,
2283
- "data-panel-resize-handle-id": resizeHandleId
2306
+ [DATA_ATTRIBUTES.groupDirection]: direction,
2307
+ [DATA_ATTRIBUTES.groupId]: groupId,
2308
+ [DATA_ATTRIBUTES.resizeHandle]: "",
2309
+ [DATA_ATTRIBUTES.resizeHandleActive]: state === "drag" ? "pointer" : isFocused ? "keyboard" : undefined,
2310
+ [DATA_ATTRIBUTES.resizeHandleEnabled]: !disabled,
2311
+ [DATA_ATTRIBUTES.resizeHandleId]: resizeHandleId,
2312
+ [DATA_ATTRIBUTES.resizeHandleState]: state
2284
2313
  });
2285
2314
  }
2286
2315
  PanelResizeHandle.displayName = "PanelResizeHandle";
@@ -2314,6 +2343,7 @@ function getIntersectingRectangle(rectOne, rectTwo, strict) {
2314
2343
  };
2315
2344
  }
2316
2345
 
2346
+ exports.DATA_ATTRIBUTES = DATA_ATTRIBUTES;
2317
2347
  exports.Panel = Panel;
2318
2348
  exports.PanelGroup = PanelGroup;
2319
2349
  exports.PanelResizeHandle = PanelResizeHandle;
@@ -1,4 +1,5 @@
1
1
  export {
2
+ DATA_ATTRIBUTES,
2
3
  Panel,
3
4
  PanelGroup,
4
5
  PanelResizeHandle,