Lzh on GitHub

Avatar

用于代表用户的具有 fallback 功能的图像元素。
<script setup lang="ts">
import { AvatarFallback, AvatarImage, AvatarRoot } from 'reka-ui'
</script>

<template>
  <div class="flex gap-5">
    <AvatarRoot class="bg-blackA3 inline-flex h-[45px] w-[45px] select-none items-center justify-center overflow-hidden rounded-full align-middle">
      <AvatarImage
        class="h-full w-full rounded-[inherit] object-cover"
        src="https://images.unsplash.com/photo-1492633423870-43d1cd2775eb?&w=128&h=128&dpr=2&q=80"
        alt="Colm Tuite"
      />
      <AvatarFallback
        class="text-grass11 dark:text-stone-300 leading-1 flex h-full w-full items-center justify-center bg-white dark:bg-stone-800 text-sm font-medium"
        :delay-ms="600"
      >
        CT
      </AvatarFallback>
    </AvatarRoot>
    <AvatarRoot class="bg-blackA3 inline-flex h-[45px] w-[45px] select-none items-center justify-center overflow-hidden rounded-full align-middle">
      <AvatarImage
        class="h-full w-full rounded-[inherit] object-cover"
        src="https://images.unsplash.com/photo-1511485977113-f34c92461ad9?ixlib=rb-1.2.1&w=128&h=128&dpr=2&q=80"
        alt="Pedro Duarte"
      />
      <AvatarFallback
        class="text-grass11 dark:text-stone-300 leading-1 flex h-full w-full items-center justify-center bg-white dark:bg-stone-800 text-sm font-medium"
        :delay-ms="600"
      >
        JD
      </AvatarFallback>
    </AvatarRoot>
    <AvatarRoot class="bg-blackA3 inline-flex h-[45px] w-[45px] select-none items-center justify-center overflow-hidden rounded-full align-middle border dark:border-stone-700">
      <AvatarFallback class=" text-grass11 dark:text-stone-300 leading-1 flex h-full w-full items-center justify-center bg-white dark:bg-stone-800 text-sm font-medium">
        PD
      </AvatarFallback>
    </AvatarRoot>
  </div>
</template>

功能特点

  • 自动和手动控制图片渲染时机。
  • 回退部分接受任何子元素。
  • 可选择延迟回退渲染以避免内容闪烁。

安装

从命令行安装组件。

$ npm add reka-ui

结构

导入所有部分并将其组合在一起。

<script setup>
  import { AvatarImage, AvatarRoot } from 'reka-ui'
</script>

<template>
  <AvatarRoot>
    <AvatarImage />
    <AvatarFallback />
  </AvatarRoot>
</template>

API 参考

Root

包含头像的所有部分。

属性默认值类型描述
as'span'AsTag | Component此组件应渲染为的元素或组件。可以通过 asChild 覆盖。
asChildfalseboolean将默认渲染的元素更改为作为子元素传递的元素,合并它们的 props 和行为。有关详细信息,请阅读我们的组合指南

Image

要渲染的图片。默认情况下,它只会在加载完成后渲染。如果需要更多控制,可以使用 @loadingStatusChange 处理程序。

属性默认值类型描述
as'img'AsTag | Component此组件应渲染为的元素或组件。可以通过 asChild 覆盖。
asChildfalseboolean将默认渲染的元素更改为作为子元素传递的元素,合并它们的 props 和行为。有关详细信息,请阅读我们的组合指南
crossOrigin'''' | 'anonymous' | 'use-credentials'
referrerPolicy'''' | 'no-referrer' | 'no-referrer-when-downgrade' | 'origin' | 'origin-when-cross-origin' | 'same-origin' | 'strict-origin' | 'strict-origin-when-cross-origin' | 'unsafe-url'
src*string

触发事件 (Emit)

事件名称Payload描述
loadingStatusChange[value: ImageLoadingStatus]提供有关图片加载状态的回调信息。这在您想要更精确地控制图片加载时渲染的内容时很有用。

Fallback

当图片未加载时渲染的元素。这意味着在加载过程中,或出现错误时。如果您在加载期间注意到闪烁,可以提供 delayMs prop 来延迟其渲染,使其仅在连接较慢的情况下显示。如需更多控制,请使用 AvatarImage 上的 @loadingStatusChange emit。

属性默认值类型描述
as'span'AsTag | Component此组件应渲染为的元素或组件。可以通过 asChild 覆盖。
asChildfalseboolean将默认渲染的元素更改为作为子元素传递的元素,合并它们的 props 和行为。有关详细信息,请阅读我们的组合指南
delayMsnumber用于延迟渲染,使其仅在连接较慢的情况下显示。

示例

可点击的带提示框的头像

您可以将头像与 Tooltip 组合以显示额外信息。

<script setup>
  import { AvatarImage, AvatarRoot, TooltipArrow, TooltipRoot, TooltipTrigger } from 'reka-ui'
</script>

<template>
  <TooltipRoot>
    <TooltipTrigger>
      <AvatarRoot></AvatarRoot>
    </TooltipTrigger>
    
    <TooltipContent side="top">
      Tooltip content
      <TooltipArrow />
    </TooltipContent>
  </TooltipRoot>
</template>