CommonPopoverMenu.vue 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. item.onClick(props.entity)
  26. }
  27. if (!item.noCloseOnClick) {
  28. props.popover?.closePopover()
  29. }
  30. }
  31. const getHoverFocusStyles = (variant?: Variant) => {
  32. if (variant === 'secondary') {
  33. 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'
  34. }
  35. if (variant === 'danger') {
  36. 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'
  37. }
  38. 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'
  39. }
  40. </script>
  41. <template>
  42. <section class="min-w-58 flex max-w-64 flex-col gap-0.5">
  43. <div
  44. v-if="showHeaderLabel"
  45. role="heading"
  46. aria-level="2"
  47. class="px-2 py-1.5"
  48. >
  49. <slot name="header">
  50. <CommonLabel
  51. size="small"
  52. class="line-clamp-1 text-stone-200 dark:text-neutral-500"
  53. >{{ i18n.t(headerLabel) }}
  54. </CommonLabel>
  55. </slot>
  56. </div>
  57. <template v-if="filteredMenuItems || $slots.default">
  58. <slot>
  59. <ul role="menu" v-bind="$attrs" class="flex w-full flex-col">
  60. <template v-for="item in filteredMenuItems" :key="item.key">
  61. <li
  62. role="menuitem"
  63. class="group flex items-center justify-between last:rounded-b-[10px]"
  64. :class="[
  65. {
  66. 'first:rounded-t-[10px]': !showHeaderLabel,
  67. 'border-t border-neutral-100 dark:border-gray-900':
  68. item.separatorTop,
  69. },
  70. getHoverFocusStyles(item.variant),
  71. ]"
  72. >
  73. <slot :name="`item-${item.key}`" v-bind="item">
  74. <component
  75. :is="item.component || CommonPopoverMenuItem"
  76. class="flex grow p-2.5"
  77. :label="item.label"
  78. :variant="item.variant"
  79. :link="item.link"
  80. :icon="item.icon"
  81. :label-placeholder="item.labelPlaceholder"
  82. @click="onClickItem($event, item)"
  83. />
  84. <slot :name="`itemRight-${item.key}`" v-bind="item" />
  85. </slot>
  86. </li>
  87. </template>
  88. </ul>
  89. </slot>
  90. </template>
  91. </section>
  92. </template>