Skip to content

Commit

Permalink
fix(suite): Let Sidebar update its height with window change or zoom
Browse files Browse the repository at this point in the history
  • Loading branch information
hstastna authored and Hilda Stastna committed Jul 9, 2024
1 parent 4a557b3 commit 510ba01
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,41 @@ export const ResizableBox: StoryObj<ResizableBoxProps> = {
height: 100,
minHeight: 50,
maxHeight: 300,
updateWidthOnWindowResize: false,
updateHeightOnWindowResize: false,
},
argTypes: {
children: {
table: {
type: {
summary: 'ReactNode',
},
},
},
directions: {
control: {
type: 'check',
},
options: ['top', 'left', 'right', 'bottom'],
},
isLocked: { control: 'boolean' },
width: { control: 'number' },
minWidth: { control: 'number' },
maxWidth: { control: 'number' },
height: { control: 'number' },
minHeight: { control: 'number' },
maxHeight: { control: 'number' },
updateWidthOnWindowResize: {
control: 'boolean',
description:
'Maximize the box width to the window width when resizing window or zooming',
defaultValue: false,
},
updateHeightOnWindowResize: {
control: 'boolean',
description:
'Maximize the box height to the window height when resizing window or zooming',
defaultValue: false,
},
},
};
21 changes: 15 additions & 6 deletions packages/components/src/components/ResizableBox/ResizableBox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ export type ResizableBoxProps = {
height?: number;
minHeight?: number;
maxHeight?: number;
updateWidthOnWindowResize?: boolean;
updateHeightOnWindowResize?: boolean;
zIndex?: ZIndexValues;
};

Expand Down Expand Up @@ -177,6 +179,8 @@ export const ResizableBox = ({
height,
minHeight = 0,
maxHeight,
updateWidthOnWindowResize = false,
updateHeightOnWindowResize = false,
zIndex = zIndices.draggableComponent,
}: ResizableBoxProps) => {
const resizableBoxRef = useRef<HTMLDivElement>(null);
Expand All @@ -188,7 +192,6 @@ export const ResizableBox = ({
const [isResizing, setIsResizing] = useState<boolean>(false);
const [isHovering, setIsHovering] = useState<boolean>(false);
const [direction, setDirection] = useState<Direction | null>(null);
const [isMaxHeight, setIsMaxHeight] = useState<boolean>(false);

const resizeCooldown = createCooldown(150);

Expand Down Expand Up @@ -261,8 +264,6 @@ export const ResizableBox = ({
if (newHeight === 0) {
setNewHeight(rect.height);
}

setIsMaxHeight(Math.floor(rect.height) === window.innerHeight);
}

document.onmousemove = event => {
Expand All @@ -274,20 +275,28 @@ export const ResizableBox = ({
document.onmouseup = () => setIsResizing(false);

window.onresize = () => {
if (isMaxHeight && resizeCooldown() === true) {
setNewHeight(maxHeight || window.innerHeight);
if (resizeCooldown() === true) {
if (updateHeightOnWindowResize) {
setNewHeight(getMaxResult(maxHeight, window.innerHeight));
}
if (updateWidthOnWindowResize) {
setNewWidth(getMaxResult(maxWidth, window.innerWidth));
}
}
};
}, [
direction,
isMaxHeight,
directions,
isResizing,
maxHeight,
maxWidth,
newHeight,
newWidth,
resizableBoxRef,
resize,
resizeCooldown,
updateHeightOnWindowResize,
updateWidthOnWindowResize,
]);

const handleMouseOverDirection = (direction: Direction) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export const Sidebar = () => {
minWidth={230}
maxWidth={400}
zIndex={zIndices.draggableComponent}
updateHeightOnWindowResize
>
<Container $elevation={elevation}>
<ElevationUp>
Expand Down

0 comments on commit 510ba01

Please sign in to comment.