react-resizable-panels 0.0.60 → 0.0.62

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/src/PanelGroup.ts CHANGED
@@ -123,9 +123,7 @@ function PanelGroupWithForwardedRef({
123
123
  id: string;
124
124
  keyboardResizeByPercentage: number | null;
125
125
  keyboardResizeByPixels: number | null;
126
- layout: number[];
127
126
  onLayout: PanelGroupOnLayout | null;
128
- panelDataArray: PanelData[];
129
127
  storage: PanelGroupStorage;
130
128
  }>({
131
129
  autoSaveId,
@@ -134,12 +132,18 @@ function PanelGroupWithForwardedRef({
134
132
  id: groupId,
135
133
  keyboardResizeByPercentage,
136
134
  keyboardResizeByPixels,
137
- layout,
138
135
  onLayout,
139
- panelDataArray: [],
140
136
  storage,
141
137
  });
142
138
 
139
+ const eagerValuesRef = useRef<{
140
+ layout: number[];
141
+ panelDataArray: PanelData[];
142
+ }>({
143
+ layout,
144
+ panelDataArray: [],
145
+ });
146
+
143
147
  const devWarningsRef = useRef<{
144
148
  didLogIdAndOrderWarning: boolean;
145
149
  didLogPanelConstraintsWarning: boolean;
@@ -155,7 +159,8 @@ function PanelGroupWithForwardedRef({
155
159
  () => ({
156
160
  getId: () => committedValuesRef.current.id,
157
161
  getLayout: () => {
158
- const { id: groupId, layout } = committedValuesRef.current;
162
+ const { id: groupId } = committedValuesRef.current;
163
+ const { layout } = eagerValuesRef.current;
159
164
 
160
165
  const groupSizePixels = calculateAvailablePanelSizeInPixels(groupId);
161
166
 
@@ -170,12 +175,8 @@ function PanelGroupWithForwardedRef({
170
175
  });
171
176
  },
172
177
  setLayout: (mixedSizes: Partial<MixedSizes>[]) => {
173
- const {
174
- id: groupId,
175
- layout: prevLayout,
176
- onLayout,
177
- panelDataArray,
178
- } = committedValuesRef.current;
178
+ const { id: groupId, onLayout } = committedValuesRef.current;
179
+ const { layout: prevLayout, panelDataArray } = eagerValuesRef.current;
179
180
 
180
181
  const groupSizePixels = calculateAvailablePanelSizeInPixels(groupId);
181
182
 
@@ -195,7 +196,7 @@ function PanelGroupWithForwardedRef({
195
196
  if (!areEqual(prevLayout, safeLayout)) {
196
197
  setLayout(safeLayout);
197
198
 
198
- committedValuesRef.current.layout = safeLayout;
199
+ eagerValuesRef.current.layout = safeLayout;
199
200
 
200
201
  if (onLayout) {
201
202
  onLayout(
@@ -235,14 +236,15 @@ function PanelGroupWithForwardedRef({
235
236
 
236
237
  useWindowSplitterPanelGroupBehavior({
237
238
  committedValuesRef,
239
+ eagerValuesRef,
238
240
  groupId,
239
241
  layout,
240
- panelDataArray: committedValuesRef.current.panelDataArray,
242
+ panelDataArray: eagerValuesRef.current.panelDataArray,
241
243
  setLayout,
242
244
  });
243
245
 
244
246
  useEffect(() => {
245
- const { panelDataArray } = committedValuesRef.current;
247
+ const { panelDataArray } = eagerValuesRef.current;
246
248
 
247
249
  // If this panel has been configured to persist sizing information, save sizes to local storage.
248
250
  if (autoSaveId) {
@@ -262,7 +264,7 @@ function PanelGroupWithForwardedRef({
262
264
  }, [autoSaveId, layout, storage]);
263
265
 
264
266
  useIsomorphicLayoutEffect(() => {
265
- const { panelDataArray } = committedValuesRef.current;
267
+ const { layout: prevLayout, panelDataArray } = eagerValuesRef.current;
266
268
 
267
269
  const constraints = panelDataArray.map(({ constraints }) => constraints);
268
270
  if (!shouldMonitorPixelBasedConstraints(constraints)) {
@@ -278,7 +280,7 @@ function PanelGroupWithForwardedRef({
278
280
  const resizeObserver = new ResizeObserver(() => {
279
281
  const groupSizePixels = calculateAvailablePanelSizeInPixels(groupId);
280
282
 
281
- const { layout: prevLayout, onLayout } = committedValuesRef.current;
283
+ const { onLayout } = committedValuesRef.current;
282
284
 
283
285
  const nextLayout = validatePanelGroupLayout({
284
286
  groupSizePixels,
@@ -291,7 +293,7 @@ function PanelGroupWithForwardedRef({
291
293
  if (!areEqual(prevLayout, nextLayout)) {
292
294
  setLayout(nextLayout);
293
295
 
294
- committedValuesRef.current.layout = nextLayout;
296
+ eagerValuesRef.current.layout = nextLayout;
295
297
 
296
298
  if (onLayout) {
297
299
  onLayout(
@@ -325,7 +327,7 @@ function PanelGroupWithForwardedRef({
325
327
  // DEV warnings
326
328
  useEffect(() => {
327
329
  if (isDevelopment) {
328
- const { panelDataArray } = committedValuesRef.current;
330
+ const { panelDataArray } = eagerValuesRef.current;
329
331
 
330
332
  const {
331
333
  didLogIdAndOrderWarning,
@@ -387,11 +389,8 @@ function PanelGroupWithForwardedRef({
387
389
  // External APIs are safe to memoize via committed values ref
388
390
  const collapsePanel = useCallback(
389
391
  (panelData: PanelData) => {
390
- const {
391
- layout: prevLayout,
392
- onLayout,
393
- panelDataArray,
394
- } = committedValuesRef.current;
392
+ const { onLayout } = committedValuesRef.current;
393
+ const { layout: prevLayout, panelDataArray } = eagerValuesRef.current;
395
394
 
396
395
  if (panelData.constraints.collapsible) {
397
396
  const panelConstraintsArray = panelDataArray.map(
@@ -431,7 +430,7 @@ function PanelGroupWithForwardedRef({
431
430
  if (!compareLayouts(prevLayout, nextLayout)) {
432
431
  setLayout(nextLayout);
433
432
 
434
- committedValuesRef.current.layout = nextLayout;
433
+ eagerValuesRef.current.layout = nextLayout;
435
434
 
436
435
  if (onLayout) {
437
436
  onLayout(
@@ -461,11 +460,8 @@ function PanelGroupWithForwardedRef({
461
460
  // External APIs are safe to memoize via committed values ref
462
461
  const expandPanel = useCallback(
463
462
  (panelData: PanelData) => {
464
- const {
465
- layout: prevLayout,
466
- onLayout,
467
- panelDataArray,
468
- } = committedValuesRef.current;
463
+ const { onLayout } = committedValuesRef.current;
464
+ const { layout: prevLayout, panelDataArray } = eagerValuesRef.current;
469
465
 
470
466
  if (panelData.constraints.collapsible) {
471
467
  const panelConstraintsArray = panelDataArray.map(
@@ -486,7 +482,8 @@ function PanelGroupWithForwardedRef({
486
482
  panelSizeBeforeCollapseRef.current.get(panelData.id);
487
483
 
488
484
  const baseSizePercentage =
489
- prevPanelSizePercentage != null
485
+ prevPanelSizePercentage != null &&
486
+ prevPanelSizePercentage >= minSizePercentage
490
487
  ? prevPanelSizePercentage
491
488
  : minSizePercentage;
492
489
 
@@ -508,7 +505,7 @@ function PanelGroupWithForwardedRef({
508
505
  if (!compareLayouts(prevLayout, nextLayout)) {
509
506
  setLayout(nextLayout);
510
507
 
511
- committedValuesRef.current.layout = nextLayout;
508
+ eagerValuesRef.current.layout = nextLayout;
512
509
 
513
510
  if (onLayout) {
514
511
  onLayout(
@@ -538,7 +535,7 @@ function PanelGroupWithForwardedRef({
538
535
  // External APIs are safe to memoize via committed values ref
539
536
  const getPanelSize = useCallback(
540
537
  (panelData: PanelData) => {
541
- const { layout, panelDataArray } = committedValuesRef.current;
538
+ const { layout, panelDataArray } = eagerValuesRef.current;
542
539
 
543
540
  const { panelSizePercentage, panelSizePixels } = panelDataHelper(
544
541
  groupId,
@@ -558,7 +555,7 @@ function PanelGroupWithForwardedRef({
558
555
  // This API should never read from committedValuesRef
559
556
  const getPanelStyle = useCallback(
560
557
  (panelData: PanelData) => {
561
- const { panelDataArray } = committedValuesRef.current;
558
+ const { panelDataArray } = eagerValuesRef.current;
562
559
 
563
560
  const panelIndex = panelDataArray.indexOf(panelData);
564
561
 
@@ -575,7 +572,7 @@ function PanelGroupWithForwardedRef({
575
572
  // External APIs are safe to memoize via committed values ref
576
573
  const isPanelCollapsed = useCallback(
577
574
  (panelData: PanelData) => {
578
- const { layout, panelDataArray } = committedValuesRef.current;
575
+ const { layout, panelDataArray } = eagerValuesRef.current;
579
576
 
580
577
  const { collapsedSizePercentage, collapsible, panelSizePercentage } =
581
578
  panelDataHelper(groupId, panelDataArray, panelData, layout);
@@ -590,7 +587,7 @@ function PanelGroupWithForwardedRef({
590
587
  // External APIs are safe to memoize via committed values ref
591
588
  const isPanelExpanded = useCallback(
592
589
  (panelData: PanelData) => {
593
- const { layout, panelDataArray } = committedValuesRef.current;
590
+ const { layout, panelDataArray } = eagerValuesRef.current;
594
591
 
595
592
  const { collapsedSizePercentage, collapsible, panelSizePercentage } =
596
593
  panelDataHelper(groupId, panelDataArray, panelData, layout);
@@ -604,11 +601,10 @@ function PanelGroupWithForwardedRef({
604
601
  const {
605
602
  autoSaveId,
606
603
  id: groupId,
607
- layout: prevLayout,
608
604
  onLayout,
609
- panelDataArray,
610
605
  storage,
611
606
  } = committedValuesRef.current;
607
+ const { layout: prevLayout, panelDataArray } = eagerValuesRef.current;
612
608
 
613
609
  panelDataArray.push(panelData);
614
610
  panelDataArray.sort((panelA, panelB) => {
@@ -668,11 +664,14 @@ function PanelGroupWithForwardedRef({
668
664
  ),
669
665
  });
670
666
 
671
- if (!areEqual(prevLayout, nextLayout)) {
672
- setLayout(nextLayout);
667
+ // Offscreen mode makes this a bit weird;
668
+ // Panels unregister when hidden and re-register when shown again,
669
+ // but the overall layout doesn't change between these two cases.
670
+ setLayout(nextLayout);
673
671
 
674
- committedValuesRef.current.layout = nextLayout;
672
+ eagerValuesRef.current.layout = nextLayout;
675
673
 
674
+ if (!areEqual(prevLayout, nextLayout)) {
676
675
  if (onLayout) {
677
676
  onLayout(
678
677
  nextLayout.map((sizePercentage) => ({
@@ -705,9 +704,8 @@ function PanelGroupWithForwardedRef({
705
704
  keyboardResizeByPercentage,
706
705
  keyboardResizeByPixels,
707
706
  onLayout,
708
- panelDataArray,
709
- layout: prevLayout,
710
707
  } = committedValuesRef.current;
708
+ const { layout: prevLayout, panelDataArray } = eagerValuesRef.current;
711
709
 
712
710
  const { initialLayout } = dragState ?? {};
713
711
 
@@ -781,7 +779,7 @@ function PanelGroupWithForwardedRef({
781
779
  if (layoutChanged) {
782
780
  setLayout(nextLayout);
783
781
 
784
- committedValuesRef.current.layout = nextLayout;
782
+ eagerValuesRef.current.layout = nextLayout;
785
783
 
786
784
  if (onLayout) {
787
785
  onLayout(
@@ -808,11 +806,9 @@ function PanelGroupWithForwardedRef({
808
806
  // External APIs are safe to memoize via committed values ref
809
807
  const resizePanel = useCallback(
810
808
  (panelData: PanelData, mixedSizes: Partial<MixedSizes>) => {
811
- const {
812
- layout: prevLayout,
813
- onLayout,
814
- panelDataArray,
815
- } = committedValuesRef.current;
809
+ const { onLayout } = committedValuesRef.current;
810
+
811
+ const { layout: prevLayout, panelDataArray } = eagerValuesRef.current;
816
812
 
817
813
  const panelConstraintsArray = panelDataArray.map(
818
814
  (panelData) => panelData.constraints
@@ -844,7 +840,7 @@ function PanelGroupWithForwardedRef({
844
840
  if (!compareLayouts(prevLayout, nextLayout)) {
845
841
  setLayout(nextLayout);
846
842
 
847
- committedValuesRef.current.layout = nextLayout;
843
+ eagerValuesRef.current.layout = nextLayout;
848
844
 
849
845
  if (onLayout) {
850
846
  onLayout(
@@ -871,7 +867,8 @@ function PanelGroupWithForwardedRef({
871
867
 
872
868
  const startDragging = useCallback(
873
869
  (dragHandleId: string, event: ResizeEvent) => {
874
- const { direction, layout } = committedValuesRef.current;
870
+ const { direction } = committedValuesRef.current;
871
+ const { layout } = eagerValuesRef.current;
875
872
 
876
873
  const handleElement = getResizeHandleElement(dragHandleId)!;
877
874
 
@@ -903,12 +900,8 @@ function PanelGroupWithForwardedRef({
903
900
  timeout: null,
904
901
  });
905
902
  const unregisterPanel = useCallback((panelData: PanelData) => {
906
- const {
907
- id: groupId,
908
- layout: prevLayout,
909
- onLayout,
910
- panelDataArray,
911
- } = committedValuesRef.current;
903
+ const { id: groupId, onLayout } = committedValuesRef.current;
904
+ const { layout: prevLayout, panelDataArray } = eagerValuesRef.current;
912
905
 
913
906
  const index = panelDataArray.indexOf(panelData);
914
907
  if (index >= 0) {
@@ -940,7 +933,7 @@ function PanelGroupWithForwardedRef({
940
933
  // When a panel is removed from the group, we should delete the most recent prev-size entry for it.
941
934
  // If we don't do this, then a conditionally rendered panel might not call onResize when it's re-mounted.
942
935
  // Strict effects mode makes this tricky though because all panels will be registered, unregistered, then re-registered on mount.
943
- delete panelIdToLastNotifiedMixedSizesMapRef.current[panelData.id];
936
+ delete map[panelData.id];
944
937
  }
945
938
  });
946
939
 
@@ -973,7 +966,7 @@ function PanelGroupWithForwardedRef({
973
966
  if (!areEqual(prevLayout, nextLayout)) {
974
967
  setLayout(nextLayout);
975
968
 
976
- committedValuesRef.current.layout = nextLayout;
969
+ eagerValuesRef.current.layout = nextLayout;
977
970
 
978
971
  if (onLayout) {
979
972
  onLayout(
@@ -19,6 +19,7 @@ import useIsomorphicLayoutEffect from "./useIsomorphicEffect";
19
19
 
20
20
  export function useWindowSplitterPanelGroupBehavior({
21
21
  committedValuesRef,
22
+ eagerValuesRef,
22
23
  groupId,
23
24
  layout,
24
25
  panelDataArray,
@@ -26,6 +27,8 @@ export function useWindowSplitterPanelGroupBehavior({
26
27
  }: {
27
28
  committedValuesRef: RefObject<{
28
29
  direction: Direction;
30
+ }>;
31
+ eagerValuesRef: RefObject<{
29
32
  panelDataArray: PanelData[];
30
33
  }>;
31
34
  groupId: string;
@@ -95,7 +98,7 @@ export function useWindowSplitterPanelGroupBehavior({
95
98
  }, [groupId, layout, panelDataArray]);
96
99
 
97
100
  useEffect(() => {
98
- const { panelDataArray } = committedValuesRef.current!;
101
+ const { panelDataArray } = eagerValuesRef.current!;
99
102
 
100
103
  const groupElement = getPanelGroupElement(groupId);
101
104
  assert(groupElement != null, `No group found for id "${groupId}"`);
@@ -181,5 +184,12 @@ export function useWindowSplitterPanelGroupBehavior({
181
184
  return () => {
182
185
  cleanupFunctions.forEach((cleanupFunction) => cleanupFunction());
183
186
  };
184
- }, [committedValuesRef, groupId, layout, panelDataArray, setLayout]);
187
+ }, [
188
+ committedValuesRef,
189
+ eagerValuesRef,
190
+ groupId,
191
+ layout,
192
+ panelDataArray,
193
+ setLayout,
194
+ ]);
185
195
  }