CommonAvatar.vue 2.4 KB

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