Skip to content

Commit

Permalink
fix(Drawer): initial hidden state when keepMouted true
Browse files Browse the repository at this point in the history
  • Loading branch information
flops committed Oct 10, 2024
1 parent 9f455f7 commit 0c0eb6f
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/components/Drawer/Drawer.scss
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ $block: '.#{variables.$ns}drawer';
}

&__item-transition-exit-done,
&__item-transition_direction_right-exit-done {
&__item-transition_direction_right-exit-done,
&__item_hidden {
visibility: hidden;
}

Expand Down
14 changes: 13 additions & 1 deletion src/components/Drawer/Drawer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,10 @@ export const DrawerItem = React.forwardRef<HTMLDivElement, DrawerItemProps>(
keepMounted = false,
} = props;

const [isInitialRender, setInitialRender] = React.useState(true);
const itemRef = React.useRef<HTMLDivElement>(null);
const handleRef = useForkRef(ref, itemRef);

const cssDirection = direction === 'left' ? undefined : direction;

const {resizedWidth, resizerHandlers} = useResizableDrawerItem({
Expand All @@ -92,6 +94,10 @@ export const DrawerItem = React.forwardRef<HTMLDivElement, DrawerItemProps>(
onResize,
});

React.useEffect(() => {
setInitialRender(true);
}, [direction]);

const resizerElement = resizable ? (
<div className={b('resizer', {direction})} {...resizerHandlers}>
<div className={b('resizer-handle')} />
Expand All @@ -106,10 +112,16 @@ export const DrawerItem = React.forwardRef<HTMLDivElement, DrawerItemProps>(
unmountOnExit={!keepMounted}
classNames={b('item-transition', {direction: cssDirection})}
nodeRef={itemRef}
onEnter={() => setInitialRender(false)}
onExit={() => setInitialRender(false)}
>
<div
ref={handleRef}
className={b('item', {direction: cssDirection}, className)}
className={b(
'item',
{direction: cssDirection, hidden: isInitialRender && !visible},
[className],
)}
style={{width: resizable ? `${resizedWidth}px` : undefined}}
>
{resizerElement}
Expand Down
4 changes: 2 additions & 2 deletions src/components/Drawer/__stories__/DrawerShowcase.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ export function DrawerShowcase() {
const [direction, setDirection] = React.useState<string>('left');
const [direction2, setDirection2] = React.useState<string>('left');

const [keepMountedGlobal, setKeepMountedGlobal] = React.useState<boolean>(false);
const [keepMounted1, setKeepMounted1] = React.useState<boolean>(false);
const [keepMountedGlobal, setKeepMountedGlobal] = React.useState<boolean>(true);
const [keepMounted1, setKeepMounted1] = React.useState<boolean>(true);

const hideAll = React.useCallback(() => {
setVisible1(false);
Expand Down

0 comments on commit 0c0eb6f

Please sign in to comment.