Skip to content

Commit

Permalink
Merge pull request #1311 from Bynder/fix/GC-1464_on-window-resize-use…
Browse files Browse the repository at this point in the history
…-correct-width

🐛  GC-1464 - On window resize ensure resized element shrinks too
  • Loading branch information
timbryandev authored Oct 9, 2023
2 parents 1733203 + 056e580 commit 6153c20
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions lib/Resizable/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ export function Resizable(props: PropsWithChildren<ResizableProps>) {
initialWidth
);

const containerWidth = () =>
const getContainerWidth = () =>
toPixels(props.containerWidth ?? document.body.offsetWidth);

const minWidth = () =>
toPixels(props.minResizableWidth ?? 0, containerWidth());
const getMinWidth = () =>
toPixels(props.minResizableWidth ?? 0, getContainerWidth());

const maxWidth = () =>
toPixels(props.maxResizableWidth ?? "100%", containerWidth());
const getMaxWidth = () =>
toPixels(props.maxResizableWidth ?? "100%", getContainerWidth());

const getWrapperWidth = () =>
toPixels(resizeWrapperRef.current?.style.width ?? 0);
Expand All @@ -56,7 +56,7 @@ export function Resizable(props: PropsWithChildren<ResizableProps>) {
if (resizeWrapperRef.current === null) return;

const newWidth =
keepValueWithinRange(value, minWidth(), maxWidth()) - gutterSize;
keepValueWithinRange(value, getMinWidth(), getMaxWidth()) - gutterSize;

resizeWrapperRef.current.style.width = `${newWidth}px`;
setLastPosition(newWidth);
Expand Down Expand Up @@ -111,11 +111,18 @@ export function Resizable(props: PropsWithChildren<ResizableProps>) {

useEffect(() => {
doResize(rememberPosition ? lastPosition : initialWidth);

// remember to remove global listeners on dismount
// remember to remove global listeners on unmounting
return () => stopDrag();
}, []);

useEffect(() => {
const handeWindowResize = () => doResize(getWrapperWidth() + gutterSize);
window.addEventListener("resize", handeWindowResize);
return () => {
window.removeEventListener("resize", handeWindowResize);
};
}, []);

return (
<div ref={resizeRef} className="resizable">
<div ref={resizeWrapperRef} className="resizable__wrapper">
Expand Down

0 comments on commit 6153c20

Please sign in to comment.