1
0

drawer.vue 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <script lang="ts" setup>
  2. import type { DrawerProps, ExtendedDrawerApi } from './drawer';
  3. import { ref, watch } from 'vue';
  4. import {
  5. useIsMobile,
  6. usePriorityValues,
  7. useSimpleLocale,
  8. } from '@vben-core/composables';
  9. import { X } from '@vben-core/icons';
  10. import {
  11. Sheet,
  12. SheetClose,
  13. SheetContent,
  14. SheetDescription,
  15. SheetFooter,
  16. SheetHeader,
  17. SheetTitle,
  18. VbenButton,
  19. VbenHelpTooltip,
  20. VbenIconButton,
  21. VbenLoading,
  22. VisuallyHidden,
  23. } from '@vben-core/shadcn-ui';
  24. import { cn } from '@vben-core/shared/utils';
  25. interface Props extends DrawerProps {
  26. drawerApi?: ExtendedDrawerApi;
  27. }
  28. const props = withDefaults(defineProps<Props>(), {
  29. drawerApi: undefined,
  30. });
  31. const wrapperRef = ref<HTMLElement>();
  32. const { $t } = useSimpleLocale();
  33. const { isMobile } = useIsMobile();
  34. const state = props.drawerApi?.useStore?.();
  35. const {
  36. cancelText,
  37. class: drawerClass,
  38. closable,
  39. closeOnClickModal,
  40. closeOnPressEscape,
  41. confirmLoading,
  42. confirmText,
  43. contentClass,
  44. description,
  45. footer: showFooter,
  46. loading: showLoading,
  47. modal,
  48. showCancelButton,
  49. showConfirmButton,
  50. title,
  51. titleTooltip,
  52. } = usePriorityValues(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', drawerClass, {
  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. <VbenHelpTooltip v-if="titleTooltip" trigger-class="pb-1">
  111. {{ titleTooltip }}
  112. </VbenHelpTooltip>
  113. </slot>
  114. </SheetTitle>
  115. <SheetDescription v-if="description" class="mt-1 text-xs">
  116. <slot name="description">
  117. {{ description }}
  118. </slot>
  119. </SheetDescription>
  120. </div>
  121. <VisuallyHidden v-if="!title || !description">
  122. <SheetTitle v-if="!title" />
  123. <SheetDescription v-if="!description" />
  124. </VisuallyHidden>
  125. <div class="flex-center">
  126. <slot name="extra"></slot>
  127. <SheetClose
  128. v-if="closable"
  129. as-child
  130. 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"
  131. >
  132. <VbenIconButton>
  133. <X class="size-4" />
  134. </VbenIconButton>
  135. </SheetClose>
  136. </div>
  137. </SheetHeader>
  138. <div
  139. ref="wrapperRef"
  140. :class="
  141. cn('relative flex-1 overflow-y-auto p-3', contentClass, {
  142. 'overflow-hidden': showLoading,
  143. })
  144. "
  145. >
  146. <VbenLoading v-if="showLoading" class="size-full" spinning />
  147. <slot></slot>
  148. </div>
  149. <SheetFooter
  150. v-if="showFooter"
  151. class="w-full flex-row items-center justify-end border-t p-2 px-3"
  152. >
  153. <slot name="prepend-footer"></slot>
  154. <slot name="footer">
  155. <VbenButton
  156. v-if="showCancelButton"
  157. variant="ghost"
  158. @click="() => drawerApi?.onCancel()"
  159. >
  160. <slot name="cancelText">
  161. {{ cancelText || $t('cancel') }}
  162. </slot>
  163. </VbenButton>
  164. <VbenButton
  165. v-if="showConfirmButton"
  166. :loading="confirmLoading"
  167. @click="() => drawerApi?.onConfirm()"
  168. >
  169. <slot name="confirmText">
  170. {{ confirmText || $t('confirm') }}
  171. </slot>
  172. </VbenButton>
  173. </slot>
  174. <slot name="append-footer"></slot>
  175. </SheetFooter>
  176. </SheetContent>
  177. </Sheet>
  178. </template>