B: Prevent default behavior when ESC is intercepted
Some checks failed
Frontend CI / build (22.x) (push) Has been cancelled
Frontend CI / notify-failure (push) Has been cancelled

This commit is contained in:
Ivan 2025-04-02 17:44:37 +03:00
parent 1e96ffe594
commit 78bd97ec51

View File

@ -6,6 +6,8 @@ export function useEscapeKey(handleClose: () => void, isEnabled: boolean = true)
const handleEscKey = useCallback(
(event: KeyboardEvent) => {
if (event.key === 'Escape') {
event.preventDefault();
event.stopPropagation();
handleClose();
}
},
@ -14,9 +16,9 @@ export function useEscapeKey(handleClose: () => void, isEnabled: boolean = true)
useEffect(() => {
if (isEnabled) {
document.addEventListener('keyup', handleEscKey, false);
document.addEventListener('keydown', handleEscKey, false);
return () => {
document.removeEventListener('keyup', handleEscKey, false);
document.removeEventListener('keydown', handleEscKey, false);
};
}
}, [handleEscKey, isEnabled]);