react-resizable-panels 0.0.51 → 0.0.52
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 +4 -0
- package/README.md +15 -14
- package/dist/declarations/src/Panel.d.ts +1 -0
- package/dist/declarations/src/types.d.ts +2 -1
- package/dist/react-resizable-panels.cjs.js +70 -41
- package/dist/react-resizable-panels.cjs.js.map +1 -0
- package/dist/react-resizable-panels.development.cjs.js +70 -41
- package/dist/react-resizable-panels.development.esm.js +70 -41
- package/dist/react-resizable-panels.esm.js +70 -41
- package/dist/react-resizable-panels.esm.js.map +1 -0
- package/package.json +1 -1
- package/src/Panel.ts +5 -0
- package/src/PanelGroup.ts +47 -35
- package/src/types.ts +2 -1
- package/src/utils/group.ts +20 -16
package/src/PanelGroup.ts
CHANGED
|
@@ -206,9 +206,9 @@ function PanelGroupWithForwardedRef({
|
|
|
206
206
|
panelIdToLastNotifiedSizeMapRef.current;
|
|
207
207
|
const panelsArray = panelsMapToSortedArray(panels);
|
|
208
208
|
|
|
209
|
-
callPanelCallbacks(panelsArray, sizes, panelIdToLastNotifiedSizeMap);
|
|
210
|
-
|
|
211
209
|
setSizes(sizes);
|
|
210
|
+
|
|
211
|
+
callPanelCallbacks(panelsArray, sizes, panelIdToLastNotifiedSizeMap);
|
|
212
212
|
},
|
|
213
213
|
}),
|
|
214
214
|
[]
|
|
@@ -232,24 +232,24 @@ function PanelGroupWithForwardedRef({
|
|
|
232
232
|
// Notify external code when sizes have changed.
|
|
233
233
|
useEffect(() => {
|
|
234
234
|
const { onLayout } = callbacksRef.current!;
|
|
235
|
-
|
|
236
|
-
const { panels, sizes } = committedValuesRef.current;
|
|
235
|
+
const { panels, sizes } = committedValuesRef.current;
|
|
237
236
|
|
|
238
|
-
|
|
239
|
-
|
|
237
|
+
// Don't commit layout until all panels have registered and re-rendered with their actual sizes.
|
|
238
|
+
if (sizes.length > 0) {
|
|
239
|
+
if (onLayout) {
|
|
240
240
|
onLayout(sizes);
|
|
241
|
+
}
|
|
241
242
|
|
|
242
|
-
|
|
243
|
-
|
|
243
|
+
const panelIdToLastNotifiedSizeMap =
|
|
244
|
+
panelIdToLastNotifiedSizeMapRef.current;
|
|
244
245
|
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
}
|
|
246
|
+
// When possible, we notify before the next render so that rendering work can be batched together.
|
|
247
|
+
// Some cases are difficult to detect though,
|
|
248
|
+
// for example– panels that are conditionally rendered can affect the size of neighboring panels.
|
|
249
|
+
// In this case, the best we can do is notify on commit.
|
|
250
|
+
// The callPanelCallbacks() uses its own memoization to avoid notifying panels twice in these cases.
|
|
251
|
+
const panelsArray = panelsMapToSortedArray(panels);
|
|
252
|
+
callPanelCallbacks(panelsArray, sizes, panelIdToLastNotifiedSizeMap);
|
|
253
253
|
}
|
|
254
254
|
}, [sizes]);
|
|
255
255
|
|
|
@@ -487,14 +487,15 @@ function PanelGroupWithForwardedRef({
|
|
|
487
487
|
const panelIdToLastNotifiedSizeMap =
|
|
488
488
|
panelIdToLastNotifiedSizeMapRef.current;
|
|
489
489
|
|
|
490
|
+
setSizes(nextSizes);
|
|
491
|
+
|
|
490
492
|
// If resize change handlers have been declared, this is the time to call them.
|
|
493
|
+
// Trigger user callbacks after updating state, so that user code can override the sizes.
|
|
491
494
|
callPanelCallbacks(
|
|
492
495
|
panelsArray,
|
|
493
496
|
nextSizes,
|
|
494
497
|
panelIdToLastNotifiedSizeMap
|
|
495
498
|
);
|
|
496
|
-
|
|
497
|
-
setSizes(nextSizes);
|
|
498
499
|
}
|
|
499
500
|
|
|
500
501
|
prevDeltaRef.current = delta;
|
|
@@ -522,7 +523,12 @@ function PanelGroupWithForwardedRef({
|
|
|
522
523
|
const { panels, sizes: prevSizes } = committedValuesRef.current;
|
|
523
524
|
|
|
524
525
|
const panel = panels.get(id);
|
|
525
|
-
if (panel == null
|
|
526
|
+
if (panel == null) {
|
|
527
|
+
return;
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
const { collapsedSize, collapsible } = panel.current;
|
|
531
|
+
if (!collapsible) {
|
|
526
532
|
return;
|
|
527
533
|
}
|
|
528
534
|
|
|
@@ -534,7 +540,7 @@ function PanelGroupWithForwardedRef({
|
|
|
534
540
|
}
|
|
535
541
|
|
|
536
542
|
const currentSize = prevSizes[index];
|
|
537
|
-
if (currentSize ===
|
|
543
|
+
if (currentSize === collapsedSize) {
|
|
538
544
|
// Panel is already collapsed.
|
|
539
545
|
return;
|
|
540
546
|
}
|
|
@@ -547,7 +553,7 @@ function PanelGroupWithForwardedRef({
|
|
|
547
553
|
}
|
|
548
554
|
|
|
549
555
|
const isLastPanel = index === panelsArray.length - 1;
|
|
550
|
-
const delta = isLastPanel ? currentSize :
|
|
556
|
+
const delta = isLastPanel ? currentSize : collapsedSize - currentSize;
|
|
551
557
|
|
|
552
558
|
const nextSizes = adjustByDelta(
|
|
553
559
|
null,
|
|
@@ -563,10 +569,11 @@ function PanelGroupWithForwardedRef({
|
|
|
563
569
|
const panelIdToLastNotifiedSizeMap =
|
|
564
570
|
panelIdToLastNotifiedSizeMapRef.current;
|
|
565
571
|
|
|
572
|
+
setSizes(nextSizes);
|
|
573
|
+
|
|
566
574
|
// If resize change handlers have been declared, this is the time to call them.
|
|
575
|
+
// Trigger user callbacks after updating state, so that user code can override the sizes.
|
|
567
576
|
callPanelCallbacks(panelsArray, nextSizes, panelIdToLastNotifiedSizeMap);
|
|
568
|
-
|
|
569
|
-
setSizes(nextSizes);
|
|
570
577
|
}
|
|
571
578
|
}, []);
|
|
572
579
|
|
|
@@ -578,8 +585,10 @@ function PanelGroupWithForwardedRef({
|
|
|
578
585
|
return;
|
|
579
586
|
}
|
|
580
587
|
|
|
588
|
+
const { collapsedSize, minSize } = panel.current;
|
|
589
|
+
|
|
581
590
|
const sizeBeforeCollapse =
|
|
582
|
-
panelSizeBeforeCollapse.current.get(id) ||
|
|
591
|
+
panelSizeBeforeCollapse.current.get(id) || minSize;
|
|
583
592
|
if (!sizeBeforeCollapse) {
|
|
584
593
|
return;
|
|
585
594
|
}
|
|
@@ -592,7 +601,7 @@ function PanelGroupWithForwardedRef({
|
|
|
592
601
|
}
|
|
593
602
|
|
|
594
603
|
const currentSize = prevSizes[index];
|
|
595
|
-
if (currentSize !==
|
|
604
|
+
if (currentSize !== collapsedSize) {
|
|
596
605
|
// Panel is already expanded.
|
|
597
606
|
return;
|
|
598
607
|
}
|
|
@@ -603,7 +612,9 @@ function PanelGroupWithForwardedRef({
|
|
|
603
612
|
}
|
|
604
613
|
|
|
605
614
|
const isLastPanel = index === panelsArray.length - 1;
|
|
606
|
-
const delta = isLastPanel
|
|
615
|
+
const delta = isLastPanel
|
|
616
|
+
? collapsedSize - sizeBeforeCollapse
|
|
617
|
+
: sizeBeforeCollapse;
|
|
607
618
|
|
|
608
619
|
const nextSizes = adjustByDelta(
|
|
609
620
|
null,
|
|
@@ -619,10 +630,11 @@ function PanelGroupWithForwardedRef({
|
|
|
619
630
|
const panelIdToLastNotifiedSizeMap =
|
|
620
631
|
panelIdToLastNotifiedSizeMapRef.current;
|
|
621
632
|
|
|
633
|
+
setSizes(nextSizes);
|
|
634
|
+
|
|
622
635
|
// If resize change handlers have been declared, this is the time to call them.
|
|
636
|
+
// Trigger user callbacks after updating state, so that user code can override the sizes.
|
|
623
637
|
callPanelCallbacks(panelsArray, nextSizes, panelIdToLastNotifiedSizeMap);
|
|
624
|
-
|
|
625
|
-
setSizes(nextSizes);
|
|
626
638
|
}
|
|
627
639
|
}, []);
|
|
628
640
|
|
|
@@ -634,6 +646,8 @@ function PanelGroupWithForwardedRef({
|
|
|
634
646
|
return;
|
|
635
647
|
}
|
|
636
648
|
|
|
649
|
+
const { collapsedSize, collapsible, maxSize, minSize } = panel.current;
|
|
650
|
+
|
|
637
651
|
const panelsArray = panelsMapToSortedArray(panels);
|
|
638
652
|
|
|
639
653
|
const index = panelsArray.indexOf(panel);
|
|
@@ -646,13 +660,10 @@ function PanelGroupWithForwardedRef({
|
|
|
646
660
|
return;
|
|
647
661
|
}
|
|
648
662
|
|
|
649
|
-
if (
|
|
663
|
+
if (collapsible && nextSize === collapsedSize) {
|
|
650
664
|
// This is a valid resize state.
|
|
651
665
|
} else {
|
|
652
|
-
nextSize = Math.min(
|
|
653
|
-
panel.current.maxSize,
|
|
654
|
-
Math.max(panel.current.minSize, nextSize)
|
|
655
|
-
);
|
|
666
|
+
nextSize = Math.min(maxSize, Math.max(minSize, nextSize));
|
|
656
667
|
}
|
|
657
668
|
|
|
658
669
|
const [idBefore, idAfter] = getBeforeAndAfterIds(id, panelsArray);
|
|
@@ -677,10 +688,11 @@ function PanelGroupWithForwardedRef({
|
|
|
677
688
|
const panelIdToLastNotifiedSizeMap =
|
|
678
689
|
panelIdToLastNotifiedSizeMapRef.current;
|
|
679
690
|
|
|
691
|
+
setSizes(nextSizes);
|
|
692
|
+
|
|
680
693
|
// If resize change handlers have been declared, this is the time to call them.
|
|
694
|
+
// Trigger user callbacks after updating state, so that user code can override the sizes.
|
|
681
695
|
callPanelCallbacks(panelsArray, nextSizes, panelIdToLastNotifiedSizeMap);
|
|
682
|
-
|
|
683
|
-
setSizes(nextSizes);
|
|
684
696
|
}
|
|
685
697
|
}, []);
|
|
686
698
|
|
package/src/types.ts
CHANGED
|
@@ -9,7 +9,7 @@ export type PanelGroupStorage = {
|
|
|
9
9
|
|
|
10
10
|
export type PanelGroupOnLayout = (sizes: number[]) => void;
|
|
11
11
|
export type PanelOnCollapse = (collapsed: boolean) => void;
|
|
12
|
-
export type PanelOnResize = (size: number) => void;
|
|
12
|
+
export type PanelOnResize = (size: number, prevSize: number) => void;
|
|
13
13
|
export type PanelResizeHandleOnDragging = (isDragging: boolean) => void;
|
|
14
14
|
|
|
15
15
|
export type PanelCallbackRef = RefObject<{
|
|
@@ -20,6 +20,7 @@ export type PanelCallbackRef = RefObject<{
|
|
|
20
20
|
export type PanelData = {
|
|
21
21
|
current: {
|
|
22
22
|
callbacksRef: PanelCallbackRef;
|
|
23
|
+
collapsedSize: number;
|
|
23
24
|
collapsible: boolean;
|
|
24
25
|
defaultSize: number | null;
|
|
25
26
|
id: string;
|
package/src/utils/group.ts
CHANGED
|
@@ -123,7 +123,8 @@ export function callPanelCallbacks(
|
|
|
123
123
|
panelIdToLastNotifiedSizeMap: Record<string, number>
|
|
124
124
|
) {
|
|
125
125
|
sizes.forEach((size, index) => {
|
|
126
|
-
const { callbacksRef, collapsible, id } =
|
|
126
|
+
const { callbacksRef, collapsedSize, collapsible, id } =
|
|
127
|
+
panelsArray[index].current;
|
|
127
128
|
|
|
128
129
|
const lastNotifiedSize = panelIdToLastNotifiedSizeMap[id];
|
|
129
130
|
if (lastNotifiedSize !== size) {
|
|
@@ -132,15 +133,19 @@ export function callPanelCallbacks(
|
|
|
132
133
|
const { onCollapse, onResize } = callbacksRef.current!;
|
|
133
134
|
|
|
134
135
|
if (onResize) {
|
|
135
|
-
onResize(size);
|
|
136
|
+
onResize(size, lastNotifiedSize);
|
|
136
137
|
}
|
|
137
138
|
|
|
138
139
|
if (collapsible && onCollapse) {
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
140
|
+
if (
|
|
141
|
+
(lastNotifiedSize == null || lastNotifiedSize === collapsedSize) &&
|
|
142
|
+
size !== collapsedSize
|
|
143
|
+
) {
|
|
142
144
|
onCollapse(false);
|
|
143
|
-
} else if (
|
|
145
|
+
} else if (
|
|
146
|
+
lastNotifiedSize !== collapsedSize &&
|
|
147
|
+
size === collapsedSize
|
|
148
|
+
) {
|
|
144
149
|
onCollapse(true);
|
|
145
150
|
}
|
|
146
151
|
}
|
|
@@ -277,11 +282,13 @@ function safeResizePanel(
|
|
|
277
282
|
): number {
|
|
278
283
|
const nextSizeUnsafe = prevSize + delta;
|
|
279
284
|
|
|
280
|
-
|
|
281
|
-
|
|
285
|
+
const { collapsedSize, collapsible, maxSize, minSize } = panel.current;
|
|
286
|
+
|
|
287
|
+
if (collapsible) {
|
|
288
|
+
if (prevSize > collapsedSize) {
|
|
282
289
|
// Mimic VS COde behavior; collapse a panel if it's smaller than half of its min-size
|
|
283
|
-
if (nextSizeUnsafe <=
|
|
284
|
-
return
|
|
290
|
+
if (nextSizeUnsafe <= minSize / 2 + collapsedSize) {
|
|
291
|
+
return collapsedSize;
|
|
285
292
|
}
|
|
286
293
|
} else {
|
|
287
294
|
const isKeyboardEvent = event?.type?.startsWith("key");
|
|
@@ -289,17 +296,14 @@ function safeResizePanel(
|
|
|
289
296
|
// Keyboard events should expand a collapsed panel to the min size,
|
|
290
297
|
// but mouse events should wait until the panel has reached its min size
|
|
291
298
|
// to avoid a visual flickering when dragging between collapsed and min size.
|
|
292
|
-
if (nextSizeUnsafe <
|
|
293
|
-
return
|
|
299
|
+
if (nextSizeUnsafe < minSize) {
|
|
300
|
+
return collapsedSize;
|
|
294
301
|
}
|
|
295
302
|
}
|
|
296
303
|
}
|
|
297
304
|
}
|
|
298
305
|
|
|
299
|
-
const nextSize = Math.min(
|
|
300
|
-
panel.current.maxSize,
|
|
301
|
-
Math.max(panel.current.minSize, nextSizeUnsafe)
|
|
302
|
-
);
|
|
306
|
+
const nextSize = Math.min(maxSize, Math.max(minSize, nextSizeUnsafe));
|
|
303
307
|
|
|
304
308
|
return nextSize;
|
|
305
309
|
}
|