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.
@@ -6,6 +6,22 @@ import { createContext, useRef, forwardRef, createElement, useContext, useImpera
6
6
  const PanelGroupContext = createContext(null);
7
7
  PanelGroupContext.displayName = "PanelGroupContext";
8
8
 
9
+ const DATA_ATTRIBUTES = {
10
+ group: "data-panel-group",
11
+ groupDirection: "data-panel-group-direction",
12
+ groupId: "data-panel-group-id",
13
+ panel: "data-panel",
14
+ panelCollapsible: "data-panel-collapsible",
15
+ panelId: "data-panel-id",
16
+ panelSize: "data-panel-size",
17
+ resizeHandle: "data-resize-handle",
18
+ resizeHandleActive: "data-resize-handle-active",
19
+ resizeHandleEnabled: "data-panel-resize-handle-enabled",
20
+ resizeHandleId: "data-panel-resize-handle-id",
21
+ resizeHandleState: "data-resize-handle-state"
22
+ };
23
+ const PRECISION = 10;
24
+
9
25
  const useId = React["useId".toString()];
10
26
  const wrappedUseId = typeof useId === "function" ? useId : () => null;
11
27
  let counter = 0;
@@ -118,11 +134,11 @@ function PanelWithForwardedRef({
118
134
  ...styleFromProps
119
135
  },
120
136
  // CSS selectors
121
- "data-panel": "",
122
- "data-panel-collapsible": collapsible || undefined,
123
- "data-panel-group-id": groupId,
124
- "data-panel-id": panelId,
125
- "data-panel-size": parseFloat("" + style.flexGrow).toFixed(1)
137
+ [DATA_ATTRIBUTES.groupId]: groupId,
138
+ [DATA_ATTRIBUTES.panel]: "",
139
+ [DATA_ATTRIBUTES.panelCollapsible]: collapsible || undefined,
140
+ [DATA_ATTRIBUTES.panelId]: panelId,
141
+ [DATA_ATTRIBUTES.panelSize]: parseFloat("" + style.flexGrow).toFixed(1)
126
142
  });
127
143
  }
