Skip to content

Commit

Permalink
Prevent modals from closing for first second after opening
Browse files Browse the repository at this point in the history
  • Loading branch information
Iapetus-11 committed Nov 28, 2024
1 parent 956f4b0 commit cd5830d
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/components/Modal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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<HTMLDialogElement>();
const innerContainer = ref<HTMLElement>();
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);
});
Expand Down

0 comments on commit cd5830d

Please sign in to comment.