react-resizable-panels 2.0.12 → 2.0.14

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.
@@ -144,6 +144,55 @@ describe("PanelGroup", () => {
144
144
  });
145
145
  verifyExpandedPanelGroupLayout(mostRecentLayout, [30, 70]);
146
146
  });
147
+
148
+ it("should report the correct state with collapsedSizes that have many decimal places", () => {
149
+ act(() => {
150
+ root.render(
151
+ <PanelGroup direction="horizontal">
152
+ <Panel
153
+ collapsedSize={3.8764385221078133}
154
+ collapsible
155
+ defaultSize={50}
156
+ minSize={15}
157
+ ref={leftPanelRef}
158
+ />
159
+ <PanelResizeHandle />
160
+ <Panel collapsible defaultSize={50} ref={rightPanelRef} />
161
+ </PanelGroup>
162
+ );
163
+ });
164
+
165
+ act(() => {
166
+ leftPanelRef.current?.collapse();
167
+ });
168
+ expect(leftPanelRef.current?.isCollapsed()).toBe(true);
169
+ expect(leftPanelRef.current?.isExpanded()).toBe(false);
170
+
171
+ act(() => {
172
+ root.render(
173
+ <PanelGroup direction="horizontal">
174
+ <Panel
175
+ collapsedSize={3.8764385221078132}
176
+ collapsible
177
+ defaultSize={50}
178
+ minSize={15}
179
+ ref={leftPanelRef}
180
+ />
181
+ <PanelResizeHandle />
182
+ <Panel collapsible defaultSize={50} ref={rightPanelRef} />
183
+ </PanelGroup>
184
+ );
185
+ });
186
+
187
+ expect(leftPanelRef.current?.isCollapsed()).toBe(true);
188
+ expect(leftPanelRef.current?.isExpanded()).toBe(false);
189
+
190
+ act(() => {
191
+ leftPanelRef.current?.expand();
192
+ });
193
+ expect(leftPanelRef.current?.isCollapsed()).toBe(false);
194
+ expect(leftPanelRef.current?.isExpanded()).toBe(true);
195
+ });
147
196
  });
148
197
 
