CommonActionMenu.vue 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. 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 { popover, isOpen: popoverIsOpen, popoverTarget, toggle } = usePopover()
  45. const { actions, entity } = toRefs(props)
  46. const { filteredMenuItems, singleMenuItemPresent, singleMenuItem } =
  47. usePopoverMenu(actions, entity, { provides: true })
  48. const entityId = computed(() => props.entity?.id || getUuid())
  49. const menuId = computed(() => `popover-${entityId.value}`)
  50. const singleActionAriaLabel = computed(() => {
  51. if (typeof singleMenuItem.value?.ariaLabel === 'function') {
  52. return singleMenuItem.value.ariaLabel(props.entity)
  53. }
  54. return singleMenuItem.value?.ariaLabel || singleMenuItem.value?.label
  55. })
  56. const buttonVariantClassExtension = computed(() => {
  57. if (props.defaultButtonVariant === 'neutral-dark')
  58. 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'
  59. if (props.defaultButtonVariant === 'neutral-light')
  60. 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'
  61. return ''
  62. })
  63. const singleActionMode = computed(() => {
  64. if (props.noSingleActionMode) return false
  65. return singleMenuItemPresent.value
  66. })
  67. const variantClasses = computed(() => {
  68. if (singleMenuItem.value?.variant === 'secondary') return 'text-blue-800'
  69. if (singleMenuItem.value?.variant === 'danger') return 'text-red-500'
  70. return 'text-stone-200 dark:text-neutral-500'
  71. })
  72. </script>
  73. <template>
  74. <div
  75. v-if="filteredMenuItems && filteredMenuItems.length > 0"
  76. class="-:inline-block"
  77. >
  78. <template v-if="singleActionMode">
  79. <CommonLink
  80. v-if="singleMenuItem?.link"
  81. v-tooltip="$t(singleActionAriaLabel)"
  82. class="flex focus:outline-none focus-visible:outline-1 focus-visible:outline-offset-1 focus-visible:outline-blue-800"
  83. :aria-label="$t(singleActionAriaLabel)"
  84. :link="singleMenuItem.link"
  85. >
  86. <CommonIcon
  87. v-if="singleMenuItem?.icon"
  88. :size="linkSize"
  89. :class="variantClasses"
  90. :name="singleMenuItem?.icon"
  91. />
  92. </CommonLink>
  93. <CommonButton
  94. v-else
  95. v-tooltip="$t(singleActionAriaLabel)"
  96. class="rounded-sm p-0"
  97. :class="[variantClasses]"
  98. :size="buttonSize"
  99. :aria-label="$t(singleActionAriaLabel)"
  100. :icon="singleMenuItem?.icon"
  101. @click="singleMenuItem?.onClick?.(props.entity as ObjectLike)"
  102. />
  103. </template>
  104. <template v-else>
  105. <CommonButton
  106. :id="`action-menu-${entityId}`"
  107. ref="popoverTarget"
  108. :aria-label="$t(customMenuButtonLabel || 'Action menu button')"
  109. aria-haspopup="true"
  110. :aria-controls="popoverIsOpen ? menuId : undefined"
  111. :class="[
  112. {
  113. 'outline outline-1 outline-offset-1 outline-blue-800':
  114. popoverIsOpen,
  115. 'p-0': noPaddedDefaultButton,
  116. 'rounded-sm': !noSmallRoundingDefaultButton,
  117. },
  118. buttonVariantClassExtension,
  119. ]"
  120. :variant="defaultButtonVariant as ButtonVariant"
  121. :size="buttonSize"
  122. :icon="defaultIcon"
  123. @click="toggle"
  124. />
  125. <CommonPopover
  126. :id="menuId"
  127. ref="popover"
  128. :placement="placement"
  129. :hide-arrow="hideArrow"
  130. :orientation="orientation"
  131. :owner="popoverTarget"
  132. >
  133. <CommonPopoverMenu :entity="entity" :popover="popover" />
  134. </CommonPopover>
  135. </template>
  136. </div>
  137. </template>