JGuang

头像

Avatar · Astryx

交互预览
npm install @astryxdesign/core@0.7.1 @astryxdesign/theme-neutral@0.7.1 @stylexjs/stylex@0.19.0

头像 · Avatar

头像用于内容展示场景。下面的官方示例展示原生用法与状态,属性名保持上游 API。

安装与前置条件

npm install @astryxdesign/core@0.7.1 @astryxdesign/theme-neutral@0.7.1 @stylexjs/stylex@0.19.0

需要 React 19+,按顺序导入基础 CSS 和主题,并设置 Theme Provider。参见开始使用。

示例与使用方式

  • 头像 · 综合展示 1:首先体验这个示例,再查看代码理解组件的组合方式。
  • 头像 · 综合展示 / 状态 2:可交互查看这一用法的行为,复制代码时保留它实际使用的属性与导入。
  • 头像 · 交互示例 3:可交互查看这一用法的行为,复制代码时保留它实际使用的属性与导入。
  • 头像 · 交互示例 4:可交互查看这一用法的行为,复制代码时保留它实际使用的属性与导入。
  • 头像 · 交互示例 5:可交互查看这一用法的行为,复制代码时保留它实际使用的属性与导入。
  • 头像 · 样式变化 / 状态 / 样式 6:可交互查看这一用法的行为,复制代码时保留它实际使用的属性与导入。
  • 头像 · 交互示例 7:可交互查看这一用法的行为,复制代码时保留它实际使用的属性与导入。
  • 头像 · 交互示例 8:可交互查看这一用法的行为,复制代码时保留它实际使用的属性与导入。
  • 头像 · 交互示例 9:可交互查看这一用法的行为,复制代码时保留它实际使用的属性与导入。
  • 头像 · 状态 10:可交互查看这一用法的行为,复制代码时保留它实际使用的属性与导入。

API 与上游详细参考

优先使用 0.7.1 官方中文说明;上游尚未提供中文的条目保留英文。属性名、类型及默认值保持原生 API,不把示例状态名称当成组件属性。

Avatar

Avatar displays a user or entity's profile picture with automatic fallback to initials or a default icon. Use it alongside user information to visually represent people, teams, or entities throughout the interface.

属性类型默认值说明
srcstringPrimary image source URL.
fallbackSrcstringFallback image when primary fails.
namestringUser name for initials and alt text.
altstringAlt text (falls back to name).
size'xsm' | 'sm' | 'md' | 'lg' | 'xl' | number'md'Avatar size. Use a named size ('xsm' 20px, 'sm' 24px, 'md' 36px, 'lg' 48px, 'xl' 128px) or a numeric pixel value. Avatar shares Icon's abbreviated scale, but its tiers are larger because avatars align with media rather than glyphs. Inside an AvatarGroup the group's size wins and this prop is ignored.
shape'circle' | 'rounded' | 'square''circle'Shape variant of the avatar. 'circle' (default) stays fully round. 'rounded' uses the element radius token so it matches UI corner rounding and can be set globally via theme. 'square' has no radius. Status dot positioning adapts automatically: 4-o'clock on circle, bottom-right corner on rounded/square.
statusReactNodeCorner content for status indicators. AvatarStatusDot reports its label to the avatar, which composes it into the accessible name (e.g. "Jane Doe, Online") so screen readers announce the status. Reporting goes through context, so it still works when the dot sits inside a wrapper component of your own.
tooltipstring | booleantrueTooltip shown on hover and keyboard focus. Omitted or true shows the avatar's name; a string shows that text instead; false shows no tooltip. Not auto-disabled when wrapped in your own Tooltip/HoverCard. Set tooltip={false} if you supply your own overlay. No tooltip is shown when tooltip is true/omitted and there is no name.
hrefstringWhen set, the avatar renders as an interactive link (<a> or a custom link component) pointing here. This follows the same element-swap rule as Button. Requires a meaningful accessible name via alt or name: an interactive avatar without one warns in development. Inside an AvatarGroup, interactive avatars share a single Tab stop and are reached with arrow keys.
asElementTypeCustom link component used when href is set (e.g. next/link). Overrides the provider-level LinkProvider default. Only applies with href.
targetstringLink target attribute. Only applies with href.
relstringLink rel attribute. Only applies with href.
onClick(e: MouseEvent) => voidClick handler. When set without href, the avatar renders as a focusable <button type="button">. Requires a meaningful accessible name via alt or name: an interactive avatar without one warns in development.

使用建议

  • Always provide a name prop so the component can generate meaningful initials and alt text when the image fails to load.
  • Use the status slot with AvatarStatusDot to indicate online presence or availability when relevant to the context.
  • Give every interactive avatar (href or onClick) a name or alt. It is the control's accessible name, and it warns in development when it is missing.
  • 避免:Use Avatar for decorative images or logos that aren't representing a person or entity. Use an image or icon component instead.
  • 避免:Override the circular shape. Avatars are always round to maintain visual consistency across the system.

主题 API

{
  "targets": [
    {
      "className": "astryx-avatar",
      "visualProps": [
        "size",
        "shape"
      ]
    },
    {
      "className": "astryx-avatar-fallback",
      "visualProps": [
        "size"
      ]
    },
    {
      "className": "astryx-avatar-status-dot",
      "visualProps": [
        "variant"
      ]
    },
    {
      "className": "astryx-avatar-status-dot-glyph",
      "visualProps": [
        "shape"
      ]
    }
  ],
  "vars": [
    {
      "name": "--_avatar-group-overlap",
      "description": "Negative inline offset applied to every avatar stacked in an AvatarGroup; the group pads its start edge by the same amount so the first avatar stays inside it. Set from the group size; a more negative value tightens the stack.",
      "default": "set at runtime from the group avatar size (px)",
      "private": true
    },
    {
      "name": "--_avatar-radius",
      "description": "Border radius of the avatar wrapper, content, focus ring, and the AvatarGroupOverflow \"+N\" chip. Set per shape variant by shapeStyles.",
      "default": "var(--radius-full)",
      "private": true
    }
  ],
  "derived": [
    {
      "property": "borderRadius",
      "vars": [
        "--_avatar-radius"
      ]
    }
  ]
}

AvatarStatusDot

组件结构

  • Photo: The profile image, loaded from the src URL. Shown when available.
  • Initials: The first letter, digit or emoji of the first and last words of the name; punctuation is skipped. Shown when no photo is available. A name with no letters, digits or emoji shows the default icon instead.
  • Default icon: A generic person silhouette. Shown when there is no photo or name.
  • Status dot: A small indicator in the bottom-right corner showing availability (online, away, busy). Each variant pairs colour with a distinct shape so status does not rely on colour alone.

Avatar Status Dot

尺寸感知的状态指示点,从上下文中读取头像尺寸并等比缩放。每个变体将颜色与独特形状(实心、圆环、横杠)配对,确保状态不仅靠颜色传达(WCAG 1.4.1)。

属性类型默认值说明
variant'success' | 'neutral' | 'error''success'语义变体,颜色与形状配对:success = 绿色实心点,neutral = 灰色圆环,error = 带横杠的红色点。
labelstring描述状态的无障碍标签。在 Avatar 内会被合成进头像自身的无障碍名称(如 "Jane Doe, Online")——Avatar 根元素为 role="img",会裁剪子元素语义,因此合成名称是辅助技术获知状态的途径。独立使用时状态点直接以 role="img" 暴露该标签。
iconReactNode居中显示在状态点内的图标(tiny 尺寸时隐藏)。渲染的图标会替换内置形状标记,请为每个状态使用不同的图标。