react-resizable-panels 1.0.4 → 1.0.6

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 (36) hide show
  1. package/CHANGELOG.md +9 -1
  2. package/README.md +202 -15
  3. package/dist/declarations/src/index.d.ts +10 -1
  4. package/dist/declarations/src/utils/dom/calculateAvailablePanelSizeInPixels.d.ts +1 -0
  5. package/dist/declarations/src/utils/dom/getAvailableGroupSizePixels.d.ts +1 -0
  6. package/dist/declarations/src/utils/dom/getPanelElement.d.ts +1 -0
  7. package/dist/declarations/src/utils/dom/getPanelElementsForGroup.d.ts +1 -0
  8. package/dist/declarations/src/utils/dom/getPanelGroupElement.d.ts +1 -0
  9. package/dist/declarations/src/utils/dom/getResizeHandleElement.d.ts +1 -0
  10. package/dist/declarations/src/utils/dom/getResizeHandleElementIndex.d.ts +1 -0
  11. package/dist/declarations/src/utils/dom/getResizeHandleElementsForGroup.d.ts +1 -0
  12. package/dist/declarations/src/utils/dom/getResizeHandlePanelIds.d.ts +2 -0
  13. package/dist/react-resizable-panels.browser.cjs.js +66 -5
  14. package/dist/react-resizable-panels.browser.cjs.mjs +10 -1
  15. package/dist/react-resizable-panels.browser.development.cjs.js +66 -5
  16. package/dist/react-resizable-panels.browser.development.cjs.mjs +10 -1
  17. package/dist/react-resizable-panels.browser.development.esm.js +58 -6
  18. package/dist/react-resizable-panels.browser.esm.js +58 -6
  19. package/dist/react-resizable-panels.cjs.js +66 -5
  20. package/dist/react-resizable-panels.cjs.mjs +10 -1
  21. package/dist/react-resizable-panels.development.cjs.js +66 -5
  22. package/dist/react-resizable-panels.development.cjs.mjs +10 -1
  23. package/dist/react-resizable-panels.development.esm.js +58 -6
  24. package/dist/react-resizable-panels.development.node.cjs.js +66 -5
  25. package/dist/react-resizable-panels.development.node.cjs.mjs +10 -1
  26. package/dist/react-resizable-panels.development.node.esm.js +58 -6
  27. package/dist/react-resizable-panels.esm.js +58 -6
  28. package/dist/react-resizable-panels.node.cjs.js +66 -5
  29. package/dist/react-resizable-panels.node.cjs.mjs +10 -1
  30. package/dist/react-resizable-panels.node.esm.js +58 -6
  31. package/package.json +1 -1
  32. package/src/Panel.ts +1 -1
  33. package/src/PanelGroup.ts +2 -1
  34. package/src/PanelGroupContext.ts +4 -1
  35. package/src/index.ts +23 -3
  36. package/src/utils/computePanelFlexBoxStyle.ts +7 -3
@@ -141,7 +141,7 @@ function PanelWithForwardedRef({
141
141
  resizePanel(panelDataRef.current, size);
142
142
  }
143
143
  }), [collapsePanel, expandPanel, getPanelSize, isPanelCollapsed, panelId, resizePanel]);
