File tree Expand file tree Collapse file tree
libs/@guardian/source-development-kitchen/src/react-components/popover Expand file tree Collapse file tree Original file line number Diff line number Diff line change 66 textSansBold15 ,
77} from '@guardian/source/foundations' ;
88import { Button , SvgCross } from '@guardian/source/react-components' ;
9+ import { useEffect } from 'react' ;
910
1011export interface PopoverProps {
1112 /**
@@ -236,6 +237,18 @@ export const Popover = ({
236237 showPointer,
237238 positionOverrides,
238239} : PopoverProps ) => {
240+ // Respond to escape key by dismissing Popover
241+ useEffect ( ( ) => {
242+ const dismissOnEsc = ( event : KeyboardEvent ) => {
243+ if ( isOpen && event . code === 'Escape' ) {
244+ handleClose ( ) ;
245+ }
246+ } ;
247+ document . addEventListener ( 'keydown' , dismissOnEsc ) ;
248+ // Remove listeners on unmount
249+ return ( ) => document . removeEventListener ( 'keydown' , dismissOnEsc ) ;
250+ } , [ isOpen , handleClose ] ) ;
251+
239252 return (
240253 < div
241254 className = "popover-root"
You can’t perform that action at this time.
0 commit comments