Skip to content
This repository was archived by the owner on Sep 24, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/nuxt-ui-vue/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@
</script>

<template>
<div />
<div class="grid place-items-center w-full min-h-screen">
<UAvatar src="https://github.com/selemondev.pg" icon="ph:moon" />
</div>
</template>
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ export interface UAlert extends UComponentRoot {
export interface UAvatar extends UComponentRoot {
avatarWrapper?: string
avatarChipClass?: string
avatarBackground?: string
avatarText?: string
avatarSize?: string
avatarIconSize?: string
avatarRounded?: string
Expand Down
41 changes: 34 additions & 7 deletions packages/nuxt-ui-vue/src/components/elements/Avatar/UAvatar.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<script setup lang='ts'>
import type { PropType } from 'vue'
import { computed, defineComponent } from 'vue'
import { omit } from 'lodash-es'
import { computed, defineComponent, ref, useAttrs, watch } from 'vue'
import classNames from 'classnames'
import { Icon } from '@iconify/vue'
import type { VariantJSWithClassesListProps } from '../../../utils/getVariantProps'
Expand All @@ -21,7 +22,11 @@ const props = defineProps({
},
src: {
type: String,
default: '',
default: null,
},
text: {
type: String,
default: null,
},
size: {
type: String as PropType<AvatarSize>,
Expand All @@ -40,7 +45,7 @@ const props = defineProps({

chipColor: {
type: String,
default: '',
default: null,
},

chipSize: {
Expand All @@ -55,11 +60,29 @@ const props = defineProps({

chipText: {
type: String,
default: '',
default: null,
},

})

const url = computed(() => {
if (typeof props.src === 'boolean')
return null

return props.src
})

const error = ref(false)

watch(() => props.src, () => {
if (error.value)
error.value = false
})

function onError() {
error.value = true
}

const placeholder = computed(() => {
return (props.alt || '').split(' ').map(word => word.charAt(0)).join('').substring(0, 2)
})
Expand All @@ -78,6 +101,7 @@ const variant = computed(() => {
const avatarWrapperClasses = computed<string>(() => {
return classNames(
variant.value.root,
(error.value || !url.value) && variant.value.avatarBackground,
variant.value.avatarSize && variant.value[props.size],
)
})
Expand Down Expand Up @@ -121,6 +145,8 @@ const avatarIconSize = computed(() => {
const avatarChipColorStyles = computed(() => ({
'background-color': props.chipColor || '',
}))
const attrs = useAttrs()
const attrsOmitted = omit(attrs, ['class'])
</script>

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

<template>
<span :class="[avatarWrapperClasses, avatarClasses]" :title="props.name">
<img v-if="props.src" :class="avatarClasses" :src="props.src" :alt="props.name">
<span v-else-if="!props.src" :class="variant.avatarPlaceholderClass">{{ placeholder }}</span>
<Icon v-if="!props.src && !placeholder" :icon="props.icon" :class="[avatarIconSize, variant.avatarIconColor]" />
<img v-if="url && !error" v-bind="attrsOmitted" :class="avatarClasses" :src="url" :alt="props.name" @error="onError">
<span v-else-if="text" :class="variant.avatarText">{{ props.text }}</span>
<span v-else-if="placeholder" :class="variant.avatarPlaceholderClass">{{ placeholder }}</span>
<Icon v-else-if="props.icon" :icon="props.icon" :class="[avatarIconSize, variant.avatarIconColor]" />
<span v-if="props.chipColor" :style="avatarChipColorStyles" :class="[avatarChipClass, avatarChipSize]">
{{ chipText }}
</span>
Expand Down
2 changes: 2 additions & 0 deletions packages/nuxt-ui-vue/src/theme/nuxtLabsTheme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ export default {
base: {
'root': 'relative cursor-pointer inline-flex items-center justify-center bg-gray-100 rounded-full',
'avatarRounded': 'rounded-full',
'avatarBackground': 'bg-gray-100 dark:bg-gray-800',
'avatarText': 'font-medium leading-none text-gray-900 dark:text-white truncate',
'avatarPlaceholderClass': 'font-medium cursor-pointer text-gray-600 upperCase',
'avatarIconColor': 'text-black',
'3xs': 'h-4 w-4 text-[8px]',
Expand Down