144
- const style = getPanelStyle(panelDataRef.current);
144
+ const style = getPanelStyle(panelDataRef.current, defaultSize);
145
145
  return createElement(Type, {
146
146
  ...rest,
147
147
  children,
@@ -836,6 +836,7 @@ function compareLayouts(a, b) {
836
836
 
837
837
  // the % of the group's overall space this panel should occupy.
838
838
  function computePanelFlexBoxStyle({
839
+ defaultSize,
839
840
  dragState,
840
841
  layout,
841
842
  panelData,
@@ -844,10 +845,12 @@ function computePanelFlexBoxStyle({
844
845
  }) {
845
846
  const size = layout[panelIndex];
846
847
  let flexGrow;
847
- if (panelData.length === 1) {
848
- flexGrow = "1";
849
- } else if (size == null) {
848
+ if (size == null) {
850
849
  // Initial render (before panels have registered themselves)
850
+ // In order to support server rendering, fall back to default size if provided
851
+ flexGrow = defaultSize !== null && defaultSize !== void 0 ? defaultSize : "1";
852
+ } else if (panelData.length === 1) {
853
+ // Special case: Single panel group should always fill full width/height
851
854
  flexGrow = "1";
852
855
  } else {
853
856
  flexGrow = size.toPrecision(precision);
@@ -1284,12 +1287,13 @@ function PanelGroupWithForwardedRef({
1284
1287
  }, []);
1285
1288
 
1286
1289
  // This API should never read from committedValuesRef
1287
- const getPanelStyle = useCallback(panelData => {
1290
+ const getPanelStyle = useCallback((panelData, defaultSize) => {
1288
1291
  const {
1289
1292
  panelDataArray
1290
1293
  } = eagerValuesRef.current;
1291
1294
  const panelIndex = findPanelDataIndex(panelDataArray, panelData);
1292
1295
  return computePanelFlexBoxStyle({
1296
+ defaultSize,
1293
1297
  dragState,
1294
1298
  layout,
1295
1299
  panelData: panelDataArray,
@@ -1797,4 +1801,52 @@ function PanelResizeHandle({
1797
1801
  }
1798
1802
  PanelResizeHandle.displayName = "PanelResizeHandle";
1799
1803
 
1800
- export { Panel, PanelGroup, PanelResizeHandle, assert };
1804
+ function calculateAvailablePanelSizeInPixels(groupId) {
1805
+ const panelGroupElement = getPanelGroupElement(groupId);
1806
+ if (panelGroupElement == null) {
1807
+ return NaN;
1808
+ }
1809
+ const direction = panelGroupElement.getAttribute("data-panel-group-direction");
1810
+ const resizeHandles = getResizeHandleElementsForGroup(groupId);
1811
+ if (direction === "horizontal") {
1812
+ return panelGroupElement.offsetWidth - resizeHandles.reduce((accumulated, handle) => {
1813
+ return accumulated + handle.offsetWidth;
1814
+ }, 0);
1815
+ } else {
1816
+ return panelGroupElement.offsetHeight - resizeHandles.reduce((accumulated, handle) => {
1817
+ return accumulated + handle.offsetHeight;
1818
+ }, 0);
1819
+ }
1820
+ }
1821
+
1822
+ function getAvailableGroupSizePixels(groupId) {
1823
+ const panelGroupElement = getPanelGroupElement(groupId);
1824
+ if (panelGroupElement == null) {
1825
+ return NaN;
1826
+ }
1827
+ const direction = panelGroupElement.getAttribute("data-panel-group-direction");
1828
+ const resizeHandles = getResizeHandleElementsForGroup(groupId);
1829
+ if (direction === "horizontal") {
1830
+ return panelGroupElement.offsetWidth - resizeHandles.reduce((accumulated, handle) => {
1831
+ return accumulated + handle.offsetWidth;
1832
+ }, 0);
1833
+ } else {
1834
+ return panelGroupElement.offsetHeight - resizeHandles.reduce((accumulated, handle) => {
1835
+ return accumulated + handle.offsetHeight;
1836
+ }, 0);
1837
+ }
1838
+ }
1839
+
1840
+ function getPanelElement(id) {
1841
+ const element = document.querySelector(`[data-panel-id="${id}"]`);
1842
+ if (element) {
1843
+ return element;
1844
+ }
1845
+ return null;
1846
+ }
1847
+
1848
+ function getPanelElementsForGroup(groupId) {
1849
+ return Array.from(document.querySelectorAll(`[data-panel][data-panel-group-id="${groupId}"]`));
1850
+ }
1851
+
1852
+ export { Panel, PanelGroup, PanelResizeHandle, assert, calculateAvailablePanelSizeInPixels, getAvailableGroupSizePixels, getPanelElement, getPanelElementsForGroup, getPanelGroupElement, getResizeHandleElement, getResizeHandleElementIndex, getResizeHandleElementsForGroup, getResizeHandlePanelIds };
@@ -137,7 +137,7 @@ function PanelWithForwardedRef({
137
137
  resizePanel(panelDataRef.current, size);
138
138
  }
139
139
  }), [collapsePanel, expandPanel, getPanelSize, isPanelCollapsed, panelId, resizePanel]);
140
- const style = getPanelStyle(panelDataRef.current);
140
+ const style = getPanelStyle(panelDataRef.current, defaultSize);
141
141
  return createElement(Type, {
142
142
  ...rest,
143
143
  children,
@@ -721,6 +721,7 @@ function compareLayouts(a, b) {
721
721
 
722
722
  // the % of the group's overall space this panel should occupy.
723
723
  function computePanelFlexBoxStyle({
724
+ defaultSize,
724
725
  dragState,
725
726
  layout,
726
727
  panelData,
@@ -729,10 +730,12 @@ function computePanelFlexBoxStyle({
729
730
  }) {
730
731
  const size = layout[panelIndex];
731
732
  let flexGrow;
732
- if (panelData.length === 1) {
733
- flexGrow = "1";
734
- } else if (size == null) {
733
+ if (size == null) {
735
734
  // Initial render (before panels have registered themselves)
735
+ // In order to support server rendering, fall back to default size if provided
736
+ flexGrow = defaultSize !== null && defaultSize !== void 0 ? defaultSize : "1";
737
+ } else if (panelData.length === 1) {
738
+ // Special case: Single panel group should always fill full width/height
736
739
  flexGrow = "1";
737
740
  } else {
738
741
  flexGrow = size.toPrecision(precision);
@@ -1155,12 +1158,13 @@ function PanelGroupWithForwardedRef({
1155
1158
  }, []);
1156
1159
 
1157
1160
  // This API should never read from committedValuesRef
1158
- const getPanelStyle = useCallback(panelData => {
1161
+ const getPanelStyle = useCallback((panelData, defaultSize) => {
1159
1162
  const {
1160
1163
  panelDataArray
1161
1164
  } = eagerValuesRef.current;
1162
1165
  const panelIndex = findPanelDataIndex(panelDataArray, panelData);
1163
1166
  return computePanelFlexBoxStyle({
1167
+ defaultSize,
1164
1168
  dragState,
1165
1169
  layout,
1166
1170
  panelData: panelDataArray,
@@ -1620,7 +1624,64 @@ function PanelResizeHandle({
1620
1624
  }
1621
1625
  PanelResizeHandle.displayName = "PanelResizeHandle";
1622
1626
 
1627
+ function calculateAvailablePanelSizeInPixels(groupId) {
1628
+ const panelGroupElement = getPanelGroupElement(groupId);
1629
+ if (panelGroupElement == null) {
1630
+ return NaN;
1631
+ }
1632
+ const direction = panelGroupElement.getAttribute("data-panel-group-direction");
1633
+ const resizeHandles = getResizeHandleElementsForGroup(groupId);
1634
+ if (direction === "horizontal") {
1635
+ return panelGroupElement.offsetWidth - resizeHandles.reduce((accumulated, handle) => {
1636
+ return accumulated + handle.offsetWidth;
1637
+ }, 0);
1638
+ } else {
1639
+ return panelGroupElement.offsetHeight - resizeHandles.reduce((accumulated, handle) => {
1640
+ return accumulated + handle.offsetHeight;
1641
+ }, 0);
1642
+ }
1643
+ }
1644
+
1645
+ function getAvailableGroupSizePixels(groupId) {
1646
+ const panelGroupElement = getPanelGroupElement(groupId);
1647
+ if (panelGroupElement == null) {
1648
+ return NaN;
1649
+ }
1650
+ const direction = panelGroupElement.getAttribute("data-panel-group-direction");
1651
+ const resizeHandles = getResizeHandleElementsForGroup(groupId);
1652
+ if (direction === "horizontal") {
1653
+ return panelGroupElement.offsetWidth - resizeHandles.reduce((accumulated, handle) => {
1654
+ return accumulated + handle.offsetWidth;
1655
+ }, 0);
1656
+ } else {
1657
+ return panelGroupElement.offsetHeight - resizeHandles.reduce((accumulated, handle) => {
1658
+ return accumulated + handle.offsetHeight;
1659
+ }, 0);
1660
+ }
1661
+ }
1662
+
1663
+ function getPanelElement(id) {
1664
+ const element = document.querySelector(`[data-panel-id="${id}"]`);
1665
+ if (element) {
1666
+ return element;
1667
+ }
1668
+ return null;
1669
+ }
1670
+
1671
+ function getPanelElementsForGroup(groupId) {
1672
+ return Array.from(document.querySelectorAll(`[data-panel][data-panel-group-id="${groupId}"]`));
1673
+ }
1674
+
1623
1675
  exports.Panel = Panel;
1624
1676
  exports.PanelGroup = PanelGroup;
1625
1677
  exports.PanelResizeHandle = PanelResizeHandle;
1626
1678
  exports.assert = assert;
1679
+ exports.calculateAvailablePanelSizeInPixels = calculateAvailablePanelSizeInPixels;
1680
+ exports.getAvailableGroupSizePixels = getAvailableGroupSizePixels;
1681
+ exports.getPanelElement = getPanelElement;
1682
+ exports.getPanelElementsForGroup = getPanelElementsForGroup;
1683
+ exports.getPanelGroupElement = getPanelGroupElement;
1684
+ exports.getResizeHandleElement = getResizeHandleElement;
1685
+ exports.getResizeHandleElementIndex = getResizeHandleElementIndex;
1686
+ exports.getResizeHandleElementsForGroup = getResizeHandleElementsForGroup;
1687
+ exports.getResizeHandlePanelIds = getResizeHandlePanelIds;
@@ -2,5 +2,14 @@ export {
2
2
  Panel,
3
3
  PanelGroup,
4
4
  PanelResizeHandle,
5
- assert
5
+ assert,
6
+ calculateAvailablePanelSizeInPixels,
7
+ getAvailableGroupSizePixels,
8
+ getPanelElement,
9
+ getPanelElementsForGroup,
10
+ getPanelGroupElement,
11
+ getResizeHandleElement,
12
+ getResizeHandleElementIndex,
13
+ getResizeHandleElementsForGroup,
14
+ getResizeHandlePanelIds
6
15
  } from "./react-resizable-panels.node.cjs.js";
@@ -113,7 +113,7 @@ function PanelWithForwardedRef({
113
113
  resizePanel(panelDataRef.current, size);
114
114
  }
115
115
  }), [collapsePanel, expandPanel, getPanelSize, isPanelCollapsed, panelId, resizePanel]);
116
- const style = getPanelStyle(panelDataRef.current);
116
+ const style = getPanelStyle(panelDataRef.current, defaultSize);
117
117
  return createElement(Type, {
118
118
  ...rest,
119
119
  children,
@@ -697,6 +697,7 @@ function compareLayouts(a, b) {
697
697
 
698
698
  // the % of the group's overall space this panel should occupy.
699
699
  function computePanelFlexBoxStyle({
700
+ defaultSize,
700
701
  dragState,
701
702
  layout,
702
703
  panelData,
@@ -705,10 +706,12 @@ function computePanelFlexBoxStyle({
705
706
  }) {
706
707
  const size = layout[panelIndex];
707
708
  let flexGrow;
708
- if (panelData.length === 1) {
709
- flexGrow = "1";
710
- } else if (size == null) {
709
+ if (size == null) {
711
710
  // Initial render (before panels have registered themselves)
711
+ // In order to support server rendering, fall back to default size if provided
712
+ flexGrow = defaultSize !== null && defaultSize !== void 0 ? defaultSize : "1";
713
+ } else if (panelData.length === 1) {
714
+ // Special case: Single panel group should always fill full width/height
712
715
  flexGrow = "1";
713
716
  } else {
714
717
  flexGrow = size.toPrecision(precision);
@@ -1131,12 +1134,13 @@ function PanelGroupWithForwardedRef({
1131
1134
  }, []);
1132
1135
 
1133
1136
  // This API should never read from committedValuesRef
1134
- const getPanelStyle = useCallback(panelData => {
1137
+ const getPanelStyle = useCallback((panelData, defaultSize) => {
1135
1138
  const {
1136
1139
  panelDataArray
1137
1140
  } = eagerValuesRef.current;
1138
1141
  const panelIndex = findPanelDataIndex(panelDataArray, panelData);
1139
1142
  return computePanelFlexBoxStyle({
1143
+ defaultSize,
1140
1144
  dragState,
1141
1145
  layout,
1142
1146
  panelData: panelDataArray,
@@ -1596,4 +1600,52 @@ function PanelResizeHandle({
1596
1600
  }
1597
1601
  PanelResizeHandle.displayName = "PanelResizeHandle";
1598
1602
 
1599
- export { Panel, PanelGroup, PanelResizeHandle, assert };
1603
+ function calculateAvailablePanelSizeInPixels(groupId) {
1604
+ const panelGroupElement = getPanelGroupElement(groupId);
1605
+ if (panelGroupElement == null) {
1606
+ return NaN;
1607
+ }
1608
+ const direction = panelGroupElement.getAttribute("data-panel-group-direction");
1609
+ const resizeHandles = getResizeHandleElementsForGroup(groupId);
1610
+ if (direction === "horizontal") {
1611
+ return panelGroupElement.offsetWidth - resizeHandles.reduce((accumulated, handle) => {
1612
+ return accumulated + handle.offsetWidth;
1613
+ }, 0);
1614
+ } else {
1615
+ return panelGroupElement.offsetHeight - resizeHandles.reduce((accumulated, handle) => {
1616
+ return accumulated + handle.offsetHeight;
1617
+ }, 0);
1618
+ }
1619
+ }
1620
+
1621
+ function getAvailableGroupSizePixels(groupId) {
1622
+ const panelGroupElement = getPanelGroupElement(groupId);
1623
+ if (panelGroupElement == null) {
1624
+ return NaN;
1625
+ }
1626
+ const direction = panelGroupElement.getAttribute("data-panel-group-direction");
1627
+ const resizeHandles = getResizeHandleElementsForGroup(groupId);
1628
+ if (direction === "horizontal") {
1629
+ return panelGroupElement.offsetWidth - resizeHandles.reduce((accumulated, handle) => {
1630
+ return accumulated + handle.offsetWidth;
1631
+ }, 0);
1632
+ } else {
1633
+ return panelGroupElement.offsetHeight - resizeHandles.reduce((accumulated, handle) => {
1634
+ return accumulated + handle.offsetHeight;
1635
+ }, 0);
1636
+ }
1637
+ }
1638
+
1639
+ function getPanelElement(id) {
1640
+ const element = document.querySelector(`[data-panel-id="${id}"]`);
1641
+ if (element) {
1642
+ return element;
1643
+ }
1644
+ return null;
1645
+ }
1646
+
1647
+ function getPanelElementsForGroup(groupId) {
1648
+ return Array.from(document.querySelectorAll(`[data-panel][data-panel-group-id="${groupId}"]`));
1649
+ }
1650
+
1651
+ export { Panel, PanelGroup, PanelResizeHandle, assert, calculateAvailablePanelSizeInPixels, getAvailableGroupSizePixels, getPanelElement, getPanelElementsForGroup, getPanelGroupElement, getResizeHandleElement, getResizeHandleElementIndex, getResizeHandleElementsForGroup, getResizeHandlePanelIds };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-resizable-panels",
3
- "version": "1.0.4",
3
+ "version": "1.0.6",
4
4
  "description": "React components for resizable panel groups/layouts",
5
5
  "author": "Brian Vaughn <brian.david.vaughn@gmail.com>",
6
6
  "license": "MIT",
package/src/Panel.ts CHANGED
@@ -212,7 +212,7 @@ export function PanelWithForwardedRef({
212
212
  ]
213
213
  );
214
214
 
215
- const style = getPanelStyle(panelDataRef.current);
215
+ const style = getPanelStyle(panelDataRef.current, defaultSize);
216
216
 
217
217
  return createElement(Type, {
218
218
  ...rest,
package/src/PanelGroup.ts CHANGED
@@ -435,12 +435,13 @@ function PanelGroupWithForwardedRef({
435
435
 
436
436
  // This API should never read from committedValuesRef
437
437
  const getPanelStyle = useCallback(
438
- (panelData: PanelData) => {
438
+ (panelData: PanelData, defaultSize: number | undefined) => {
439
439
  const { panelDataArray } = eagerValuesRef.current;
440
440
 
441
441
  const panelIndex = findPanelDataIndex(panelDataArray, panelData);
442
442
 
443
443
  return computePanelFlexBoxStyle({
444
+ defaultSize,
444
445
  dragState,
445
446
  layout,
446
447
  panelData: panelDataArray,
@@ -17,7 +17,10 @@ export const PanelGroupContext = createContext<{
17
17
  dragState: DragState | null;
18
18
  expandPanel: (panelData: PanelData) => void;
19
19
  getPanelSize: (panelData: PanelData) => number;
20
- getPanelStyle: (panelData: PanelData) => CSSProperties;
20
+ getPanelStyle: (
21
+ panelData: PanelData,
22
+ defaultSize: number | undefined
23
+ ) => CSSProperties;
21
24
  groupId: string;
22
25
  isPanelCollapsed: (panelData: PanelData) => boolean;
23
26
  isPanelExpanded: (panelData: PanelData) => boolean;
package/src/index.ts CHANGED
@@ -2,6 +2,15 @@ import { Panel } from "./Panel";
2
2
  import { PanelGroup } from "./PanelGroup";
3
3
  import { PanelResizeHandle } from "./PanelResizeHandle";
4
4
  import { assert } from "./utils/assert";
5
+ import { calculateAvailablePanelSizeInPixels } from "./utils/dom/calculateAvailablePanelSizeInPixels";
6
+ import { getAvailableGroupSizePixels } from "./utils/dom/getAvailableGroupSizePixels";
7
+ import { getPanelElement } from "./utils/dom/getPanelElement";
8
+ import { getPanelElementsForGroup } from "./utils/dom/getPanelElementsForGroup";
9
+ import { getPanelGroupElement } from "./utils/dom/getPanelGroupElement";
10
+ import { getResizeHandleElement } from "./utils/dom/getResizeHandleElement";
11
+ import { getResizeHandleElementIndex } from "./utils/dom/getResizeHandleElementIndex";
12
+ import { getResizeHandleElementsForGroup } from "./utils/dom/getResizeHandleElementsForGroup";
13
+ import { getResizeHandlePanelIds } from "./utils/dom/getResizeHandlePanelIds";
5
14
 
6
15
  import type {
7
16
  ImperativePanelHandle,
@@ -35,11 +44,22 @@ export {
35
44
  PanelResizeHandleOnDragging,
36
45
  PanelResizeHandleProps,
37
46
 
38
- // Utiltiy methods
39
- assert,
40
-
41
47
  // React components
42
48
  Panel,
43
49
  PanelGroup,
44
50
  PanelResizeHandle,
51
+
52
+ // Utility methods
53
+ assert,
54
+
55
+ // DOM helpers
56
+ calculateAvailablePanelSizeInPixels,
57
+ getAvailableGroupSizePixels,
58
+ getPanelElement,
59
+ getPanelElementsForGroup,
60
+ getPanelGroupElement,
61
+ getResizeHandleElement,
62
+ getResizeHandleElementIndex,
63
+ getResizeHandleElementsForGroup,
64
+ getResizeHandlePanelIds,
45
65
  };
@@ -6,12 +6,14 @@ import { CSSProperties } from "../vendor/react";
6
6
 
7
7
  // the % of the group's overall space this panel should occupy.
8
8
  export function computePanelFlexBoxStyle({
9
+ defaultSize,
9
10
  dragState,
10
11
  layout,
11
12
  panelData,
12
13
  panelIndex,
13
14
  precision = 3,
14
15
  }: {
16
+ defaultSize: number | undefined;
15
17
  layout: number[];
16
18
  dragState: DragState | null;
17
19
  panelData: PanelData[];
@@ -21,10 +23,12 @@ export function computePanelFlexBoxStyle({
21
23
  const size = layout[panelIndex];
22
24
 
23
25
  let flexGrow;
24
- if (panelData.length === 1) {
25
- flexGrow = "1";
26
- } else if (size == null) {
26
+ if (size == null) {
27
27
  // Initial render (before panels have registered themselves)
28
+ // In order to support server rendering, fall back to default size if provided
29
+ flexGrow = defaultSize ?? "1";
30
+ } else if (panelData.length === 1) {
31
+ // Special case: Single panel group should always fill full width/height
28
32
  flexGrow = "1";
29
33
  } else {
30
34
  flexGrow = size.toPrecision(precision);