index.mjs 723 B

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