Skip to content

Commit 6e0b43d

Browse files
committed
feat: add closeByEsc prop to the afcl modal and rename clickToCloseOutside -> closeByClickOutside
https://web.tracklify.com/project/2b7ZVgE5/AdminForth/1298/EeUgSNFH/modal-afcl-component-should-ha
1 parent c91ef4e commit 6e0b43d

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

adminforth/spa/src/afcl/Modal.vue

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ const removeFromDom = computed(() => {
5757
})
5858
5959
interface DialogProps {
60-
clickToCloseOutside?: boolean
60+
closeByClickOutside?: boolean
61+
closeByEsc?: boolean
6162
beforeCloseFunction?: (() => void | Promise<void>) | null
6263
beforeOpenFunction?: (() => void | Promise<void>) | null
6364
askForCloseConfirmation?: boolean
@@ -66,7 +67,8 @@ interface DialogProps {
6667
}
6768
6869
const props = withDefaults(defineProps<DialogProps>(), {
69-
clickToCloseOutside: true,
70+
closeByClickOutside: true,
71+
closeByEsc: true,
7072
beforeCloseFunction: null,
7173
beforeOpenFunction: null,
7274
askForCloseConfirmation: false,
@@ -109,8 +111,25 @@ function toggleModal() {
109111
}
110112
}
111113
114+
function onEsc(event: KeyboardEvent) {
115+
if (event.key === 'Escape') {
116+
if (props.closeByEsc) {
117+
tryToHideModal();
118+
}
119+
}
120+
}
121+
122+
onMounted(() => {
123+
document.addEventListener('keydown', onEsc)
124+
})
125+
126+
onUnmounted(() => {
127+
document.removeEventListener('keydown', onEsc)
128+
})
129+
130+
112131
function backdropClick(e: MouseEvent) {
113-
if (props.clickToCloseOutside && e.target === e.currentTarget) {
132+
if (props.closeByClickOutside && e.target === e.currentTarget) {
114133
tryToHideModal();
115134
}
116135
}

0 commit comments

Comments
 (0)