react-resizable-panels 2.1.6 → 2.1.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 (92) hide show
  1. package/README.md +4 -0
  2. package/dist/declarations/src/Panel.d.ts +1 -1
  3. package/dist/declarations/src/PanelGroup.d.ts +1 -1
  4. package/dist/declarations/src/PanelResizeHandle.d.ts +6 -3
  5. package/dist/react-resizable-panels.browser.cjs.js +90 -94
  6. package/dist/react-resizable-panels.browser.development.cjs.js +90 -94
  7. package/dist/react-resizable-panels.browser.development.esm.js +38 -41
  8. package/dist/react-resizable-panels.browser.esm.js +38 -41
  9. package/dist/react-resizable-panels.cjs.js +90 -94
  10. package/dist/react-resizable-panels.development.cjs.js +90 -94
  11. package/dist/react-resizable-panels.development.esm.js +38 -41
  12. package/dist/react-resizable-panels.development.node.cjs.js +89 -92
  13. package/dist/react-resizable-panels.development.node.esm.js +37 -39
  14. package/dist/react-resizable-panels.esm.js +38 -41
  15. package/dist/react-resizable-panels.node.cjs.js +89 -92
  16. package/dist/react-resizable-panels.node.esm.js +37 -39
  17. package/package.json +7 -2
  18. package/.eslintrc.cjs +0 -27
  19. package/CHANGELOG.md +0 -574
  20. package/dist/declarations/src/vendor/react.d.ts +0 -7
  21. package/jest.config.js +0 -10
  22. package/src/Panel.test.tsx +0 -1084
  23. package/src/Panel.ts +0 -259
  24. package/src/PanelGroup.test.tsx +0 -443
  25. package/src/PanelGroup.ts +0 -999
  26. package/src/PanelGroupContext.ts +0 -42
  27. package/src/PanelResizeHandle.test.tsx +0 -367
  28. package/src/PanelResizeHandle.ts +0 -246
  29. package/src/PanelResizeHandleRegistry.ts +0 -336
  30. package/src/constants.ts +0 -1
  31. package/src/env-conditions/browser.ts +0 -1
  32. package/src/env-conditions/development.ts +0 -1
  33. package/src/env-conditions/node.ts +0 -1
  34. package/src/env-conditions/production.ts +0 -1
  35. package/src/env-conditions/unknown.ts +0 -1
  36. package/src/hooks/useForceUpdate.ts +0 -7
  37. package/src/hooks/useIsomorphicEffect.ts +0 -8
  38. package/src/hooks/useUniqueId.ts +0 -19
  39. package/src/hooks/useWindowSplitterBehavior.ts +0 -90
  40. package/src/hooks/useWindowSplitterPanelGroupBehavior.ts +0 -201
  41. package/src/index.ts +0 -77
  42. package/src/types.ts +0 -5
  43. package/src/utils/adjustLayoutByDelta.test.ts +0 -2061
  44. package/src/utils/adjustLayoutByDelta.ts +0 -308
  45. package/src/utils/arrays.ts +0 -13
  46. package/src/utils/assert.ts +0 -10
  47. package/src/utils/calculateAriaValues.test.ts +0 -106
  48. package/src/utils/calculateAriaValues.ts +0 -45
  49. package/src/utils/calculateDeltaPercentage.ts +0 -63
  50. package/src/utils/calculateDragOffsetPercentage.ts +0 -40
  51. package/src/utils/calculateUnsafeDefaultLayout.test.ts +0 -87
  52. package/src/utils/calculateUnsafeDefaultLayout.ts +0 -50
  53. package/src/utils/callPanelCallbacks.ts +0 -49
  54. package/src/utils/compareLayouts.test.ts +0 -9
  55. package/src/utils/compareLayouts.ts +0 -12
  56. package/src/utils/computePanelFlexBoxStyle.test.ts +0 -123
  57. package/src/utils/computePanelFlexBoxStyle.ts +0 -50
  58. package/src/utils/csp.ts +0 -9
  59. package/src/utils/cursor.ts +0 -103
  60. package/src/utils/debounce.ts +0 -18
  61. package/src/utils/determinePivotIndices.ts +0 -15
  62. package/src/utils/dom/getPanelElement.ts +0 -10
  63. package/src/utils/dom/getPanelElementsForGroup.ts +0 -8
  64. package/src/utils/dom/getPanelGroupElement.ts +0 -21
  65. package/src/utils/dom/getResizeHandleElement.ts +0 -10
  66. package/src/utils/dom/getResizeHandleElementIndex.ts +0 -13
  67. package/src/utils/dom/getResizeHandleElementsForGroup.ts +0 -10
  68. package/src/utils/dom/getResizeHandlePanelIds.ts +0 -19
  69. package/src/utils/events/getResizeEventCoordinates.ts +0 -23
  70. package/src/utils/events/getResizeEventCursorPosition.ts +0 -14
  71. package/src/utils/events/index.ts +0 -13
  72. package/src/utils/getInputType.ts +0 -5
  73. package/src/utils/initializeDefaultStorage.ts +0 -26
  74. package/src/utils/numbers/fuzzyCompareNumbers.test.ts +0 -16
  75. package/src/utils/numbers/fuzzyCompareNumbers.ts +0 -21
  76. package/src/utils/numbers/fuzzyLayoutsEqual.ts +0 -22
  77. package/src/utils/numbers/fuzzyNumbersEqual.ts +0 -9
  78. package/src/utils/rects/getIntersectingRectangle.test.ts +0 -198
  79. package/src/utils/rects/getIntersectingRectangle.ts +0 -28
  80. package/src/utils/rects/intersects.test.ts +0 -197
  81. package/src/utils/rects/intersects.ts +0 -23
  82. package/src/utils/rects/types.ts +0 -6
  83. package/src/utils/resizePanel.test.ts +0 -59
  84. package/src/utils/resizePanel.ts +0 -47
  85. package/src/utils/serialization.ts +0 -87
  86. package/src/utils/test-utils.ts +0 -205
  87. package/src/utils/validatePanelConstraints.test.ts +0 -143
  88. package/src/utils/validatePanelConstraints.ts +0 -69
  89. package/src/utils/validatePanelGroupLayout.test.ts +0 -148
  90. package/src/utils/validatePanelGroupLayout.ts +0 -95
  91. package/src/vendor/react.ts +0 -73
  92. package/src/vendor/stacking-order.ts +0 -139
