1
0

drawer.vue 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <script lang="ts" setup>
  2. import type { DrawerProps, ExtendedDrawerApi } from './drawer';
  3. import { ref, watch } from 'vue';
  4. import {
  5. useIsMobile,
  6. usePriorityValue,
  7. useSimpleLocale,
  8. } from '@vben-core/composables';
  9. import { Info, X } from '@vben-core/icons';
  10. import {
  11. Sheet,
  12. SheetClose,
  13. SheetContent,
  14. SheetDescription,
  15. SheetFooter,
  16. SheetHeader,
  17. SheetTitle,
  18. VbenButton,
  19. VbenIconButton,
  20. VbenLoading,
  21. VbenTooltip,
  22. VisuallyHidden,
  23. } from '@vben-core/shadcn-ui';
  24. import { cn } from '@vben-core/shared';
  25. interface Props extends DrawerProps {
  26. class?: string;
  27. contentClass?: string;
  28. drawerApi?: ExtendedDrawerApi;
  29. }
  30. const props = withDefaults(defineProps<Props>(), {
  31. class: '',
  32. contentClass: '',
  33. drawerApi: undefined,
  34. });
  35. const wrapperRef = ref<HTMLElement>();
  36. const { $t } = useSimpleLocale();
  37. const { isMobile } = useIsMobile();
  38. const state = props.drawerApi?.useStore?.();
  39. const title = usePriorityValue('title', props, state);
  40. const description = usePriorityValue('description', props, state);
  41. const titleTooltip = usePriorityValue('titleTooltip', props, state);
  42. const showFooter = usePriorityValue('footer', props, state);
  43. const showLoading = usePriorityValue('loading', props, state);
  44. const closable = usePriorityValue('closable', props, state);
  45. const modal = usePriorityValue('modal', props, state);
  46. const confirmLoading = usePriorityValue('confirmLoading', props, state);
  47. const cancelText = usePriorityValue('cancelText', props, state);
  48. const confirmText = usePriorityValue('confirmText', props, state);
  49. const closeOnClickModal = usePriorityValue('closeOnClickModal', props, state);
  50. const closeOnPressEscape = usePriorityValue('closeOnPressEscape', props, state);
  51. const showCancelButton = usePriorityValue('showCancelButton', props, state);
  52. const showConfirmButton = usePriorityValue('showConfirmButton', props, state);
  53. watch(
  54. () => showLoading.value,
  55. (v) => {
  56. if (v && wrapperRef.value) {
  57. wrapperRef.value.scrollTo({
  58. // behavior: 'smooth',
  59. top: 0,
  60. });
  61. }
  62. },
  63. );
  64. function interactOutside(e: Event) {
  65. if (!closeOnClickModal.value) {
  66. e.preventDefault();
  67. }
  68. }
  69. function escapeKeyDown(e: KeyboardEvent) {
  70. if (!closeOnPressEscape.value) {
  71. e.preventDefault();
  72. }
  73. }
  74. // pointer-down-outside
  75. function pointerDownOutside(e: Event) {
  76. const target = e.target as HTMLElement;
  77. const isDismissableModal = !!target?.dataset.dismissableModal;
  78. if (!closeOnClickModal.value || !isDismissableModal) {
  79. e.preventDefault();
  80. }
  81. }
  82. </script>
  83. <template>
  84. <Sheet
  85. :modal="modal"
  86. :open="state?.isOpen"
  87. @update:open="() => drawerApi?.close()"
  88. >
  89. <SheetContent
  90. :class="
  91. cn('flex w-[520px] flex-col', props.class, {
  92. '!w-full': isMobile,
  93. })
  94. "
  95. @escape-key-down="escapeKeyDown"
  96. @interact-outside="interactOutside"
  97. @pointer-down-outside="pointerDownOutside"
  98. >
  99. <SheetHeader
  100. :class="
  101. cn('!flex flex-row items-center justify-between border-b px-6 py-5', {
  102. 'px-4 py-3': closable,
  103. })
  104. "
  105. >
  106. <div>
  107. <SheetTitle v-if="title" class="text-left">
  108. <slot name="title">
  109. {{ title }}
  110. <VbenTooltip v-if="titleTooltip" side="right">
  111. <template #trigger>
  112. <Info class="inline-flex size-5 cursor-pointer pb-1" />
  113. </template>
  114. {{ titleTooltip }}
  115. </VbenTooltip>
  116. </slot>
  117. </SheetTitle>
  118. <SheetDescription v-if="description" class="mt-1 text-xs">
  119. <slot name="description">
  120. {{ description }}
  121. </slot>
  122. </SheetDescription>
  123. </div>
  124. <VisuallyHidden v-if="!title || !description">
  125. <SheetTitle v-if="!title" />
  126. <SheetDescription v-if="!description" />
  127. </VisuallyHidden>
  128. <div class="flex-center">
  129. <slot name="extra"></slot>
  130. <SheetClose
  131. v-if="closable"
  132. as-child
  133. class="data-[state=open]:bg-secondary ml-[2px] cursor-pointer rounded-full opacity-80 transition-opacity hover:opacity-100 focus:outline-none disabled:pointer-events-none"
  134. >
  135. <VbenIconButton>
  136. <X class="size-4" />
  137. </VbenIconButton>
  138. </SheetClose>
  139. </div>
  140. </SheetHeader>
  141. <div
  142. ref="wrapperRef"
  143. :class="
  144. cn('relative flex-1 overflow-y-auto p-3', contentClass, {
  145. 'overflow-hidden': showLoading,
  146. })
  147. "
  148. >
  149. <VbenLoading v-if="showLoading" class="size-full" spinning />
  150. <slot></slot>
  151. </div>
  152. <SheetFooter
  153. v-if="showFooter"
  154. class="w-full flex-row items-center justify-end border-t p-2 px-3"
  155. >
  156. <slot name="prepend-footer"></slot>
  157. <slot name="footer">
  158. <VbenButton
  159. v-if="showCancelButton"
  160. variant="ghost"
  161. @click="() => drawerApi?.onCancel()"
  162. >
  163. <slot name="cancelText">
  164. {{ cancelText || $t('cancel') }}
  165. </slot>
  166. </VbenButton>
  167. <VbenButton
  168. v-if="showConfirmButton"
  169. :loading="confirmLoading"
  170. @click="() => drawerApi?.onConfirm()"
  171. >
  172. <slot name="confirmText">
  173. {{ confirmText || $t('confirm') }}
  174. </slot>
  175. </VbenButton>
  176. </slot>
  177. <slot name="append-footer"></slot>
  178. </SheetFooter>
  179. </SheetContent>
  180. </Sheet>
  181. </template>