modal.vue 8.1 KB

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