Skip to Content
文档

头像

用于显示用户头像或名称首字母

SourceStorybookRecipeArk
SA

用法

¥Usage

import { Avatar, AvatarGroup } from "@chakra-ui/react"
<AvatarGroup>
  <Avatar.Root>
    <Avatar.Fallback />
    <Avatar.Image />
  </Avatar.Root>
</AvatarGroup>
info

如果你更喜欢封闭的组件组合,请查看 以下代码片段

¥If you prefer a closed component composition, check out the snippet below.

示例

¥Examples

尺寸

¥Sizes

使用 size 属性更改头像的大小

¥Use the size prop to change the size of the avatar

SA
SA
SA
SA
SA
SA

变量

¥Variants

使用 variant 属性更改头像的变体

¥Use the variant prop to change the variant of the avatar

SA
SA
SA

形状

¥Shape

使用 shape 属性更改头像的形状,从 rounded 更改为 square

¥Use the shape prop to change the shape of the avatar, from rounded to square

DA
SA
RU

颜色

¥Colors

使用 colorPalette 属性更改头像的颜色

¥Use the colorPalette prop to change the color of the avatar

gray

SA
SA

red

SA
SA

green

SA
SA

blue

SA
SA

teal

SA
SA

pink

SA
SA

purple

SA
SA

cyan

SA
SA

orange

SA
SA

yellow

SA
SA

回退

¥Fallback

当未提供名称或图片加载失败时,渲染 Avatar.Icon 作为后备组件。

¥Render Avatar.Icon as the fallback when the name is not provided or when the image fails to load.

OK
SU

随机颜色

¥Random Color

colorPalette 属性与一些自定义逻辑组合,以动态更改头像的颜色。

¥Combine the colorPalette prop with some custom logic to dynamically change the color of the avatar

SN
BL
JL

¥Ring

使用 outline* 属性在头像周围添加一个圆环。

¥Use the outline* props to add a ring around the avatar

R
R
R

¥Group

使用 Group 组件将多个头像分组。

¥Use the Group component to group multiple avatars together

US
BA
UC
+3

堆叠

¥Stacking

使用 AvatarGroup 组件时,你可以使用 stacking 属性更改头像的堆叠顺序。

¥When using the AvatarGroup component, you can use the stacking prop to change the stacking order of the avatars

US
BA
UC
+3
US
BA
UC
+3
US
BA
UC
+3

人物角色

¥Persona

以下是如何使用 Avatar 组件显示用户画像的示例。

¥Here's an example of how to use the Avatar component to display a user persona.

JM

John Mason

john.mason@example.com

MJ

Melissa Jones

melissa.jones@example.com

徽章

¥Badge

通过组合 FloatCircle 组件,在头像右上角显示徽章。

¥Show a badge on the right corner of the avatar by composing the Float and Circle components

DA

溢出

¥Overflow

以下是如何通过组合 MenuAvatar 组件来处理头像溢出的示例。

¥Here's an example of how to handle an overflow of avatars by composing the Menu and Avatar components.

NU
SH
KH

Next.js

以下示例展示了如何使用 Next.js Image 组合头像。

¥Here's an example of how to compose the avatar with Next.js Image.

import { getImageProps } from "next/image"

function Demo() {
  const imageProps = getImageProps({
    src: "/image.png",
  })
  return (
    <Avatar.Root>
      <Avatar.Fallback name="Segun Adebayo" />
      <Avatar.Image {...imageProps} />
    </Avatar.Root>
  )
}

商店

¥Store

访问头像状态和方法的另一种方法是使用 RootProvider 组件和 useAvatar 存储钩子。

¥An alternative way to access the avatar state and methods is to use the RootProvider component and the useAvatar store hook.

SA
not loaded

已关闭组件

¥Closed Component

以下是如何为封闭组件组合设置头像。

¥Here's how to setup the Avatar for a closed component composition.

import {
  Avatar as ChakraAvatar,
  AvatarGroup as ChakraAvatarGroup,
} from "@chakra-ui/react"
import * as React from "react"

type ImageProps = React.ImgHTMLAttributes<HTMLImageElement>

export interface AvatarProps extends ChakraAvatar.RootProps {
  name?: string
  src?: string
  srcSet?: string
  loading?: ImageProps["loading"]
  icon?: React.ReactElement
  fallback?: React.ReactNode
}

export const Avatar = React.forwardRef<HTMLDivElement, AvatarProps>(
  function Avatar(props, ref) {
    const { name, src, srcSet, loading, icon, fallback, children, ...rest } =
      props
    return (
      <ChakraAvatar.Root ref={ref} {...rest}>
        <ChakraAvatar.Fallback name={name}>
          {fallback || icon}
        </ChakraAvatar.Fallback>
        <ChakraAvatar.Image src={src} srcSet={srcSet} loading={loading} />
        {children}
      </ChakraAvatar.Root>
    )
  },
)

export const AvatarGroup = ChakraAvatarGroup

如果你想自动将封闭式组件添加到项目中,请运行以下命令:

¥If you want to automatically add the closed component to your project, run the command:

npx @chakra-ui/cli snippet add avatar

属性

¥Props

根元素

¥Root

PropDefaultType
colorPalette 'gray'
'gray' | 'red' | 'orange' | 'yellow' | 'green' | 'teal' | 'blue' | 'cyan' | 'purple' | 'pink'

The color palette of the component

size 'md'
'full' | '2xs' | 'xs' | 'sm' | 'md' | 'lg' | 'xl' | '2xl'

The size of the component

variant 'subtle'
'solid' | 'subtle' | 'outline'

The variant of the component

shape 'full'
'square' | 'rounded' | 'full'

The shape of the component

as
React.ElementType

The underlying element to render.

asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior.

For more details, read our Composition guide.
unstyled
boolean

Whether to remove the component's style.

ids
Partial<{ root: string; image: string; fallback: string }>

The ids of the elements in the avatar. Useful for composition.

onStatusChange
(details: StatusChangeDetails) => void

Functional called when the image loading status changes.

borderless
'true' | 'false'

The borderless of the component

回退

¥Fallback

PropDefaultType
name
string | undefined

The name to derive the initials from. If not provided, the fallback will display a generic icon.

as
React.ElementType

The underlying element to render.

asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior.

For more details, read our Composition guide.

图片

¥Image

PropDefaultType
as
React.ElementType

The underlying element to render.

asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior.

For more details, read our Composition guide.

Previous

提示框

Next

徽章