forked from fdzcxy212206413/gsl_grs
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
33 lines
1.0 KiB
33 lines
1.0 KiB
2 months ago
|
import { onMounted, onBeforeUnmount } from 'vue';
|
||
|
import '../../utils/index.mjs';
|
||
|
import '../../constants/index.mjs';
|
||
|
import { EVENT_CODE } from '../../constants/aria.mjs';
|
||
|
import { isClient } from '@vueuse/core';
|
||
|
|
||
|
let registeredEscapeHandlers = [];
|
||
|
const cachedHandler = (e) => {
|
||
|
const event = e;
|
||
|
if (event.key === EVENT_CODE.esc) {
|
||
|
registeredEscapeHandlers.forEach((registeredHandler) => registeredHandler(event));
|
||
|
}
|
||
|
};
|
||
|
const useEscapeKeydown = (handler) => {
|
||
|
onMounted(() => {
|
||
|
if (registeredEscapeHandlers.length === 0) {
|
||
|
document.addEventListener("keydown", cachedHandler);
|
||
|
}
|
||
|
if (isClient)
|
||
|
registeredEscapeHandlers.push(handler);
|
||
|
});
|
||
|
onBeforeUnmount(() => {
|
||
|
registeredEscapeHandlers = registeredEscapeHandlers.filter((registeredHandler) => registeredHandler !== handler);
|
||
|
if (registeredEscapeHandlers.length === 0) {
|
||
|
if (isClient)
|
||
|
document.removeEventListener("keydown", cachedHandler);
|
||
|
}
|
||
|
});
|
||
|
};
|
||
|
|
||
|
export { useEscapeKeydown };
|
||
|
//# sourceMappingURL=index.mjs.map
|