Skip to content

Commit

Permalink
fix(blade): content width was set to zero on initially expanded blade
Browse files Browse the repository at this point in the history
  • Loading branch information
nantunes authored and plagoa committed Feb 22, 2024
1 parent 58af7da commit 87ebc30
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions packages/lab/src/Blade/Blade.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import React, {
useMemo,
HTMLAttributes,
useEffect,
useRef,
useState,
} from "react";

import {
Expand Down Expand Up @@ -244,13 +246,22 @@ export const HvBlade = (props: HvBladeProps) => {
labelVariant,
]);

const bladeRef = React.useRef<HTMLDivElement>(null);
const maxWidthRef = React.useRef<number>(0);
const bladeRef = useRef<HTMLDivElement>(null);
const [maxWidth, setMaxWidth] = useState<number | undefined>(undefined);
useEffect(() => {
if (bladeRef.current) {
maxWidthRef.current = bladeRef.current.parentElement?.clientWidth || 0;
const resizeObserver = new ResizeObserver((entries) => {
setMaxWidth(entries[0].target.clientWidth);
});
resizeObserver.observe(
// using the blade's container as reference max-width is more stable
bladeRef.current.parentElement ?? bladeRef.current
);
return () => {
resizeObserver.disconnect();
};
}
}, []);
}, [isExpanded]);

const { style: containerStyle, ...otherContainerProps } =
containerProps || {};
Expand All @@ -274,7 +285,7 @@ export const HvBlade = (props: HvBladeProps) => {
hidden={!isExpanded}
style={{
...containerStyle,
maxWidth: isExpanded ? maxWidthRef.current : 0,
maxWidth: isExpanded ? maxWidth : 0,
}}
{...otherContainerProps}
>
Expand Down

0 comments on commit 87ebc30

Please sign in to comment.