CommonPopoverMenu.vue 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <!-- Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/ -->
  2. <script setup lang="ts">
  3. import { computed, toRefs, useSlots } from 'vue'
  4. import type { CommonPopoverInstance } from '#shared/components/CommonPopover/types.ts'
  5. import type { ObjectLike } from '#shared/types/utils.ts'
  6. import { usePopoverMenu } from '#desktop/components/CommonPopoverMenu/usePopoverMenu.ts'
  7. import CommonPopoverMenuItem from './CommonPopoverMenuItem.vue'
  8. import type { MenuItem, Variant } from './types'
  9. export interface Props {
  10. popover: CommonPopoverInstance | undefined
  11. headerLabel?: string
  12. items?: MenuItem[]
  13. entity?: ObjectLike
  14. }
  15. const props = defineProps<Props>()
  16. const { items, entity } = toRefs(props)
  17. const { filteredMenuItems } = usePopoverMenu(items, entity)
  18. const slots = useSlots()
  19. const showHeaderLabel = computed(() => {
  20. if (!filteredMenuItems.value && !slots.default) return false
  21. return slots.header || props.headerLabel
  22. })
  23. const onClickItem = (event: MouseEvent, item: MenuItem) => {
  24. if (item.onClick) {
  25. event.preventDefault()
  26. item.onClick(props.entity)
  27. }
  28. if (!item.noCloseOnClick) {
  29. props.popover?.closePopover()
  30. }
  31. }
  32. const getHoverFocusStyles = (variant?: Variant) => {
  33. if (variant === 'secondary') {
  34. return 'focus-within:bg-blue-500 hover:bg-blue-500 hover:focus-within:bg-blue-500 dark:focus-within:bg-blue-950 dark:hover:bg-blue-950 dark:hover:focus-within:bg-blue-950'
  35. }
  36. if (variant === 'danger') {
  37. return 'focus-within:bg-pink-100 hover:bg-pink-100 hover:focus-within:bg-pink-100 dark:focus-within:bg-red-900 dark:hover:bg-red-900 dark:hover:focus-within:bg-red-900'
  38. }
  39. return 'focus-within:bg-blue-800 focus-within:text-white hover:bg-blue-600 hover:focus-within:bg-blue-800 dark:hover:bg-blue-900 dark:hover:focus-within:bg-blue-800'
  40. }
  41. </script>
  42. <template>
  43. <section class="min-w-58 flex max-w-64 flex-col gap-0.5">
  44. <div
  45. v-if="showHeaderLabel"
  46. role="heading"
  47. aria-level="2"
  48. class="px-2 py-1.5"
  49. >
  50. <slot name="header">
  51. <CommonLabel
  52. size="small"
  53. class="line-clamp-1 text-stone-200 dark:text-neutral-500"
  54. >{{ i18n.t(headerLabel) }}
  55. </CommonLabel>
  56. </slot>
  57. </div>
  58. <template v-if="filteredMenuItems || $slots.default">
  59. <slot>
  60. <ul role="menu" v-bind="$attrs" class="flex w-full flex-col">
  61. <template v-for="item in filteredMenuItems" :key="item.key">
  62. <li
  63. v-if="'array' in item"
  64. class="flex flex-col overflow-clip pt-2.5 last:rounded-b-[10px] [&:nth-child(n+2)]:border-t [&:nth-child(n+2)]:border-neutral-100 [&:nth-child(n+2)]:dark:border-gray-900"
  65. role="menuitem"
  66. >
  67. <CommonLabel
  68. size="small"
  69. class="line-clamp-1 px-2 text-stone-200 dark:text-neutral-500"
  70. role="heading"
  71. aria-level="3"
  72. >{{ item.groupLabel }}</CommonLabel
  73. >
  74. <template v-for="subItem in item.array" :key="subItem.key">
  75. <slot :name="`item-${subItem.key}`" v-bind="subItem">
  76. <component
  77. :is="subItem.component || CommonPopoverMenuItem"
  78. class="flex grow p-2.5"
  79. :class="getHoverFocusStyles(subItem.variant)"
  80. :label="subItem.label"
  81. :variant="subItem.variant"
  82. :link="subItem.link"
  83. :icon="subItem.icon"
  84. :label-placeholder="subItem.labelPlaceholder"
  85. @click="onClickItem($event, subItem)"
  86. />
  87. <slot :name="`itemRight-${subItem.key}`" v-bind="subItem" />
  88. </slot>
  89. </template>
  90. </li>
  91. <li
  92. v-else
  93. role="menuitem"
  94. class="group flex items-center justify-between last:rounded-b-[10px]"
  95. :class="[
  96. {
  97. 'first:rounded-t-[10px]': !showHeaderLabel,
  98. 'border-t border-neutral-100 dark:border-gray-900':
  99. item.separatorTop,
  100. },
  101. getHoverFocusStyles(item.variant),
  102. ]"
  103. >
  104. <slot :name="`item-${item.key}`" v-bind="item">
  105. <component
  106. :is="item.component || CommonPopoverMenuItem"
  107. class="flex grow p-2.5"
  108. :label="item.label"
  109. :variant="item.variant"
  110. :link="item.link"
  111. :icon="item.icon"
  112. :label-placeholder="item.labelPlaceholder"
  113. @click="onClickItem($event, item)"
  114. />
  115. <slot :name="`itemRight-${item.key}`" v-bind="item" />
  116. </slot>
  117. </li>
  118. </template>
  119. </ul>
  120. </slot>
  121. </template>
  122. </section>
  123. </template>