modal.vue 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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 handleOpenAutoFocus(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. const handleOpened = () => {
  188. requestAnimationFrame(() => {
  189. props.modalApi?.onOpened();
  190. });
  191. };
  192. function handleClosed() {
  193. isClosed.value = true;
  194. props.modalApi?.onClosed();
  195. }
  196. </script>
  197. <template>
  198. <Dialog
  199. :modal="false"
  200. :open="state?.isOpen"
  201. @update:open="() => (!submitting ? modalApi?.close() : undefined)"
  202. >
  203. <DialogContent
  204. ref="contentRef"
  205. :append-to="getAppendTo"
  206. :class="
  207. cn(
  208. 'left-0 right-0 top-[10vh] mx-auto flex max-h-[80%] w-[520px] flex-col p-0',
  209. shouldFullscreen ? 'sm:rounded-none' : 'sm:rounded-[var(--radius)]',
  210. modalClass,
  211. {
  212. 'border-border border': bordered,
  213. 'shadow-3xl': !bordered,
  214. 'left-0 top-0 size-full max-h-full !translate-x-0 !translate-y-0':
  215. shouldFullscreen,
  216. 'top-1/2 !-translate-y-1/2': centered && !shouldFullscreen,
  217. 'duration-300': !dragging,
  218. hidden: isClosed,
  219. },
  220. )
  221. "
  222. :force-mount="getForceMount"
  223. :modal="modal"
  224. :open="state?.isOpen"
  225. :show-close="closable"
  226. :animation-type="animationType"
  227. :z-index="zIndex"
  228. :overlay-blur="overlayBlur"
  229. close-class="top-3"
  230. @close-auto-focus="handleFocusOutside"
  231. @closed="handleClosed"
  232. :close-disabled="submitting"
  233. @escape-key-down="escapeKeyDown"
  234. @focus-outside="handleFocusOutside"
  235. @interact-outside="interactOutside"
  236. @open-auto-focus="handleOpenAutoFocus"
  237. @opened="handleOpened"
  238. @pointer-down-outside="pointerDownOutside"
  239. >
  240. <DialogHeader
  241. ref="headerRef"
  242. :class="
  243. cn(
  244. 'px-5 py-4',
  245. {
  246. 'border-b': bordered,
  247. hidden: !header,
  248. 'cursor-move select-none': shouldDraggable,
  249. },
  250. headerClass,
  251. )
  252. "
  253. >
  254. <DialogTitle v-if="title" class="text-left">
  255. <slot name="title">
  256. {{ title }}
  257. <slot v-if="titleTooltip" name="titleTooltip">
  258. <VbenHelpTooltip trigger-class="pb-1">
  259. {{ titleTooltip }}
  260. </VbenHelpTooltip>
  261. </slot>
  262. </slot>
  263. </DialogTitle>
  264. <DialogDescription v-if="description">
  265. <slot name="description">
  266. {{ description }}
  267. </slot>
  268. </DialogDescription>
  269. <VisuallyHidden v-if="!title || !description">
  270. <DialogTitle v-if="!title" />
  271. <DialogDescription v-if="!description" />
  272. </VisuallyHidden>
  273. </DialogHeader>
  274. <div
  275. ref="wrapperRef"
  276. :class="
  277. cn('relative min-h-40 flex-1 overflow-y-auto p-3', contentClass, {
  278. 'pointer-events-none': showLoading || submitting,
  279. })
  280. "
  281. >
  282. <slot></slot>
  283. </div>
  284. <VbenLoading v-if="showLoading || submitting" spinning />
  285. <VbenIconButton
  286. v-if="fullscreenButton"
  287. 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"
  288. @click="handleFullscreen"
  289. >
  290. <Shrink v-if="fullscreen" class="size-3.5" />
  291. <Expand v-else class="size-3.5" />
  292. </VbenIconButton>
  293. <DialogFooter
  294. v-if="showFooter"
  295. ref="footerRef"
  296. :class="
  297. cn(
  298. 'flex-row items-center justify-end p-2',
  299. {
  300. 'border-t': bordered,
  301. },
  302. footerClass,
  303. )
  304. "
  305. >
  306. <slot name="prepend-footer"></slot>
  307. <slot name="footer">
  308. <component
  309. :is="components.DefaultButton || VbenButton"
  310. v-if="showCancelButton"
  311. variant="ghost"
  312. :disabled="submitting"
  313. @click="() => modalApi?.onCancel()"
  314. >
  315. <slot name="cancelText">
  316. {{ cancelText || $t('cancel') }}
  317. </slot>
  318. </component>
  319. <slot name="center-footer"></slot>
  320. <component
  321. :is="components.PrimaryButton || VbenButton"
  322. v-if="showConfirmButton"
  323. :disabled="confirmDisabled"
  324. :loading="confirmLoading || submitting"
  325. @click="() => modalApi?.onConfirm()"
  326. >
  327. <slot name="confirmText">
  328. {{ confirmText || $t('confirm') }}
  329. </slot>
  330. </component>
  331. </slot>
  332. <slot name="append-footer"></slot>
  333. </DialogFooter>
  334. </DialogContent>
  335. </Dialog>
  336. </template>