Skip to content
This repository was archived by the owner on Sep 24, 2024. It is now read-only.

Commit cc2009e

Browse files
SelemondevSelemondev
authored andcommitted
Update UAvatar component to include background color and text styles
1 parent 8d7112c commit cc2009e

File tree

4 files changed

+41
-8
lines changed

4 files changed

+41
-8
lines changed

packages/nuxt-ui-vue/src/App.vue

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,7 @@
22
</script>
33

44
<template>
5-
<div />
5+
<div class="grid place-items-center w-full min-h-screen">
6+
<UAvatar src="https://github.com/selemondev.pg" icon="ph:moon" />
7+
</div>
68
</template>

packages/nuxt-ui-vue/src/Types/componentsTypes/components.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ export interface UAlert extends UComponentRoot {
9393
export interface UAvatar extends UComponentRoot {
9494
avatarWrapper?: string
9595
avatarChipClass?: string
96+
avatarBackground?: string
97+
avatarText?: string
9698
avatarSize?: string
9799
avatarIconSize?: string
98100
avatarRounded?: string

packages/nuxt-ui-vue/src/components/elements/Avatar/UAvatar.vue

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<script setup lang='ts'>
22
import type { PropType } from 'vue'
3-
import { computed, defineComponent } from 'vue'
3+
import { omit } from 'lodash-es'
4+
import { computed, defineComponent, ref, useAttrs, watch } from 'vue'
45
import classNames from 'classnames'
56
import { Icon } from '@iconify/vue'
67
import type { VariantJSWithClassesListProps } from '../../../utils/getVariantProps'
@@ -21,7 +22,11 @@ const props = defineProps({
2122
},
2223
src: {
2324
type: String,
24-
default: '',
25+
default: null,
26+
},
27+
text: {
28+
type: String,
29+
default: null,
2530
},
2631
size: {
2732
type: String as PropType<AvatarSize>,
@@ -40,7 +45,7 @@ const props = defineProps({
4045
4146
chipColor: {
4247
type: String,
43-
default: '',
48+
default: null,
4449
},
4550
4651
chipSize: {
@@ -55,11 +60,29 @@ const props = defineProps({
5560
5661
chipText: {
5762
type: String,
58-
default: '',
63+
default: null,
5964
},
6065
6166
})
6267
68+
const url = computed(() => {
69+
if (typeof props.src === 'boolean')
70+
return null
71+
72+
return props.src
73+
})
74+
75+
const error = ref(false)
76+
77+
watch(() => props.src, () => {
78+
if (error.value)
79+
error.value = false
80+
})
81+
82+
function onError() {
83+
error.value = true
84+
}
85+
6386
const placeholder = computed(() => {
6487
return (props.alt || '').split(' ').map(word => word.charAt(0)).join('').substring(0, 2)
6588
})
@@ -78,6 +101,7 @@ const variant = computed(() => {
78101
const avatarWrapperClasses = computed<string>(() => {
79102
return classNames(
80103
variant.value.root,
104+
(error.value || !url.value) && variant.value.avatarBackground,
81105
variant.value.avatarSize && variant.value[props.size],
82106
)
83107
})
@@ -121,6 +145,8 @@ const avatarIconSize = computed(() => {
121145
const avatarChipColorStyles = computed(() => ({
122146
'background-color': props.chipColor || '',
123147
}))
148+
const attrs = useAttrs()
149+
const attrsOmitted = omit(attrs, ['class'])
124150
</script>
125151

126152
<script lang="ts">
@@ -132,9 +158,10 @@ export default defineComponent({
132158

133159
<template>
134160
<span :class="[avatarWrapperClasses, avatarClasses]" :title="props.name">
135-
<img v-if="props.src" :class="avatarClasses" :src="props.src" :alt="props.name">
136-
<span v-else-if="!props.src" :class="variant.avatarPlaceholderClass">{{ placeholder }}</span>
137-
<Icon v-if="!props.src && !placeholder" :icon="props.icon" :class="[avatarIconSize, variant.avatarIconColor]" />
161+
<img v-if="url && !error" v-bind="attrsOmitted" :class="avatarClasses" :src="url" :alt="props.name" @error="onError">
162+
<span v-else-if="text" :class="variant.avatarText">{{ props.text }}</span>
163+
<span v-else-if="placeholder" :class="variant.avatarPlaceholderClass">{{ placeholder }}</span>
164+
<Icon v-else-if="props.icon" :icon="props.icon" :class="[avatarIconSize, variant.avatarIconColor]" />
138165
<span v-if="props.chipColor" :style="avatarChipColorStyles" :class="[avatarChipClass, avatarChipSize]">
139166
{{ chipText }}
140167
</span>

packages/nuxt-ui-vue/src/theme/nuxtLabsTheme.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ export default {
7171
base: {
7272
'root': 'relative cursor-pointer inline-flex items-center justify-center bg-gray-100 rounded-full',
7373
'avatarRounded': 'rounded-full',
74+
'avatarBackground': 'bg-gray-100 dark:bg-gray-800',
75+
'avatarText': 'font-medium leading-none text-gray-900 dark:text-white truncate',
7476
'avatarPlaceholderClass': 'font-medium cursor-pointer text-gray-600 upperCase',
7577
'avatarIconColor': 'text-black',
7678
'3xs': 'h-4 w-4 text-[8px]',

0 commit comments

Comments
 (0)