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;
@@ -130,11 +146,11 @@ function PanelWithForwardedRef({
130
146
  ...styleFromProps
131
147
  },
132
148
  // CSS selectors
133
- "data-panel": "",
134
- "data-panel-collapsible": collapsible || undefined,
135
- "data-panel-group-id": groupId,
136
- "data-panel-id": panelId,
137
- "data-panel-size": parseFloat("" + style.flexGrow).toFixed(1)
149
+ [DATA_ATTRIBUTES.groupId]: groupId,
150
+ [DATA_ATTRIBUTES.panel]: "",
151
+ [DATA_ATTRIBUTES.panelCollapsible]: collapsible || undefined,
152
+ [DATA_ATTRIBUTES.panelId]: panelId,
153
+ [DATA_ATTRIBUTES.panelSize]: parseFloat("" + style.flexGrow).toFixed(1)
138
154
  });
139
155
  }
140
156
  const Panel = React.forwardRef((props, ref) => React.createElement(PanelWithForwardedRef, {
@@ -154,6 +170,7 @@ function setNonce(value) {
154
170
 
155
171
  let currentCursorStyle = null;
156
172
  let enabled = true;
173
+ let prevRuleIndex = -1;
157
174
  let styleElement = null;
158
175
  function disableGlobalCursorStyles() {
159
176
  enabled = false;
@@ -203,9 +220,11 @@ function resetGlobalCursorStyle() {
203
220
  document.head.removeChild(styleElement);
204
221
  currentCursorStyle = null;
205
222
  styleElement = null;
223
+ prevRuleIndex = -1;
206
224
  }
207
225
  }
208
226
  function setGlobalCursorStyle(state, constraintFlags) {
227
+ var _styleElement$sheet$i, _styleElement$sheet2;
209
228
  if (!enabled) {
210
229
  return;
211
230
  }
@@ -222,7 +241,11 @@ function setGlobalCursorStyle(state, constraintFlags) {
222
241
  }
223
242
  document.head.appendChild(styleElement);
224
243
  }
225
- styleElement.innerHTML = `*{cursor: ${style}!important;}`;
244
+ if (prevRuleIndex >= 0) {
245
+ var _styleElement$sheet;
246
+ (_styleElement$sheet = styleElement.sheet) === null || _styleElement$sheet === void 0 ? void 0 : _styleElement$sheet.removeRule(prevRuleIndex);
247
+ }
248
+ 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;
226
249
  }
227
250
 
228
251
  function isKeyDown(event) {
@@ -453,7 +476,9 @@ function handlePointerDown(event) {
453
476
  if (intersectingHandles.length > 0) {
454
477
  updateResizeHandlerStates("down", event);
455
478
  event.preventDefault();
456
- event.stopPropagation();
479
+ if (!isWithinResizeHandle(target)) {
480
+ event.stopImmediatePropagation();
481
+ }
457
482
  }
458
483
  }
459
484
  function handlePointerMove(event) {
@@ -502,6 +527,9 @@ function handlePointerUp(event) {
502
527
  isPointerDown = false;
503
528
  if (intersectingHandles.length > 0) {
504
529
  event.preventDefault();
530
+ if (!isWithinResizeHandle(target)) {
531
+ event.stopImmediatePropagation();
532
+ }
505
533
  }
506
534
  updateResizeHandlerStates("up", event);
507
535
  recalculateIntersectingHandles({
@@ -512,6 +540,16 @@ function handlePointerUp(event) {
512
540
  updateCursor();
513
541
  updateListeners();
514
542
  }
543
+ function isWithinResizeHandle(element) {
544
+ let currentElement = element;
545
+ while (currentElement) {
546
+ if (currentElement.hasAttribute(DATA_ATTRIBUTES.resizeHandle)) {
547
+ return true;
548
+ }
549
+ currentElement = currentElement.parentElement;
550
+ }
551
+ return false;
552
+ }
515
553
  function recalculateIntersectingHandles({
516
554
  target,
517
555
  x,
@@ -603,49 +641,42 @@ function updateCursor() {
603
641
  resetGlobalCursorStyle();
604
642
  }
605
643
  }
644
+ let listenersAbortController = new AbortController();
606
645
  function updateListeners() {
607
- ownerDocumentCounts.forEach((_, ownerDocument) => {
608
- const {
609
- body
610
- } = ownerDocument;
611
- body.removeEventListener("contextmenu", handlePointerUp);
612
- body.removeEventListener("pointerdown", handlePointerDown, {
613
- capture: true
614
- });
615
- body.removeEventListener("pointerleave", handlePointerMove);
616
- body.removeEventListener("pointermove", handlePointerMove);
617
- });
618
- window.removeEventListener("pointerup", handlePointerUp);
619
- window.removeEventListener("pointercancel", handlePointerUp);
620
- if (registeredResizeHandlers.size > 0) {
621
- if (isPointerDown) {
622
- if (intersectingHandles.length > 0) {
623
- ownerDocumentCounts.forEach((count, ownerDocument) => {
624
- const {
625
- body
626
- } = ownerDocument;
627
- if (count > 0) {
628
- body.addEventListener("contextmenu", handlePointerUp);
629
- body.addEventListener("pointerleave", handlePointerMove);
630
- body.addEventListener("pointermove", handlePointerMove);
631
- }
632
- });
633
- }
634
- window.addEventListener("pointerup", handlePointerUp);
635
- window.addEventListener("pointercancel", handlePointerUp);
636
- } else {
646
+ listenersAbortController.abort();
647
+ listenersAbortController = new AbortController();
648
+ const options = {
649
+ capture: true,
650
+ signal: listenersAbortController.signal
651
+ };
652
+ if (!registeredResizeHandlers.size) {
653
+ return;
654
+ }
655
+ if (isPointerDown) {
656
+ if (intersectingHandles.length > 0) {
637
657
  ownerDocumentCounts.forEach((count, ownerDocument) => {
638
658
  const {
639
659
  body
640
660
  } = ownerDocument;
641
661
  if (count > 0) {
642
- body.addEventListener("pointerdown", handlePointerDown, {
643
- capture: true
644
- });
645
- body.addEventListener("pointermove", handlePointerMove);
662
+ body.addEventListener("contextmenu", handlePointerUp, options);
663
+ body.addEventListener("pointerleave", handlePointerMove, options);
664
+ body.addEventListener("pointermove", handlePointerMove, options);
646
665
  }
647
666
  });
648
667
  }
668
+ window.addEventListener("pointerup", handlePointerUp, options);
669
+ window.addEventListener("pointercancel", handlePointerUp, options);
670
+ } else {
671
+ ownerDocumentCounts.forEach((count, ownerDocument) => {
672
+ const {
673
+ body
674
+ } = ownerDocument;
675
+ if (count > 0) {
676
+ body.addEventListener("pointerdown", handlePointerDown, options);
677
+ body.addEventListener("pointermove", handlePointerMove, options);
678
+ }
679
+ });
649
680
  }
650
681
  }
651
682
  function updateResizeHandlerStates(action, event) {
@@ -670,8 +701,6 @@ function assert(expectedCondition, message) {
670
701
  }
671
702
  }
672
703
 
673
- const PRECISION = 10;
674
-
675
704
  function fuzzyCompareNumbers(actual, expected, fractionDigits = PRECISION) {
676
705
  if (actual.toFixed(fractionDigits) === expected.toFixed(fractionDigits)) {
677
706
  return 0;
@@ -976,12 +1005,12 @@ function adjustLayoutByDelta({
976
1005
  }
977
1006
 
978
1007
  function getResizeHandleElementsForGroup(groupId, scope = document) {
979
- return Array.from(scope.querySelectorAll(`[data-panel-resize-handle-id][data-panel-group-id="${groupId}"]`));
1008
+ return Array.from(scope.querySelectorAll(`[${DATA_ATTRIBUTES.resizeHandleId}][data-panel-group-id="${groupId}"]`));
980
1009
  }
981
1010
 
982
1011
  function getResizeHandleElementIndex(groupId, id, scope = document) {
983
1012
  const handles = getResizeHandleElementsForGroup(groupId, scope);
984
- const index = handles.findIndex(handle => handle.getAttribute("data-panel-resize-handle-id") === id);
1013
+ const index = handles.findIndex(handle => handle.getAttribute(DATA_ATTRIBUTES.resizeHandleId) === id);
985
1014
  return index !== null && index !== void 0 ? index : null;
986
1015
  }
987
1016
 
@@ -1006,7 +1035,7 @@ function getPanelGroupElement(id, rootElement = document) {
1006
1035
  }
1007
1036
 
1008
1037
  function getResizeHandleElement(id, scope = document) {
1009
- const element = scope.querySelector(`[data-panel-resize-handle-id="${id}"]`);
1038
+ const element = scope.querySelector(`[${DATA_ATTRIBUTES.resizeHandleId}="${id}"]`);
1010
1039
  if (element) {
1011
1040
  return element;
1012
1041
  }
@@ -1051,7 +1080,7 @@ function useWindowSplitterPanelGroupBehavior({
1051
1080
  const handles = getResizeHandleElementsForGroup(groupId, panelGroupElement);
1052
1081
  assert(handles, `No resize handles found for group id "${groupId}"`);
1053
1082
  const cleanupFunctions = handles.map(handle => {
1054
- const handleId = handle.getAttribute("data-panel-resize-handle-id");
1083
+ const handleId = handle.getAttribute(DATA_ATTRIBUTES.resizeHandleId);
1055
1084
  assert(handleId, `Resize handle element has no handle id attribute`);
1056
1085
  const [idBefore, idAfter] = getResizeHandlePanelIds(groupId, handleId, panelDataArray, panelGroupElement);
1057
1086
  if (idBefore == null || idAfter == null) {
@@ -1129,7 +1158,7 @@ function calculateDragOffsetPercentage(event, dragHandleId, direction, initialDr
1129
1158
  const isHorizontal = direction === "horizontal";
1130
1159
  const handleElement = getResizeHandleElement(dragHandleId, panelGroupElement);
1131
1160
  assert(handleElement, `No resize handle element found for id "${dragHandleId}"`);
1132
- const groupId = handleElement.getAttribute("data-panel-group-id");
1161
+ const groupId = handleElement.getAttribute(DATA_ATTRIBUTES.groupId);
1133
1162
  assert(groupId, `Resize handle element has no group id attribute`);
1134
1163
  let {
1135
1164
  initialCursorPosition
@@ -1934,9 +1963,9 @@ function PanelGroupWithForwardedRef({
1934
1963
  ...styleFromProps
1935
1964
  },
1936
1965
  // CSS selectors
1937
- "data-panel-group": "",
1938
- "data-panel-group-direction": direction,
1939
- "data-panel-group-id": groupId
1966
+ [DATA_ATTRIBUTES.group]: "",
1967
+ [DATA_ATTRIBUTES.groupDirection]: direction,
1968
+ [DATA_ATTRIBUTES.groupId]: groupId
1940
1969
  }));
1941
1970
  }
1942
1971
  const PanelGroup = React.forwardRef((props, ref) => React.createElement(PanelGroupWithForwardedRef, {
@@ -1995,7 +2024,7 @@ function useWindowSplitterResizeHandlerBehavior({
1995
2024
  case "F6":
1996
2025
  {
1997
2026
  event.preventDefault();
1998
- const groupId = handleElement.getAttribute("data-panel-group-id");
2027
+ const groupId = handleElement.getAttribute(DATA_ATTRIBUTES.groupId);
1999
2028
  assert(groupId, `No group element found for id "${groupId}"`);
2000
2029
  const handles = getResizeHandleElementsForGroup(groupId, panelGroupElement);
2001
2030
  const index = getResizeHandleElementIndex(groupId, handleId, panelGroupElement);
@@ -2087,54 +2116,54 @@ function PanelResizeHandle({
2087
2116
  assert(element, "Element ref not attached");
2088
2117
  let didMove = false;
2089
2118
  const setResizeHandlerState = (action, isActive, event) => {
2090
- if (isActive) {
2091
- switch (action) {
2092
- case "down":
2093
- {
2094
- setState("drag");
2095
- didMove = false;
2096
- assert(event, 'Expected event to be defined for "down" action');
2097
- startDragging(resizeHandleId, event);
2098
- const {
2099
- onDragging,
2100
- onPointerDown
2101
- } = callbacksRef.current;
2102
- onDragging === null || onDragging === void 0 ? void 0 : onDragging(true);
2103
- onPointerDown === null || onPointerDown === void 0 ? void 0 : onPointerDown();
2104
- break;
2105
- }
2106
- case "move":
2107
- {
2108
- const {
2109
- state
2110
- } = committedValuesRef.current;
2111
- didMove = true;
2112
- if (state !== "drag") {
2113
- setState("hover");
2114
- }
2115
- assert(event, 'Expected event to be defined for "move" action');
2116
- resizeHandler(event);
2117
- break;
2118
- }
2119
- case "up":
2120
- {
2119
+ if (!isActive) {
2120
+ setState("inactive");
2121
+ return;
2122
+ }
2123
+ switch (action) {
2124
+ case "down":
2125
+ {
2126
+ setState("drag");
2127
+ didMove = false;
2128
+ assert(event, 'Expected event to be defined for "down" action');
2129
+ startDragging(resizeHandleId, event);
2130
+ const {
2131
+ onDragging,
2132
+ onPointerDown
2133
+ } = callbacksRef.current;
2134
+ onDragging === null || onDragging === void 0 ? void 0 : onDragging(true);
2135
+ onPointerDown === null || onPointerDown === void 0 ? void 0 : onPointerDown();
2136
+ break;
2137
+ }
2138
+ case "move":
2139
+ {
2140
+ const {
2141
+ state
2142
+ } = committedValuesRef.current;
2143
+ didMove = true;
2144
+ if (state !== "drag") {
2121
2145
  setState("hover");
2122
- stopDragging();
2123
- const {
2124
- onClick,
2125
- onDragging,
2126
- onPointerUp
2127
- } = callbacksRef.current;
2128
- onDragging === null || onDragging === void 0 ? void 0 : onDragging(false);
2129
- onPointerUp === null || onPointerUp === void 0 ? void 0 : onPointerUp();
2130
- if (!didMove) {
2131
- onClick === null || onClick === void 0 ? void 0 : onClick();
2132
- }
2133
- break;
2134
2146
  }
2135
- }
2136
- } else {
2137
- setState("inactive");
2147
+ assert(event, 'Expected event to be defined for "move" action');
2148
+ resizeHandler(event);
2149
+ break;
2150
+ }
2151
+ case "up":
2152
+ {
2153
+ setState("hover");
2154
+ stopDragging();
2155
+ const {
2156
+ onClick,
2157
+ onDragging,
2158
+ onPointerUp
2159
+ } = callbacksRef.current;
2160
+ onDragging === null || onDragging === void 0 ? void 0 : onDragging(false);
2161
+ onPointerUp === null || onPointerUp === void 0 ? void 0 : onPointerUp();
2162
+ if (!didMove) {
2163
+ onClick === null || onClick === void 0 ? void 0 : onClick();
2164
+ }
2165
+ break;
2166
+ }
2138
2167
  }
2139
2168
  };
2140
2169
  return registerResizeHandle(resizeHandleId, element, direction, {
@@ -2173,13 +2202,13 @@ function PanelResizeHandle({
2173
2202
  },
2174
2203
  tabIndex,
2175
2204
  // CSS selectors
2176
- "data-panel-group-direction": direction,
2177
- "data-panel-group-id": groupId,
2178
- "data-resize-handle": "",
2179
- "data-resize-handle-active": state === "drag" ? "pointer" : isFocused ? "keyboard" : undefined,
2180
- "data-resize-handle-state": state,
2181
- "data-panel-resize-handle-enabled": !disabled,
2182
- "data-panel-resize-handle-id": resizeHandleId
2205
+ [DATA_ATTRIBUTES.groupDirection]: direction,
2206
+ [DATA_ATTRIBUTES.groupId]: groupId,
2207
+ [DATA_ATTRIBUTES.resizeHandle]: "",
2208
+ [DATA_ATTRIBUTES.resizeHandleActive]: state === "drag" ? "pointer" : isFocused ? "keyboard" : undefined,
2209
+ [DATA_ATTRIBUTES.resizeHandleEnabled]: !disabled,
2210
+ [DATA_ATTRIBUTES.resizeHandleId]: resizeHandleId,
2211
+ [DATA_ATTRIBUTES.resizeHandleState]: state
2183
2212
  });
2184
2213
  }
2185
2214
  PanelResizeHandle.displayName = "PanelResizeHandle";
@@ -2213,6 +2242,7 @@ function getIntersectingRectangle(rectOne, rectTwo, strict) {
2213
2242
  };
2214
2243
  }
2215
2244
 
2245
+ exports.DATA_ATTRIBUTES = DATA_ATTRIBUTES;
2216
2246
  exports.Panel = Panel;
2217
2247
  exports.PanelGroup = PanelGroup;
2218
2248
  exports.PanelResizeHandle = PanelResizeHandle;
@@ -1,4 +1,5 @@
1
1
  export {
2
+ DATA_ATTRIBUTES,
2
3
  Panel,
3
4
  PanelGroup,
4
5
  PanelResizeHandle,