modal.vue 8.9 KB

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