modal.vue 9.2 KB

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