JGuang

头像

Avatar · Material UI

交互预览
npm install @mui/material@9.4.0 @emotion/react@11.14.0 @emotion/styled@11.14.1

头像

用图片、首字母或图标识别人或实体,并支持头像组。

本指南覆盖默认 Emotion 环境的 Material UI Core;扩展包与额外示例按清单单独标注。

使用场景与业务边界

图片需要有意义的替代文字和网络降级;头像组总数来自给定数据,不是自动查询的人数。

原生组成与范围

Avatar, AvatarGroup, Badge

保持 Material UI 原生组合与类型,不替换为 JGuang/Base UI/Radix 同名 API。示例条目不是额外组件。

原生安装与初始化

npm install @mui/material@9.4.0 @emotion/react@11.14.0 @emotion/styled@11.14.1

采用原生 npm 包与 Emotion,不提供虚构的 JGuang UI Registry 地址。将示例放在原生 ThemeProvider 内,按项目选择 CssBaseline 或 ScopedCssBaseline。先检查 React、现有 Provider、锁文件与 SSR 配置;不要覆盖用户文件或忽略 peer 冲突。本批实际验收 React 19.2.8;React 18 及以下须匹配 react-is。完整初始化规则见随附的双语 setup 文档。

关键原生参数

仅解释本指南选出的关键参数,不是平铺的通用 API。原生继承来源、slot 结构、可空值与泛型仍按固定源码保留。

所属组件.属性用途说明声明来源
Avatar.src宿主提供的图片地址,需要媒体加载失败处理。Avatar
Avatar.alt给定图片的有意义替代文字。Avatar
Avatar.variant所属组件的原生视觉枚举,不可替换为通用 variant 属性。Avatar
AvatarGroup.maxBadge 显示数量上限或 Slider 最大值,区分所属组件和单位。AvatarGroup
AvatarGroup.total宿主提供的头像总数,与渲染的子元素数量分开。AvatarGroup

可运行原生示例

下列机器键只选择示例入口,不会作为通用 variant 属性传入每个组件。原生示例 JSX 与数据保持上游原文;JGuang 编写的验收示例明确标注。

  • 图片头像 · ImageAvatars · 上游原生示例
  • 字母头像 · LetterAvatars · 上游原生示例
  • 字母头像 · BackgroundLetterAvatars · 上游原生示例
  • 多个尺寸 · SizeAvatars · 上游原生示例
  • 图标头像 · IconAvatars · 上游原生示例
  • 多种变体 · VariantAvatars · 上游原生示例
  • 降级显示 · FallbackAvatars · 上游原生示例
  • 分组 · GroupAvatars · 上游原生示例
  • 头像总数 · TotalAvatars · 上游原生示例
  • 自定义剩余数量 · CustomSurplusAvatars · 上游原生示例
  • 间距 · Spacing · 上游原生示例
  • 带徽章 · BadgeAvatars · 上游原生示例
  • 头像上传控件 · UploadAvatars · 上游原生示例

真实使用示例

固定版本的未修改原生示例;复制时同时保留所需的局部辅助文件。

import Avatar from '@mui/material/Avatar';
import Stack from '@mui/material/Stack';

export default function ImageAvatars() {
  return (
    <Stack direction="row" spacing={2}>
      <Avatar alt="Remy Sharp" src="/static/images/avatar/1.jpg" />
      <Avatar alt="Travis Howard" src="/static/images/avatar/2.jpg" />
      <Avatar alt="Cindy Baker" src="/static/images/avatar/3.jpg" />
    </Stack>
  );
}

示例依赖边界

@mui/material/Avatar, @mui/material/Stack, @mui/material/colors, @mui/icons-material/Folder, @mui/icons-material/Pageview, @mui/icons-material/Assignment, @mui/material/AvatarGroup, @mui/material/styles, @mui/material/Badge, @mui/icons-material/InsertDriveFile, react, @mui/material/ButtonBase

直接导入 Icons/System/Utils 的示例需要匹配 9.4.0 包。局部数据与辅助文件属于示例闭包,不是额外组件。远程图片和模拟操作不提供后端。

固定上游 API 元数据参考

以下表格保留上游元数据,不是重新推断的通用类型体系。条件泛型、继承属性与回调细节以以下源码声明为准。主题默认配置可能覆盖元数据默认值。

Avatar

import Avatar from '@mui/material/Avatar';
import { Avatar } from '@mui/material';
属性必需上游元数据类型/约束元数据默认值
alt否string—
children否node—
classes否object—
component否elementType—
sizes否string—
slotProps否{ fallback?: func | object, img?: func | object, root?: func | object }{}
slots否{ fallback?: elementType, img?: elementType, root?: elementType }{}
src否string—
srcSet否string—
sx否Array<func | object | bool> | func | object—
variant否'circular' | 'rounded' | 'square' | string'circular'