@@ -1,308 +0,0 @@
1
- import { PanelConstraints } from "../Panel";
2
- import { assert } from "./assert";
3
- import { fuzzyCompareNumbers } from "./numbers/fuzzyCompareNumbers";
4
- import { fuzzyLayoutsEqual } from "./numbers/fuzzyLayoutsEqual";
5
- import { fuzzyNumbersEqual } from "./numbers/fuzzyNumbersEqual";
6
- import { resizePanel } from "./resizePanel";
7
-
8
- // All units must be in percentages; pixel values should be pre-converted
9
- export function adjustLayoutByDelta({
10
- delta,
11
- initialLayout,
12
- panelConstraints: panelConstraintsArray,
13
- pivotIndices,
14
- prevLayout,
15
- trigger,
16
- }: {
17
- delta: number;
18
- initialLayout: number[];
19
- panelConstraints: PanelConstraints[];
20
- pivotIndices: number[];
21
- prevLayout: number[];
22
- trigger: "imperative-api" | "keyboard" | "mouse-or-touch";
23
- }): number[] {
24
- if (fuzzyNumbersEqual(delta, 0)) {
25
- return initialLayout;
26
- }
27
-
28
- const nextLayout = [...initialLayout];
29
-
30
- const [firstPivotIndex, secondPivotIndex] = pivotIndices;
31
- assert(firstPivotIndex != null, "Invalid first pivot index");
32
- assert(secondPivotIndex != null, "Invalid second pivot index");
33
-
34
- let deltaApplied = 0;
35
-
36
- // const DEBUG = [];
37
- // DEBUG.push(`adjustLayoutByDelta()`);
38
- // DEBUG.push(` initialLayout: ${initialLayout.join(", ")}`);
39
- // DEBUG.push(` prevLayout: ${prevLayout.join(", ")}`);
40
- // DEBUG.push(` delta: ${delta}`);
41
- // DEBUG.push(` pivotIndices: ${pivotIndices.join(", ")}`);
42
- // DEBUG.push(` trigger: ${trigger}`);
43
- // DEBUG.push("");
44
-
45
- // A resizing panel affects the panels before or after it.
46
- //
47
- // A negative delta means the panel(s) immediately after the resize handle should grow/expand by decreasing its offset.
48
- // Other panels may also need to shrink/contract (and shift) to make room, depending on the min weights.
49
- //
50
- // A positive delta means the panel(s) immediately before the resize handle should "expand".
51
- // This is accomplished by shrinking/contracting (and shifting) one or more of the panels after the resize handle.
52
-
53
- {
54
- // If this is a resize triggered by a keyboard event, our logic for expanding/collapsing is different.
55
- // We no longer check the halfway threshold because this may prevent the panel from expanding at all.
56
- if (trigger === "keyboard") {
57
- {
58
- // Check if we should expand a collapsed panel
59
- const index = delta < 0 ? secondPivotIndex : firstPivotIndex;
60
- const panelConstraints = panelConstraintsArray[index];
61
- assert(
62
- panelConstraints,
63
- `Panel constraints not found for index ${index}`
64
- );
65
-
66
- const {
67
- collapsedSize = 0,
68
- collapsible,
69
- minSize = 0,
70
- } = panelConstraints;
71
-
72
- // DEBUG.push(`edge case check 1: ${index}`);
73
- // DEBUG.push(` -> collapsible? ${collapsible}`);
74
- if (collapsible) {
75
- const prevSize = initialLayout[index];
76
- assert(
77
- prevSize != null,
78
- `Previous layout not found for panel index ${index}`
79
- );
80
-
81
- if (fuzzyNumbersEqual(prevSize, collapsedSize)) {
82
- const localDelta = minSize - prevSize;
83
- // DEBUG.push(` -> expand delta: ${localDelta}`);
84
-
85
- if (fuzzyCompareNumbers(localDelta, Math.abs(delta)) > 0) {
86
- delta = delta < 0 ? 0 - localDelta : localDelta;
87
- // DEBUG.push(` -> delta: ${delta}`);
88
- }
89
- }
90
- }
91
- }
92
-
93
- {
94
- // Check if we should collapse a panel at its minimum size
95
- const index = delta < 0 ? firstPivotIndex : secondPivotIndex;
96
- const panelConstraints = panelConstraintsArray[index];
97
- assert(
98
- panelConstraints,
99
- `No panel constraints found for index ${index}`
100
- );
101
-
102
- const {
103
- collapsedSize = 0,
104
- collapsible,
105
- minSize = 0,
106
- } = panelConstraints;
107
-
108
- // DEBUG.push(`edge case check 2: ${index}`);
109
- // DEBUG.push(` -> collapsible? ${collapsible}`);
110
- if (collapsible) {
111
- const prevSize = initialLayout[index];
112
- assert(
113
- prevSize != null,
114
- `Previous layout not found for panel index ${index}`
115
- );
116
-
117
- if (fuzzyNumbersEqual(prevSize, minSize)) {
118
- const localDelta = prevSize - collapsedSize;
119
- // DEBUG.push(` -> expand delta: ${localDelta}`);
120
-
121
- if (fuzzyCompareNumbers(localDelta, Math.abs(delta)) > 0) {
122
- delta = delta < 0 ? 0 - localDelta : localDelta;
123
- // DEBUG.push(` -> delta: ${delta}`);
124
- }
125
- }
126
- }
127
- }
128
- }
129
- // DEBUG.push("");
130
- }
131
-
132
- {
133
- // Pre-calculate max available delta in the opposite direction of our pivot.
134
- // This will be the maximum amount we're allowed to expand/contract the panels in the primary direction.
135
- // If this amount is less than the requested delta, adjust the requested delta.
136
- // If this amount is greater than the requested delta, that's useful information too–
137
- // as an expanding panel might change from collapsed to min size.
138
-
139
- const increment = delta < 0 ? 1 : -1;
140
-
141
- let index = delta < 0 ? secondPivotIndex : firstPivotIndex;
142
- let maxAvailableDelta = 0;
143
-
144
- // DEBUG.push("pre calc...");
145
- while (true) {
146
- const prevSize = initialLayout[index];
147
- assert(
148
- prevSize != null,
149
- `Previous layout not found for panel index ${index}`
150
- );
151
-
152
- const maxSafeSize = resizePanel({
153
- panelConstraints: panelConstraintsArray,
154
- panelIndex: index,
155
- size: 100,
156
- });
157
- const delta = maxSafeSize - prevSize;
158
- // DEBUG.push(` ${index}: ${prevSize} -> ${maxSafeSize}`);
159
-
160
- maxAvailableDelta += delta;
161
- index += increment;
162
-
163
- if (index < 0 || index >= panelConstraintsArray.length) {
164
- break;
165
- }
166
- }
167
-
168
- // DEBUG.push(` -> max available delta: ${maxAvailableDelta}`);
169
- const minAbsDelta = Math.min(Math.abs(delta), Math.abs(maxAvailableDelta));
170
- delta = delta < 0 ? 0 - minAbsDelta : minAbsDelta;
171
- // DEBUG.push(` -> adjusted delta: ${delta}`);
172
- // DEBUG.push("");
173
- }
174
-
175
- {
176
- // Delta added to a panel needs to be subtracted from other panels (within the constraints that those panels allow).
177
-
178
- const pivotIndex = delta < 0 ? firstPivotIndex : secondPivotIndex;
179
- let index = pivotIndex;
180
- while (index >= 0 && index < panelConstraintsArray.length) {
181
- const deltaRemaining = Math.abs(delta) - Math.abs(deltaApplied);
182
-
183
- const prevSize = initialLayout[index];
184
- assert(
185
- prevSize != null,
186
- `Previous layout not found for panel index ${index}`
187
- );
188
-
189
- const unsafeSize = prevSize - deltaRemaining;
190
- const safeSize = resizePanel({
191
- panelConstraints: panelConstraintsArray,
192
- panelIndex: index,
193
- size: unsafeSize,
194
- });
195
-
196
- if (!fuzzyNumbersEqual(prevSize, safeSize)) {
197
- deltaApplied += prevSize - safeSize;
198
-
199
- nextLayout[index] = safeSize;
200
-
201
- if (
202
- deltaApplied
203
- .toPrecision(3)
204
- .localeCompare(Math.abs(delta).toPrecision(3), undefined, {
205
- numeric: true,
206
- }) >= 0
207
- ) {
208
- break;
209
- }
210
- }
211
-
212
- if (delta < 0) {
213
- index--;
214
- } else {
215
- index++;
216
- }
217
- }
218
- }
219
- // DEBUG.push(`after 1: ${nextLayout.join(", ")}`);
220
- // DEBUG.push(` deltaApplied: ${deltaApplied}`);
221
- // DEBUG.push("");
222
-
223
- // If we were unable to resize any of the panels panels, return the previous state.
224
- // This will essentially bailout and ignore e.g. drags past a panel's boundaries
225
- if (fuzzyLayoutsEqual(prevLayout, nextLayout)) {
226
- // DEBUG.push(`bailout to previous layout: ${prevLayout.join(", ")}`);
227
- // console.log(DEBUG.join("\n"));
228
-
229
- return prevLayout;
230
- }
231
-
232
- {
233
- // Now distribute the applied delta to the panels in the other direction
234
- const pivotIndex = delta < 0 ? secondPivotIndex : firstPivotIndex;
235
-
236
- const prevSize = initialLayout[pivotIndex];
237
- assert(
238
- prevSize != null,
239
- `Previous layout not found for panel index ${pivotIndex}`
240
- );
241
-
242
- const unsafeSize = prevSize + deltaApplied;
243
- const safeSize = resizePanel({
244
- panelConstraints: panelConstraintsArray,
245
- panelIndex: pivotIndex,
246
- size: unsafeSize,
247
- });
248
-
249
- // Adjust the pivot panel before, but only by the amount that surrounding panels were able to shrink/contract.
250
- nextLayout[pivotIndex] = safeSize;
251
-
252
- // Edge case where expanding or contracting one panel caused another one to change collapsed state
253
- if (!fuzzyNumbersEqual(safeSize, unsafeSize)) {
254
- let deltaRemaining = unsafeSize - safeSize;
255
-
256
- const pivotIndex = delta < 0 ? secondPivotIndex : firstPivotIndex;
257
- let index = pivotIndex;
258
- while (index >= 0 && index < panelConstraintsArray.length) {
259
- const prevSize = nextLayout[index];
260
- assert(
261
- prevSize != null,
262
- `Previous layout not found for panel index ${index}`
263
- );
264
-
265
- const unsafeSize = prevSize + deltaRemaining;
266
- const safeSize = resizePanel({
267
- panelConstraints: panelConstraintsArray,
268
- panelIndex: index,
269
- size: unsafeSize,
270
- });
271
-
272
- if (!fuzzyNumbersEqual(prevSize, safeSize)) {
273
- deltaRemaining -= safeSize - prevSize;
274
-
275
- nextLayout[index] = safeSize;
276
- }
277
-
278
- if (fuzzyNumbersEqual(deltaRemaining, 0)) {
279
- break;
280
- }
281
-
282
- if (delta > 0) {
283
- index--;
284
- } else {
285
- index++;
286
- }
287
- }
288
- }
289
- }
290
- // DEBUG.push(`after 2: ${nextLayout.join(", ")}`);
291
- // DEBUG.push(` deltaApplied: ${deltaApplied}`);
292
- // DEBUG.push("");
293
-
294
- const totalSize = nextLayout.reduce((total, size) => size + total, 0);
295
- // DEBUG.push(`total size: ${totalSize}`);
296
-
297
- // If our new layout doesn't add up to 100%, that means the requested delta can't be applied
298
- // In that case, fall back to our most recent valid layout
299
- if (!fuzzyNumbersEqual(totalSize, 100)) {
300
- // DEBUG.push(`bailout to previous layout: ${prevLayout.join(", ")}`);
301
- // console.log(DEBUG.join("\n"));
302
-
303
- return prevLayout;
304
- }
305
-
306
- // console.log(DEBUG.join("\n"));
307
- return nextLayout;
308
- }
@@ -1,13 +0,0 @@
1
- export function areEqual(arrayA: any[], arrayB: any[]): boolean {
2
- if (arrayA.length !== arrayB.length) {
3
- return false;
4
- }
5
-
6
- for (let index = 0; index < arrayA.length; index++) {
7
- if (arrayA[index] !== arrayB[index]) {
8
- return false;
9
- }
10
- }
11
-
12
- return true;
13
- }
@@ -1,10 +0,0 @@
1
- export function assert(
2
- expectedCondition: any,
3
- message: string
4
- ): asserts expectedCondition {
5
- if (!expectedCondition) {
6
- console.error(message);
7
-
8
- throw Error(message);
9
- }
10
- }
@@ -1,106 +0,0 @@
1
- import { PanelConstraints, PanelData } from "../Panel";
2
- import { calculateAriaValues } from "./calculateAriaValues";
3
-
4
- describe("calculateAriaValues", () => {
5
- let idCounter = 0;
6
- let orderCounter = 0;
7
-
8
- function createPanelData(constraints: PanelConstraints = {}): PanelData {
9
- return {
10
- callbacks: {
11
- onCollapse: undefined,
12
- onExpand: undefined,
13
- onResize: undefined,
14
- },
15
- constraints,
16
- id: `${idCounter++}`,
17
- idIsFromProps: false,
18
- order: orderCounter++,
19
- };
20
- }
21
-
22
- beforeEach(() => {
23
- idCounter = 0;
24
- orderCounter = 0;
25
- });
26
-
27
- it("should work correctly for panels with no min/max constraints", () => {
28
- expect(
29
- calculateAriaValues({
30
- layout: [50, 50],
31
- panelsArray: [createPanelData(), createPanelData()],
32
- pivotIndices: [0, 1],
33
- })
34
- ).toEqual({
35
- valueMax: 100,
36
- valueMin: 0,
37
- valueNow: 50,
38
- });
39
-
40
- expect(
41
- calculateAriaValues({
42
- layout: [20, 50, 30],
43
- panelsArray: [createPanelData(), createPanelData(), createPanelData()],
44
- pivotIndices: [0, 1],
45
- })
46
- ).toEqual({
47
- valueMax: 100,
48
- valueMin: 0,
49
- valueNow: 20,
50
- });
51
-
52
- expect(
53
- calculateAriaValues({
54
- layout: [20, 50, 30],
55
- panelsArray: [createPanelData(), createPanelData(), createPanelData()],
56
- pivotIndices: [1, 2],
57
- })
58
- ).toEqual({
59
- valueMax: 100,
60
- valueMin: 0,
61
- valueNow: 50,
62
- });
63
- });
64
-
65
- it("should work correctly for panels with min/max constraints", () => {
66
- expect(
67
- calculateAriaValues({
68
- layout: [25, 75],
69
- panelsArray: [
70
- createPanelData({
71
- maxSize: 35,
72
- minSize: 10,
73
- }),
74
- createPanelData(),
75
- ],
76
- pivotIndices: [0, 1],
77
- })
78
- ).toEqual({
79
- valueMax: 35,
80
- valueMin: 10,
81
- valueNow: 25,
82
- });
83
-
84
- expect(
85
- calculateAriaValues({
86
- layout: [25, 50, 25],
87
- panelsArray: [
88
- createPanelData({
89
- maxSize: 35,
90
- minSize: 10,
91
- }),
92
- createPanelData(),
93
- createPanelData({
94
- maxSize: 35,
95
- minSize: 10,
96
- }),
97
- ],
98
- pivotIndices: [1, 2],
99
- })
100
- ).toEqual({
101
- valueMax: 80,
102
- valueMin: 30,
103
- valueNow: 50,
104
- });
105
- });
106
- });
@@ -1,45 +0,0 @@
1
- import { PanelData } from "../Panel";
2
- import { assert } from "./assert";
3
-
4
- export function calculateAriaValues({
5
- layout,
6
- panelsArray,
7
- pivotIndices,
8
- }: {
9
- layout: number[];
10
- panelsArray: PanelData[];
11
- pivotIndices: number[];
12
- }) {
13
- let currentMinSize = 0;
14
- let currentMaxSize = 100;
15
- let totalMinSize = 0;
16
- let totalMaxSize = 0;
17
-
18
- const firstIndex = pivotIndices[0];
19
- assert(firstIndex != null, "No pivot index found");
20
-
21
- // A panel's effective min/max sizes also need to account for other panel's sizes.
22
- panelsArray.forEach((panelData, index) => {
23
- const { constraints } = panelData;
24
- const { maxSize = 100, minSize = 0 } = constraints;
25
-
26
- if (index === firstIndex) {
27
- currentMinSize = minSize;
28
- currentMaxSize = maxSize;
29
- } else {
30
- totalMinSize += minSize;
31
- totalMaxSize += maxSize;
32
- }
33
- });
34
-
35
- const valueMax = Math.min(currentMaxSize, 100 - totalMinSize);
36
- const valueMin = Math.max(currentMinSize, 100 - totalMaxSize);
37
-
38
- const valueNow = layout[firstIndex];
39
-
40
- return {
41
- valueMax,
42
- valueMin,
43
- valueNow,
44
- };
45
- }
@@ -1,63 +0,0 @@
1
- import { DragState, ResizeEvent } from "../PanelGroupContext";
2
- import { Direction } from "../types";
3
- import { calculateDragOffsetPercentage } from "./calculateDragOffsetPercentage";
4
- import { isKeyDown } from "./events";
5
-
6
- // https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/movementX
7
- export function calculateDeltaPercentage(
8
- event: ResizeEvent,
9
- dragHandleId: string,
10
- direction: Direction,
11
- initialDragState: DragState | null,
12
- keyboardResizeBy: number | null,
13
- panelGroupElement: HTMLElement
14
- ): number {
15
- if (isKeyDown(event)) {
16
- const isHorizontal = direction === "horizontal";
17
-
18
- let delta = 0;
19
- if (event.shiftKey) {
20
- delta = 100;
21
- } else if (keyboardResizeBy != null) {
22
- delta = keyboardResizeBy;
23
- } else {
24
- delta = 10;
25
- }
26
-
27
- let movement = 0;
28
- switch (event.key) {
29
- case "ArrowDown":
30
- movement = isHorizontal ? 0 : delta;
31
- break;
32
- case "ArrowLeft":
33
- movement = isHorizontal ? -delta : 0;
34
- break;
35
- case "ArrowRight":
36
- movement = isHorizontal ? delta : 0;
37
- break;
38
- case "ArrowUp":
39
- movement = isHorizontal ? 0 : -delta;
40
- break;
41
- case "End":
42
- movement = 100;
43
- break;
44
- case "Home":
45
- movement = -100;
46
- break;
47
- }
48
-
49
- return movement;
50
- } else {
51
- if (initialDragState == null) {
52
- return 0;
53
- }
54
-
55
- return calculateDragOffsetPercentage(
56
- event,
57
- dragHandleId,
58
- direction,
59
- initialDragState,
60
- panelGroupElement
61
- );
62
- }
63
- }
@@ -1,40 +0,0 @@
1
- import { DragState, ResizeEvent } from "../PanelGroupContext";
2
- import { Direction } from "../types";
3
- import { assert } from "./assert";
4
- import { getPanelGroupElement } from "./dom/getPanelGroupElement";
5
- import { getResizeHandleElement } from "./dom/getResizeHandleElement";
6
- import { getResizeEventCursorPosition } from "./events/getResizeEventCursorPosition";
7
-
8
- export function calculateDragOffsetPercentage(
9
- event: ResizeEvent,
10
- dragHandleId: string,
11
- direction: Direction,
12
- initialDragState: DragState,
13
- panelGroupElement: HTMLElement
14
- ): number {
15
- const isHorizontal = direction === "horizontal";
16
-
17
- const handleElement = getResizeHandleElement(dragHandleId, panelGroupElement);
18
- assert(
19
- handleElement,
20
- `No resize handle element found for id "${dragHandleId}"`
21
- );
22
-
23
- const groupId = handleElement.getAttribute("data-panel-group-id");
24
- assert(groupId, `Resize handle element has no group id attribute`);
25
-
26
- let { initialCursorPosition } = initialDragState;
27
-
28
- const cursorPosition = getResizeEventCursorPosition(direction, event);
29
-
30
- const groupElement = getPanelGroupElement(groupId, panelGroupElement);
31
- assert(groupElement, `No group element found for id "${groupId}"`);
32
-
33
- const groupRect = groupElement.getBoundingClientRect();
34
- const groupSizeInPixels = isHorizontal ? groupRect.width : groupRect.height;
35
-
36
- const offsetPixels = cursorPosition - initialCursorPosition;
37
- const offsetPercentage = (offsetPixels / groupSizeInPixels) * 100;
38
-
39
- return offsetPercentage;
40
- }
@@ -1,87 +0,0 @@
1
- import { PanelConstraints, PanelData } from "../Panel";
2
- import { calculateUnsafeDefaultLayout } from "./calculateUnsafeDefaultLayout";
3
- import { expectToBeCloseToArray } from "./test-utils";
4
-
5
- describe("calculateUnsafeDefaultLayout", () => {
6
- let idCounter = 0;
7
- let orderCounter = 0;
8
-
9
- function createPanelData(constraints: PanelConstraints = {}): PanelData {
10
- return {
11
- callbacks: {
12
- onCollapse: undefined,
13
- onExpand: undefined,
14
- onResize: undefined,
15
- },
16
- constraints,
17
- id: `${idCounter++}`,
18
- idIsFromProps: false,
19
- order: orderCounter++,
20
- };
21
- }
22
-
23
- beforeEach(() => {
24
- idCounter = 0;
25
- orderCounter = 0;
26
- });
27
-
28
- it("should assign even sizes for every panel by default", () => {
29
- expectToBeCloseToArray(
30
- calculateUnsafeDefaultLayout({
31
- panelDataArray: [createPanelData()],
32
- }),
33
- [100]
34
- );
35
-
36
- expectToBeCloseToArray(
37
- calculateUnsafeDefaultLayout({
38
- panelDataArray: [createPanelData(), createPanelData()],
39
- }),
40
- [50, 50]
41
- );
42
-
43
- expectToBeCloseToArray(
44
- calculateUnsafeDefaultLayout({
45
- panelDataArray: [
46
- createPanelData(),
47
- createPanelData(),
48
- createPanelData(),
49
- ],
50
- }),
51
- [33.3, 33.3, 33.3]
52
- );
53
- });
54
-
55
- it("should respect default panel size constraints", () => {
56
- expectToBeCloseToArray(
57
- calculateUnsafeDefaultLayout({
58
- panelDataArray: [
59
- createPanelData({
60
- defaultSize: 15,
61
- }),
62
- createPanelData({
63
- defaultSize: 85,
64
- }),
65
- ],
66
- }),
67
- [15, 85]
68
- );
69
- });
70
-
71
- it("should ignore min and max panel size constraints", () => {
72
- expectToBeCloseToArray(
73
- calculateUnsafeDefaultLayout({
74
- panelDataArray: [
75
- createPanelData({
76
- minSize: 40,
77
- }),
78
- createPanelData(),
79
- createPanelData({
80
- maxSize: 10,
81
- }),
82
- ],
83
- }),
84
- [33.3, 33.3, 33.3]
85
- );
86
- });
87
- });