/app/javascript/dashboard/components/widgets/Avatar.vue

https://github.com/chatwoot/chatwoot · Vue · 86 lines · 84 code · 2 blank · 0 comment · 4 complexity · 1f7961f686fb121a11c1960ab8bba5d0 MD5 · raw file

  1. <template>
  2. <div
  3. class="avatar-container"
  4. :style="[style, customStyle]"
  5. aria-hidden="true"
  6. >
  7. <span>{{ userInitial }}</span>
  8. </div>
  9. </template>
  10. <script>
  11. export default {
  12. name: 'Avatar',
  13. props: {
  14. username: {
  15. type: String,
  16. default: '',
  17. },
  18. backgroundColor: {
  19. type: String,
  20. default: 'white',
  21. },
  22. color: {
  23. type: String,
  24. default: '#1f93ff',
  25. },
  26. customStyle: {
  27. type: Object,
  28. default: undefined,
  29. },
  30. size: {
  31. type: Number,
  32. default: 40,
  33. },
  34. src: {
  35. type: String,
  36. default: '',
  37. },
  38. rounded: {
  39. type: Boolean,
  40. default: true,
  41. },
  42. },
  43. computed: {
  44. style() {
  45. return {
  46. width: `${this.size}px`,
  47. height: `${this.size}px`,
  48. borderRadius: this.rounded ? '50%' : 0,
  49. lineHeight: `${this.size + Math.floor(this.size / 20)}px`,
  50. backgroundColor: this.backgroundColor,
  51. fontSize: `${Math.floor(this.size / 2.5)}px`,
  52. color: this.color,
  53. };
  54. },
  55. userInitial() {
  56. return this.initials || this.initial(this.username);
  57. },
  58. },
  59. methods: {
  60. initial(username) {
  61. const parts = username ? username.split(/[ -]/) : [];
  62. let initials = '';
  63. for (let i = 0; i < parts.length; i += 1) {
  64. initials += parts[i].charAt(0);
  65. }
  66. if (initials.length > 2 && initials.search(/[A-Z]/) !== -1) {
  67. initials = initials.replace(/[a-z]+/g, '');
  68. }
  69. initials = initials.substr(0, 2).toUpperCase();
  70. return initials;
  71. },
  72. },
  73. };
  74. </script>
  75. <style lang="scss" scoped>
  76. .avatar-container {
  77. display: flex;
  78. font-weight: 500;
  79. align-items: center;
  80. justify-content: center;
  81. text-align: center;
  82. background-image: linear-gradient(to top, #4481eb 0%, #04befe 100%);
  83. }
  84. </style>