Skip to content

Commit e0a7fd4

Browse files
committed
fix(ResizablePanel): update visual size when container resizes
1 parent 457183e commit e0a7fd4

File tree

4 files changed

+42
-13
lines changed

4 files changed

+42
-13
lines changed

.changeset/curly-boxes-shop.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@cube-dev/ui-kit': patch
3+
---
4+
5+
Recalculate the sidebar position on container resize.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
"react-transition-group": "^4.4.5",
8383
"react-types": "^0.1.0",
8484
"tiny-invariant": "^1.3.3",
85+
"usehooks-ts": "^3.1.0",
8586
"valid-url": "^1.0.9"
8687
},
8788
"devDependencies": {

pnpm-lock.yaml

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/components/layout/ResizablePanel.tsx

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { useResizeObserver } from '@react-aria/utils';
12
import { ForwardedRef, forwardRef, useEffect, useMemo, useState } from 'react';
23
import { useHover, useMove } from 'react-aria';
34

@@ -153,7 +154,7 @@ interface HandlerProps extends BasePropsWithoutChildren {
153154
}
154155

155156
const Handler = (props: HandlerProps) => {
156-
const { direction = 'right', isDisabled } = props;
157+
const { direction = 'right', isDisabled, ...otherProps } = props;
157158
const { hoverProps, isHovered } = useHover({});
158159
const isHorizontal = direction === 'left' || direction === 'right';
159160
const localIsHovered = useDebouncedValue(isHovered, 150);
@@ -170,7 +171,7 @@ const Handler = (props: HandlerProps) => {
170171
},
171172
'data-direction': direction,
172173
},
173-
props,
174+
otherProps,
174175
)}
175176
>
176177
<div data-element="Track" />
@@ -222,12 +223,29 @@ function ResizablePanel(
222223
maxSize = isControllable ? undefined : 'min(50%, 400px)',
223224
...restProps
224225
} = props;
225-
226226
const [isDragging, setIsDragging] = useState(false);
227227
const isHorizontal = direction === 'left' || direction === 'right';
228228

229229
ref = useCombinedRefs(ref);
230230

231+
const onResize = useEvent(() => {
232+
if (ref?.current) {
233+
const offsetProp = isHorizontal ? 'offsetWidth' : 'offsetHeight';
234+
const containerSize = ref.current[offsetProp];
235+
236+
if (Math.abs(containerSize - size) < 1 && !isDisabled) {
237+
setVisualSize(size);
238+
} else {
239+
setVisualSize(containerSize);
240+
}
241+
}
242+
});
243+
244+
useResizeObserver({
245+
ref,
246+
onResize,
247+
});
248+
231249
function clamp(size: number) {
232250
if (typeof maxSize === 'number') {
233251
size = Math.min(maxSize, size);
@@ -296,16 +314,7 @@ function ResizablePanel(
296314
});
297315

298316
useEffect(() => {
299-
if (ref.current) {
300-
const offsetProp = isHorizontal ? 'offsetWidth' : 'offsetHeight';
301-
const containerSize = ref.current[offsetProp];
302-
303-
if (Math.abs(containerSize - size) < 1 && !isDisabled) {
304-
setVisualSize(size);
305-
} else {
306-
setVisualSize(containerSize);
307-
}
308-
}
317+
onResize();
309318
}, [size, isDisabled]);
310319

311320
useEffect(() => {

0 commit comments

Comments
 (0)