modal.vue 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. <script lang="ts" setup>
  2. import type { ExtendedModalApi, ModalProps } from './modal';
  3. import { computed, nextTick, provide, ref, unref, useId, 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 { ELEMENT_ID_MAIN_CONTENT } from '@vben-core/shared/constants';
  24. import { globalShareState } from '@vben-core/shared/global-state';
  25. import { cn } from '@vben-core/shared/utils';
  26. import { useModalDraggable } from './use-modal-draggable';
  27. interface Props extends ModalProps {
  28. modalApi?: ExtendedModalApi;
  29. }
  30. const props = withDefaults(defineProps<Props>(), {
  31. appendToMain: false,
  32. destroyOnClose: true,
  33. modalApi: undefined,
  34. });
  35. const components = globalShareState.getComponents();
  36. const contentRef = ref();
  37. const wrapperRef = ref<HTMLElement>();
  38. const dialogRef = ref();
  39. const headerRef = ref();
  40. const footerRef = ref();
  41. const id = useId();
  42. provide('DISMISSABLE_MODAL_ID', id);
  43. const { $t } = useSimpleLocale();
  44. const { isMobile } = useIsMobile();
  45. const state = props.modalApi?.useStore?.();
  46. const {
  47. appendToMain,
  48. bordered,
  49. cancelText,
  50. centered,
  51. class: modalClass,
  52. closable,
  53. closeOnClickModal,
  54. closeOnPressEscape,
  55. confirmDisabled,
  56. confirmLoading,
  57. confirmText,
  58. contentClass,
  59. description,
  60. destroyOnClose,
  61. draggable,
  62. footer: showFooter,
  63. footerClass,
  64. fullscreen,
  65. fullscreenButton,
  66. header,
  67. headerClass,
  68. loading: showLoading,
  69. modal,
  70. openAutoFocus,
  71. overlayBlur,
  72. showCancelButton,
  73. showConfirmButton,
  74. submitting,
  75. title,
  76. titleTooltip,
  77. zIndex,
  78. } = usePriorityValues(props, state);
  79. const shouldFullscreen = computed(
  80. () => (fullscreen.value && header.value) || isMobile.value,
  81. );
  82. const shouldDraggable = computed(
  83. () => draggable.value && !shouldFullscreen.value && header.value,
  84. );
  85. const { dragging, transform } = useModalDraggable(
  86. dialogRef,
  87. headerRef,
  88. shouldDraggable,
  89. );
  90. const firstOpened = ref(false);
  91. const isClosed = ref(true);
  92. watch(
  93. () => state?.value?.isOpen,
  94. async (v) => {
  95. if (v) {
  96. isClosed.value = false;
  97. if (!firstOpened.value) firstOpened.value = true;
  98. await nextTick();
  99. if (!contentRef.value) return;
  100. const innerContentRef = contentRef.value.getContentRef();
  101. dialogRef.value = innerContentRef.$el;
  102. // reopen modal reassign value
  103. const { offsetX, offsetY } = transform;
  104. dialogRef.value.style.transform = `translate(${offsetX}px, ${offsetY}px)`;
  105. }
  106. },
  107. { immediate: true },
  108. );
  109. watch(
  110. () => [showLoading.value, submitting.value],
  111. ([l, s]) => {
  112. if ((s || l) && wrapperRef.value) {
  113. wrapperRef.value.scrollTo({
  114. // behavior: 'smooth',
  115. top: 0,
  116. });
  117. }
  118. },
  119. );
  120. function handleFullscreen() {
  121. props.modalApi?.setState((prev) => {
  122. // if (prev.fullscreen) {
  123. // resetPosition();
  124. // }
  125. return { ...prev, fullscreen: !fullscreen.value };
  126. });
  127. }
  128. function interactOutside(e: Event) {
  129. if (!closeOnClickModal.value || submitting.value) {
  130. e.preventDefault();
  131. e.stopPropagation();
  132. }
  133. }
  134. function escapeKeyDown(e: KeyboardEvent) {
  135. if (!closeOnPressEscape.value || submitting.value) {
  136. e.preventDefault();
  137. }
  138. }
  139. function handerOpenAutoFocus(e: Event) {
  140. if (!openAutoFocus.value) {
  141. e?.preventDefault();
  142. }
  143. }
  144. // pointer-down-outside
  145. function pointerDownOutside(e: Event) {
  146. const target = e.target as HTMLElement;
  147. const isDismissableModal = target?.dataset.dismissableModal;
  148. if (
  149. !closeOnClickModal.value ||
  150. isDismissableModal !== id ||
  151. submitting.value
  152. ) {
  153. e.preventDefault();
  154. e.stopPropagation();
  155. }
  156. }
  157. function handleFocusOutside(e: Event) {
  158. e.preventDefault();
  159. e.stopPropagation();
  160. }
  161. const getAppendTo = computed(() => {
  162. return appendToMain.value
  163. ? `#${ELEMENT_ID_MAIN_CONTENT}>div:not(.absolute)>div`
  164. : undefined;
  165. });
  166. const getForceMount = computed(() => {
  167. return !unref(destroyOnClose);
  168. });
  169. function handleClosed() {
  170. isClosed.value = true;
  171. props.modalApi?.onClosed();
  172. }
  173. </script>
  174. <template>
  175. <Dialog
  176. :modal="false"
  177. :open="state?.isOpen"
  178. @update:open="() => (!submitting ? modalApi?.close() : undefined)"
  179. >
  180. <DialogContent
  181. ref="contentRef"
  182. :append-to="getAppendTo"
  183. :class="
  184. cn(
  185. 'left-0 right-0 top-[10vh] mx-auto flex max-h-[80%] w-[520px] flex-col p-0 sm:rounded-[var(--radius)]',
  186. modalClass,
  187. {
  188. 'border-border border': bordered,
  189. 'shadow-3xl': !bordered,
  190. 'left-0 top-0 size-full max-h-full !translate-x-0 !translate-y-0':
  191. shouldFullscreen,
  192. 'top-1/2 !-translate-y-1/2': centered && !shouldFullscreen,
  193. 'duration-300': !dragging,
  194. hidden: isClosed,
  195. },
  196. )
  197. "
  198. :force-mount="getForceMount"
  199. :modal="modal"
  200. :open="state?.isOpen"
  201. :show-close="closable"
  202. :z-index="zIndex"
  203. :overlay-blur="overlayBlur"
  204. close-class="top-3"
  205. @close-auto-focus="handleFocusOutside"
  206. @closed="handleClosed"
  207. :close-disabled="submitting"
  208. @escape-key-down="escapeKeyDown"
  209. @focus-outside="handleFocusOutside"
  210. @interact-outside="interactOutside"
  211. @open-auto-focus="handerOpenAutoFocus"
  212. @opened="() => modalApi?.onOpened()"
  213. @pointer-down-outside="pointerDownOutside"
  214. >
  215. <DialogHeader
  216. ref="headerRef"
  217. :class="
  218. cn(
  219. 'px-5 py-4',
  220. {
  221. 'border-b': bordered,
  222. hidden: !header,
  223. 'cursor-move select-none': shouldDraggable,
  224. },
  225. headerClass,
  226. )
  227. "
  228. >
  229. <DialogTitle v-if="title" class="text-left">
  230. <slot name="title">
  231. {{ title }}
  232. <slot v-if="titleTooltip" name="titleTooltip">
  233. <VbenHelpTooltip trigger-class="pb-1">
  234. {{ titleTooltip }}
  235. </VbenHelpTooltip>
  236. </slot>
  237. </slot>
  238. </DialogTitle>
  239. <DialogDescription v-if="description">
  240. <slot name="description">
  241. {{ description }}
  242. </slot>
  243. </DialogDescription>
  244. <VisuallyHidden v-if="!title || !description">
  245. <DialogTitle v-if="!title" />
  246. <DialogDescription v-if="!description" />
  247. </VisuallyHidden>
  248. </DialogHeader>
  249. <div
  250. ref="wrapperRef"
  251. :class="
  252. cn('relative min-h-40 flex-1 overflow-y-auto p-3', contentClass, {
  253. 'overflow-hidden': showLoading || submitting,
  254. })
  255. "
  256. >
  257. <VbenLoading
  258. v-if="showLoading || submitting"
  259. class="size-full h-auto min-h-full"
  260. spinning
  261. />
  262. <slot></slot>
  263. </div>
  264. <VbenIconButton
  265. v-if="fullscreenButton"
  266. 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"
  267. @click="handleFullscreen"
  268. >
  269. <Shrink v-if="fullscreen" class="size-3.5" />
  270. <Expand v-else class="size-3.5" />
  271. </VbenIconButton>
  272. <DialogFooter
  273. v-if="showFooter"
  274. ref="footerRef"
  275. :class="
  276. cn(
  277. 'flex-row items-center justify-end p-2',
  278. {
  279. 'border-t': bordered,
  280. },
  281. footerClass,
  282. )
  283. "
  284. >
  285. <slot name="prepend-footer"></slot>
  286. <slot name="footer">
  287. <component
  288. :is="components.DefaultButton || VbenButton"
  289. v-if="showCancelButton"
  290. variant="ghost"
  291. :disabled="submitting"
  292. @click="() => modalApi?.onCancel()"
  293. >
  294. <slot name="cancelText">
  295. {{ cancelText || $t('cancel') }}
  296. </slot>
  297. </component>
  298. <slot name="center-footer"></slot>
  299. <component
  300. :is="components.PrimaryButton || VbenButton"
  301. v-if="showConfirmButton"
  302. :disabled="confirmDisabled"
  303. :loading="confirmLoading || submitting"
  304. @click="() => modalApi?.onConfirm()"
  305. >
  306. <slot name="confirmText">
  307. {{ confirmText || $t('confirm') }}
  308. </slot>
  309. </component>
  310. </slot>
  311. <slot name="append-footer"></slot>
  312. </DialogFooter>
  313. </DialogContent>
  314. </Dialog>
  315. </template>