From cd5830da174d048b96ad9dc2f26bb87bcd642988 Mon Sep 17 00:00:00 2001 From: Milo Weinberg Date: Thu, 28 Nov 2024 01:27:08 -0500 Subject: [PATCH] Prevent modals from closing for first second after opening --- src/components/Modal.vue | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/components/Modal.vue b/src/components/Modal.vue index 99d4a75..23ee703 100644 --- a/src/components/Modal.vue +++ b/src/components/Modal.vue @@ -11,25 +11,33 @@ close: []; }>(); + const shownTime = performance.now(); + + function close() { + // Prevent modal closing for first second, in case they're spamming + if (performance.now() - shownTime >= 1000) { + emit('close'); + } + } + const dialog = ref(); const innerContainer = ref(); async function onWindowClick(ev: MouseEvent) { if (ev.target instanceof HTMLElement && !innerContainer.value!.contains(ev.target)) { - emit('close'); + close(); } } function onWindowKeyPress(ev: KeyboardEvent) { if (ev.key === 'Escape') { - emit('close'); + close(); } } onMounted(async () => { dialog.value!.showModal(); - await new Promise((resolve) => setTimeout(resolve, 200)); window.addEventListener('click', onWindowClick); window.addEventListener('keydown', onWindowKeyPress); });