react-resizable-panels 1.0.7 → 1.0.8

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 (51) hide show
  1. package/CHANGELOG.md +6 -0
  2. package/dist/declarations/src/Panel.d.ts +3 -3
  3. package/dist/declarations/src/PanelGroup.d.ts +2 -2
  4. package/dist/declarations/src/PanelResizeHandle.d.ts +2 -2
  5. package/dist/declarations/src/utils/dom/calculateAvailablePanelSizeInPixels.d.ts +1 -1
  6. package/dist/declarations/src/utils/dom/getAvailableGroupSizePixels.d.ts +1 -1
  7. package/dist/declarations/src/utils/dom/getPanelElement.d.ts +1 -1
  8. package/dist/declarations/src/utils/dom/getPanelElementsForGroup.d.ts +1 -1
  9. package/dist/declarations/src/utils/dom/getPanelGroupElement.d.ts +1 -1
  10. package/dist/declarations/src/utils/dom/getResizeHandleElement.d.ts +1 -1
  11. package/dist/declarations/src/utils/dom/getResizeHandleElementIndex.d.ts +1 -1
  12. package/dist/declarations/src/utils/dom/getResizeHandleElementsForGroup.d.ts +1 -1
  13. package/dist/declarations/src/utils/dom/getResizeHandlePanelIds.d.ts +1 -1
  14. package/dist/declarations/src/vendor/react.d.ts +2 -2
  15. package/dist/react-resizable-panels.browser.cjs.js +74 -51
  16. package/dist/react-resizable-panels.browser.development.cjs.js +76 -52
  17. package/dist/react-resizable-panels.browser.development.esm.js +76 -52
  18. package/dist/react-resizable-panels.browser.esm.js +74 -51
  19. package/dist/react-resizable-panels.cjs.js +74 -51
  20. package/dist/react-resizable-panels.development.cjs.js +76 -52
  21. package/dist/react-resizable-panels.development.esm.js +76 -52
  22. package/dist/react-resizable-panels.development.node.cjs.js +71 -50
  23. package/dist/react-resizable-panels.development.node.esm.js +71 -50
  24. package/dist/react-resizable-panels.esm.js +74 -51
  25. package/dist/react-resizable-panels.node.cjs.js +69 -49
  26. package/dist/react-resizable-panels.node.esm.js +69 -49
  27. package/package.json +1 -1
  28. package/src/Panel.test.tsx +3 -2
  29. package/src/Panel.ts +2 -2
  30. package/src/PanelGroup.test.tsx +3 -2
  31. package/src/PanelGroup.ts +48 -28
  32. package/src/PanelGroupContext.ts +4 -2
  33. package/src/PanelResizeHandle.test.tsx +3 -3
  34. package/src/PanelResizeHandle.ts +4 -2
  35. package/src/hooks/useWindowSplitterBehavior.ts +14 -5
  36. package/src/hooks/useWindowSplitterPanelGroupBehavior.ts +23 -7
  37. package/src/utils/calculateDeltaPercentage.ts +4 -2
  38. package/src/utils/calculateDragOffsetPercentage.ts +4 -3
  39. package/src/utils/determinePivotIndices.ts +7 -2
  40. package/src/utils/dom/calculateAvailablePanelSizeInPixels.ts +8 -3
  41. package/src/utils/dom/getAvailableGroupSizePixels.ts +8 -7
  42. package/src/utils/dom/getPanelElement.ts +5 -2
  43. package/src/utils/dom/getPanelElementsForGroup.ts +7 -2
  44. package/src/utils/dom/getPanelGroupElement.ts +14 -2
  45. package/src/utils/dom/getResizeHandleElement.ts +5 -2
  46. package/src/utils/dom/getResizeHandleElementIndex.ts +3 -2
  47. package/src/utils/dom/getResizeHandleElementsForGroup.ts +3 -2
  48. package/src/utils/dom/getResizeHandlePanelIds.ts +4 -3
  49. package/src/utils/validatePanelConstraints.test.ts +45 -0
  50. package/src/utils/validatePanelConstraints.ts +5 -1
  51. package/src/vendor/react.ts +2 -0
