Skip to content

Commit f14ad83

Browse files
committed
allow dismiss using escape key
1 parent 09945f9 commit f14ad83

1 file changed

Lines changed: 13 additions & 0 deletions

File tree

  • libs/@guardian/source-development-kitchen/src/react-components/popover

libs/@guardian/source-development-kitchen/src/react-components/popover/Popover.tsx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
textSansBold15,
77
} from '@guardian/source/foundations';
88
import { Button, SvgCross } from '@guardian/source/react-components';
9+
import { useEffect } from 'react';
910

1011
export 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"

0 commit comments

Comments
 (0)