-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
112 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,140 +1,130 @@ | ||
import { computed, getCurrentInstance, inject, nextTick, onMounted, onUnmounted, watch } from 'vue' | ||
import { computed, inject, nextTick, onMounted, onUnmounted, watch } from 'vue' | ||
import { PLUGIN_NAME, CLASS_NAME } from './index' | ||
|
||
import type { ComputedRef, Ref } from 'vue' | ||
import type { Provide } from './index' | ||
|
||
type UseOrder = ({ modelValue, show }: { | ||
modelValue: Ref<boolean|undefined>; | ||
type UseA11Y = ({ modalRef, latest, show }: { | ||
modalRef: Ref<null|HTMLElement>; | ||
latest: ComputedRef<boolean>; | ||
show: Ref<boolean>; | ||
}) => void | ||
|
||
type UseClose = ({ close, closeKeyCode, latest }: { | ||
close: Ref; | ||
closeClickDimmed: boolean; | ||
closeKeyCode: number | false; | ||
latest: ComputedRef<boolean>; | ||
}) => { | ||
onClickDimmed: () => void | ||
} | ||
|
||
type UseOrder = ({ modalRef, show }: { | ||
modalRef: Ref<HTMLElement|null> | ||
show: Ref<boolean>; | ||
}) => { | ||
latest: ComputedRef<boolean> | ||
} | ||
export const useOrder: UseOrder = ({ modelValue, show }) => { | ||
const { visibleModals, addVisibleModals, removeVisibleModals } = inject(PLUGIN_NAME) as Provide | ||
const { uid } = getCurrentInstance() || {} | ||
|
||
const latest = computed(() => { | ||
if (!uid || !visibleModals.value.length) return false | ||
return uid === visibleModals.value[visibleModals.value.length - 1] | ||
}) | ||
export const useA11Y: UseA11Y = ({ modalRef, latest, show }) => { | ||
let activeElement: Element | null | ||
|
||
watch([ | ||
() => modelValue.value, | ||
() => show.value | ||
], () => { | ||
if (!uid) { | ||
return | ||
} | ||
function setLastActiveElement (event: Event) { | ||
const isModalEvent = (event.target as Element).closest(`.${CLASS_NAME}`) | ||
|
||
const isShow = modelValue.value && show.value | ||
// skip when this not latest modal | ||
if (!latest.value) return | ||
|
||
if (isShow && visibleModals.value.indexOf(uid) < 0) { | ||
addVisibleModals(uid) | ||
// set activeElement when fired outside this modal | ||
if (!isModalEvent || (isModalEvent !== modalRef.value)) { | ||
// skip when modal status is closing | ||
if (isModalEvent && !isModalEvent.classList.contains(`${CLASS_NAME}-show`)) { | ||
return | ||
} | ||
activeElement = event.target as Element | ||
} | ||
} | ||
|
||
if (!isShow && visibleModals.value.indexOf(uid) > -1) { | ||
removeVisibleModals(uid) | ||
function setFocus (value: boolean) { | ||
if (value) { | ||
if (modalRef.value) { | ||
(modalRef.value as unknown as HTMLElement).focus() | ||
} | ||
} else { | ||
if (activeElement) { | ||
(activeElement as HTMLElement).focus() | ||
} | ||
} | ||
}, { immediate: true }) | ||
|
||
return { | ||
latest | ||
} | ||
} | ||
|
||
type UseColse = ({ close, options, latest }: { | ||
close: Ref; | ||
options: Ref<{ [key: string]: unknown }>; | ||
latest: ComputedRef<boolean> | ||
}) => { | ||
mergeOptions: { | ||
transition: number | false, | ||
closeClickDimmed: boolean, | ||
closeKeyCode: number | false, | ||
styleModalContent: { [key: string]: unknown } | ||
}; | ||
onClickDimmed: () => void | ||
onMounted(() => { | ||
document.addEventListener('click', setLastActiveElement) | ||
watch(() => show.value, (value) => { | ||
nextTick(() => setFocus(value)) | ||
}, { immediate: show.value }) | ||
}) | ||
|
||
onUnmounted(() => { | ||
document.removeEventListener('click', setLastActiveElement) | ||
}) | ||
} | ||
export const useClose: UseColse = ({ close, options, latest }) => { | ||
const mergeOptions = { | ||
transition: 300, | ||
closeClickDimmed: true, | ||
closeKeyCode: 27, | ||
styleModalContent: {}, | ||
...options.value | ||
} | ||
|
||
export const useClose: UseClose = ({ close, closeClickDimmed, closeKeyCode, latest }) => { | ||
function onClickDimmed () { | ||
if (mergeOptions.closeClickDimmed) { | ||
if (closeClickDimmed) { | ||
close.value() | ||
} | ||
} | ||
|
||
function closeKeyEvent (event: KeyboardEvent) { | ||
if (event.keyCode === mergeOptions.closeKeyCode && latest.value) { | ||
if (event.keyCode === closeKeyCode && latest.value) { | ||
close.value() | ||
} | ||
} | ||
|
||
onMounted(() => { | ||
if (mergeOptions.closeKeyCode) { | ||
if (closeKeyCode) { | ||
document.addEventListener('keyup', closeKeyEvent) | ||
} | ||
}) | ||
|
||
onUnmounted(() => { | ||
if (mergeOptions.closeKeyCode) { | ||
if (closeKeyCode) { | ||
document.removeEventListener('keyup', closeKeyEvent) | ||
} | ||
}) | ||
|
||
return { | ||
mergeOptions, | ||
onClickDimmed | ||
} | ||
} | ||
|
||
type UseA11Y = ({ modalRef, latest, show }: { | ||
modalRef: Ref<null|HTMLElement>; | ||
latest: ComputedRef<boolean>; | ||
show: Ref<boolean>; | ||
}) => void | ||
export const useA11Y: UseA11Y = ({ modalRef, latest, show }) => { | ||
let activeElement: Element | null | ||
|
||
function setLastActiveElement (event: Event) { | ||
const isModalEvent = (event.target as Element).closest(`.${CLASS_NAME}`) | ||
export const useOrder: UseOrder = ({ modalRef, show }) => { | ||
const { visibleModals, addVisibleModals, removeVisibleModals } = inject(PLUGIN_NAME) as Provide | ||
|
||
// skip when this not latest modal | ||
if (!latest.value) return | ||
const latest = computed(() => { | ||
const arr = [...visibleModals.value.values()] | ||
|
||
// set activeElement when fired outside this modal | ||
if (!isModalEvent || (isModalEvent !== modalRef.value)) { | ||
// skip when modal status is closing | ||
if (isModalEvent && !isModalEvent.classList.contains(`${CLASS_NAME}-show`)) return | ||
activeElement = event.target as Element | ||
if (!arr.length || !modalRef.value) { | ||
return false | ||
} | ||
} | ||
|
||
onMounted(() => { | ||
document.addEventListener('click', setLastActiveElement) | ||
function setFocus (value: boolean) { | ||
if (value) { | ||
if (modalRef.value) { | ||
(modalRef.value as unknown as HTMLElement).focus() | ||
} | ||
return arr[arr.length - 1] === modalRef.value | ||
}) | ||
|
||
watch(() => show.value, () => { | ||
nextTick(() => { | ||
if (!modalRef.value) return | ||
|
||
if (show.value) { | ||
addVisibleModals(modalRef.value) | ||
} else { | ||
if (activeElement) { | ||
(activeElement as HTMLElement).focus() | ||
} | ||
removeVisibleModals(modalRef.value) | ||
} | ||
} | ||
watch(() => show.value, (value) => { | ||
nextTick(() => setFocus(value)) | ||
}, { immediate: show.value }) | ||
}) | ||
}) | ||
}, { immediate: true }) | ||
|
||
onUnmounted(() => { | ||
document.removeEventListener('click', setLastActiveElement) | ||
}) | ||
return { | ||
latest | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters