Represents a person: shows the image when there is one, otherwise the first character of the name, with a person glyph as the customer fallback; a presence status can sit at the bottom-right corner.
Status: stable · Components: Avatar
Import
Section titled “Import”import { Avatar } from 'nacre'import { Text, View } from 'nsh'import { Avatar, TEXT_SIZE } from 'nacre'
export function AvatarUsage() { return ( <View class="flex-row items-center gap-2"> <Avatar name="Chen Zhiyuan" /> <Text class={`${TEXT_SIZE.small} text-foreground`}>Chen Zhiyuan</Text> </View> )}Examples
Section titled “Examples”size is the side length, 28 by default on desktop and 40 on phones; 36 in the chat list, 24 inline. Below 32 the initial uses the body size; larger avatars use title.
Phone: On phones an avatar without size is 40 (the list avatar size in Ionic and antd-mobile); the example sets 24 / 28 / 36 explicitly.
import { View } from 'nsh'import { Avatar } from 'nacre'
export function AvatarSizes() { return ( <View class="flex-row items-center gap-3"> <Avatar name="Chen Zhiyuan" size={24} /> <Avatar name="Chen Zhiyuan" size={28} /> <Avatar testID="avatar-36" name="Chen Zhiyuan" size={36} /> </View> )}Fallback
Section titled “Fallback”With no image, while loading, or when loading fails, the fallback shows: agents show their initial; customer nicknames are often system-generated, so pass person to show a person glyph. tone sets the fill and foreground colors by role.
import { View } from 'nsh'import { Avatar } from 'nacre'
export function AvatarFallback() { return ( <View class="flex-row items-center gap-3"> <Avatar name="Lin Jiayi" size={36} /> <Avatar name="Customer" person size={36} fallbackTestID="avatar-person" /> <Avatar name="Customer" person size={36} tone={['secondary', 'foreground']} /> </View> )}Your own avatar
Section titled “Your own avatar”self uses the primary color fill (“mine”), setting it apart from the neutral gray of other agents.
import { View } from 'nsh'import { Avatar } from 'nacre'
export function AvatarSelf() { return ( <View class="flex-row items-center gap-3"> <Avatar name="Lin Jiayi" size={36} /> <Avatar name="Chen Zhiyuan" size={36} self /> </View> )}With status
Section titled “With status”badge sits at the bottom-right corner, usually a PresenceDot with ring.
import { View } from 'nsh'import { Avatar, PresenceDot } from 'nacre'
export function AvatarWithPresence() { return ( <View class="flex-row items-center gap-3"> <Avatar name="Lin Jiayi" size={36} badge={<PresenceDot ring kind="online" label="Online" />} /> <Avatar name="Wang Simin" size={36} badge={<PresenceDot ring kind="offline" label="Offline" />} /> </View> )}Pressable avatar
Section titled “Pressable avatar”Passing onPress makes it a button: clicking a customer’s avatar in a message opens the customer panel. Pressing drops it to 80% opacity without scaling (like media thumbnails); it takes Tab focus and draws a focus ring. label is the screen reader name; pass expanded when it opens a collapsible panel.
import { Text, View, signal } from 'nsh'import { Avatar, TEXT_SIZE } from 'nacre'
export function AvatarPressable() { const open = signal(false) return ( <View class="flex-row flex-wrap items-center gap-3"> <Avatar testID="avatar-pressable" name="Customer" person size={36} tone={['secondary', 'foreground']} label="View customer info" expanded={open()} onPress={() => open.update((v) => !v)} /> <Text testID="avatar-pressable-state" class={`${TEXT_SIZE.small} text-muted-foreground`}> {open() ? 'Customer panel open' : 'Click the avatar to open the customer panel'} </Text> </View> )}Accessibility
Section titled “Accessibility”- The avatar itself is not a screen reader node: the name must appear as text next to it.
- An overlaid PresenceDot reads its label (“Online”).
- A pressable avatar (with onPress) is a button: it reads label (name if not given), is reachable with Tab, and fires on Enter / Space; with expanded it reads expanded / collapsed.
Do / Don’t
Section titled “Do / Don’t”Do
- Use self to mark yourself
- Use person when the customer has no reliable nickname
- Show status by overlaying a PresenceDot via badge
- Use onPress for a clickable avatar instead of wrapping it in your own role=“button”
Don’t
- Put a logo or icon in it
- Show an avatar without the name
- Scale a pressable avatar on press
Property descriptions follow the Chinese source comments.
Avatar
Section titled “Avatar”| Prop | Type | Default | Description |
|---|---|---|---|
name * |
string |
— | |
src |
string |
— | |
size |
number |
— | |
self |
boolean |
— | 当前坐席自己的头像:主色底(「我的」,ADR 0016 允许的品牌位置),和其他坐席的中性灰区分开 |
badge |
JSX.Element |
— | 右下角的角标(在线点、渠道) |
tone |
readonly [ColorToken, ColorToken] |
— | 按身份取的底色与前景色(客户头像);不给就是中性 avatar 色 |
person |
boolean |
— | 没有图片时露出人形图标而不是首字(客户:昵称常是系统生成的,首字没有意义) |
onPress |
() => void |
— | 给了就是可点头像(点客户头像打开客户栏):整个头像是按钮,按下降到 80% 透明度、不缩放(同媒体缩略图,ADR 0020), 键盘可聚焦、画焦点环,Enter / 空格触发 |
label |
string |
— | 可点头像的读屏名,不给读 name |
expanded |
boolean |
— | 可点头像打开的是可收起的面板时给:读屏读展开 / 收起 |
testID |
string |
— | |
fallbackTestID |
string |
— | 兜底图(人形或首字)自己的 testID |
* required