Skip to content

fix: drawer is broken #2351

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 31, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 30 additions & 23 deletions src/components/Drawer/Drawer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import {Xmark} from '@gravity-ui/icons';
import {DrawerItem, Drawer as GravityDrawer} from '@gravity-ui/navigation';
import {ActionTooltip, Button, Flex, Icon, Text} from '@gravity-ui/uikit';
import {ActionTooltip, Button, Flex, Icon, Portal, Text} from '@gravity-ui/uikit';

import {cn} from '../../utils/cn';
import {isNumeric} from '../../utils/utils';
Expand Down Expand Up @@ -52,7 +52,7 @@
});

const drawerRef = React.useRef<HTMLDivElement>(null);
const {containerWidth} = useDrawerContext();
const {containerWidth, itemContainerRef} = useDrawerContext();
// Calculate drawer width based on container width percentage if specified
const calculatedWidth = React.useMemo(() => {
if (isPercentageWidth && containerWidth > 0) {
Expand Down Expand Up @@ -100,32 +100,39 @@
};

const handleClickInsideDrawer = (event: React.MouseEvent<HTMLDivElement, MouseEvent>) => {
(event.nativeEvent as DrawerEvent)._capturedInsideDrawer = true;

Check warning on line 103 in src/components/Drawer/Drawer.tsx

View workflow job for this annotation

GitHub Actions / Verify Files

Assignment to property of function parameter 'event'
};

const itemContainer = itemContainerRef?.current;
if (!itemContainer) {
return null;
}

return (
<GravityDrawer
onEscape={onClose}
onVeilClick={onClose}
hideVeil
className={b('container', className)}
>
<DrawerItem
id={drawerId}
visible={isVisible}
resizable
maxResizeWidth={containerWidth}
width={isPercentageWidth ? calculatedWidth : drawerWidth}
onResize={handleResizeDrawer}
direction={direction}
className={b('item')}
ref={detectClickOutside ? drawerRef : undefined}
<Portal container={itemContainer}>
<GravityDrawer
onEscape={onClose}
onVeilClick={onClose}
hideVeil
className={b('container', className)}
>
<div className={b('click-handler')} onClickCapture={handleClickInsideDrawer}>
{children}
</div>
</DrawerItem>
</GravityDrawer>
<DrawerItem
id={drawerId}
visible={isVisible}
resizable
maxResizeWidth={containerWidth}
width={isPercentageWidth ? calculatedWidth : drawerWidth}
onResize={handleResizeDrawer}
direction={direction}
className={b('item')}
ref={detectClickOutside ? drawerRef : undefined}
>
<div className={b('click-handler')} onClickCapture={handleClickInsideDrawer}>
{children}
</div>
</DrawerItem>
</GravityDrawer>
</Portal>
);
};

Expand Down
5 changes: 5 additions & 0 deletions src/components/Drawer/DrawerContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@

export interface DrawerContextType {
containerWidth: number;
itemContainerRef: React.RefObject<HTMLDivElement> | null;
setContainerWidth: React.Dispatch<React.SetStateAction<number>>;
}

const DrawerContext = React.createContext<DrawerContextType>({
containerWidth: 0,
itemContainerRef: null,
setContainerWidth: () => {},
});

Expand All @@ -24,6 +26,7 @@
export const DrawerContextProvider = ({children, className}: DrawerContextProviderProps) => {
const [containerWidth, setContainerWidth] = React.useState(0);
const containerRef = React.useRef<HTMLDivElement>(null);
const itemContainerRef = React.useRef<HTMLDivElement>(null);

React.useEffect(() => {
if (!containerRef.current) {
Expand All @@ -44,7 +47,7 @@
resizeObserver.observe(containerRef.current);

return () => {
if (containerRef.current) {

Check warning on line 50 in src/components/Drawer/DrawerContext.tsx

View workflow job for this annotation

GitHub Actions / Verify Files

The ref value 'containerRef.current' will likely have changed by the time this effect cleanup function runs. If this ref points to a node rendered by React, copy 'containerRef.current' to a variable inside the effect, and use that variable in the cleanup function
resizeObserver.disconnect();
}
};
Expand All @@ -55,6 +58,7 @@
() => ({
containerWidth,
setContainerWidth,
itemContainerRef,
}),
[containerWidth],
);
Expand All @@ -63,6 +67,7 @@
<DrawerContext.Provider value={value}>
<div ref={containerRef} className={b('drawer-container', className)}>
{children}
<div ref={itemContainerRef} className={b('item-container')} />
</div>
</DrawerContext.Provider>
);
Expand Down
Loading