CommonAvatar.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <!-- Copyright (C) 2012-2025 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 flex shrink-0 items-center justify-center rounded-full bg-cover bg-center select-none"
  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. Sure, here is the refactored style using native CSS: ```css
  72. <style scoped>
  73. .size-xs {
  74. height: 1.5rem;
  75. width: 1.5rem;
  76. font-size: 0.75rem;
  77. line-height: 1.5rem;
  78. }
  79. .size-xs .vip {
  80. transform: translateY(-0.75rem);
  81. }
  82. .size-small {
  83. height: 2rem;
  84. width: 2rem;
  85. font-size: 0.75rem;
  86. line-height: 2rem;
  87. }
  88. .size-small .vip {
  89. transform: translateY(-1rem);
  90. }
  91. .size-medium {
  92. height: 2.5rem;
  93. width: 2.5rem;
  94. font-size: 1rem;
  95. line-height: 2.5rem;
  96. }
  97. .size-medium .vip {
  98. transform: translateY(-1.25rem);
  99. }
  100. .size-normal {
  101. height: 3.5rem;
  102. width: 3.5rem;
  103. font-size: 1.5rem;
  104. line-height: 5rem;
  105. }
  106. .size-normal .vip {
  107. transform: translateY(-1.85rem);
  108. }
  109. .size-large {
  110. height: 5rem;
  111. width: 5rem;
  112. font-size: 2.25rem;
  113. line-height: 5rem;
  114. }
  115. .size-large .vip {
  116. transform: translateY(-2.65rem);
  117. }
  118. .size-xl {
  119. height: 9rem;
  120. width: 9rem;
  121. font-size: 3.75rem;
  122. line-height: 5rem;
  123. }
  124. .size-xl .vip {
  125. transform: translateY(-4.85rem);
  126. }
  127. </style>
  128. ```