modal.vue 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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 ? `#${ELEMENT_ID_MAIN_CONTENT}` : undefined;
  156. });
  157. </script>
  158. <template>
  159. <Dialog
  160. :modal="false"
  161. :open="state?.isOpen"
  162. @update:open="() => (!submitting ? modalApi?.close() : undefined)"
  163. >
  164. <DialogContent
  165. ref="contentRef"
  166. :append-to="getAppendTo"
  167. :class="
  168. cn(
  169. 'left-0 right-0 top-[10vh] mx-auto flex max-h-[80%] w-[520px] flex-col p-0 sm:rounded-[var(--radius)]',
  170. modalClass,
  171. {
  172. 'border-border border': bordered,
  173. 'shadow-3xl': !bordered,
  174. 'left-0 top-0 size-full max-h-full !translate-x-0 !translate-y-0':
  175. shouldFullscreen,
  176. 'top-1/2 !-translate-y-1/2': centered && !shouldFullscreen,
  177. 'duration-300': !dragging,
  178. },
  179. )
  180. "
  181. :modal="modal"
  182. :open="state?.isOpen"
  183. :show-close="closable"
  184. :z-index="zIndex"
  185. :overlay-blur="overlayBlur"
  186. close-class="top-3"
  187. @close-auto-focus="handleFocusOutside"
  188. @closed="() => modalApi?.onClosed()"
  189. :close-disabled="submitting"
  190. @escape-key-down="escapeKeyDown"
  191. @focus-outside="handleFocusOutside"
  192. @interact-outside="interactOutside"
  193. @open-auto-focus="handerOpenAutoFocus"
  194. @opened="() => modalApi?.onOpened()"
  195. @pointer-down-outside="pointerDownOutside"
  196. >
  197. <DialogHeader
  198. ref="headerRef"
  199. :class="
  200. cn(
  201. 'px-5 py-4',
  202. {
  203. 'border-b': bordered,
  204. hidden: !header,
  205. 'cursor-move select-none': shouldDraggable,
  206. },
  207. headerClass,
  208. )
  209. "
  210. >
  211. <DialogTitle v-if="title" class="text-left">
  212. <slot name="title">
  213. {{ title }}
  214. <slot v-if="titleTooltip" name="titleTooltip">
  215. <VbenHelpTooltip trigger-class="pb-1">
  216. {{ titleTooltip }}
  217. </VbenHelpTooltip>
  218. </slot>
  219. </slot>
  220. </DialogTitle>
  221. <DialogDescription v-if="description">
  222. <slot name="description">
  223. {{ description }}
  224. </slot>
  225. </DialogDescription>
  226. <VisuallyHidden v-if="!title || !description">
  227. <DialogTitle v-if="!title" />
  228. <DialogDescription v-if="!description" />
  229. </VisuallyHidden>
  230. </DialogHeader>
  231. <div
  232. ref="wrapperRef"
  233. :class="
  234. cn('relative min-h-40 flex-1 overflow-y-auto p-3', contentClass, {
  235. 'overflow-hidden': showLoading || submitting,
  236. })
  237. "
  238. >
  239. <VbenLoading
  240. v-if="showLoading || submitting"
  241. class="size-full h-auto min-h-full"
  242. spinning
  243. />
  244. <slot></slot>
  245. </div>
  246. <VbenIconButton
  247. v-if="fullscreenButton"
  248. 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"
  249. @click="handleFullscreen"
  250. >
  251. <Shrink v-if="fullscreen" class="size-3.5" />
  252. <Expand v-else class="size-3.5" />
  253. </VbenIconButton>
  254. <DialogFooter
  255. v-if="showFooter"
  256. ref="footerRef"
  257. :class="
  258. cn(
  259. 'flex-row items-center justify-end p-2',
  260. {
  261. 'border-t': bordered,
  262. },
  263. footerClass,
  264. )
  265. "
  266. >
  267. <slot name="prepend-footer"></slot>
  268. <slot name="footer">
  269. <component
  270. :is="components.DefaultButton || VbenButton"
  271. v-if="showCancelButton"
  272. variant="ghost"
  273. :disabled="submitting"
  274. @click="() => modalApi?.onCancel()"
  275. >
  276. <slot name="cancelText">
  277. {{ cancelText || $t('cancel') }}
  278. </slot>
  279. </component>
  280. <component
  281. :is="components.PrimaryButton || VbenButton"
  282. v-if="showConfirmButton"
  283. :disabled="confirmDisabled"
  284. :loading="confirmLoading || submitting"
  285. @click="() => modalApi?.onConfirm()"
  286. >
  287. <slot name="confirmText">
  288. {{ confirmText || $t('confirm') }}
  289. </slot>
  290. </component>
  291. </slot>
  292. <slot name="append-footer"></slot>
  293. </DialogFooter>
  294. </DialogContent>
  295. </Dialog>
  296. </template>