AvatarGroup

import AvatarGroup from '@mui/material/AvatarGroup';
import { AvatarGroup } from '@mui/material';
属性必需上游元数据类型/约束元数据默认值
children否node—
classes否object—
component否elementType—
max否number5
renderSurplus否func—
slotProps否{ surplus?: func | object }{}
slots否{ surplus?: elementType }{}
spacing否'medium' | 'small' | number'medium'
sx否Array<func | object | bool> | func | object—
total否numberchildren.length
variant否'circular' | 'rounded' | 'square' | string'circular'

Badge

import Badge from '@mui/material/Badge';
import { Badge } from '@mui/material';
属性必需上游元数据类型/约束元数据默认值
anchorOrigin否{ horizontal?: 'left' | 'right', vertical?: 'bottom' | 'top' }{ vertical: 'top', horizontal: 'right', }
badgeContent否node—
children否node—
classes否object—
color否'default' | 'primary' | 'secondary' | 'error' | 'info' | 'success' | 'warning' | string'default'
component否elementType—
invisible否boolfalse
max否number99
overlap否'circular' | 'rectangular''rectangular'
showZero否boolfalse
slotProps否{ badge?: func | object, root?: func | object }{}
slots否{ badge?: elementType, root?: elementType }{}
sx否Array<func | object | bool> | func | object—
variant否'dot' | 'standard' | string'standard'

原生源码声明

声明与 import 从固定源码提取,不平铺联合类型或外部继承。英文注释/类型属于原始源码参考,不代表上游正文已全文翻译。共享类型仍位于原生包中。

vendor/src/Avatar/Avatar.d.ts

import * as React from 'react';

import { SxProps } from '@mui/system';

import { OverridableStringUnion } from '@mui/types';

import { Theme } from '../styles';

import { OverridableComponent, OverrideProps } from '../OverridableComponent';

import { AvatarClasses } from './avatarClasses';

import { CreateSlotsAndSlotProps, SlotProps } from '../utils/types';

import { SvgIconProps } from '../SvgIcon';

export interface AvatarSlots {
  /**
   * The component that renders the root slot.
   * @default 'div'
   */
  root: React.ElementType;
  /**
   * The component that renders the img slot.
   * @default 'img'
   */
  img: React.ElementType;
  /**
   * The component that renders the fallback slot.
   * @default Person icon
   */
  fallback: React.ElementType;
}

export interface AvatarPropsVariantOverrides {}

export interface AvatarRootSlotPropsOverrides {}

export interface AvatarImgSlotPropsOverrides {}

export interface AvatarFallbackSlotPropsOverrides {}

export type AvatarSlotsAndSlotProps = CreateSlotsAndSlotProps<
  AvatarSlots,
  {
    /**
     * Props forwarded to the root slot.
     * By default, the available props are based on the div element.
     */
    root: SlotProps<'div', AvatarRootSlotPropsOverrides, AvatarOwnProps>;
    /**
     * Props forwarded to the img slot.
     * By default, the available props are based on the img element.
     */
    img: SlotProps<'img', AvatarImgSlotPropsOverrides, AvatarOwnProps>;
    /**
     * Props forwarded to the fallback slot.
     * By default, the available props are based on the [SvgIcon](https://mui.com/material-ui/api/svg-icon/#props) component.
     */
    fallback: SlotProps<
      React.ElementType<SvgIconProps>,
      AvatarFallbackSlotPropsOverrides,
      AvatarOwnProps
    >;
  }
>;

export interface AvatarOwnProps {
  /**
   * Used in combination with `src` or `srcSet` to
   * provide an alt attribute for the rendered `img` element.
   */
  alt?: string | undefined;
  /**
   * Used to render icon or text elements inside the Avatar if `src` is not set.
   * This can be an element, or just a string.
   */
  children?: React.ReactNode;
  /**
   * Override or extend the styles applied to the component.
   */
  classes?: Partial<AvatarClasses> | undefined;
  /**
   * The `sizes` attribute for the `img` element.
   */
  sizes?: string | undefined;
  /**
   * The `src` attribute for the `img` element.
   */
  src?: string | undefined;
  /**
   * The `srcSet` attribute for the `img` element.
   * Use this attribute for responsive image display.
   */
  srcSet?: string | undefined;
  /**
   * The system prop that allows defining system overrides as well as additional CSS styles.
   */
  sx?: SxProps<Theme> | undefined;
  /**
   * The shape of the avatar.
   * @default 'circular'
   */
  variant?:
    | OverridableStringUnion<'circular' | 'rounded' | 'square', AvatarPropsVariantOverrides>
    | undefined;
}

