react-resizable-panels 0.0.54 → 0.0.56

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 (86) hide show
  1. package/.eslintrc.cjs +26 -0
  2. package/CHANGELOG.md +253 -80
  3. package/README.md +55 -49
  4. package/dist/declarations/src/Panel.d.ts +76 -20
  5. package/dist/declarations/src/PanelGroup.d.ts +29 -21
  6. package/dist/declarations/src/PanelResizeHandle.d.ts +1 -1
  7. package/dist/declarations/src/index.d.ts +5 -5
  8. package/dist/declarations/src/types.d.ts +3 -25
  9. package/dist/declarations/src/vendor/react.d.ts +4 -4
  10. package/dist/react-resizable-panels.browser.cjs.js +1279 -796
  11. package/dist/react-resizable-panels.browser.development.cjs.js +1404 -809
  12. package/dist/react-resizable-panels.browser.development.esm.js +1398 -803
  13. package/dist/react-resizable-panels.browser.esm.js +1279 -796
  14. package/dist/react-resizable-panels.cjs.js +1279 -796
  15. package/dist/react-resizable-panels.cjs.js.map +1 -0
  16. package/dist/react-resizable-panels.development.cjs.js +1399 -804
  17. package/dist/react-resizable-panels.development.esm.js +1400 -805
  18. package/dist/react-resizable-panels.development.node.cjs.js +1172 -755
  19. package/dist/react-resizable-panels.development.node.esm.js +1173 -756
  20. package/dist/react-resizable-panels.esm.js +1279 -796
  21. package/dist/react-resizable-panels.esm.js.map +1 -0
  22. package/dist/react-resizable-panels.node.cjs.js +1064 -749
  23. package/dist/react-resizable-panels.node.esm.js +1065 -750
  24. package/jest.config.js +10 -0
  25. package/package.json +3 -1
  26. package/src/Panel.test.tsx +308 -0
  27. package/src/Panel.ts +179 -127
  28. package/src/PanelGroup.test.tsx +210 -0
  29. package/src/PanelGroup.ts +751 -580
  30. package/src/PanelGroupContext.ts +33 -0
  31. package/src/PanelResizeHandle.ts +13 -8
  32. package/src/hooks/useUniqueId.ts +1 -1
  33. package/src/hooks/useWindowSplitterBehavior.ts +9 -161
  34. package/src/hooks/useWindowSplitterPanelGroupBehavior.ts +185 -0
  35. package/src/index.ts +24 -11
  36. package/src/types.ts +3 -29
  37. package/src/utils/adjustLayoutByDelta.test.ts +1808 -0
  38. package/src/utils/adjustLayoutByDelta.ts +211 -0
  39. package/src/utils/calculateAriaValues.test.ts +111 -0
  40. package/src/utils/calculateAriaValues.ts +67 -0
  41. package/src/utils/calculateDeltaPercentage.ts +68 -0
  42. package/src/utils/calculateDragOffsetPercentage.ts +30 -0
  43. package/src/utils/calculateUnsafeDefaultLayout.test.ts +92 -0
  44. package/src/utils/calculateUnsafeDefaultLayout.ts +55 -0
  45. package/src/utils/callPanelCallbacks.ts +81 -0
  46. package/src/utils/compareLayouts.test.ts +9 -0
  47. package/src/utils/compareLayouts.ts +12 -0
  48. package/src/utils/computePanelFlexBoxStyle.ts +44 -0
  49. package/src/utils/computePercentagePanelConstraints.test.ts +71 -0
  50. package/src/utils/computePercentagePanelConstraints.ts +56 -0
  51. package/src/utils/convertPercentageToPixels.test.ts +9 -0
  52. package/src/utils/convertPercentageToPixels.ts +6 -0
  53. package/src/utils/convertPixelConstraintsToPercentages.ts +55 -0
  54. package/src/utils/convertPixelsToPercentage.test.ts +9 -0
  55. package/src/utils/convertPixelsToPercentage.ts +6 -0
  56. package/src/utils/determinePivotIndices.ts +10 -0
  57. package/src/utils/dom/calculateAvailablePanelSizeInPixels.ts +29 -0
  58. package/src/utils/dom/getAvailableGroupSizePixels.ts +29 -0
  59. package/src/utils/dom/getPanelElement.ts +7 -0
  60. package/src/utils/dom/getPanelGroupElement.ts +7 -0
  61. package/src/utils/dom/getResizeHandleElement.ts +9 -0
  62. package/src/utils/dom/getResizeHandleElementIndex.ts +12 -0
  63. package/src/utils/dom/getResizeHandleElementsForGroup.ts +9 -0
  64. package/src/utils/dom/getResizeHandlePanelIds.ts +18 -0
  65. package/src/utils/events.ts +13 -0
  66. package/src/utils/getPercentageSizeFromMixedSizes.test.ts +47 -0
  67. package/src/utils/getPercentageSizeFromMixedSizes.ts +15 -0
  68. package/src/utils/getResizeEventCursorPosition.ts +19 -0
  69. package/src/utils/initializeDefaultStorage.ts +26 -0
  70. package/src/utils/numbers/fuzzyCompareNumbers.test.ts +16 -0
  71. package/src/utils/numbers/fuzzyCompareNumbers.ts +17 -0
  72. package/src/utils/numbers/fuzzyNumbersEqual.ts +9 -0
  73. package/src/utils/resizePanel.ts +41 -0
  74. package/src/utils/serialization.ts +9 -4
  75. package/src/utils/shouldMonitorPixelBasedConstraints.test.ts +23 -0
  76. package/src/utils/shouldMonitorPixelBasedConstraints.ts +13 -0
  77. package/src/utils/test-utils.ts +136 -0
  78. package/src/utils/validatePanelConstraints.test.ts +151 -0
  79. package/src/utils/validatePanelConstraints.ts +103 -0
  80. package/src/utils/validatePanelGroupLayout.test.ts +233 -0
  81. package/src/utils/validatePanelGroupLayout.ts +88 -0
  82. package/src/vendor/react.ts +4 -0
  83. package/.eslintrc.json +0 -22
  84. package/src/PanelContexts.ts +0 -20
  85. package/src/utils/coordinates.ts +0 -149
  86. package/src/utils/group.ts +0 -315
