index.mjs 811 B

123456789101112131415161718192021222324252627282930
  1. import { watch } from 'vue';
  2. import { isClient, useEventListener } from '@vueuse/core';
  3. import { getEventCode } from '../../utils/dom/event.mjs';
  4. import { EVENT_CODE } from '../../constants/aria.mjs';
  5. const modalStack = [];
  6. const closeModal = (e) => {
  7. if (modalStack.length === 0)
  8. return;
  9. const code = getEventCode(e);
  10. if (code === EVENT_CODE.esc) {
  11. e.stopPropagation();
  12. const topModal = modalStack[modalStack.length - 1];
  13. topModal.handleClose();
  14. }
  15. };
  16. const useModal = (instance, visibleRef) => {
  17. watch(visibleRef, (val) => {
  18. if (val) {
  19. modalStack.push(instance);
  20. } else {
  21. modalStack.splice(modalStack.indexOf(instance), 1);
  22. }
  23. });
  24. };
  25. if (isClient)
  26. useEventListener(document, "keydown", closeModal);
  27. export { useModal };
  28. //# sourceMappingURL=index.mjs.map