Skip to content

Commit

Permalink
WEB-2113 close with esc + modal state effect
Browse files Browse the repository at this point in the history
  • Loading branch information
Pedro Ladaria committed Dec 17, 2024
1 parent 89fee12 commit e3bba8b
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions src/drawer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ import classnames from 'classnames';
import {Portal} from './portal';
import {useScreenSize} from './hooks';
import FocusTrap from './focus-trap';
import {useSetModalStateEffect} from './modal-context-provider';

const PADDING_X_DESKTOP = 40;
const PADDING_X_MOBILE = 16;
const CONTENT_WIDTH_DESKTOP = 388;
const WIDTH_CONTENT_DESKTOP = 388;
const WIDTH_DESKTOP = WIDTH_CONTENT_DESKTOP + PADDING_X_DESKTOP * 2;

type DrawerLayoutProps = {
width?: number;
Expand All @@ -29,10 +31,8 @@ type DrawerPropsRef = {

const DrawerLayout = React.forwardRef<DrawerPropsRef, DrawerLayoutProps>(
({width, children, onClose}, ref) => {
const defaultWidth = CONTENT_WIDTH_DESKTOP + PADDING_X_DESKTOP * 2;

useSetModalStateEffect();
const {isDesktopOrBigger} = useScreenSize();

const [isOpen, setIsOpen] = React.useState(false);

const open = React.useCallback((node: HTMLDivElement) => {
Expand All @@ -53,6 +53,18 @@ const DrawerLayout = React.forwardRef<DrawerPropsRef, DrawerLayoutProps>(
close,
}));

React.useEffect(() => {
const handleKeyDown = (event: KeyboardEvent) => {
if (event.key === 'Escape') {
close();
}
};
document.addEventListener('keydown', handleKeyDown);
return () => {
document.removeEventListener('keydown', handleKeyDown);
};
}, [close]);

return (
<Portal>
<FocusTrap>
Expand All @@ -66,7 +78,7 @@ const DrawerLayout = React.forwardRef<DrawerPropsRef, DrawerLayoutProps>(
/>
<div
ref={open}
style={{width: isDesktopOrBigger ? width || defaultWidth : 'unset'}}
style={{width: isDesktopOrBigger ? width || WIDTH_DESKTOP : 'unset'}}
className={classnames(styles.container, isOpen ? styles.open : styles.closed)}
>
{children}
Expand Down

0 comments on commit e3bba8b

Please sign in to comment.