react-resizable-panels 2.0.1 → 2.0.3

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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  # Changelog
2
2
 
3
+ ## 2.0.3
4
+
5
+ - Fix resize handle onDragging callback (#278)
6
+
7
+ ## 2.0.2
8
+
9
+ - Fixed an issue where size might not be re-initialized correctly after a panel was hidden by the `unstable_Activity` (previously "Offscreen") API.
10
+
3
11
  ## 2.0.1
4
12
 
5
13
  - Fixed a regression introduced in 2.0.0 that caused React `onClick` and `onMouseUp` handlers not to fire.
@@ -1709,6 +1709,14 @@ function PanelGroupWithForwardedRef({
1709
1709
  }
1710
1710
  }
1711
1711
  });
1712
+
1713
+ // Reset the cached layout if hidden by the Activity/Offscreen API
1714
+ useIsomorphicLayoutEffect(() => {
1715
+ const eagerValues = eagerValuesRef.current;
1716
+ return () => {
1717
+ eagerValues.layout = [];
1718
+ };
1719
+ }, []);
1712
1720
  const registerResizeHandle = useCallback(dragHandleId => {
1713
1721
  return function resizeHandler(event) {
1714
1722
  event.preventDefault();
@@ -2060,11 +2068,23 @@ function PanelResizeHandle({
2060
2068
  case "down":
2061
2069
  {
2062
2070
  startDragging(resizeHandleId, event);
2071
+ const {
2072
+ onDragging
2073
+ } = callbacksRef.current;
2074
+ if (onDragging) {
2075
+ onDragging(true);
2076
+ }
2063
2077
  break;
2064
2078
  }
2065
2079
  case "up":
2066
2080
  {
2067
2081
  stopDragging();
2082
+ const {
2083
+ onDragging
2084
+ } = callbacksRef.current;
2085
+ if (onDragging) {
2086
+ onDragging(false);
2087
+ }
2068
2088
  break;
2069
2089
  }
2070
2090
  }
@@ -1815,6 +1815,14 @@ function PanelGroupWithForwardedRef({
1815
1815
  }
1816
1816
  }
1817
1817
  });
1818
+
1819
+ // Reset the cached layout if hidden by the Activity/Offscreen API
1820
+ useIsomorphicLayoutEffect(() => {
1821
+ const eagerValues = eagerValuesRef.current;
1822
+ return () => {
1823
+ eagerValues.layout = [];
1824
+ };
1825
+ }, []);
1818
1826
  const registerResizeHandle = useCallback(dragHandleId => {
1819
1827
  return function resizeHandler(event) {
1820
1828
  event.preventDefault();
@@ -2166,11 +2174,23 @@ function PanelResizeHandle({
2166
2174
  case "down":
2167
2175
  {
2168
2176
  startDragging(resizeHandleId, event);
2177
+ const {
2178
+ onDragging
2179
+ } = callbacksRef.current;
2180
+ if (onDragging) {
2181
+ onDragging(true);
2182
+ }
2169
2183
  break;
2170
2184
  }
2171
2185
  case "up":
2172
2186
  {
2173
2187
  stopDragging();
2188
+ const {
2189
+ onDragging
2190
+ } = callbacksRef.current;
2191
+ if (onDragging) {
2192
+ onDragging(false);
2193
+ }
2174
2194
  break;
2175
2195
  }
2176
2196
  }
@@ -1791,6 +1791,14 @@ function PanelGroupWithForwardedRef({
1791
1791
  }
1792
1792
  }
1793
1793
  });
1794
+
1795
+ // Reset the cached layout if hidden by the Activity/Offscreen API
1796
+ useIsomorphicLayoutEffect(() => {
1797
+ const eagerValues = eagerValuesRef.current;
1798
+ return () => {
1799
+ eagerValues.layout = [];
1800
+ };
1801
+ }, []);
1794
1802
  const registerResizeHandle = useCallback(dragHandleId => {
1795
1803
  return function resizeHandler(event) {
1796
1804
  event.preventDefault();
@@ -2142,11 +2150,23 @@ function PanelResizeHandle({
2142
2150
  case "down":
2143
2151
  {
2144
2152
  startDragging(resizeHandleId, event);
2153
+ const {
2154
+ onDragging
2155
+ } = callbacksRef.current;
2156
+ if (onDragging) {
2157
+ onDragging(true);
2158
+ }
2145
2159
  break;
2146
2160
  }
2147
2161
  case "up":
2148
2162
  {
2149
2163
  stopDragging();
2164
+ const {
2165
+ onDragging
2166
+ } = callbacksRef.current;
2167
+ if (onDragging) {
2168
+ onDragging(false);
2169
+ }
2150
2170
  break;
2151
2171
  }
2152
2172
  }
@@ -1685,6 +1685,14 @@ function PanelGroupWithForwardedRef({
1685
1685
  }
1686
1686
  }
1687
1687
  });
1688
+
1689
+ // Reset the cached layout if hidden by the Activity/Offscreen API
1690
+ useIsomorphicLayoutEffect(() => {
1691
+ const eagerValues = eagerValuesRef.current;
1692
+ return () => {
1693
+ eagerValues.layout = [];
1694
+ };
1695
+ }, []);
1688
1696
  const registerResizeHandle = useCallback(dragHandleId => {
1689
1697
  return function resizeHandler(event) {
1690
1698
  event.preventDefault();
@@ -2036,11 +2044,23 @@ function PanelResizeHandle({
2036
2044
  case "down":
2037
2045
  {
2038
2046
  startDragging(resizeHandleId, event);
2047
+ const {
2048
+ onDragging
2049
+ } = callbacksRef.current;
2050
+ if (onDragging) {
2051
+ onDragging(true);
2052
+ }
2039
2053
  break;
2040
2054
  }
2041
2055
  case "up":
2042
2056
  {
2043
2057
  stopDragging();
2058
+ const {
2059
+ onDragging
2060
+ } = callbacksRef.current;
2061
+ if (onDragging) {
2062
+ onDragging(false);
2063
+ }
2044
2064
  break;
2045
2065
  }
2046
2066
  }
@@ -1711,6 +1711,14 @@ function PanelGroupWithForwardedRef({
1711
1711
  }
1712
1712
  }
1713
1713
  });
1714
+
1715
+ // Reset the cached layout if hidden by the Activity/Offscreen API
1716
+ useIsomorphicLayoutEffect(() => {
1717
+ const eagerValues = eagerValuesRef.current;
1718
+ return () => {
1719
+ eagerValues.layout = [];
1720
+ };
1721
+ }, []);
1714
1722
  const registerResizeHandle = useCallback(dragHandleId => {
1715
1723
  return function resizeHandler(event) {
1716
1724
  event.preventDefault();
@@ -2062,11 +2070,23 @@ function PanelResizeHandle({
2062
2070
  case "down":
2063
2071
  {
2064
2072
  startDragging(resizeHandleId, event);
2073
+ const {
2074
+ onDragging
2075
+ } = callbacksRef.current;
2076
+ if (onDragging) {
2077
+ onDragging(true);
2078
+ }
2065
2079
  break;
2066
2080
  }
2067
2081
  case "up":
2068
2082
  {
2069
2083
  stopDragging();
2084
+ const {
2085
+ onDragging
2086
+ } = callbacksRef.current;
2087
+ if (onDragging) {
2088
+ onDragging(false);
2089
+ }
2070
2090
  break;
2071
2091
  }
2072
2092
  }
@@ -1822,6 +1822,14 @@ function PanelGroupWithForwardedRef({
1822
1822
  }
1823
1823
  }
1824
1824
  });
1825
+
1826
+ // Reset the cached layout if hidden by the Activity/Offscreen API
1827
+ useIsomorphicLayoutEffect(() => {
1828
+ const eagerValues = eagerValuesRef.current;
1829
+ return () => {
1830
+ eagerValues.layout = [];
1831
+ };
1832
+ }, []);
1825
1833
  const registerResizeHandle = useCallback(dragHandleId => {
1826
1834
  return function resizeHandler(event) {
1827
1835
  event.preventDefault();
@@ -2173,11 +2181,23 @@ function PanelResizeHandle({
2173
2181
  case "down":
2174
2182
  {
2175
2183
  startDragging(resizeHandleId, event);
2184
+ const {
2185
+ onDragging
2186
+ } = callbacksRef.current;
2187
+ if (onDragging) {
2188
+ onDragging(true);
2189
+ }
2176
2190
  break;
2177
2191
  }
2178
2192
  case "up":
2179
2193
  {
2180
2194
  stopDragging();
2195
+ const {
2196
+ onDragging
2197
+ } = callbacksRef.current;
2198
+ if (onDragging) {
2199
+ onDragging(false);
2200
+ }
2181
2201
  break;
2182
2202
  }
2183
2203
  }
@@ -1798,6 +1798,14 @@ function PanelGroupWithForwardedRef({
1798
1798
  }
1799
1799
  }
1800
1800
  });
1801
+
1802
+ // Reset the cached layout if hidden by the Activity/Offscreen API
1803
+ useIsomorphicLayoutEffect(() => {
1804
+ const eagerValues = eagerValuesRef.current;
1805
+ return () => {
1806
+ eagerValues.layout = [];
1807
+ };
1808
+ }, []);
1801
1809
  const registerResizeHandle = useCallback(dragHandleId => {
1802
1810
  return function resizeHandler(event) {
1803
1811
  event.preventDefault();
@@ -2149,11 +2157,23 @@ function PanelResizeHandle({
2149
2157
  case "down":
2150
2158
  {
2151
2159
  startDragging(resizeHandleId, event);
2160
+ const {
2161
+ onDragging
2162
+ } = callbacksRef.current;
2163
+ if (onDragging) {
2164
+ onDragging(true);
2165
+ }
2152
2166
  break;
2153
2167
  }
2154
2168
  case "up":
2155
2169
  {
2156
2170
  stopDragging();
2171
+ const {
2172
+ onDragging
2173
+ } = callbacksRef.current;
2174
+ if (onDragging) {
2175
+ onDragging(false);
2176
+ }
2157
2177
  break;
2158
2178
  }
2159
2179
  }
@@ -1950,11 +1950,23 @@ function PanelResizeHandle({
1950
1950
  case "down":
1951
1951
  {
1952
1952
  startDragging(resizeHandleId, event);
1953
+ const {
1954
+ onDragging
1955
+ } = callbacksRef.current;
1956
+ if (onDragging) {
1957
+ onDragging(true);
1958
+ }
1953
1959
  break;
1954
1960
  }
1955
1961
  case "up":
1956
1962
  {
1957
1963
  stopDragging();
1964
+ const {
1965
+ onDragging
1966
+ } = callbacksRef.current;
1967
+ if (onDragging) {
1968
+ onDragging(false);
1969
+ }
1958
1970
  break;
1959
1971
  }
1960
1972
  }
@@ -1926,11 +1926,23 @@ function PanelResizeHandle({
1926
1926
  case "down":
1927
1927
  {
1928
1928
  startDragging(resizeHandleId, event);
1929
+ const {
1930
+ onDragging
1931
+ } = callbacksRef.current;
1932
+ if (onDragging) {
1933
+ onDragging(true);
1934
+ }
1929
1935
  break;
1930
1936
  }
1931
1937
  case "up":
1932
1938
  {
1933
1939
  stopDragging();
1940
+ const {
1941
+ onDragging
1942
+ } = callbacksRef.current;
1943
+ if (onDragging) {
1944
+ onDragging(false);
1945
+ }
1934
1946
  break;
1935
1947
  }
1936
1948
  }
@@ -1687,6 +1687,14 @@ function PanelGroupWithForwardedRef({
1687
1687
  }
1688
1688
  }
1689
1689
  });
1690
+
1691
+ // Reset the cached layout if hidden by the Activity/Offscreen API
1692
+ useIsomorphicLayoutEffect(() => {
1693
+ const eagerValues = eagerValuesRef.current;
1694
+ return () => {
1695
+ eagerValues.layout = [];
1696
+ };
1697
+ }, []);
1690
1698
  const registerResizeHandle = useCallback(dragHandleId => {
1691
1699
  return function resizeHandler(event) {
1692
1700
  event.preventDefault();
@@ -2038,11 +2046,23 @@ function PanelResizeHandle({
2038
2046
  case "down":
2039
2047
  {
2040
2048
  startDragging(resizeHandleId, event);
2049
+ const {
2050
+ onDragging
2051
+ } = callbacksRef.current;
2052
+ if (onDragging) {
2053
+ onDragging(true);
2054
+ }
2041
2055
  break;
2042
2056
  }
2043
2057
  case "up":
2044
2058
  {
2045
2059
  stopDragging();
2060
+ const {
2061
+ onDragging
2062
+ } = callbacksRef.current;
2063
+ if (onDragging) {
2064
+ onDragging(false);
2065
+ }
2046
2066
  break;
2047
2067
  }
2048
2068
  }
@@ -1849,11 +1849,23 @@ function PanelResizeHandle({
1849
1849
  case "down":
1850
1850
  {
1851
1851
  startDragging(resizeHandleId, event);
1852
+ const {
1853
+ onDragging
1854
+ } = callbacksRef.current;
1855
+ if (onDragging) {
1856
+ onDragging(true);
1857
+ }
1852
1858
  break;
1853
1859
  }
1854
1860
  case "up":
1855
1861
  {
1856
1862
  stopDragging();
1863
+ const {
1864
+ onDragging
1865
+ } = callbacksRef.current;
1866
+ if (onDragging) {
1867
+ onDragging(false);
1868
+ }
1857
1869
  break;
1858
1870
  }
1859
1871
  }
@@ -1825,11 +1825,23 @@ function PanelResizeHandle({
1825
1825
  case "down":
1826
1826
  {
1827
1827
  startDragging(resizeHandleId, event);
1828
+ const {
1829
+ onDragging
1830
+ } = callbacksRef.current;
1831
+ if (onDragging) {
1832
+ onDragging(true);
1833
+ }
1828
1834
  break;
1829
1835
  }
1830
1836
  case "up":
1831
1837
  {
1832
1838
  stopDragging();
1839
+ const {
1840
+ onDragging
1841
+ } = callbacksRef.current;
1842
+ if (onDragging) {
1843
+ onDragging(false);
1844
+ }
1833
1845
  break;
1834
1846
  }
1835
1847
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-resizable-panels",
3
- "version": "2.0.1",
3
+ "version": "2.0.3",
4
4
  "description": "React components for resizable panel groups/layouts",
5
5
  "author": "Brian Vaughn <brian.david.vaughn@gmail.com>",
6
6
  "license": "MIT",
@@ -72,8 +72,10 @@
72
72
  "eslint": "^8.37.0",
73
73
  "eslint-plugin-no-restricted-imports": "^0.0.0",
74
74
  "eslint-plugin-react-hooks": "^4.6.0",
75
- "react": "latest",
76
- "react-dom": "latest"
75
+ "jest": "^29.7.0",
76
+ "jest-environment-jsdom": "^29.7.0",
77
+ "react": "experimental",
78
+ "react-dom": "experimental"
77
79
  },
78
80
  "peerDependencies": {
79
81
  "react": "^16.14.0 || ^17.0.0 || ^18.0.0",
@@ -1,3 +1,6 @@
1
+ // @ts-expect-error This is an experimental API
2
+ // eslint-disable-next-line no-restricted-imports
3
+ import { unstable_Activity as Activity } from "react";
1
4
  import { Root, createRoot } from "react-dom/client";
2
5
  import { act } from "react-dom/test-utils";
3
6
  import {
@@ -6,6 +9,7 @@ import {
6
9
  Panel,
7
10
  PanelGroup,
8
11
  PanelResizeHandle,
12
+ getPanelElement,
9
13
  } from ".";
10
14
  import { assert } from "./utils/assert";
11
15
  import { getPanelGroupElement } from "./utils/dom/getPanelGroupElement";
@@ -62,6 +66,67 @@ describe("PanelGroup", () => {
62
66
  expect(expectedWarnings).toHaveLength(0);
63
67
  });
64
68
 
69
+ it("should recalculate layout after being hidden by Activity", () => {
70
+ const panelRef = createRef<ImperativePanelHandle>();
71
+
72
+ let mostRecentLayout: number[] | null = null;
73
+
74
+ const onLayout = (layout: number[]) => {
75
+ mostRecentLayout = layout;
76
+ };
77
+
78
+ act(() => {
79
+ root.render(
80
+ <Activity mode="visible">
81
+ <PanelGroup direction="horizontal" onLayout={onLayout}>
82
+ <Panel id="left" ref={panelRef} />
83
+ <PanelResizeHandle />
84
+ <Panel defaultSize={40} id="right" />
85
+ </PanelGroup>
86
+ </Activity>
87
+ );
88
+ });
89
+
90
+ expect(mostRecentLayout).toEqual([60, 40]);
91
+ expect(panelRef.current?.getSize()).toEqual(60);
92
+
93
+ const leftPanelElement = getPanelElement("left");
94
+ const rightPanelElement = getPanelElement("right");
95
+ expect(leftPanelElement?.getAttribute("data-panel-size")).toBe("60.0");
96
+ expect(rightPanelElement?.getAttribute("data-panel-size")).toBe("40.0");
97
+
98
+ act(() => {
99
+ root.render(
100
+ <Activity mode="hidden">
101
+ <PanelGroup direction="horizontal" onLayout={onLayout}>
102
+ <Panel id="left" ref={panelRef} />
103
+ <PanelResizeHandle />
104
+ <Panel defaultSize={40} id="right" />
105
+ </PanelGroup>
106
+ </Activity>
107
+ );
108
+ });
109
+
110
+ act(() => {
111
+ root.render(
112
+ <Activity mode="visible">
113
+ <PanelGroup direction="horizontal" onLayout={onLayout}>
114
+ <Panel id="left" ref={panelRef} />
115
+ <PanelResizeHandle />
116
+ <Panel defaultSize={40} id="right" />
117
+ </PanelGroup>
118
+ </Activity>
119
+ );
120
+ });
121
+
122
+ expect(mostRecentLayout).toEqual([60, 40]);
123
+ expect(panelRef.current?.getSize()).toEqual(60);
124
+
125
+ // This bug is only observable in the DOM; callbacks will not re-fire
126
+ expect(leftPanelElement?.getAttribute("data-panel-size")).toBe("60.0");
127
+ expect(rightPanelElement?.getAttribute("data-panel-size")).toBe("40.0");
128
+ });
129
+
65
130
  describe("imperative handle API", () => {
66
131
  it("should report the most recently rendered group id", () => {
67
132
  const ref = createRef<ImperativePanelGroupHandle>();
package/src/PanelGroup.ts CHANGED
@@ -25,7 +25,6 @@ import { calculateUnsafeDefaultLayout } from "./utils/calculateUnsafeDefaultLayo
25
25
  import { callPanelCallbacks } from "./utils/callPanelCallbacks";
26
26
  import { compareLayouts } from "./utils/compareLayouts";
27
27
  import { computePanelFlexBoxStyle } from "./utils/computePanelFlexBoxStyle";
28
- import { resetGlobalCursorStyle, setGlobalCursorStyle } from "./utils/cursor";
29
28
  import debounce from "./utils/debounce";
30
29
  import { determinePivotIndices } from "./utils/determinePivotIndices";
31
30
  import { getResizeHandleElement } from "./utils/dom/getResizeHandleElement";
@@ -571,6 +570,14 @@ function PanelGroupWithForwardedRef({
571
570
  }
572
571
  });
573
572
 
573
+ // Reset the cached layout if hidden by the Activity/Offscreen API
574
+ useIsomorphicLayoutEffect(() => {
575
+ const eagerValues = eagerValuesRef.current;
576
+ return () => {
577
+ eagerValues.layout = [];
578
+ };
579
+ }, []);
580
+
574
581
  const registerResizeHandle = useCallback((dragHandleId: string) => {
575
582
  return function resizeHandler(event: ResizeEvent) {
576
583
  event.preventDefault();
@@ -3,6 +3,7 @@ import { act } from "react-dom/test-utils";
3
3
  import { Panel, PanelGroup, PanelResizeHandle } from ".";
4
4
  import { assert } from "./utils/assert";
5
5
  import { getResizeHandleElement } from "./utils/dom/getResizeHandleElement";
6
+ import { dispatchPointerEvent } from "./utils/test-utils";
6
7
 
7
8
  describe("PanelResizeHandle", () => {
8
9
  let expectedWarnings: string[] = [];
@@ -68,7 +69,45 @@ describe("PanelResizeHandle", () => {
68
69
 
69
70
  describe("callbacks", () => {
70
71
  describe("onDragging", () => {
71
- // TODO: Test this
72
+ it("should fire when dragging starts/stops", async () => {
73
+ const onDragging = jest.fn();
74
+
75
+ act(() => {
76
+ root.render(
77
+ <PanelGroup direction="horizontal">
78
+ <Panel />
79
+ <PanelResizeHandle
80
+ id="handle"
81
+ onDragging={onDragging}
82
+ tabIndex={123}
83
+ title="bar"
84
+ />
85
+ <Panel />
86
+ </PanelGroup>
87
+ );
88
+ });
89
+
90
+ const handleElement = container.querySelector(
91
+ '[data-panel-resize-handle-id="handle"]'
92
+ ) as HTMLElement;
93
+
94
+ act(() => {
95
+ dispatchPointerEvent("mouseover", handleElement);
96
+ });
97
+ expect(onDragging).not.toHaveBeenCalled();
98
+
99
+ act(() => {
100
+ dispatchPointerEvent("mousedown", handleElement);
101
+ });
102
+ expect(onDragging).toHaveBeenCalledTimes(1);
103
+ expect(onDragging).toHaveBeenCalledWith(true);
104
+
105
+ act(() => {
106
+ dispatchPointerEvent("mouseup", handleElement);
107
+ });
108
+ expect(onDragging).toHaveBeenCalledTimes(2);
109
+ expect(onDragging).toHaveBeenCalledWith(false);
110
+ });
72
111
  });
73
112
  });
74
113
  });
@@ -117,10 +117,20 @@ export function PanelResizeHandle({
117
117
  switch (action) {
118
118
  case "down": {
119
119
  startDragging(resizeHandleId, event);
120
+
121
+ const { onDragging } = callbacksRef.current;
122
+ if (onDragging) {
123
+ onDragging(true);
124
+ }
120
125
  break;
121
126
  }
122
127
  case "up": {
123
128
  stopDragging();
129
+
130
+ const { onDragging } = callbacksRef.current;
131
+ if (onDragging) {
132
+ onDragging(false);
133
+ }
124
134
  break;
125
135
  }
126
136
  }
@@ -68,8 +68,8 @@ export function useWindowSplitterResizeHandlerBehavior({
68
68
  ? index - 1
69
69
  : handles.length - 1
70
70
  : index + 1 < handles.length
71
- ? index + 1
72
- : 0;
71
+ ? index + 1
72
+ : 0;
73
73
 
74
74
  const nextHandle = handles[nextIndex] as HTMLElement;
75
75
  nextHandle.focus();
@@ -2,6 +2,33 @@ import { assert } from "./assert";
2
2
 
3
3
  const util = require("util");
4
4
 
5
+ export function dispatchPointerEvent(type: string, target: HTMLElement) {
6
+ const rect = target.getBoundingClientRect();
7
+
8
+ const clientX = rect.left + rect.width / 2;
9
+ const clientY = rect.top + rect.height / 2;
10
+
11
+ const event = new MouseEvent(type, {
12
+ bubbles: true,
13
+ clientX,
14
+ clientY,
15
+ });
16
+ Object.defineProperties(event, {
17
+ pageX: {
18
+ get() {
19
+ return clientX;
20
+ },
21
+ },
22
+ pageY: {
23
+ get() {
24
+ return clientY;
25
+ },
26
+ },
27
+ });
28
+
29
+ target.dispatchEvent(event);
30
+ }
31
+
5
32
  export function expectToBeCloseToArray(
6
33
  actualNumbers: number[],
7
34
  expectedNumbers: number[]