CommonActionMenu.vue 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <!-- Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/ -->
  2. <script setup lang="ts">
  3. import { computed, 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. disabled?: boolean
  30. noSingleActionMode?: boolean
  31. customMenuButtonLabel?: string
  32. defaultIcon?: string
  33. defaultButtonVariant?: ButtonVariant | 'neutral-light' | 'neutral-dark'
  34. noPaddedDefaultButton?: boolean
  35. noSmallRoundingDefaultButton?: boolean
  36. }
  37. const props = withDefaults(defineProps<Props>(), {
  38. buttonSize: 'medium',
  39. placement: 'arrowStart',
  40. orientation: 'autoVertical',
  41. defaultButtonVariant: 'neutral',
  42. defaultIcon: 'three-dots-vertical',
  43. noPaddedDefaultButton: true,
  44. })
  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 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 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. :disabled="disabled"
  86. :link="singleMenuItem.link"
  87. >
  88. <CommonIcon
  89. v-if="singleMenuItem?.icon"
  90. :size="linkSize"
  91. :class="variantClasses"
  92. :name="singleMenuItem?.icon"
  93. />
  94. </CommonLink>
  95. <CommonButton
  96. v-else
  97. v-tooltip="$t(singleActionAriaLabel)"
  98. class="rounded-sm p-0"
  99. :class="[variantClasses]"
  100. :size="buttonSize"
  101. :disabled="disabled"
  102. :aria-label="$t(singleActionAriaLabel)"
  103. :icon="singleMenuItem?.icon"
  104. @click="singleMenuItem?.onClick?.(props.entity as ObjectLike)"
  105. />
  106. </template>
  107. <template v-else>
  108. <CommonButton
  109. :id="`action-menu-${entityId}`"
  110. ref="popoverTarget"
  111. :aria-label="$t(customMenuButtonLabel || 'Action menu button')"
  112. aria-haspopup="true"
  113. :aria-controls="popoverIsOpen ? menuId : undefined"
  114. :disabled="disabled"
  115. :class="[
  116. {
  117. 'outline outline-1 outline-offset-1 outline-blue-800 hover:outline-blue-800 hover:dark:outline-blue-800':
  118. popoverIsOpen,
  119. 'p-0': noPaddedDefaultButton,
  120. 'rounded-sm': !noSmallRoundingDefaultButton,
  121. },
  122. buttonVariantClassExtension,
  123. ]"
  124. :variant="defaultButtonVariant as ButtonVariant"
  125. :size="buttonSize"
  126. :icon="defaultIcon"
  127. @click="toggle"
  128. />
  129. <CommonPopover
  130. :id="menuId"
  131. ref="popover"
  132. :placement="placement"
  133. :hide-arrow="hideArrow"
  134. :orientation="orientation"
  135. :owner="popoverTarget"
  136. >
  137. <CommonPopoverMenu :entity="entity" :popover="popover" />
  138. </CommonPopover>
  139. </template>
  140. </div>
  141. </template>