export interface AvatarTypeMap<
  AdditionalProps = {},
  RootComponent extends React.ElementType = 'div',
> {
  props: AdditionalProps & AvatarOwnProps & AvatarSlotsAndSlotProps;
  defaultComponent: RootComponent;
}

declare const Avatar: OverridableComponent<AvatarTypeMap>;

export type AvatarProps<
  RootComponent extends React.ElementType = AvatarTypeMap['defaultComponent'],
  AdditionalProps = {},
> = OverrideProps<AvatarTypeMap<AdditionalProps, RootComponent>, RootComponent> & {
  component?: React.ElementType | undefined;
};

vendor/src/AvatarGroup/AvatarGroup.d.ts

import * as React from 'react';

import {
  OverridableComponent,
  OverridableStringUnion,
  OverrideProps,
  PartiallyRequired,
} from '@mui/types';

import { SxProps } from '@mui/system';

import { Theme } from '../styles';

import { AvatarGroupClasses } from './avatarGroupClasses';

import Avatar from '../Avatar';

import { CreateSlotsAndSlotProps, SlotProps } from '../utils/types';

export interface AvatarGroupPropsVariantOverrides {}

export interface AvatarGroupComponentsPropsOverrides {}

export interface AvatarGroupSlots {
  surplus: React.ElementType;
}

export type AvatarGroupSlotsAndSlotProps = CreateSlotsAndSlotProps<
  AvatarGroupSlots,
  {
    surplus: SlotProps<
      React.ElementType<React.ComponentPropsWithRef<typeof Avatar>>,
      AvatarGroupComponentsPropsOverrides,
      AvatarGroupOwnerState
    >;
  }
>;

export interface AvatarGroupOwnProps extends AvatarGroupSlotsAndSlotProps {
  /**
   * The avatars to stack.
   */
  children?: React.ReactNode;
  /**
   * Override or extend the styles applied to the component.
   */
  classes?: Partial<AvatarGroupClasses> | undefined;
  /**
   * The component used for the root node.
   * Either a string to use a HTML element or a component.
   */
  component?: React.ElementType | undefined;
  /**
   * Max avatars to show before +x.
   * @default 5
   */
  max?: number | undefined;
  /**
   * custom renderer of extraAvatars
   * @param {number} surplus number of extra avatars
   * @returns {React.ReactNode} custom element to display
   */
  renderSurplus?: ((surplus: number) => React.ReactNode) | undefined;
  /**
   * Spacing between avatars.
   * @default 'medium'
   */
  spacing?: 'small' | 'medium' | number | undefined;
  /**
   * The system prop that allows defining system overrides as well as additional CSS styles.
   */
  sx?: SxProps<Theme> | undefined;
  /**
   * The total number of avatars. Used for calculating the number of extra avatars.
   * @default children.length
   */
  total?: number | undefined;
  /**
   * The variant to use.
   * @default 'circular'
   */
  variant?:
    | OverridableStringUnion<'circular' | 'rounded' | 'square', AvatarGroupPropsVariantOverrides>
    | undefined;
}

export interface AvatarGroupTypeMap<
  AdditionalProps = {},
  RootComponent extends React.ElementType = 'div',
> {
  props: AdditionalProps & AvatarGroupOwnProps;
  defaultComponent: RootComponent;
}

declare const AvatarGroup: OverridableComponent<AvatarGroupTypeMap>;

export type AvatarGroupProps<
  RootComponent extends React.ElementType = AvatarGroupTypeMap['defaultComponent'],
  AdditionalProps = {},
> = OverrideProps<AvatarGroupTypeMap<AdditionalProps, RootComponent>, RootComponent> & {
  component?: React.ElementType | undefined;
};

export interface AvatarGroupOwnerState extends PartiallyRequired<
  AvatarGroupProps,
  'max' | 'spacing' | 'component' | 'variant'
> {}

vendor/src/Badge/Badge.d.ts

import * as React from 'react';

import { SxProps } from '@mui/system';

import { OverridableStringUnion, Simplify } from '@mui/types';

import { CreateSlotsAndSlotProps, SlotProps } from '../utils/types';

import { Theme } from '../styles';

import { OverridableComponent, OverrideProps } from '../OverridableComponent';

import { BadgeClasses } from './badgeClasses';

export interface BadgePropsVariantOverrides {}

export interface BadgePropsColorOverrides {}

export interface BadgeRootSlotPropsOverrides {}

export interface BadgeBadgeSlotPropsOverrides {}

export interface BadgeSlots {
  /**
   * The component that renders the root.
   * @default span
   */
  root: React.ElementType;
  /**
   * The component that renders the badge.
   * @default span
   */
  badge: React.ElementType;
}

