index.mjs 951 B

1234567891011121314151617181920212223242526272829
  1. import { onMounted, onBeforeUnmount } from 'vue';
  2. import { isClient } from '@vueuse/core';
  3. import { EVENT_CODE } from '../../constants/aria.mjs';
  4. let registeredEscapeHandlers = [];
  5. const cachedHandler = (event) => {
  6. if (event.code === EVENT_CODE.esc) {
  7. registeredEscapeHandlers.forEach((registeredHandler) => registeredHandler(event));
  8. }
  9. };
  10. const useEscapeKeydown = (handler) => {
  11. onMounted(() => {
  12. if (registeredEscapeHandlers.length === 0) {
  13. document.addEventListener("keydown", cachedHandler);
  14. }
  15. if (isClient)
  16. registeredEscapeHandlers.push(handler);
  17. });
  18. onBeforeUnmount(() => {
  19. registeredEscapeHandlers = registeredEscapeHandlers.filter((registeredHandler) => registeredHandler !== handler);
  20. if (registeredEscapeHandlers.length === 0) {
  21. if (isClient)
  22. document.removeEventListener("keydown", cachedHandler);
  23. }
  24. });
  25. };
  26. export { useEscapeKeydown };
  27. //# sourceMappingURL=index.mjs.map