Skip to content

Commit 3ebe46f

Browse files
committed
fix(ResizablePanel): infinite loop in controllable mode * 5
1 parent 4908726 commit 3ebe46f

File tree

2 files changed

+16
-17
lines changed

2 files changed

+16
-17
lines changed

src/components/layout/ResizablePanel.stories.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ const TemplateTop: StoryFn<CubeResizablePanelProps> = (args) => {
4444

4545
const TemplateControllable: StoryFn<CubeResizablePanelProps> = (args) => {
4646
const [size, setSize] = useState(200);
47-
4847
return (
4948
<ResizablePanel
5049
size={size}

src/components/layout/ResizablePanel.tsx

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -217,19 +217,6 @@ function ResizablePanel(
217217
);
218218
let [visualSize, setVisualSize] = useState<number | null>(null);
219219

220-
useEffect(() => {
221-
if (ref.current) {
222-
const offsetProp = isHorizontal ? 'offsetWidth' : 'offsetHeight';
223-
const containerSize = ref.current[offsetProp];
224-
225-
if (Math.abs(containerSize - size) < 1 && !isDisabled) {
226-
setVisualSize(size);
227-
} else {
228-
setVisualSize(containerSize);
229-
}
230-
}
231-
}, [size, isDisabled]);
232-
233220
let { moveProps } = useMove({
234221
onMoveStart(e) {
235222
if (isDisabled) {
@@ -267,14 +254,27 @@ function ResizablePanel(
267254
},
268255
});
269256

257+
useEffect(() => {
258+
if (ref.current) {
259+
const offsetProp = isHorizontal ? 'offsetWidth' : 'offsetHeight';
260+
const containerSize = ref.current[offsetProp];
261+
262+
if (Math.abs(containerSize - size) < 1 && !isDisabled) {
263+
setVisualSize(size);
264+
} else {
265+
setVisualSize(containerSize);
266+
}
267+
}
268+
}, [size, isDisabled]);
269+
270270
useEffect(() => {
271271
if (
272-
(providedSize == null || Math.abs(providedSize - size) > 0.5) &&
272+
(providedSize == null || Math.abs(providedSize - visualSize) > 0.5) &&
273273
!isDragging
274274
) {
275-
onSizeChange?.(Math.round(size));
275+
onSizeChange?.(Math.round(visualSize));
276276
}
277-
}, [size]);
277+
}, [visualSize]);
278278

279279
useEffect(() => {
280280
if (providedSize && Math.abs(providedSize - size) > 0.5) {

0 commit comments

Comments
 (0)