CommonDialog.vue 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <!-- Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/ -->
  2. <script setup lang="ts">
  3. import { onKeyUp } from '@vueuse/core'
  4. import { useTemplateRef, nextTick, onMounted, computed } from 'vue'
  5. import { useRoute, useRouter } from 'vue-router'
  6. import { useTrapTab } from '#shared/composables/useTrapTab.ts'
  7. import stopEvent from '#shared/utils/events.ts'
  8. import { getFirstFocusableElement } from '#shared/utils/getFocusableElements.ts'
  9. import CommonButton from '#desktop/components/CommonButton/CommonButton.vue'
  10. import CommonOverlayContainer from '#desktop/components/CommonOverlayContainer/CommonOverlayContainer.vue'
  11. import CommonDialogActionFooter, {
  12. type Props as ActionFooterProps,
  13. } from './CommonDialogActionFooter.vue'
  14. import { closeDialog } from './useDialog.ts'
  15. export interface Props {
  16. name: string
  17. headerTitle?: string
  18. headerIcon?: string
  19. content?: string
  20. contentPlaceholder?: string[]
  21. hideFooter?: boolean
  22. /**
  23. * Inner wrapper for the dialog content.
  24. * */
  25. wrapperTag?: 'div' | 'article'
  26. footerActionOptions?: ActionFooterProps
  27. // Don't focus the first element inside a Dialog after being mounted
  28. // if nothing is focusable, will focus "Close" button when dismissable is active.
  29. noAutofocus?: boolean
  30. fullscreen?: boolean
  31. global?: boolean
  32. }
  33. const props = withDefaults(defineProps<Props>(), {
  34. wrapperTag: 'div',
  35. })
  36. defineOptions({
  37. inheritAttrs: false,
  38. })
  39. const emit = defineEmits<{
  40. close: [cancel?: boolean]
  41. }>()
  42. const { path } = useRoute()
  43. const router = useRouter()
  44. const isActive = computed(() =>
  45. props.fullscreen ? true : path === router.currentRoute.value.path,
  46. )
  47. const dialogElement = useTemplateRef<HTMLElement>('dialog')
  48. const footerElement = useTemplateRef('footer')
  49. const contentElement = useTemplateRef('content')
  50. const close = async (cancel?: boolean) => {
  51. emit('close', cancel)
  52. await closeDialog(props.name, props.global)
  53. }
  54. const dialogId = `dialog-${props.name}`
  55. onKeyUp('Escape', (e) => {
  56. stopEvent(e)
  57. close()
  58. })
  59. useTrapTab(dialogElement)
  60. onMounted(() => {
  61. if (props.noAutofocus) return
  62. // Will try to find focusable element inside dialog main and footer content.
  63. // If it won't find it, will try to find inside the header most likely will find "Close" button.
  64. const firstFocusable =
  65. getFirstFocusableElement(contentElement.value) ||
  66. getFirstFocusableElement(footerElement.value) ||
  67. getFirstFocusableElement(dialogElement.value)
  68. nextTick(() => {
  69. firstFocusable?.focus()
  70. firstFocusable?.scrollIntoView({ block: 'nearest' })
  71. })
  72. })
  73. // It is the same as flyout, but could be changed in the future?
  74. const transition = VITE_TEST_MODE
  75. ? undefined
  76. : {
  77. enterActiveClass: 'duration-300 ease-out',
  78. enterFromClass: 'opacity-0 rtl:-translate-x-3/4 ltr:translate-x-3/4',
  79. enterToClass: 'opacity-100 rtl:-translate-x-0 ltr:translate-x-0',
  80. leaveActiveClass: 'duration-200 ease-in',
  81. leaveFromClass: 'opacity-100 rtl:-translate-x-0 ltr:translate-x-0',
  82. leaveToClass: 'opacity-0 rtl:-translate-x-3/4 ltr:translate-x-3/4',
  83. }
  84. </script>
  85. <template>
  86. <!-- `display:none` to prevent showing up inactive dialog for cached instance -->
  87. <Transition :appear="isActive" v-bind="transition">
  88. <!-- We use teleport here to center it to target node and increase z index on fullscreen to avoid clicking collapse and resize buttons -->
  89. <Teleport :to="fullscreen ? '#app' : '#main-content'">
  90. <CommonOverlayContainer
  91. :id="dialogId"
  92. tag="div"
  93. disable-teleport
  94. class="absolute top-[50%] z-50 h-full w-full translate-y-[-50%] ltr:left-[50%] ltr:translate-x-[-50%] rtl:right-[50%] rtl:-translate-x-[-50%]"
  95. :class="{ 'z-40': fullscreen, hidden: !isActive }"
  96. role="dialog"
  97. backdrop-class="z-40"
  98. :show-backdrop="isActive"
  99. :fullscreen="fullscreen"
  100. :aria-labelledby="`${dialogId}-title`"
  101. @click-background="close()"
  102. >
  103. <component
  104. :is="wrapperTag"
  105. ref="dialog"
  106. data-common-dialog
  107. class="!absolute top-1/2 z-50 flex w-[500px] -translate-y-1/2 flex-col gap-3 rounded-xl border border-neutral-100 bg-neutral-50 p-3 ltr:left-1/2 ltr:-translate-x-1/2 rtl:right-1/2 rtl:translate-x-1/2 dark:border-gray-900 dark:bg-gray-500"
  108. >
  109. <div
  110. class="flex items-center justify-between bg-neutral-50 dark:bg-gray-500"
  111. >
  112. <slot name="header">
  113. <div
  114. class="flex items-center gap-2 text-xl leading-snug text-gray-100 dark:text-neutral-400"
  115. >
  116. <CommonIcon v-if="headerIcon" size="small" :name="headerIcon" />
  117. <h3 :id="`${dialogId}-title`">{{ $t(headerTitle) }}</h3>
  118. </div>
  119. </slot>
  120. <CommonButton
  121. class="ms-auto"
  122. variant="neutral"
  123. size="medium"
  124. icon="x-lg"
  125. :aria-label="$t('Close dialog')"
  126. @click="close()"
  127. />
  128. </div>
  129. <div ref="content" v-bind="$attrs" class="py-6 text-center">
  130. <slot>
  131. <CommonLabel size="large">{{
  132. $t(content, ...(contentPlaceholder || []))
  133. }}</CommonLabel>
  134. </slot>
  135. </div>
  136. <div v-if="$slots.footer || !hideFooter" ref="footer">
  137. <slot name="footer">
  138. <CommonDialogActionFooter
  139. v-bind="footerActionOptions"
  140. @cancel="close(true)"
  141. @action="close(false)"
  142. />
  143. </slot>
  144. </div>
  145. </component>
  146. </CommonOverlayContainer>
  147. </Teleport>
  148. </Transition>
  149. </template>