149
198
  describe("resize", () => {
package/src/PanelGroup.ts CHANGED
@@ -31,6 +31,10 @@ import { getResizeHandleElement } from "./utils/dom/getResizeHandleElement";
31
31
  import { isKeyDown, isMouseEvent, isTouchEvent } from "./utils/events";
32
32
  import { getResizeEventCursorPosition } from "./utils/events/getResizeEventCursorPosition";
33
33
  import { initializeDefaultStorage } from "./utils/initializeDefaultStorage";
34
+ import {
35
+ fuzzyCompareNumbers,
36
+ fuzzyNumbersEqual,
37
+ } from "./utils/numbers/fuzzyCompareNumbers";
34
38
  import {
35
39
  loadPanelGroupState,
36
40
  savePanelGroupState,
@@ -341,7 +345,7 @@ function PanelGroupWithForwardedRef({
341
345
  `Panel size not found for panel "${panelData.id}"`
342
346
  );
343
347
 
344
- if (panelSize !== collapsedSize) {
348
+ if (!fuzzyNumbersEqual(panelSize, collapsedSize)) {
345
349
  // Store size before collapse;
346
350
  // This is the size that gets restored if the expand() API is used.
347
351
  panelSizeBeforeCollapseRef.current.set(panelData.id, panelSize);
@@ -393,12 +397,12 @@ function PanelGroupWithForwardedRef({
393
397
 
394
398
  const {
395
399
  collapsedSize = 0,
396
- panelSize,
400
+ panelSize = 0,
397
401
  minSize = 0,
398
402
  pivotIndices,
399
403
  } = panelDataHelper(panelDataArray, panelData, prevLayout);
400
404
 
401
- if (panelSize === collapsedSize) {
405
+ if (fuzzyNumbersEqual(panelSize, collapsedSize)) {
402
406
  // Restore this panel to the size it was before it was collapsed, if possible.
403
407
  const prevPanelSize = panelSizeBeforeCollapseRef.current.get(
404
408
  panelData.id
@@ -484,7 +488,12 @@ function PanelGroupWithForwardedRef({
484
488
  panelSize,
485
489
  } = panelDataHelper(panelDataArray, panelData, layout);
486
490
 
487
- return collapsible === true && panelSize === collapsedSize;
491
+ assert(
492
+ panelSize != null,
493
+ `Panel size not found for panel "${panelData.id}"`
494
+ );
495
+
496
+ return collapsible === true && fuzzyNumbersEqual(panelSize, collapsedSize);
488
497
  }, []);
489
498
 
490
499
  // External APIs are safe to memoize via committed values ref
@@ -502,7 +511,7 @@ function PanelGroupWithForwardedRef({
502
511
  `Panel size not found for panel "${panelData.id}"`
503
512
  );
504
513
 
505
- return !collapsible || panelSize > collapsedSize;
514
+ return !collapsible || fuzzyCompareNumbers(panelSize, collapsedSize) > 0;
506
515
  }, []);
507
516
 
508
517
  const registerPanel = useCallback((panelData: PanelData) => {
@@ -780,9 +789,9 @@ function PanelGroupWithForwardedRef({
780
789
  if (
781
790
  prevCollapsible &&
782
791
  nextCollapsible &&
783
- prevPanelSize === prevCollapsedSize
792
+ fuzzyNumbersEqual(prevPanelSize, prevCollapsedSize)
784
793
  ) {
785
- if (prevCollapsedSize !== nextCollapsedSize) {
794
+ if (!fuzzyNumbersEqual(prevCollapsedSize, nextCollapsedSize)) {
786
795
  resizePanel(panelData, nextCollapsedSize);
787
796
  } else {
788
797
  // Stay collapsed
@@ -5,13 +5,17 @@ export function fuzzyCompareNumbers(
5
5
  expected: number,
6
6
  fractionDigits: number = PRECISION
7
7
  ): number {
8
- actual = parseFloat(actual.toFixed(fractionDigits));
9
- expected = parseFloat(expected.toFixed(fractionDigits));
10
-
11
- const delta = actual - expected;
12
- if (delta === 0) {
8
+ if (actual.toFixed(fractionDigits) === expected.toFixed(fractionDigits)) {
13
9
  return 0;
14
10
  } else {
15
- return delta > 0 ? 1 : -1;
11
+ return actual > expected ? 1 : -1;
16
12
  }
17
13
  }
14
+
15
+ export function fuzzyNumbersEqual(
16
+ actual: number,
17
+ expected: number,
18
+ fractionDigits: number = PRECISION
19
+ ): boolean {
20
+ return fuzzyCompareNumbers(actual, expected, fractionDigits) === 0;
21
+ }
@@ -1,5 +1,6 @@
1
1
  // Forked from NPM stacking-order@2.0.0
2
2
  // Background at https://github.com/Rich-Harris/stacking-order/issues/3
3
+ // Background at https://github.com/Rich-Harris/stacking-order/issues/6
3
4
 
4
5
  import { assert } from "..";
5
6
 
@@ -61,7 +62,8 @@ const props =
61
62
 
62
63
  /** @param {HTMLElement} node */
63
64
  function is_flex_item(node: HTMLElement) {
64
- const display = getComputedStyle(get_parent(node)).display;
65
+ // @ts-ignore
66
+ const display = getComputedStyle(get_parent(node) ?? node).display;
65
67
  return display === "flex" || display === "inline-flex";
66
68
  }
67
69
 
@@ -115,11 +117,12 @@ function get_z_index(node: HTMLElement | null) {
115
117
  }
116
118
 
117
119
  /** @param {HTMLElement} node */
118
- function get_ancestors(node: HTMLElement) {
120
+ function get_ancestors(node: HTMLElement | null) {
119
121
  const ancestors = [];
120
122
 
121
123
  while (node) {
122
124
  ancestors.push(node);
125
+ // @ts-ignore
123
126
  node = get_parent(node);
124
127
  }
125
128
 
@@ -128,6 +131,9 @@ function get_ancestors(node: HTMLElement) {
128
131
 
129
132
  /** @param {HTMLElement} node */
130
133
  function get_parent(node: HTMLElement) {
131
- // @ts-ignore
132
- return node.parentNode?.host || node.parentNode;
134
+ const { parentNode } = node;
135
+ if (parentNode && parentNode instanceof ShadowRoot) {
136
+ return parentNode.host
137
+ }
138
+ return parentNode;
133
139
  }