export type BadgeSlotsAndSlotProps = CreateSlotsAndSlotProps<
  BadgeSlots,
  {
    /**
     * Props forwarded to the root slot.
     * By default, the available props are based on the span element.
     */
    root: SlotProps<'span', BadgeRootSlotPropsOverrides, BadgeOwnerState>;
    /**
     * Props forwarded to the label slot.
     * By default, the available props are based on the span element.
     */
    badge: SlotProps<'span', BadgeBadgeSlotPropsOverrides, BadgeOwnerState>;
  }
>;

export type BadgeOwnerState = Simplify<
  Omit<BadgeOwnProps, 'slotProps' | 'slots'> & {
    badgeContent: React.ReactNode;
    invisible: boolean;
    max: number;
    displayValue: React.ReactNode;
    showZero: boolean;
    anchorOrigin: BadgeOrigin;
    color: OverridableStringUnion<
      'primary' | 'secondary' | 'default' | 'error' | 'info' | 'success' | 'warning',
      BadgePropsColorOverrides
    >;
    overlap: 'rectangular' | 'circular';
    variant: OverridableStringUnion<'standard' | 'dot', BadgePropsVariantOverrides>;
  }
>;

export interface BadgeOrigin {
  vertical?: 'top' | 'bottom' | undefined;
  horizontal?: 'left' | 'right' | undefined;
}

export interface BadgeOwnProps extends BadgeSlotsAndSlotProps {
  /**
   * The anchor of the badge.
   * @default {
   *   vertical: 'top',
   *   horizontal: 'right',
   * }
   */
  anchorOrigin?: BadgeOrigin | undefined;
  /**
   * The content rendered within the badge.
   */
  badgeContent?: React.ReactNode;
  /**
   * The badge will be added relative to this node.
   */
  children?: React.ReactNode;
  /**
   * Override or extend the styles applied to the component.
   */
  classes?: Partial<BadgeClasses> | undefined;
  /**
   * @ignore
   */
  className?: string | undefined;
  /**
   * The color of the component.
   * It supports both default and custom theme colors, which can be added as shown in the
   * [palette customization guide](https://mui.com/material-ui/customization/palette/#custom-colors).
   * @default 'default'
   */
  color?:
    | OverridableStringUnion<
        'primary' | 'secondary' | 'default' | 'error' | 'info' | 'success' | 'warning',
        BadgePropsColorOverrides
      >
    | undefined;
  /**
   * If `true`, the badge is invisible.
   * @default false
   */
  invisible?: boolean | undefined;
  /**
   * Max count to show.
   * @default 99
   */
  max?: number | undefined;
  /**
   * Wrapped shape the badge should overlap.
   * @default 'rectangular'
   */
  overlap?: 'rectangular' | 'circular' | undefined;
  /**
   * Controls whether the badge is hidden when `badgeContent` is zero.
   * @default false
   */
  showZero?: boolean | undefined;
  /**
   * The system prop that allows defining system overrides as well as additional CSS styles.
   */
  sx?: SxProps<Theme> | undefined;
  /**
   * The variant to use.
   * @default 'standard'
   */
  variant?: OverridableStringUnion<'standard' | 'dot', BadgePropsVariantOverrides> | undefined;
}

export interface BadgeTypeMap<
  RootComponent extends React.ElementType = 'span',
  AdditionalProps = {},
> {
  props: AdditionalProps & BadgeOwnProps;
  defaultComponent: RootComponent;
}

type BadgeRootProps = NonNullable<BadgeTypeMap['props']['slotProps']>['root'];

type BadgeBadgeProps = NonNullable<BadgeTypeMap['props']['slotProps']>['badge'];

export declare const BadgeRoot: React.FC<BadgeRootProps>;

export declare const BadgeMark: React.FC<BadgeBadgeProps>;

declare const Badge: OverridableComponent<BadgeTypeMap>;

export type BadgeProps<
  RootComponent extends React.ElementType = BadgeTypeMap['defaultComponent'],
  AdditionalProps = {},
> = OverrideProps<BadgeTypeMap<RootComponent, AdditionalProps>, RootComponent> & {
  component?: React.ElementType | undefined;
};

安装后验收

执行严格类型检查、生产构建和实际交互。检查键盘标签/焦点、原生支持的禁用/错误/只读语义、深浅色、窄屏和减少动画。按真实宿主数据检查回调,不把复制提示词当作安装完成。初始化工具还需检查服务端 HTML 与无告警水合。

来源与许可

https://github.com/mui/material-ui/tree/809a7717b4c050ba3f69b75300689f07c050a16e

MIT · @mui/material@9.4.0. 保留上游原始内容与许可证。上游完整原文保留于 vendor/docs,不标为全文中文化。