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
@@ -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,47 @@ 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
+ ownerDocumentCounts.forEach((_, ownerDocument) => {
657
+ const {
658
+ body
659
+ } = ownerDocument;
660
+ body.addEventListener("pointerup", handlePointerUp, options);
661
+ body.addEventListener("pointercancel", handlePointerUp, options);
662
+ });
663
+ } else {
664
+ ownerDocumentCounts.forEach((count, ownerDocument) => {
665
+ const {
666
+ body
667
+ } = ownerDocument;
668
+ if (count > 0) {
669
+ body.addEventListener("pointerdown", handlePointerDown, options);
670
+ body.addEventListener("pointermove", handlePointerMove, options);
671
+ }
672
+ });
637
673
  }
638
674
  }
639
675
  function updateResizeHandlerStates(action, event) {
@@ -658,8 +694,6 @@ function assert(expectedCondition, message) {
658
694
  }
659
695
  }
660
696
 
661
- const PRECISION = 10;
662
-
663
697
  function fuzzyCompareNumbers(actual, expected, fractionDigits = PRECISION) {
664
698
  if (actual.toFixed(fractionDigits) === expected.toFixed(fractionDigits)) {
665
699
  return 0;
@@ -964,12 +998,12 @@ function adjustLayoutByDelta({
964
998
  }
965
999
 
966
1000
  function getResizeHandleElementsForGroup(groupId, scope = document) {
967
- return Array.from(scope.querySelectorAll(`[data-panel-resize-handle-id][data-panel-group-id="${groupId}"]`));
1001
+ return Array.from(scope.querySelectorAll(`[${DATA_ATTRIBUTES.resizeHandleId}][data-panel-group-id="${groupId}"]`));
968
1002
  }
969
1003
 
970
1004
  function getResizeHandleElementIndex(groupId, id, scope = document) {
971
1005
  const handles = getResizeHandleElementsForGroup(groupId, scope);
972
- const index = handles.findIndex(handle => handle.getAttribute("data-panel-resize-handle-id") === id);
1006
+ const index = handles.findIndex(handle => handle.getAttribute(DATA_ATTRIBUTES.resizeHandleId) === id);
973
1007
  return index !== null && index !== void 0 ? index : null;
974
1008
  }
975
1009
 
@@ -994,7 +1028,7 @@ function getPanelGroupElement(id, rootElement = document) {
994
1028
  }
995
1029
 
996
1030
  function getResizeHandleElement(id, scope = document) {
997
- const element = scope.querySelector(`[data-panel-resize-handle-id="${id}"]`);
1031
+ const element = scope.querySelector(`[${DATA_ATTRIBUTES.resizeHandleId}="${id}"]`);
998
1032
  if (element) {
999
1033
  return element;
1000
1034
  }
@@ -1039,7 +1073,7 @@ function useWindowSplitterPanelGroupBehavior({
1039
1073
  const handles = getResizeHandleElementsForGroup(groupId, panelGroupElement);
1040
1074
  assert(handles, `No resize handles found for group id "${groupId}"`);
1041
1075
  const cleanupFunctions = handles.map(handle => {
1042
- const handleId = handle.getAttribute("data-panel-resize-handle-id");
1076
+ const handleId = handle.getAttribute(DATA_ATTRIBUTES.resizeHandleId);
1043
1077
  assert(handleId, `Resize handle element has no handle id attribute`);
1044
1078
  const [idBefore, idAfter] = getResizeHandlePanelIds(groupId, handleId, panelDataArray, panelGroupElement);
1045
1079
  if (idBefore == null || idAfter == null) {
@@ -1117,7 +1151,7 @@ function calculateDragOffsetPercentage(event, dragHandleId, direction, initialDr
1117
1151
  const isHorizontal = direction === "horizontal";
1118
1152
  const handleElement = getResizeHandleElement(dragHandleId, panelGroupElement);
1119
1153
  assert(handleElement, `No resize handle element found for id "${dragHandleId}"`);
1120
- const groupId = handleElement.getAttribute("data-panel-group-id");
1154
+ const groupId = handleElement.getAttribute(DATA_ATTRIBUTES.groupId);
1121
1155
  assert(groupId, `Resize handle element has no group id attribute`);
1122
1156
  let {
1123
1157
  initialCursorPosition
@@ -2012,9 +2046,9 @@ function PanelGroupWithForwardedRef({
2012
2046
  ...styleFromProps
2013
2047
  },
2014
2048
  // CSS selectors
2015
- "data-panel-group": "",
2016
- "data-panel-group-direction": direction,
2017
- "data-panel-group-id": groupId
2049
+ [DATA_ATTRIBUTES.group]: "",
2050
+ [DATA_ATTRIBUTES.groupDirection]: direction,
2051
+ [DATA_ATTRIBUTES.groupId]: groupId
2018
2052
  }));
2019
2053
  }
2020
2054
  const PanelGroup = forwardRef((props, ref) => createElement(PanelGroupWithForwardedRef, {
@@ -2073,7 +2107,7 @@ function useWindowSplitterResizeHandlerBehavior({
2073
2107
  case "F6":
2074
2108
  {
2075
2109
  event.preventDefault();
2076
- const groupId = handleElement.getAttribute("data-panel-group-id");
2110
+ const groupId = handleElement.getAttribute(DATA_ATTRIBUTES.groupId);
2077
2111
  assert(groupId, `No group element found for id "${groupId}"`);
2078
2112
  const handles = getResizeHandleElementsForGroup(groupId, panelGroupElement);
2079
2113
  const index = getResizeHandleElementIndex(groupId, handleId, panelGroupElement);
@@ -2165,54 +2199,54 @@ function PanelResizeHandle({
2165
2199
  assert(element, "Element ref not attached");
2166
2200
  let didMove = false;
2167
2201
  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
- {
2202
+ if (!isActive) {
2203
+ setState("inactive");
2204
+ return;
2205
+ }
2206
+ switch (action) {
2207
+ case "down":
2208
+ {
2209
+ setState("drag");
2210
+ didMove = false;
2211
+ assert(event, 'Expected event to be defined for "down" action');
2212
+ startDragging(resizeHandleId, event);
2213
+ const {
2214
+ onDragging,
2215
+ onPointerDown
2216
+ } = callbacksRef.current;
2217
+ onDragging === null || onDragging === void 0 ? void 0 : onDragging(true);
2218
+ onPointerDown === null || onPointerDown === void 0 ? void 0 : onPointerDown();
2219
+ break;
2220
+ }
2221
+ case "move":
2222
+ {
2223
+ const {
2224
+ state
2225
+ } = committedValuesRef.current;
2226
+ didMove = true;
2227
+ if (state !== "drag") {
2199
2228
  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
2229
  }
2213
- }
2214
- } else {
2215
- setState("inactive");
2230
+ assert(event, 'Expected event to be defined for "move" action');
2231
+ resizeHandler(event);
2232
+ break;
2233
+ }
2234
+ case "up":
2235
+ {
2236
+ setState("hover");
2237
+ stopDragging();
2238
+ const {
2239
+ onClick,
2240
+ onDragging,
2241
+ onPointerUp
2242
+ } = callbacksRef.current;
2243
+ onDragging === null || onDragging === void 0 ? void 0 : onDragging(false);
2244
+ onPointerUp === null || onPointerUp === void 0 ? void 0 : onPointerUp();
2245
+ if (!didMove) {
2246
+ onClick === null || onClick === void 0 ? void 0 : onClick();
2247
+ }
2248
+ break;
2249
+ }
2216
2250
  }
2217
2251
  };
2218
2252
  return registerResizeHandle(resizeHandleId, element, direction, {
@@ -2251,17 +2285,25 @@ function PanelResizeHandle({
2251
2285
  },
2252
2286
  tabIndex,
2253
2287
  // 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
2288
+ [DATA_ATTRIBUTES.groupDirection]: direction,
2289
+ [DATA_ATTRIBUTES.groupId]: groupId,
2290
+ [DATA_ATTRIBUTES.resizeHandle]: "",
2291
+ [DATA_ATTRIBUTES.resizeHandleActive]: state === "drag" ? "pointer" : isFocused ? "keyboard" : undefined,
2292
+ [DATA_ATTRIBUTES.resizeHandleEnabled]: !disabled,
2293
+ [DATA_ATTRIBUTES.resizeHandleId]: resizeHandleId,
2294
+ [DATA_ATTRIBUTES.resizeHandleState]: state
2261
2295
  });
2262
2296
  }
2263
2297
  PanelResizeHandle.displayName = "PanelResizeHandle";
2264
2298
 
2299
+ function usePanelGroupContext() {
2300
+ const context = useContext(PanelGroupContext);
2301
+ return {
2302
+ direction: context === null || context === void 0 ? void 0 : context.direction,
2303
+ groupId: context === null || context === void 0 ? void 0 : context.groupId
2304
+ };
2305
+ }
2306
+
2265
2307
  function getPanelElement(id, scope = document) {
2266
2308
  const element = scope.querySelector(`[data-panel-id="${id}"]`);
2267
2309
  if (element) {
@@ -2291,4 +2333,4 @@ function getIntersectingRectangle(rectOne, rectTwo, strict) {
2291
2333
  };
2292
2334
  }
2293
2335
 
2294
- export { Panel, PanelGroup, PanelResizeHandle, assert, disableGlobalCursorStyles, enableGlobalCursorStyles, getIntersectingRectangle, getPanelElement, getPanelElementsForGroup, getPanelGroupElement, getResizeHandleElement, getResizeHandleElementIndex, getResizeHandleElementsForGroup, getResizeHandlePanelIds, intersects, setNonce };
2336
+ export { DATA_ATTRIBUTES, Panel, PanelGroup, PanelResizeHandle, assert, disableGlobalCursorStyles, enableGlobalCursorStyles, getIntersectingRectangle, getPanelElement, getPanelElementsForGroup, getPanelGroupElement, getResizeHandleElement, getResizeHandleElementIndex, getResizeHandleElementsForGroup, getResizeHandlePanelIds, intersects, setNonce, usePanelGroupContext };