CommonAvatar.vue 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <!-- Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/ -->
  2. <script setup lang="ts">
  3. import { computed } from 'vue'
  4. import { i18n } from '#shared/i18n.ts'
  5. import { getAvatarClasses } from '#shared/initializer/initializeAvatarClasses.ts'
  6. import type { AvatarSize } from './types.ts'
  7. export interface Props {
  8. initials?: string
  9. // path to image
  10. image?: Maybe<string>
  11. // name of the icon
  12. icon?: Maybe<string>
  13. size?: AvatarSize
  14. vipIcon?: Maybe<'vip-user' | 'vip-organization'>
  15. ariaLabel?: Maybe<string>
  16. decorative?: boolean
  17. }
  18. const props = withDefaults(defineProps<Props>(), {
  19. size: 'medium',
  20. initials: '??',
  21. })
  22. const iconSizes = {
  23. xs: 'xs',
  24. small: 'small',
  25. medium: 'base',
  26. normal: 'medium',
  27. large: 'large',
  28. xl: 'xl',
  29. } as const
  30. const iconSize = computed(() => {
  31. if (!props.icon) return 'medium'
  32. return iconSizes[props.size]
  33. })
  34. const avatarLabel = computed(() => {
  35. if (props.decorative) return undefined
  36. return props.ariaLabel || i18n.t('Avatar with initials %s', props.initials)
  37. })
  38. const classMap = getAvatarClasses()
  39. </script>
  40. <template>
  41. <span
  42. class="relative inline-flex shrink-0 select-none items-center justify-center rounded-full bg-cover bg-center"
  43. :class="[`size-${size}`, classMap.base]"
  44. :style="{
  45. backgroundImage: image ? `url(${image})` : undefined,
  46. backgroundRepeat: image ? 'no-repeat' : undefined,
  47. }"
  48. role="img"
  49. :aria-label="avatarLabel"
  50. :aria-hidden="decorative ? 'true' : undefined"
  51. data-test-id="common-avatar"
  52. >
  53. <CommonIcon
  54. v-if="vipIcon"
  55. class="vip pointer-events-none absolute"
  56. :class="
  57. vipIcon === 'vip-organization'
  58. ? classMap.vipOrganization
  59. : classMap.vipUser
  60. "
  61. :name="vipIcon"
  62. :size="iconSizes[props.size]"
  63. decorative
  64. />
  65. <CommonIcon v-if="icon" :name="icon" :size="iconSize" />
  66. <slot v-else>
  67. {{ image ? '' : initials }}
  68. </slot>
  69. </span>
  70. </template>
  71. <style scoped>
  72. .size-xs {
  73. @apply h-6 w-6 text-xs leading-6;
  74. .vip {
  75. @apply -translate-y-3;
  76. }
  77. }
  78. .size-small {
  79. @apply h-8 w-8 text-xs leading-8;
  80. .vip {
  81. @apply -translate-y-4;
  82. }
  83. }
  84. .size-medium {
  85. @apply h-10 w-10 text-base leading-10;
  86. .vip {
  87. @apply -translate-y-5;
  88. }
  89. }
  90. .size-normal {
  91. @apply h-14 w-14 text-2xl leading-[5rem];
  92. .vip {
  93. @apply -translate-y-[1.85rem];
  94. }
  95. }
  96. .size-large {
  97. @apply h-20 w-20 text-4xl leading-[5rem];
  98. .vip {
  99. @apply -translate-y-[2.65rem];
  100. }
  101. }
  102. .size-xl {
  103. @apply h-36 w-36 text-6xl leading-[5rem];
  104. .vip {
  105. @apply -translate-y-[4.85rem];
  106. }
  107. }
  108. </style>