listen for esc to close modals

This commit is contained in:
Hayden 2023-07-31 12:43:56 -05:00
parent b9a255914b
commit da2e0103c3
No known key found for this signature in database
GPG key ID: 17CF79474E257545

View file

@ -32,6 +32,12 @@
}, },
}); });
function escClose(e: KeyboardEvent) {
if (e.key === "Escape") {
close();
}
}
function close() { function close() {
if (props.readonly) { if (props.readonly) {
emit("cancel"); emit("cancel");
@ -42,4 +48,12 @@
const modalId = useId(); const modalId = useId();
const modal = useVModel(props, "modelValue", emit); const modal = useVModel(props, "modelValue", emit);
watchEffect(() => {
if (modal.value) {
document.addEventListener("keydown", escClose);
} else {
document.removeEventListener("keydown", escClose);
}
});
</script> </script>