modal.vue 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. <script lang="ts" setup>
  2. import type { ExtendedModalApi, ModalProps } from './modal';
  3. import { computed, nextTick, ref, watch } from 'vue';
  4. import {
  5. useIsMobile,
  6. usePriorityValues,
  7. useSimpleLocale,
  8. } from '@vben-core/composables';
  9. import { Expand, Shrink } from '@vben-core/icons';
  10. import {
  11. Dialog,
  12. DialogContent,
  13. DialogDescription,
  14. DialogFooter,
  15. DialogHeader,
  16. DialogTitle,
  17. VbenButton,
  18. VbenHelpTooltip,
  19. VbenIconButton,
  20. VbenLoading,
  21. VisuallyHidden,
  22. } from '@vben-core/shadcn-ui';
  23. import { cn } from '@vben-core/shared/utils';
  24. import { useModalDraggable } from './use-modal-draggable';
  25. interface Props extends ModalProps {
  26. class?: string;
  27. contentClass?: string;
  28. footerClass?: string;
  29. headerClass?: string;
  30. modalApi?: ExtendedModalApi;
  31. }
  32. const props = withDefaults(defineProps<Props>(), {
  33. class: '',
  34. contentClass: '',
  35. footerClass: '',
  36. headerClass: '',
  37. modalApi: undefined,
  38. });
  39. const contentRef = ref();
  40. const wrapperRef = ref<HTMLElement>();
  41. const dialogRef = ref();
  42. const headerRef = ref();
  43. const footerRef = ref();
  44. const { $t } = useSimpleLocale();
  45. const { isMobile } = useIsMobile();
  46. const state = props.modalApi?.useStore?.();
  47. const {
  48. cancelText,
  49. centered,
  50. closable,
  51. closeOnClickModal,
  52. closeOnPressEscape,
  53. confirmLoading,
  54. confirmText,
  55. description,
  56. draggable,
  57. footer: showFooter,
  58. fullscreen,
  59. fullscreenButton,
  60. header,
  61. loading: showLoading,
  62. modal,
  63. openAutoFocus,
  64. showCancelButton,
  65. showConfirmButton,
  66. title,
  67. titleTooltip,
  68. } = usePriorityValues(props, state);
  69. const shouldFullscreen = computed(
  70. () => (fullscreen.value && header.value) || isMobile.value,
  71. );
  72. const shouldDraggable = computed(
  73. () => draggable.value && !shouldFullscreen.value && header.value,
  74. );
  75. const { dragging, transform } = useModalDraggable(
  76. dialogRef,
  77. headerRef,
  78. shouldDraggable,
  79. );
  80. watch(
  81. () => state?.value?.isOpen,
  82. async (v) => {
  83. if (v) {
  84. await nextTick();
  85. if (!contentRef.value) return;
  86. const innerContentRef = contentRef.value.getContentRef();
  87. dialogRef.value = innerContentRef.$el;
  88. // reopen modal reassign value
  89. const { offsetX, offsetY } = transform;
  90. dialogRef.value.style.transform = `translate(${offsetX}px, ${offsetY}px)`;
  91. }
  92. },
  93. );
  94. watch(
  95. () => showLoading.value,
  96. (v) => {
  97. if (v && wrapperRef.value) {
  98. wrapperRef.value.scrollTo({
  99. // behavior: 'smooth',
  100. top: 0,
  101. });
  102. }
  103. },
  104. );
  105. function handleFullscreen() {
  106. props.modalApi?.setState((prev) => {
  107. // if (prev.fullscreen) {
  108. // resetPosition();
  109. // }
  110. return { ...prev, fullscreen: !fullscreen.value };
  111. });
  112. }
  113. function interactOutside(e: Event) {
  114. if (!closeOnClickModal.value) {
  115. e.preventDefault();
  116. }
  117. }
  118. function escapeKeyDown(e: KeyboardEvent) {
  119. if (!closeOnPressEscape.value) {
  120. e.preventDefault();
  121. }
  122. }
  123. function handerOpenAutoFocus(e: Event) {
  124. if (!openAutoFocus.value) {
  125. e?.preventDefault();
  126. }
  127. }
  128. // pointer-down-outside
  129. function pointerDownOutside(e: Event) {
  130. const target = e.target as HTMLElement;
  131. const isDismissableModal = !!target?.dataset.dismissableModal;
  132. if (!closeOnClickModal.value || !isDismissableModal) {
  133. e.preventDefault();
  134. }
  135. }
  136. </script>
  137. <template>
  138. <Dialog
  139. :modal="modal"
  140. :open="state?.isOpen"
  141. @update:open="() => modalApi?.close()"
  142. >
  143. <DialogContent
  144. ref="contentRef"
  145. :class="
  146. cn(
  147. 'border-border left-0 right-0 top-[10vh] mx-auto flex max-h-[80%] w-[520px] flex-col border p-0',
  148. props.class,
  149. {
  150. 'left-0 top-0 size-full max-h-full !translate-x-0 !translate-y-0':
  151. shouldFullscreen,
  152. 'top-1/2 !-translate-y-1/2': centered && !shouldFullscreen,
  153. 'duration-300': !dragging,
  154. },
  155. )
  156. "
  157. :show-close="closable"
  158. close-class="top-3"
  159. @escape-key-down="escapeKeyDown"
  160. @interact-outside="interactOutside"
  161. @open-auto-focus="handerOpenAutoFocus"
  162. @pointer-down-outside="pointerDownOutside"
  163. >
  164. <DialogHeader
  165. ref="headerRef"
  166. :class="
  167. cn(
  168. 'border-b px-5 py-4',
  169. {
  170. hidden: !header,
  171. 'cursor-move select-none': shouldDraggable,
  172. },
  173. props.headerClass,
  174. )
  175. "
  176. >
  177. <DialogTitle v-if="title" class="text-left">
  178. <slot name="title">
  179. {{ title }}
  180. <slot v-if="titleTooltip" name="titleTooltip">
  181. <VbenHelpTooltip trigger-class="pb-1">
  182. {{ titleTooltip }}
  183. </VbenHelpTooltip>
  184. </slot>
  185. </slot>
  186. </DialogTitle>
  187. <DialogDescription v-if="description">
  188. <slot name="description">
  189. {{ description }}
  190. </slot>
  191. </DialogDescription>
  192. <VisuallyHidden v-if="!title || !description">
  193. <DialogTitle v-if="!title" />
  194. <DialogDescription v-if="!description" />
  195. </VisuallyHidden>
  196. </DialogHeader>
  197. <div
  198. ref="wrapperRef"
  199. :class="
  200. cn('relative min-h-40 flex-1 overflow-y-auto p-3', contentClass, {
  201. 'overflow-hidden': showLoading,
  202. })
  203. "
  204. >
  205. <VbenLoading
  206. v-if="showLoading"
  207. class="size-full h-auto min-h-full"
  208. spinning
  209. />
  210. <slot></slot>
  211. </div>
  212. <VbenIconButton
  213. v-if="fullscreenButton"
  214. class="hover:bg-accent hover:text-accent-foreground text-foreground/80 flex-center absolute right-10 top-3 hidden size-6 rounded-full px-1 text-lg opacity-70 transition-opacity hover:opacity-100 focus:outline-none disabled:pointer-events-none sm:block"
  215. @click="handleFullscreen"
  216. >
  217. <Shrink v-if="fullscreen" class="size-3.5" />
  218. <Expand v-else class="size-3.5" />
  219. </VbenIconButton>
  220. <DialogFooter
  221. v-if="showFooter"
  222. ref="footerRef"
  223. :class="
  224. cn(
  225. 'flex-row items-center justify-end border-t p-2',
  226. props.footerClass,
  227. )
  228. "
  229. >
  230. <slot name="prepend-footer"></slot>
  231. <slot name="footer">
  232. <VbenButton
  233. v-if="showCancelButton"
  234. variant="ghost"
  235. @click="() => modalApi?.onCancel()"
  236. >
  237. <slot name="cancelText">
  238. {{ cancelText || $t('cancel') }}
  239. </slot>
  240. </VbenButton>
  241. <VbenButton
  242. v-if="showConfirmButton"
  243. :loading="confirmLoading"
  244. @click="() => modalApi?.onConfirm()"
  245. >
  246. <slot name="confirmText">
  247. {{ confirmText || $t('confirm') }}
  248. </slot>
  249. </VbenButton>
  250. </slot>
  251. <slot name="append-footer"></slot>
  252. </DialogFooter>
  253. </DialogContent>
  254. </Dialog>
  255. </template>