modal.vue 9.3 KB

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