@@ -1,315 +0,0 @@
1
- import { InitialDragState } from "../PanelGroup";
2
- import { PRECISION } from "../constants";
3
- import { PanelData, ResizeEvent } from "../types";
4
-
5
- export function adjustByDelta(
6
- event: ResizeEvent | null,
7
- panels: Map<string, PanelData>,
8
- idBefore: string,
9
- idAfter: string,
10
- delta: number,
11
- prevSizes: number[],
12
- panelSizeBeforeCollapse: Map<string, number>,
13
- initialDragState: InitialDragState | null
14
- ): number[] {
15
- const { sizes: initialSizes } = initialDragState || {};
16
-
17
- // If we're resizing by mouse or touch, use the initial sizes as a base.
18
- // This has the benefit of causing force-collapsed panels to spring back open if drag is reversed.
19
- const baseSizes = initialSizes || prevSizes;
20
-
21
- if (delta === 0) {
22
- return baseSizes;
23
- }
24
-
25
- const panelsArray = panelsMapToSortedArray(panels);
26
-
27
- const nextSizes = baseSizes.concat();
28
-
29
- let deltaApplied = 0;
30
-
31
- // A resizing panel affects the panels before or after it.
32
- //
33
- // A negative delta means the panel immediately after the resizer should grow/expand by decreasing its offset.
34
- // Other panels may also need to shrink/contract (and shift) to make room, depending on the min weights.
35
- //
36
- // A positive delta means the panel immediately before the resizer should "expand".
37
- // This is accomplished by shrinking/contracting (and shifting) one or more of the panels after the resizer.
38
-
39
- // Max-bounds check the panel being expanded first.
40
- {
41
- const pivotId = delta < 0 ? idAfter : idBefore;
42
- const index = panelsArray.findIndex(
43
- (panel) => panel.current.id === pivotId
44
- );
45
- const panel = panelsArray[index];
46
- const baseSize = baseSizes[index];
47
-
48
- const nextSize = safeResizePanel(panel, Math.abs(delta), baseSize, event);
49
- if (baseSize === nextSize) {
50
- // If there's no room for the pivot panel to grow, we can ignore this drag update.
51
- return baseSizes;
52
- } else {
53
- if (nextSize === 0 && baseSize > 0) {
54
- panelSizeBeforeCollapse.set(pivotId, baseSize);
55
- }
56
-
57
- delta = delta < 0 ? baseSize - nextSize : nextSize - baseSize;
58
- }
59
- }
60
-
61
- let pivotId = delta < 0 ? idBefore : idAfter;
62
- let index = panelsArray.findIndex((panel) => panel.current.id === pivotId);
63
- while (true) {
64
- const panel = panelsArray[index];
65
- const baseSize = baseSizes[index];
66
-
67
- const deltaRemaining = Math.abs(delta) - Math.abs(deltaApplied);
68
-
69
- const nextSize = safeResizePanel(
70
- panel,
71
- 0 - deltaRemaining,
72
- baseSize,
73
- event
74
- );
75
- if (baseSize !== nextSize) {
76
- if (nextSize === 0 && baseSize > 0) {
77
- panelSizeBeforeCollapse.set(panel.current.id, baseSize);
78
- }
79
-
80
- deltaApplied += baseSize - nextSize;
81
-
82
- nextSizes[index] = nextSize;
83
-
84
- if (
85
- deltaApplied
86
- .toPrecision(PRECISION)
87
- .localeCompare(Math.abs(delta).toPrecision(PRECISION), undefined, {
88
- numeric: true,
89
- }) >= 0
90
- ) {
91
- break;
92
- }
93
- }
94
-
95
- if (delta < 0) {
96
- if (--index < 0) {
97
- break;
98
- }
99
- } else {
100
- if (++index >= panelsArray.length) {
101
- break;
102
- }
103
- }
104
- }
105
-
106
- // If we were unable to resize any of the panels panels, return the previous state.
107
- // This will essentially bailout and ignore the "mousemove" event.
108
- if (deltaApplied === 0) {
109
- return baseSizes;
110
- }
111
-
112
- // Adjust the pivot panel before, but only by the amount that surrounding panels were able to shrink/contract.
113
- pivotId = delta < 0 ? idAfter : idBefore;
114
- index = panelsArray.findIndex((panel) => panel.current.id === pivotId);
115
- nextSizes[index] = baseSizes[index] + deltaApplied;
116
-
117
- return nextSizes;
118
- }
119
-
120
- export function callPanelCallbacks(
121
- panelsArray: PanelData[],
122
- sizes: number[],
123
- panelIdToLastNotifiedSizeMap: Record<string, number>
124
- ) {
125
- sizes.forEach((size, index) => {
126
- const panelRef = panelsArray[index];
127
- if (!panelRef) {
128
- // Handle initial mount (when panels are registered too late to be in the panels array)
129
- // The subsequent render+effects will handle the resize notification
130
- return;
131
- }
132
-
133
- const { callbacksRef, collapsedSize, collapsible, id } = panelRef.current;
134
-
135
- const lastNotifiedSize = panelIdToLastNotifiedSizeMap[id];
136
- if (lastNotifiedSize !== size) {
137
- panelIdToLastNotifiedSizeMap[id] = size;
138
-
139
- const { onCollapse, onResize } = callbacksRef.current!;
140
-
141
- if (onResize) {
142
- onResize(size, lastNotifiedSize);
143
- }
144
-
145
- if (collapsible && onCollapse) {
146
- if (
147
- (lastNotifiedSize == null || lastNotifiedSize === collapsedSize) &&
148
- size !== collapsedSize
149
- ) {
150
- onCollapse(false);
151
- } else if (
152
- lastNotifiedSize !== collapsedSize &&
153
- size === collapsedSize
154
- ) {
155
- onCollapse(true);
156
- }
157
- }
158
- }
159
- });
160
- }
161
-
162
- export function getBeforeAndAfterIds(
163
- id: string,
164
- panelsArray: PanelData[]
165
- ): [idBefore: string | null, idAFter: string | null] {
166
- if (panelsArray.length < 2) {
167
- return [null, null];
168
- }
169
-
170
- const index = panelsArray.findIndex((panel) => panel.current.id === id);
171
- if (index < 0) {
172
- return [null, null];
173
- }
174
-
175
- const isLastPanel = index === panelsArray.length - 1;
176
- const idBefore = isLastPanel ? panelsArray[index - 1].current.id : id;
177
- const idAfter = isLastPanel ? id : panelsArray[index + 1].current.id;
178
-
179
- return [idBefore, idAfter];
180
- }
181
-
182
- // This method returns a number between 1 and 100 representing
183
- // the % of the group's overall space this panel should occupy.
184
- export function getFlexGrow(
185
- panels: Map<string, PanelData>,
186
- id: string,
187
- sizes: number[]
188
- ): string {
189
- if (panels.size === 1) {
190
- return "100";
191
- }
192
-
193
- const panelsArray = panelsMapToSortedArray(panels);
194
-
195
- const index = panelsArray.findIndex((panel) => panel.current.id === id);
196
- const size = sizes[index];
197
- if (size == null) {
198
- return "0";
199
- }
200
-
201
- return size.toPrecision(PRECISION);
202
- }
203
-
204
- export function getPanel(id: string): HTMLDivElement | null {
205
- const element = document.querySelector(`[data-panel-id="${id}"]`);
206
- if (element) {
207
- return element as HTMLDivElement;
208
- }
209
- return null;
210
- }
211
-
212
- export function getPanelGroup(id: string): HTMLDivElement | null {
213
- const element = document.querySelector(`[data-panel-group-id="${id}"]`);
214
- if (element) {
215
- return element as HTMLDivElement;
216
- }
217
- return null;
218
- }
219
-
220
- export function getResizeHandle(id: string): HTMLDivElement | null {
221
- const element = document.querySelector(
222
- `[data-panel-resize-handle-id="${id}"]`
223
- );
224
- if (element) {
225
- return element as HTMLDivElement;
226
- }
227
- return null;
228
- }
229
-
230
- export function getResizeHandleIndex(id: string): number | null {
231
- const handles = getResizeHandles();
232
- const index = handles.findIndex(
233
- (handle) => handle.getAttribute("data-panel-resize-handle-id") === id
234
- );
235
- return index ?? null;
236
- }
237
-
238
- export function getResizeHandles(): HTMLDivElement[] {
239
- return Array.from(document.querySelectorAll(`[data-panel-resize-handle-id]`));
240
- }
241
-
242
- export function getResizeHandlesForGroup(groupId: string): HTMLDivElement[] {
243
- return Array.from(
244
- document.querySelectorAll(
245
- `[data-panel-resize-handle-id][data-panel-group-id="${groupId}"]`
246
- )
247
- );
248
- }
249
-
250
- export function getResizeHandlePanelIds(
251
- groupId: string,
252
- handleId: string,
253
- panelsArray: PanelData[]
254
- ): [idBefore: string | null, idAfter: string | null] {
255
- const handle = getResizeHandle(handleId);
256
- const handles = getResizeHandlesForGroup(groupId);
257
- const index = handle ? handles.indexOf(handle) : -1;
258
-
259
- const idBefore: string | null = panelsArray[index]?.current?.id ?? null;
260
- const idAfter: string | null = panelsArray[index + 1]?.current?.id ?? null;
261
-
262
- return [idBefore, idAfter];
263
- }
264
-
265
- export function panelsMapToSortedArray(
266
- panels: Map<string, PanelData>
267
- ): PanelData[] {
268
- return Array.from(panels.values()).sort((panelA, panelB) => {
269
- const orderA = panelA.current.order;
270
- const orderB = panelB.current.order;
271
- if (orderA == null && orderB == null) {
272
- return 0;
273
- } else if (orderA == null) {
274
- return -1;
275
- } else if (orderB == null) {
276
- return 1;
277
- } else {
278
- return orderA - orderB;
279
- }
280
- });
281
- }
282
-
283
- function safeResizePanel(
284
- panel: PanelData,
285
- delta: number,
286
- prevSize: number,
287
- event: ResizeEvent | null
288
- ): number {
289
- const nextSizeUnsafe = prevSize + delta;
290
-
291
- const { collapsedSize, collapsible, maxSize, minSize } = panel.current;
292
-
293
- if (collapsible) {
294
- if (prevSize > collapsedSize) {
295
- // Mimic VS COde behavior; collapse a panel if it's smaller than half of its min-size
296
- if (nextSizeUnsafe <= minSize / 2 + collapsedSize) {
297
- return collapsedSize;
298
- }
299
- } else {
300
- const isKeyboardEvent = event?.type?.startsWith("key");
301
- if (!isKeyboardEvent) {
302
- // Keyboard events should expand a collapsed panel to the min size,
303
- // but mouse events should wait until the panel has reached its min size
304
- // to avoid a visual flickering when dragging between collapsed and min size.
305
- if (nextSizeUnsafe < minSize) {
306
- return collapsedSize;
307
- }
308
- }
309
- }
310
- }
311
-
312
- const nextSize = Math.min(maxSize, Math.max(minSize, nextSizeUnsafe));
313
-
314
- return nextSize;
315
- }