128
144
  const Panel = forwardRef((props, ref) => createElement(PanelWithForwardedRef, {
@@ -142,6 +158,7 @@ function setNonce(value) {
142
158
 
143
159
  let currentCursorStyle = null;
144
160
  let enabled = true;
161
+ let prevRuleIndex = -1;
145
162
  let styleElement = null;
146
163
  function disableGlobalCursorStyles() {
147
164
  enabled = false;
@@ -191,9 +208,11 @@ function resetGlobalCursorStyle() {
191
208
  document.head.removeChild(styleElement);
192
209
  currentCursorStyle = null;
193
210
  styleElement = null;
211
+ prevRuleIndex = -1;
194
212
  }
195
213
  }
196
214
  function setGlobalCursorStyle(state, constraintFlags) {
215
+ var _styleElement$sheet$i, _styleElement$sheet2;
197
216
  if (!enabled) {
198
217
  return;
199
218
  }
@@ -210,7 +229,11 @@ function setGlobalCursorStyle(state, constraintFlags) {
210
229
  }
211
230
  document.head.appendChild(styleElement);
212
231
  }
213
- styleElement.innerHTML = `*{cursor: ${style}!important;}`;
232
+ if (prevRuleIndex >= 0) {
233
+ var _styleElement$sheet;
234
+ (_styleElement$sheet = styleElement.sheet) === null || _styleElement$sheet === void 0 ? void 0 : _styleElement$sheet.removeRule(prevRuleIndex);
235
+ }
236
+ 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;
214
237
  }
215
238
 
216
239
  function isKeyDown(event) {
@@ -441,7 +464,9 @@ function handlePointerDown(event) {
441
464
  if (intersectingHandles.length > 0) {
442
465
  updateResizeHandlerStates("down", event);
443
466
  event.preventDefault();
444
- event.stopPropagation();
467
+ if (!isWithinResizeHandle(target)) {
468
+ event.stopImmediatePropagation();
469
+ }
445
470
  }
446
471
  }
447
472
  function handlePointerMove(event) {
@@ -490,6 +515,9 @@ function handlePointerUp(event) {
490
515
  isPointerDown = false;
491
516
  if (intersectingHandles.length > 0) {
492
517
  event.preventDefault();
518
+ if (!isWithinResizeHandle(target)) {
519
+ event.stopImmediatePropagation();
520
+ }
493
521
  }
494
522
  updateResizeHandlerStates("up", event);
495
523
  recalculateIntersectingHandles({
@@ -500,6 +528,16 @@ function handlePointerUp(event) {
500
528
  updateCursor();
501
529
  updateListeners();
502
530
  }
531
+ function isWithinResizeHandle(element) {
532
+ let currentElement = element;
533
+ while (currentElement) {
534
+ if (currentElement.hasAttribute(DATA_ATTRIBUTES.resizeHandle)) {
535
+ return true;
536
+ }
537
+ currentElement = currentElement.parentElement;
538
+ }
539
+ return false;
540
+ }
503
541
  function recalculateIntersectingHandles({
504
542
  target,
505
543
  x,
@@ -591,49 +629,42 @@ function updateCursor() {
591
629
  resetGlobalCursorStyle();
592
630
  }
593
631
  }
632
+ let listenersAbortController = new AbortController();
594
633
  function updateListeners() {
595
- ownerDocumentCounts.forEach((_, ownerDocument) => {
596
- const {
597
- body
598
- } = ownerDocument;
599
- body.removeEventListener("contextmenu", handlePointerUp);
600
- body.removeEventListener("pointerdown", handlePointerDown, {
601
- capture: true
602
- });
603
- body.removeEventListener("pointerleave", handlePointerMove);
604
- body.removeEventListener("pointermove", handlePointerMove);
605
- });
606
- window.removeEventListener("pointerup", handlePointerUp);
607
- window.removeEventListener("pointercancel", handlePointerUp);
608
- if (registeredResizeHandlers.size > 0) {
609
- if (isPointerDown) {
610
- if (intersectingHandles.length > 0) {
611
- ownerDocumentCounts.forEach((count, ownerDocument) => {
612
- const {
613
- body
614
- } = ownerDocument;
615
- if (count > 0) {
616
- body.addEventListener("contextmenu", handlePointerUp);
617
- body.addEventListener("pointerleave", handlePointerMove);
618
- body.addEventListener("pointermove", handlePointerMove);
619
- }
620
- });
621
- }
622
- window.addEventListener("pointerup", handlePointerUp);
623
- window.addEventListener("pointercancel", handlePointerUp);
624
- } else {
634
+ listenersAbortController.abort();
635
+ listenersAbortController = new AbortController();
636
+ const options = {
637
+ capture: true,
638
+ signal: listenersAbortController.signal
639
+ };
640
+ if (!registeredResizeHandlers.size) {
641
+ return;
642
+ }
643
+ if (isPointerDown) {
644
+ if (intersectingHandles.length > 0) {
625
645
  ownerDocumentCounts.forEach((count, ownerDocument) => {
626
646
  const {
627
647
  body
628
648
  } = ownerDocument;
629
649
  if (count > 0) {
630
- body.addEventListener("pointerdown", handlePointerDown, {
631
- capture: true
632
- });
633
- body.addEventListener("pointermove", handlePointerMove);
650
+ body.addEventListener("contextmenu", handlePointerUp, options);
651
+ body.addEventListener("pointerleave", handlePointerMove, options);
652
+ body.addEventListener("pointermove", handlePointerMove, options);
634
653
  }
635
654
  });
636
655
  }
656
+ window.addEventListener("pointerup", handlePointerUp, options);
657
+ window.addEventListener("pointercancel", handlePointerUp, options);
658
+ } else {
659
+ ownerDocumentCounts.forEach((count, ownerDocument) => {
660
+ const {
661
+ body
662
+ } = ownerDocument;
663
+ if (count > 0) {
664
+ body.addEventListener("pointerdown", handlePointerDown, options);
665
+ body.addEventListener("pointermove", handlePointerMove, options);
666
+ }
667
+ });
637
668
  }
638
669
  }
639
670
  function updateResizeHandlerStates(action, event) {
@@ -658,8 +689,6 @@ function assert(expectedCondition, message) {
658
689
  }
659
690
  }
660
691
 
661
- const PRECISION = 10;
662
-
663
692
  function fuzzyCompareNumbers(actual, expected, fractionDigits = PRECISION) {
664
693
  if (actual.toFixed(fractionDigits) === expected.toFixed(fractionDigits)) {
665
694
  return 0;
@@ -964,12 +993,12 @@ function adjustLayoutByDelta({
964
993
  }
965
994
 
966
995
  function getResizeHandleElementsForGroup(groupId, scope = document) {
967
- return Array.from(scope.querySelectorAll(`[data-panel-resize-handle-id][data-panel-group-id="${groupId}"]`));
996
+ return Array.from(scope.querySelectorAll(`[${DATA_ATTRIBUTES.resizeHandleId}][data-panel-group-id="${groupId}"]`));
968
997
  }
969
998
 
970
999
  function getResizeHandleElementIndex(groupId, id, scope = document) {
971
1000
  const handles = getResizeHandleElementsForGroup(groupId, scope);
972
- const index = handles.findIndex(handle => handle.getAttribute("data-panel-resize-handle-id") === id);
1001
+ const index = handles.findIndex(handle => handle.getAttribute(DATA_ATTRIBUTES.resizeHandleId) === id);
973
1002
  return index !== null && index !== void 0 ? index : null;
974
1003
  }
975
1004
 
@@ -994,7 +1023,7 @@ function getPanelGroupElement(id, rootElement = document) {
994
1023
  }
995
1024
 
996
1025
  function getResizeHandleElement(id, scope = document) {
997
- const element = scope.querySelector(`[data-panel-resize-handle-id="${id}"]`);
1026
+ const element = scope.querySelector(`[${DATA_ATTRIBUTES.resizeHandleId}="${id}"]`);
998
1027
  if (element) {
999
1028
  return element;
1000
1029
  }
@@ -1039,7 +1068,7 @@ function useWindowSplitterPanelGroupBehavior({
1039
1068
  const handles = getResizeHandleElementsForGroup(groupId, panelGroupElement);
1040
1069
  assert(handles, `No resize handles found for group id "${groupId}"`);
1041
1070
  const cleanupFunctions = handles.map(handle => {
1042
- const handleId = handle.getAttribute("data-panel-resize-handle-id");
1071
+ const handleId = handle.getAttribute(DATA_ATTRIBUTES.resizeHandleId);
1043
1072
  assert(handleId, `Resize handle element has no handle id attribute`);
1044
1073
  const [idBefore, idAfter] = getResizeHandlePanelIds(groupId, handleId, panelDataArray, panelGroupElement);
1045
1074
  if (idBefore == null || idAfter == null) {
@@ -1117,7 +1146,7 @@ function calculateDragOffsetPercentage(event, dragHandleId, direction, initialDr
1117
1146
  const isHorizontal = direction === "horizontal";
1118
1147
  const handleElement = getResizeHandleElement(dragHandleId, panelGroupElement);
1119
1148
  assert(handleElement, `No resize handle element found for id "${dragHandleId}"`);
1120
- const groupId = handleElement.getAttribute("data-panel-group-id");
1149
+ const groupId = handleElement.getAttribute(DATA_ATTRIBUTES.groupId);
1121
1150
  assert(groupId, `Resize handle element has no group id attribute`);
1122
1151
  let {
1123
1152
  initialCursorPosition
@@ -2012,9 +2041,9 @@ function PanelGroupWithForwardedRef({
2012
2041
  ...styleFromProps
2013
2042
  },
2014
2043
  // CSS selectors
2015
- "data-panel-group": "",
2016
- "data-panel-group-direction": direction,
2017
- "data-panel-group-id": groupId
2044
+ [DATA_ATTRIBUTES.group]: "",
2045
+ [DATA_ATTRIBUTES.groupDirection]: direction,
2046
+ [DATA_ATTRIBUTES.groupId]: groupId
2018
2047
  }));
2019
2048
  }
2020
2049
  const PanelGroup = forwardRef((props, ref) => createElement(PanelGroupWithForwardedRef, {
@@ -2073,7 +2102,7 @@ function useWindowSplitterResizeHandlerBehavior({
2073
2102
  case "F6":
2074
2103
  {
2075
2104
  event.preventDefault();
2076
- const groupId = handleElement.getAttribute("data-panel-group-id");
2105
+ const groupId = handleElement.getAttribute(DATA_ATTRIBUTES.groupId);
2077
2106
  assert(groupId, `No group element found for id "${groupId}"`);
2078
2107
  const handles = getResizeHandleElementsForGroup(groupId, panelGroupElement);
2079
2108
  const index = getResizeHandleElementIndex(groupId, handleId, panelGroupElement);
@@ -2165,54 +2194,54 @@ function PanelResizeHandle({
2165
2194
  assert(element, "Element ref not attached");
2166
2195
  let didMove = false;
2167
2196
  const setResizeHandlerState = (action, isActive, event) => {
2168
- if (isActive) {
2169
- switch (action) {
2170
- case "down":
2171
- {
2172
- setState("drag");
2173
- didMove = false;
2174
- assert(event, 'Expected event to be defined for "down" action');
2175
- startDragging(resizeHandleId, event);
2176
- const {
2177
- onDragging,
2178
- onPointerDown
2179
- } = callbacksRef.current;
2180
- onDragging === null || onDragging === void 0 ? void 0 : onDragging(true);
2181
- onPointerDown === null || onPointerDown === void 0 ? void 0 : onPointerDown();
2182
- break;
2183
- }
2184
- case "move":
2185
- {
2186
- const {
2187
- state
2188
- } = committedValuesRef.current;
2189
- didMove = true;
2190
- if (state !== "drag") {
2191
- setState("hover");
2192
- }
2193
- assert(event, 'Expected event to be defined for "move" action');
2194
- resizeHandler(event);
2195
- break;
2196
- }
2197
- case "up":
2198
- {
2197
+ if (!isActive) {
2198
+ setState("inactive");
2199
+ return;
2200
+ }
2201
+ switch (action) {
2202
+ case "down":
2203
+ {
2204
+ setState("drag");
2205
+ didMove = false;
2206
+ assert(event, 'Expected event to be defined for "down" action');
2207
+ startDragging(resizeHandleId, event);
2208
+ const {
2209
+ onDragging,
2210
+ onPointerDown
2211
+ } = callbacksRef.current;
2212
+ onDragging === null || onDragging === void 0 ? void 0 : onDragging(true);
2213
+ onPointerDown === null || onPointerDown === void 0 ? void 0 : onPointerDown();
2214
+ break;
2215
+ }
2216
+ case "move":
2217
+ {
2218
+ const {
2219
+ state
2220
+ } = committedValuesRef.current;
2221
+ didMove = true;
2222
+ if (state !== "drag") {
2199
2223
  setState("hover");
2200
- stopDragging();
2201
- const {
2202
- onClick,
2203
- onDragging,
2204
- onPointerUp
2205
- } = callbacksRef.current;
2206
- onDragging === null || onDragging === void 0 ? void 0 : onDragging(false);
2207
- onPointerUp === null || onPointerUp === void 0 ? void 0 : onPointerUp();
2208
- if (!didMove) {
2209
- onClick === null || onClick === void 0 ? void 0 : onClick();
2210
- }
2211
- break;
2212
2224
  }
2213
- }
2214
- } else {
2215
- setState("inactive");
2225
+ assert(event, 'Expected event to be defined for "move" action');
2226
+ resizeHandler(event);
2227
+ break;
2228
+ }
2229
+ case "up":
2230
+ {
2231
+ setState("hover");
2232
+ stopDragging();
2233
+ const {
2234
+ onClick,
2235
+ onDragging,
2236
+ onPointerUp
2237
+ } = callbacksRef.current;
2238
+ onDragging === null || onDragging === void 0 ? void 0 : onDragging(false);
2239
+ onPointerUp === null || onPointerUp === void 0 ? void 0 : onPointerUp();
2240
+ if (!didMove) {
2241
+ onClick === null || onClick === void 0 ? void 0 : onClick();
2242
+ }
2243
+ break;
2244
+ }
2216
2245
  }
2217
2246
  };
2218
2247
  return registerResizeHandle(resizeHandleId, element, direction, {
@@ -2251,13 +2280,13 @@ function PanelResizeHandle({
2251
2280
  },
2252
2281
  tabIndex,
2253
2282
  // CSS selectors
2254
- "data-panel-group-direction": direction,
2255
- "data-panel-group-id": groupId,
2256
- "data-resize-handle": "",
2257
- "data-resize-handle-active": state === "drag" ? "pointer" : isFocused ? "keyboard" : undefined,
2258
- "data-resize-handle-state": state,
2259
- "data-panel-resize-handle-enabled": !disabled,
2260
- "data-panel-resize-handle-id": resizeHandleId
2283
+ [DATA_ATTRIBUTES.groupDirection]: direction,
2284
+ [DATA_ATTRIBUTES.groupId]: groupId,
2285
+ [DATA_ATTRIBUTES.resizeHandle]: "",
2286
+ [DATA_ATTRIBUTES.resizeHandleActive]: state === "drag" ? "pointer" : isFocused ? "keyboard" : undefined,
2287
+ [DATA_ATTRIBUTES.resizeHandleEnabled]: !disabled,
2288
+ [DATA_ATTRIBUTES.resizeHandleId]: resizeHandleId,
2289
+ [DATA_ATTRIBUTES.resizeHandleState]: state
2261
2290
  });
2262
2291
  }
2263
2292
  PanelResizeHandle.displayName = "PanelResizeHandle";
@@ -2291,4 +2320,4 @@ function getIntersectingRectangle(rectOne, rectTwo, strict) {
2291
2320
  };
2292
2321
  }
2293
2322
 
2294
- export { Panel, PanelGroup, PanelResizeHandle, assert, disableGlobalCursorStyles, enableGlobalCursorStyles, getIntersectingRectangle, getPanelElement, getPanelElementsForGroup, getPanelGroupElement, getResizeHandleElement, getResizeHandleElementIndex, getResizeHandleElementsForGroup, getResizeHandlePanelIds, intersects, setNonce };
2323
+ export { DATA_ATTRIBUTES, Panel, PanelGroup, PanelResizeHandle, assert, disableGlobalCursorStyles, enableGlobalCursorStyles, getIntersectingRectangle, getPanelElement, getPanelElementsForGroup, getPanelGroupElement, getResizeHandleElement, getResizeHandleElementIndex, getResizeHandleElementsForGroup, getResizeHandlePanelIds, intersects, setNonce };