index.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', { value: true });
  3. var vue = require('vue');
  4. require('../../utils/index.js');
  5. require('../../constants/index.js');
  6. var aria = require('../../constants/aria.js');
  7. var core = require('@vueuse/core');
  8. let registeredEscapeHandlers = [];
  9. const cachedHandler = (e) => {
  10. const event = e;
  11. if (event.key === aria.EVENT_CODE.esc) {
  12. registeredEscapeHandlers.forEach((registeredHandler) => registeredHandler(event));
  13. }
  14. };
  15. const useEscapeKeydown = (handler) => {
  16. vue.onMounted(() => {
  17. if (registeredEscapeHandlers.length === 0) {
  18. document.addEventListener("keydown", cachedHandler);
  19. }
  20. if (core.isClient)
  21. registeredEscapeHandlers.push(handler);
  22. });
  23. vue.onBeforeUnmount(() => {
  24. registeredEscapeHandlers = registeredEscapeHandlers.filter((registeredHandler) => registeredHandler !== handler);
  25. if (registeredEscapeHandlers.length === 0) {
  26. if (core.isClient)
  27. document.removeEventListener("keydown", cachedHandler);
  28. }
  29. });
  30. };
  31. exports.useEscapeKeydown = useEscapeKeydown;
  32. //# sourceMappingURL=index.js.map