Skip to content

Commit 4c3da81

Browse files
committed
refactor(modal): simplify ESC-handler by using { once: true }
1 parent 8353aeb commit 4c3da81

File tree

1 file changed

+4
-5
lines changed
  • packages/kit-headless/src/components/modal

1 file changed

+4
-5
lines changed

packages/kit-headless/src/components/modal/modal.tsx

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,12 @@ export const Modal = component$((props: ModalProps) => {
5050

5151
const focusTrap = trapFocus(modal);
5252

53-
const escapeKeydownHandler = overrideNativeDialogEscapeBehaviorWith(
54-
() => (showSig.value = false),
53+
window.addEventListener(
54+
'keydown',
55+
overrideNativeDialogEscapeBehaviorWith(() => (showSig.value = false)),
56+
{ once: true },
5557
);
5658

57-
window.addEventListener('keydown', escapeKeydownHandler);
58-
5959
if (isOpen) {
6060
showModal(modal);
6161
props.onShow$?.();
@@ -71,7 +71,6 @@ export const Modal = component$((props: ModalProps) => {
7171
cleanup(() => {
7272
deactivateFocusTrap(focusTrap);
7373
keepModalInPlaceWhileScrollbarReappears(scrollbar, modalRefSig.value);
74-
window.removeEventListener('keydown', escapeKeydownHandler);
7574
});
7675
});
7776

0 commit comments

Comments
 (0)