From e3bba8bf5d472f03fea5413cb8ec8ccfd47a0003 Mon Sep 17 00:00:00 2001 From: Pedro Ladaria Date: Tue, 17 Dec 2024 18:39:23 +0100 Subject: [PATCH] WEB-2113 close with esc + modal state effect --- src/drawer.tsx | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/src/drawer.tsx b/src/drawer.tsx index eba7fbe41..70931e37b 100644 --- a/src/drawer.tsx +++ b/src/drawer.tsx @@ -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; @@ -29,10 +31,8 @@ type DrawerPropsRef = { const DrawerLayout = React.forwardRef( ({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) => { @@ -53,6 +53,18 @@ const DrawerLayout = React.forwardRef( close, })); + React.useEffect(() => { + const handleKeyDown = (event: KeyboardEvent) => { + if (event.key === 'Escape') { + close(); + } + }; + document.addEventListener('keydown', handleKeyDown); + return () => { + document.removeEventListener('keydown', handleKeyDown); + }; + }, [close]); + return ( @@ -66,7 +78,7 @@ const DrawerLayout = React.forwardRef( />
{children}