@@ -532,41 +532,48 @@ function calculateAriaValues({
532
532
  };
533
533
  }
534
534
 
535
- function getResizeHandleElementsForGroup(groupId) {
536
- return Array.from(document.querySelectorAll(`[data-panel-resize-handle-id][data-panel-group-id="${groupId}"]`));
535
+ function getResizeHandleElementsForGroup(groupId, panelGroupElement) {
536
+ return Array.from(panelGroupElement.querySelectorAll(`[data-panel-resize-handle-id][data-panel-group-id="${groupId}"]`));
537
537
  }
538
538
 
539
- function getResizeHandleElementIndex(groupId, id) {
540
- const handles = getResizeHandleElementsForGroup(groupId);
539
+ function getResizeHandleElementIndex(groupId, id, panelGroupElement) {
540
+ const handles = getResizeHandleElementsForGroup(groupId, panelGroupElement);
541
541
  const index = handles.findIndex(handle => handle.getAttribute("data-panel-resize-handle-id") === id);
542
542
  return index !== null && index !== void 0 ? index : null;
543
543
  }
544
544
 
545
- function determinePivotIndices(groupId, dragHandleId) {
546
- const index = getResizeHandleElementIndex(groupId, dragHandleId);
545
+ function determinePivotIndices(groupId, dragHandleId, panelGroupElement) {
546
+ const index = getResizeHandleElementIndex(groupId, dragHandleId, panelGroupElement);
547
547
  return index != null ? [index, index + 1] : [-1, -1];
548
548
  }
549
549
 
550
- function getPanelGroupElement(id) {
551
- const element = document.querySelector(`[data-panel-group][data-panel-group-id="${id}"]`);
550
+ function getPanelGroupElement(id, rootElement) {
551
+ var _dataset;
552
+ //If the root element is the PanelGroup
553
+ if (rootElement instanceof HTMLElement && (rootElement === null || rootElement === void 0 ? void 0 : (_dataset = rootElement.dataset) === null || _dataset === void 0 ? void 0 : _dataset.panelGroupId) == id) {
554
+ return rootElement;
555
+ }
556
+
557
+ //Else query children
558
+ const element = rootElement.querySelector(`[data-panel-group][data-panel-group-id="${id}"]`);
552
559
  if (element) {
553
560
  return element;
554
561
  }
555
562
  return null;
556
563
  }
557
564
 
558
- function getResizeHandleElement(id) {
559
- const element = document.querySelector(`[data-panel-resize-handle-id="${id}"]`);
565
+ function getResizeHandleElement(id, panelGroupElement) {
566
+ const element = panelGroupElement.querySelector(`[data-panel-resize-handle-id="${id}"]`);
560
567
  if (element) {
561
568
  return element;
562
569
  }
563
570
  return null;
564
571
  }
565
572
 
566
- function getResizeHandlePanelIds(groupId, handleId, panelsArray) {
573
+ function getResizeHandlePanelIds(groupId, handleId, panelsArray, panelGroupElement) {
567
574
  var _panelsArray$index$id, _panelsArray$index, _panelsArray$id, _panelsArray;
568
- const handle = getResizeHandleElement(handleId);
569
- const handles = getResizeHandleElementsForGroup(groupId);
575
+ const handle = getResizeHandleElement(handleId, panelGroupElement);
576
+ const handles = getResizeHandleElementsForGroup(groupId, panelGroupElement);
570
577
  const index = handle ? handles.indexOf(handle) : -1;
571
578
  const idBefore = (_panelsArray$index$id = (_panelsArray$index = panelsArray[index]) === null || _panelsArray$index === void 0 ? void 0 : _panelsArray$index.id) !== null && _panelsArray$index$id !== void 0 ? _panelsArray$index$id : null;
572
579
  const idAfter = (_panelsArray$id = (_panelsArray = panelsArray[index + 1]) === null || _panelsArray === void 0 ? void 0 : _panelsArray.id) !== null && _panelsArray$id !== void 0 ? _panelsArray$id : null;
@@ -581,13 +588,17 @@ function useWindowSplitterPanelGroupBehavior({
581
588
  groupId,
582
589
  layout,
583
590
  panelDataArray,
591
+ panelGroupElement,
584
592
  setLayout
585
593
  }) {
586
594
  const devWarningsRef = useRef({
587
595
  didWarnAboutMissingResizeHandle: false
588
596
  });
589
597
  useIsomorphicLayoutEffect(() => {
590
- const resizeHandleElements = getResizeHandleElementsForGroup(groupId);
598
+ if (!panelGroupElement) {
599
+ return;
600
+ }
601
+ const resizeHandleElements = getResizeHandleElementsForGroup(groupId, panelGroupElement);
591
602
  for (let index = 0; index < panelDataArray.length - 1; index++) {
592
603
  const {
593
604
  valueMax,
@@ -626,21 +637,24 @@ function useWindowSplitterPanelGroupBehavior({
626
637
  resizeHandleElement.removeAttribute("aria-valuenow");
627
638
  });
628
639
  };
629
- }, [groupId, layout, panelDataArray]);
640
+ }, [groupId, layout, panelDataArray, panelGroupElement]);
630
641
  useEffect(() => {
642
+ if (!panelGroupElement) {
643
+ return;
644
+ }
631
645
  const eagerValues = eagerValuesRef.current;
632
646
  assert(eagerValues);
633
647
  const {
634
648
  panelDataArray
635
649
  } = eagerValues;
636
- const groupElement = getPanelGroupElement(groupId);
650
+ const groupElement = getPanelGroupElement(groupId, panelGroupElement);
637
651
  assert(groupElement != null, `No group found for id "${groupId}"`);
638
- const handles = getResizeHandleElementsForGroup(groupId);
652
+ const handles = getResizeHandleElementsForGroup(groupId, panelGroupElement);
639
653
  assert(handles);
640
654
  const cleanupFunctions = handles.map(handle => {
641
655
  const handleId = handle.getAttribute("data-panel-resize-handle-id");
642
656
  assert(handleId);
643
- const [idBefore, idAfter] = getResizeHandlePanelIds(groupId, handleId, panelDataArray);
657
+ const [idBefore, idAfter] = getResizeHandlePanelIds(groupId, handleId, panelDataArray, panelGroupElement);
644
658
  if (idBefore == null || idAfter == null) {
645
659
  return () => {};
646
660
  }
@@ -667,7 +681,7 @@ function useWindowSplitterPanelGroupBehavior({
667
681
  delta: fuzzyNumbersEqual(size, collapsedSize) ? minSize - collapsedSize : collapsedSize - size,
668
682
  layout,
669
683
  panelConstraints: panelDataArray.map(panelData => panelData.constraints),
670
- pivotIndices: determinePivotIndices(groupId, handleId),
684
+ pivotIndices: determinePivotIndices(groupId, handleId, panelGroupElement),
671
685
  trigger: "keyboard"
672
686
  });
673
687
  if (layout !== nextLayout) {
@@ -687,7 +701,7 @@ function useWindowSplitterPanelGroupBehavior({
687
701
  return () => {
688
702
  cleanupFunctions.forEach(cleanupFunction => cleanupFunction());
689
703
  };
690
- }, [committedValuesRef, eagerValuesRef, groupId, layout, panelDataArray, setLayout]);
704
+ }, [panelGroupElement, committedValuesRef, eagerValuesRef, groupId, layout, panelDataArray, setLayout]);
691
705
  }
692
706
 
693
707
  function areEqual(arrayA, arrayB) {
@@ -725,9 +739,9 @@ function getResizeEventCursorPosition(direction, event) {
725
739
  }
726
740
  }
727
741
 
728
- function calculateDragOffsetPercentage(event, dragHandleId, direction, initialDragState) {
742
+ function calculateDragOffsetPercentage(event, dragHandleId, direction, initialDragState, panelGroupElement) {
729
743
  const isHorizontal = direction === "horizontal";
730
- const handleElement = getResizeHandleElement(dragHandleId);
744
+ const handleElement = getResizeHandleElement(dragHandleId, panelGroupElement);
731
745
  assert(handleElement);
732
746
  const groupId = handleElement.getAttribute("data-panel-group-id");
733
747
  assert(groupId);
@@ -735,7 +749,7 @@ function calculateDragOffsetPercentage(event, dragHandleId, direction, initialDr
735
749
  initialCursorPosition
736
750
  } = initialDragState;
737
751
  const cursorPosition = getResizeEventCursorPosition(direction, event);
738
- const groupElement = getPanelGroupElement(groupId);
752
+ const groupElement = getPanelGroupElement(groupId, panelGroupElement);
739
753
  assert(groupElement);
740
754
  const groupRect = groupElement.getBoundingClientRect();
741
755
  const groupSizeInPixels = isHorizontal ? groupRect.width : groupRect.height;
@@ -745,7 +759,7 @@ function calculateDragOffsetPercentage(event, dragHandleId, direction, initialDr
745
759
  }
746
760
 
747
761
  // https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/movementX
748
- function calculateDeltaPercentage(event, dragHandleId, direction, initialDragState, keyboardResizeBy) {
762
+ function calculateDeltaPercentage(event, dragHandleId, direction, initialDragState, keyboardResizeBy, panelGroupElement) {
749
763
  if (isKeyDown(event)) {
750
764
  const isHorizontal = direction === "horizontal";
751
765
  let delta = 0;
@@ -782,7 +796,7 @@ function calculateDeltaPercentage(event, dragHandleId, direction, initialDragSta
782
796
  if (initialDragState == null) {
783
797
  return 0;
784
798
  }
785
- return calculateDragOffsetPercentage(event, dragHandleId, direction, initialDragState);
799
+ return calculateDragOffsetPercentage(event, dragHandleId, direction, initialDragState, panelGroupElement);
786
800
  }
787
801
  }
788
802
 
@@ -1056,6 +1070,7 @@ function validatePanelConstraints({
1056
1070
  assert(panelConstraints);
1057
1071
  const {
1058
1072
  collapsedSize = 0,
1073
+ collapsible = false,
1059
1074
  defaultSize,
1060
1075
  maxSize = 100,
1061
1076
  minSize = 0
@@ -1066,7 +1081,7 @@ function validatePanelConstraints({
1066
1081
  if (defaultSize != null) {
1067
1082
  if (defaultSize < 0) {
1068
1083
  warnings.push("default size should not be less than 0");
1069
- } else if (defaultSize < minSize) {
1084
+ } else if (defaultSize < minSize && (!collapsible || defaultSize !== collapsedSize)) {
1070
1085
  warnings.push("default size should not be less than min size");
1071
1086
  }
1072
1087
  if (defaultSize > 100) {
@@ -1181,6 +1196,7 @@ function PanelGroupWithForwardedRef({
1181
1196
  ...rest
1182
1197
  }) {
1183
1198
  const groupId = useUniqueId(idFromProps);
1199
+ const panelGroupElementRef = useRef(null);
1184
1200
  const [dragState, setDragState] = useState(null);
1185
1201
  const [layout, setLayout] = useState([]);
1186
1202
  const panelIdToLastNotifiedSizeMapRef = useRef({});
@@ -1249,7 +1265,8 @@ function PanelGroupWithForwardedRef({
1249
1265
  groupId,
1250
1266
  layout,
1251
1267
  panelDataArray: eagerValuesRef.current.panelDataArray,
1252
- setLayout
1268
+ setLayout,
1269
+ panelGroupElement: panelGroupElementRef.current
1253
1270
  });
1254
1271
  useEffect(() => {
1255
1272
  const {
@@ -1534,6 +1551,10 @@ function PanelGroupWithForwardedRef({
1534
1551
  const registerResizeHandle = useCallback(dragHandleId => {
1535
1552
  return function resizeHandler(event) {
1536
1553
  event.preventDefault();
1554
+ const panelGroupElement = panelGroupElementRef.current;
1555
+ if (!panelGroupElement) {
1556
+ return () => null;
1557
+ }
1537
1558
  const {
1538
1559
  direction,
1539
1560
  dragState,
@@ -1548,8 +1569,8 @@ function PanelGroupWithForwardedRef({
1548
1569
  const {
1549
1570
  initialLayout
1550
1571
  } = dragState !== null && dragState !== void 0 ? dragState : {};
1551
- const pivotIndices = determinePivotIndices(groupId, dragHandleId);
1552
- let delta = calculateDeltaPercentage(event, dragHandleId, direction, dragState, keyboardResizeBy);
1572
+ const pivotIndices = determinePivotIndices(groupId, dragHandleId, panelGroupElement);
1573
+ let delta = calculateDeltaPercentage(event, dragHandleId, direction, dragState, keyboardResizeBy, panelGroupElement);
1553
1574
  if (delta === 0) {
1554
1575
  return;
1555
1576
  }
@@ -1644,7 +1665,10 @@ function PanelGroupWithForwardedRef({
1644
1665
  const {
1645
1666
  layout
1646
1667
  } = eagerValuesRef.current;
1647
- const handleElement = getResizeHandleElement(dragHandleId);
1668
+ if (!panelGroupElementRef.current) {
1669
+ return;
1670
+ }
1671
+ const handleElement = getResizeHandleElement(dragHandleId, panelGroupElementRef.current);
1648
1672
  assert(handleElement);
1649
1673
  const initialCursorPosition = getResizeEventCursorPosition(direction, event);
1650
1674
  setDragState({
@@ -1689,7 +1713,8 @@ function PanelGroupWithForwardedRef({
1689
1713
  resizePanel,
1690
1714
  startDragging,
1691
1715
  stopDragging,
1692
- unregisterPanel
1716
+ unregisterPanel,
1717
+ panelGroupElement: panelGroupElementRef.current
1693
1718
  }), [collapsePanel, dragState, direction, expandPanel, getPanelSize, getPanelStyle, groupId, isPanelCollapsed, isPanelExpanded, registerPanel, registerResizeHandle, resizePanel, startDragging, stopDragging, unregisterPanel]);
1694
1719
  const style = {
1695
1720
  display: "flex",
@@ -1708,6 +1733,7 @@ function PanelGroupWithForwardedRef({
1708
1733
  ...style,
1709
1734
  ...styleFromProps
1710
1735
  },
1736
+ ref: panelGroupElementRef,
1711
1737
  // CSS selectors
1712
1738
  "data-panel-group": "",
1713
1739
  "data-panel-group-direction": direction,
@@ -1742,13 +1768,14 @@ function panelDataHelper(panelDataArray, panelData, layout) {
1742
1768
  function useWindowSplitterResizeHandlerBehavior({
1743
1769
  disabled,
1744
1770
  handleId,
1745
- resizeHandler
1771
+ resizeHandler,
1772
+ panelGroupElement
1746
1773
  }) {
1747
1774
  useEffect(() => {
1748
- if (disabled || resizeHandler == null) {
1775
+ if (disabled || resizeHandler == null || panelGroupElement == null) {
1749
1776
  return;
1750
1777
  }
1751
- const handleElement = getResizeHandleElement(handleId);
1778
+ const handleElement = getResizeHandleElement(handleId, panelGroupElement);
1752
1779
  if (handleElement == null) {
1753
1780
  return;
1754
1781
  }
@@ -1773,8 +1800,8 @@ function useWindowSplitterResizeHandlerBehavior({
1773
1800
  event.preventDefault();
1774
1801
  const groupId = handleElement.getAttribute("data-panel-group-id");
1775
1802
  assert(groupId);
1776
- const handles = getResizeHandleElementsForGroup(groupId);
1777
- const index = getResizeHandleElementIndex(groupId, handleId);
1803
+ const handles = getResizeHandleElementsForGroup(groupId, panelGroupElement);
1804
+ const index = getResizeHandleElementIndex(groupId, handleId, panelGroupElement);
1778
1805
  assert(index !== null);
1779
1806
  const nextIndex = event.shiftKey ? index > 0 ? index - 1 : handles.length - 1 : index + 1 < handles.length ? index + 1 : 0;
1780
1807
  const nextHandle = handles[nextIndex];
@@ -1787,7 +1814,7 @@ function useWindowSplitterResizeHandlerBehavior({
1787
1814
  return () => {
1788
1815
  handleElement.removeEventListener("keydown", onKeyDown);
1789
1816
  };
1790
- }, [disabled, handleId, resizeHandler]);
1817
+ }, [panelGroupElement, disabled, handleId, resizeHandler]);
1791
1818
  }
1792
1819
 
1793
1820
  function PanelResizeHandle({
@@ -1820,7 +1847,8 @@ function PanelResizeHandle({
1820
1847
  groupId,
1821
1848
  registerResizeHandle,
1822
1849
  startDragging,
1823
- stopDragging
1850
+ stopDragging,
1851
+ panelGroupElement
1824
1852
  } = panelGroupContext;
1825
1853
  const resizeHandleId = useUniqueId(idFromProps);
1826
1854
  const isDragging = (dragState === null || dragState === void 0 ? void 0 : dragState.dragHandleId) === resizeHandleId;
@@ -1879,7 +1907,8 @@ function PanelResizeHandle({
1879
1907
  useWindowSplitterResizeHandlerBehavior({
1880
1908
  disabled,
1881
1909
  handleId: resizeHandleId,
1882
- resizeHandler
1910
+ resizeHandler,
1911
+ panelGroupElement
1883
1912
  });
1884
1913
  const style = {
1885
1914
  cursor: getCursorStyle(direction),
@@ -1935,13 +1964,12 @@ function PanelResizeHandle({
1935
1964
  }
1936
1965
  PanelResizeHandle.displayName = "PanelResizeHandle";
1937
1966
 
1938
- function calculateAvailablePanelSizeInPixels(groupId) {
1939
- const panelGroupElement = getPanelGroupElement(groupId);
1967
+ function calculateAvailablePanelSizeInPixels(groupId, panelGroupElement) {
1940
1968
  if (panelGroupElement == null) {
1941
1969
  return NaN;
1942
1970
  }
1943
1971
  const direction = panelGroupElement.getAttribute("data-panel-group-direction");
1944
- const resizeHandles = getResizeHandleElementsForGroup(groupId);
1972
+ const resizeHandles = getResizeHandleElementsForGroup(groupId, panelGroupElement);
1945
1973
  if (direction === "horizontal") {
1946
1974
  return panelGroupElement.offsetWidth - resizeHandles.reduce((accumulated, handle) => {
1947
1975
  return accumulated + handle.offsetWidth;
@@ -1953,13 +1981,9 @@ function calculateAvailablePanelSizeInPixels(groupId) {
1953
1981
  }
1954
1982
  }
1955
1983
 
1956
- function getAvailableGroupSizePixels(groupId) {
1957
- const panelGroupElement = getPanelGroupElement(groupId);
1958
- if (panelGroupElement == null) {
1959
- return NaN;
1960
- }
1984
+ function getAvailableGroupSizePixels(groupId, panelGroupElement) {
1961
1985
  const direction = panelGroupElement.getAttribute("data-panel-group-direction");
1962
- const resizeHandles = getResizeHandleElementsForGroup(groupId);
1986
+ const resizeHandles = getResizeHandleElementsForGroup(groupId, panelGroupElement);
1963
1987
  if (direction === "horizontal") {
1964
1988
  return panelGroupElement.offsetWidth - resizeHandles.reduce((accumulated, handle) => {
1965
1989
  return accumulated + handle.offsetWidth;
@@ -1971,16 +1995,16 @@ function getAvailableGroupSizePixels(groupId) {
1971
1995
  }
1972
1996
  }
1973
1997
 
1974
- function getPanelElement(id) {
1975
- const element = document.querySelector(`[data-panel-id="${id}"]`);
1998
+ function getPanelElement(id, panelGroupElement) {
1999
+ const element = panelGroupElement.querySelector(`[data-panel-id="${id}"]`);
1976
2000
  if (element) {
1977
2001
  return element;
1978
2002
  }
1979
2003
  return null;
1980
2004
  }
1981
2005
 
1982
- function getPanelElementsForGroup(groupId) {
1983
- return Array.from(document.querySelectorAll(`[data-panel][data-panel-group-id="${groupId}"]`));
2006
+ function getPanelElementsForGroup(groupId, panelGroupElement) {
2007
+ return Array.from(panelGroupElement.querySelectorAll(`[data-panel][data-panel-group-id="${groupId}"]`));
1984
2008
  }
1985
2009
 
1986
2010
  exports.Panel = Panel;