fix(Avatar): render on SSR

Co-Authored-By: Sébastien Chopin <seb@nuxt.com>
This commit is contained in:
Benjamin Canac
2025-02-18 18:04:59 +01:00
parent 572e567528
commit 67e546504d
32 changed files with 264 additions and 285 deletions

View File

@@ -1,6 +1,5 @@
<script lang="ts">
import type { VariantProps } from 'tailwind-variants'
import type { AvatarFallbackProps } from 'reka-ui'
import type { AppConfig } from '@nuxt/schema'
import _appConfig from '#build/app.config'
import theme from '#build/ui/avatar'
@@ -13,7 +12,7 @@ const avatar = tv({ extend: tv(theme), ...(appConfigAvatar.ui?.avatar || {}) })
type AvatarVariants = VariantProps<typeof avatar>
export interface AvatarProps extends Pick<AvatarFallbackProps, 'delayMs'> {
export interface AvatarProps {
/**
* The element or component this component should render as.
* @defaultValue 'span'
@@ -36,9 +35,8 @@ extendDevtoolsMeta<AvatarProps>({ defaultProps: { src: 'https://avatars.githubus
</script>
<script setup lang="ts">
import { ref, computed, useAttrs, onMounted } from 'vue'
import { AvatarRoot, AvatarFallback, useForwardProps } from 'reka-ui'
import { reactivePick, useImage } from '@vueuse/core'
import { ref, computed, watch } from 'vue'
import { Primitive } from 'reka-ui'
import ImageComponent from '#build/ui-image-component'
import { useAvatarGroup } from '../composables/useAvatarGroup'
import UIcon from './Icon.vue'
@@ -46,14 +44,9 @@ import UIcon from './Icon.vue'
defineOptions({ inheritAttrs: false })
const props = withDefaults(defineProps<AvatarProps>(), { as: 'span' })
const attrs = useAttrs() as any
const fallbackProps = useForwardProps(reactivePick(props, 'delayMs'))
const fallback = computed(() => props.text || (props.alt || '').split(' ').map(word => word.charAt(0)).join('').substring(0, 2))
const imageLoaded = ref(false)
const { size } = useAvatarGroup(props)
// eslint-disable-next-line vue/no-dupe-keys
@@ -73,43 +66,37 @@ const sizePx = computed(() => ({
'3xl': 48
})[props.size || 'md'])
// Reproduces Reka UI's [AvatarImage](https://reka-ui.com/docs/components/avatar#image) component behavior which cannot be used with NuxtImg component
onMounted(() => {
if (!props.src || (ImageComponent as unknown as string) !== 'img') {
return
const error = ref(false)
watch(() => props.src, () => {
if (error.value) {
error.value = false
}
const { then } = useImage({ ...props, ...attrs, src: props.src! })
then((img) => {
if (img.isReady.value) {
imageLoaded.value = true
}
})
})
function onError() {
error.value = true
}
</script>
<template>
<AvatarRoot :as="as" :class="ui.root({ class: [props.class, props.ui?.root] })">
<Primitive :as="as" :class="ui.root({ class: [props.class, props.ui?.root] })">
<component
:is="ImageComponent"
v-if="src"
v-show="imageLoaded"
v-if="src && !error"
role="img"
:src="src"
:alt="alt"
:width="sizePx"
:height="sizePx"
v-bind="attrs"
v-bind="$attrs"
:class="ui.image({ class: props.ui?.image })"
@load="imageLoaded = true"
@error="onError"
/>
<AvatarFallback v-if="!imageLoaded" as-child v-bind="{ ...fallbackProps, ...$attrs }">
<slot>
<UIcon v-if="icon" :name="icon" :class="ui.icon({ class: props.ui?.icon })" />
<span v-else :class="ui.fallback({ class: props.ui?.fallback })">{{ fallback || '&nbsp;' }}</span>
</slot>
</AvatarFallback>
</AvatarRoot>
<slot v-else>
<UIcon v-if="icon" :name="icon" :class="ui.icon({ class: props.ui?.icon })" />
<span v-else :class="ui.fallback({ class: props.ui?.fallback })">{{ fallback || '&nbsp;' }}</span>
</slot>
</Primitive>
</template>

View File

@@ -1,7 +1,7 @@
export default {
slots: {
root: 'inline-flex items-center justify-center shrink-0 select-none overflow-hidden rounded-full align-middle bg-(--ui-bg-elevated)',
image: 'h-full w-full rounded-[inherit] object-cover data-[error]:hidden',
image: 'h-full w-full rounded-[inherit] object-cover',
fallback: 'font-medium leading-none text-(--ui-text-muted) truncate',
icon: 'text-(--ui-text-muted) shrink-0'
},