Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
42 changes: 30 additions & 12 deletions src/components/Drawer/Drawer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
type OnResizeContinueHandler,
type OnResizeHandler,
useResizableDrawerItem,
useScrollLock,
} from './utils';

import './Drawer.scss';
Expand Down Expand Up @@ -199,6 +200,13 @@ export interface DrawerProps {
* @default false
* */
keepMounted?: boolean;

/**
* Whether to lock page scroll when drawer is open.
* Applied only when hideVeil=true and disablePortal=false.
* @default false
*/
scrollLock?: boolean;
}

export const Drawer: React.FC<DrawerProps> = ({
Expand All @@ -211,6 +219,7 @@ export const Drawer: React.FC<DrawerProps> = ({
hideVeil,
disablePortal = true,
keepMounted = false,
scrollLock = false,
}) => {
let someItemVisible = false;
React.Children.forEach(children, (child) => {
Expand Down Expand Up @@ -239,7 +248,10 @@ export const Drawer: React.FC<DrawerProps> = ({
const containerRef = React.useRef<HTMLDivElement>(null);
const veilRef = React.useRef<HTMLDivElement>(null);

const drawer = (
const shouldApplyScrollLock = scrollLock && someItemVisible && hideVeil && !disablePortal;
useScrollLock(shouldApplyScrollLock);

return (
<Transition
in={someItemVisible}
timeout={{enter: 0, exit: TIMEOUT}}
Expand All @@ -249,7 +261,8 @@ export const Drawer: React.FC<DrawerProps> = ({
>
{(state) => {
const childrenVisible = someItemVisible && state === 'entered';
return (

const content = (
<div ref={containerRef} className={b({hideVeil}, className)} style={style}>
<CSSTransition
in={childrenVisible}
Expand Down Expand Up @@ -280,17 +293,22 @@ export const Drawer: React.FC<DrawerProps> = ({
})}
</div>
);
}}
</Transition>
);

if (disablePortal) {
return drawer;
}
if (disablePortal) {
return content;
}

return (
<Portal>
{someItemVisible ? <FloatingOverlay lockScroll>{drawer}</FloatingOverlay> : drawer}
</Portal>
// When hideVeil=true, we don't use FloatingOverlay to avoid blocking mouse events
if (hideVeil) {
return <Portal>{content}</Portal>;
}

return (
<Portal>
<FloatingOverlay lockScroll={true}>{content}</FloatingOverlay>
</Portal>
);
}}
</Transition>
);
};
4 changes: 4 additions & 0 deletions src/components/Drawer/__stories__/Drawer.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {DisablePortalShowcase} from './DisablePortal';
import {DrawerShowcase} from './DrawerShowcase';
import {HideVeilShowcase} from './HideVeil';
import {ResizableItemShowcase} from './ResizableItem';
import {ScrollLockShowcase} from './ScrollLock';
import {UsePortalShowcase} from './UsePortal';

export default {
Expand All @@ -25,5 +26,8 @@ export const DisablePortal = DisablePortalTemplate.bind({});
const HideVeilTemplate: StoryFn = () => <HideVeilShowcase />;
export const HideVeil = HideVeilTemplate.bind({});

const ScrollLockTemplate: StoryFn = () => <ScrollLockShowcase />;
export const ScrollLock = ScrollLockTemplate.bind({});

const UsePortalTemplate: StoryFn = () => <UsePortalShowcase />;
export const UsePortal = UsePortalTemplate.bind({});
42 changes: 42 additions & 0 deletions src/components/Drawer/__stories__/ScrollLock.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
.scroll-lock {
--gn-drawer-item-position: absolute;

width: 100dvw;
height: 100dvh;

display: flex;
flex-direction: column;

&__header {
padding: 20px;
border-bottom: 1px solid var(--g-color-line-generic);
display: flex;
align-items: center;
gap: 8px;
}

&__container {
position: relative;
flex-grow: 1;
padding: 16px;
}

&__drawer {
position: absolute;
inset: 0;
overflow: hidden;
}

&__item {
width: 400px;
height: 100%;
overflow-x: hidden;
}

&__item-content {
box-sizing: border-box;
padding: 8px 16px;
height: 100%;
overflow-y: scroll;
}
}
48 changes: 48 additions & 0 deletions src/components/Drawer/__stories__/ScrollLock.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import React from 'react';

import {Button, Checkbox} from '@gravity-ui/uikit';

import {cn} from '../../utils/cn';
import {Drawer, DrawerItem} from '../Drawer';

import {PlaceholderText} from './moc';

import './ScrollLock.scss';

const b = cn('scroll-lock');

export function ScrollLockShowcase() {
const [visible, setVisible] = React.useState(true);
const [scrollLock, setScrollLock] = React.useState(true);

return (
<div className={b()}>
<div className={b('header')}>
<Button view="action" onClick={() => setVisible((v) => !v)}>
{visible ? 'Hide' : 'Show'}
</Button>
<Checkbox content="Scroll lock" checked={scrollLock} onUpdate={setScrollLock} />
</div>
<div className={b('container')}>
{Array.from({length: 50}, (_, index) => (
<p key={index}>
Lorem ipsum dolor sit amet consectetur adipisicing elit. Porro, quos!
</p>
))}
<Drawer
className={b('drawer')}
hideVeil={true}
disablePortal={false}
scrollLock={scrollLock}
onVeilClick={() => setVisible(false)}
>
<DrawerItem id="item" className={b('item')} direction="right" visible={visible}>
<div className={b('item-content')} tabIndex={0}>
<PlaceholderText />
</div>
</DrawerItem>
</Drawer>
</div>
</div>
);
}
15 changes: 14 additions & 1 deletion src/components/Drawer/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,19 @@ function getEventClientPosition(
return direction === 'horizontal' ? e.clientX : e.clientY;
}

export function useScrollLock(enabled: boolean) {
React.useEffect(() => {
if (!enabled) return;

const originalStyle = window.getComputedStyle(document.body).overflow;
document.body.style.overflow = 'hidden';

return () => {
document.body.style.overflow = originalStyle;
};
}, [enabled]);
}

export interface UseResizeHandlersParams {
onStart: () => void;
onMove: (delta: number) => void;
Expand Down Expand Up @@ -99,7 +112,7 @@ export function useResizeHandlers({

onStart();
},
[handleEnd, handleMove, onStart, direction],
[handleEnd, handleMove, onStart, direction, disableSelect],
);

return {
Expand Down
Loading