CommonActionMenu.vue 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <!-- Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/ -->
  2. <script setup lang="ts">
  3. import { computed, ref, toRefs } from 'vue'
  4. import type { Sizes } from '#shared/components/CommonIcon/types.ts'
  5. import CommonPopover from '#shared/components/CommonPopover/CommonPopover.vue'
  6. import type {
  7. Orientation,
  8. Placement,
  9. } from '#shared/components/CommonPopover/types.ts'
  10. import { usePopover } from '#shared/components/CommonPopover/usePopover.ts'
  11. import type { ObjectLike } from '#shared/types/utils.ts'
  12. import getUuid from '#shared/utils/getUuid.ts'
  13. import CommonButton from '#desktop/components/CommonButton/CommonButton.vue'
  14. import type {
  15. ButtonSize,
  16. ButtonVariant,
  17. } from '#desktop/components/CommonButton/types.ts'
  18. import CommonPopoverMenu from '#desktop/components/CommonPopoverMenu/CommonPopoverMenu.vue'
  19. import type { MenuItem } from '#desktop/components/CommonPopoverMenu/types.ts'
  20. import { usePopoverMenu } from '#desktop/components/CommonPopoverMenu/usePopoverMenu.ts'
  21. export interface Props {
  22. actions: MenuItem[]
  23. entity?: ObjectLike
  24. buttonSize?: ButtonSize
  25. linkSize?: Sizes
  26. placement?: Placement
  27. orientation?: Orientation
  28. hideArrow?: boolean
  29. noSingleActionMode?: boolean
  30. customMenuButtonLabel?: string
  31. defaultIcon?: string
  32. defaultButtonVariant?: ButtonVariant | 'neutral-light' | 'neutral-dark'
  33. noPaddedDefaultButton?: boolean
  34. noSmallRoundingDefaultButton?: boolean
  35. }
  36. const props = withDefaults(defineProps<Props>(), {
  37. buttonSize: 'medium',
  38. placement: 'arrowStart',
  39. orientation: 'autoVertical',
  40. defaultButtonVariant: 'neutral',
  41. defaultIcon: 'three-dots-vertical',
  42. noPaddedDefaultButton: true,
  43. })
  44. const popoverMenu = ref<InstanceType<typeof CommonPopoverMenu>>()
  45. const { popover, isOpen: popoverIsOpen, popoverTarget, toggle } = usePopover()
  46. const { actions, entity } = toRefs(props)
  47. const { filteredMenuItems, singleMenuItemPresent, singleMenuItem } =
  48. usePopoverMenu(actions, entity, { provides: true })
  49. const entityId = computed(() => props.entity?.id || getUuid())
  50. const menuId = computed(() => `popover-${entityId.value}`)
  51. const singleActionAriaLabel = computed(() => {
  52. if (typeof singleMenuItem.value?.ariaLabel === 'function') {
  53. return singleMenuItem.value.ariaLabel(props.entity)
  54. }
  55. return singleMenuItem.value?.ariaLabel || singleMenuItem.value?.label
  56. })
  57. const buttonVariantClassExtension = computed(() => {
  58. if (props.defaultButtonVariant === 'neutral-dark')
  59. return 'border border-neutral-100 !outline-transparent hover:border-blue-700 dark:hover:border-blue-700 hover:border-blue-800 bg-neutral-50 hover:bg-white text-gray-100 dark:border-gray-900 dark:bg-gray-500 dark:text-neutral-400'
  60. if (props.defaultButtonVariant === 'neutral-light')
  61. return 'border border-neutral-100 !outline-transparent hover:border-blue-700 dark:hover:border-blue-700 hover:border-blue-800 hover:bg-white bg-blue-100 text-gray-100 dark:border-gray-900 dark:bg-stone-500 dark:text-neutral-400'
  62. return ''
  63. })
  64. const singleActionMode = computed(() => {
  65. if (props.noSingleActionMode) return false
  66. return singleMenuItemPresent.value
  67. })
  68. const variantClasses = computed(() => {
  69. if (singleMenuItem.value?.variant === 'secondary') return 'text-blue-800'
  70. if (singleMenuItem.value?.variant === 'danger') return 'text-red-500'
  71. return 'text-stone-200 dark:text-neutral-500'
  72. })
  73. </script>
  74. <template>
  75. <div
  76. v-if="filteredMenuItems && filteredMenuItems.length > 0"
  77. class="-:inline-block"
  78. >
  79. <template v-if="singleActionMode">
  80. <CommonLink
  81. v-if="singleMenuItem?.link"
  82. v-tooltip="$t(singleActionAriaLabel)"
  83. class="flex focus:outline-none focus-visible:outline-1 focus-visible:outline-offset-1 focus-visible:outline-blue-800"
  84. :aria-label="$t(singleActionAriaLabel)"
  85. :link="singleMenuItem.link"
  86. >
  87. <CommonIcon
  88. v-if="singleMenuItem?.icon"
  89. :size="linkSize"
  90. :class="variantClasses"
  91. :name="singleMenuItem?.icon"
  92. />
  93. </CommonLink>
  94. <CommonButton
  95. v-else
  96. v-tooltip="$t(singleActionAriaLabel)"
  97. class="rounded-sm p-0"
  98. :class="[variantClasses]"
  99. :size="buttonSize"
  100. :aria-label="$t(singleActionAriaLabel)"
  101. :icon="singleMenuItem?.icon"
  102. @click="singleMenuItem?.onClick?.(props.entity as ObjectLike)"
  103. />
  104. </template>
  105. <template v-else>
  106. <CommonButton
  107. :id="`action-menu-${entityId}`"
  108. ref="popoverTarget"
  109. :aria-label="$t(customMenuButtonLabel || 'Action menu button')"
  110. aria-haspopup="true"
  111. :aria-controls="popoverIsOpen ? menuId : undefined"
  112. :class="[
  113. {
  114. 'outline outline-1 outline-offset-1 outline-blue-800':
  115. popoverIsOpen,
  116. 'p-0': noPaddedDefaultButton,
  117. 'rounded-sm': !noSmallRoundingDefaultButton,
  118. },
  119. buttonVariantClassExtension,
  120. ]"
  121. :variant="defaultButtonVariant as ButtonVariant"
  122. :size="buttonSize"
  123. :icon="defaultIcon"
  124. @click="toggle"
  125. />
  126. <CommonPopover
  127. :id="menuId"
  128. ref="popover"
  129. :placement="placement"
  130. :hide-arrow="hideArrow"
  131. :orientation="orientation"
  132. :owner="popoverTarget"
  133. >
  134. <CommonPopoverMenu
  135. ref="popoverMenu"
  136. :entity="entity"
  137. :popover="popover"
  138. />
  139. </CommonPopover>
  140. </template>
  141. </div>
  142. </template>