# 徽章

- 页面: https://jiguang132.com/zh/resources/material-ui/docs/badges
- 资源: Material UI (https://jiguang132.com/zh/resources/material-ui.md)
- 全部指南: https://jiguang132.com/zh/resources/material-ui/docs.md

在其他元素旁附加计数或小型状态标记。

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

## 使用场景与业务边界

数量由宿主提供；徽章改变含义时还需可访问的文字说明。

## 原生组成与范围

`Badge`

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

## 原生安装与初始化

```bash
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 结构、可空值与泛型仍按固定源码保留。

| 所属组件.属性 | 用途说明 | 声明来源 |
| --- | --- | --- |
| `Badge.badgeContent` | 宿主提供的徽章内容，不是自动获取的未读数量。 | `Badge` |
| `Badge.max` | Badge 显示数量上限或 Slider 最大值，区分所属组件和单位。 | `Badge` |
| `Badge.invisible` | 隐藏徽章视觉，不改变宿主数据。 | `Badge` |
| `Badge.variant` | 所属组件的原生视觉枚举，不可替换为通用 variant 属性。 | `Badge` |
| `Badge.anchorOrigin` | 原生垂直／水平方向的锚点组合。 | `Badge` |

## 可运行原生示例

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

- 使用规范 · `BadgeIntro` · 上游原生示例
- 使用规范 · `BadgeListItem` · 上游原生示例
- 徽章内容 · `SimpleBadge` · 上游原生示例
- 圆点徽章 · `DotBadge` · 上游原生示例
- 可见性 · `BadgeVisibility` · 上游原生示例
- 可见性 · `ShowZeroBadge` · 上游原生示例
- 最大显示数值 · `BadgeMax` · 上游原生示例
- 颜色 · `ColorBadge` · 上游原生示例
- 徽章重叠方式 · `BadgeOverlap` · 上游原生示例
- 自定义样式 · `CustomizedBadges` · 上游原生示例

## 尚未支持的额外示例

这些条目仍保留在上游清单中，不静默跳过，也不展示为已完成预览。需要补充依赖／框架验收后再纳入可运行列表。

- 徽章对齐 · `BadgeAlignment`: `@mui/internal-core-docs/HighlightedCode`

## 真实使用示例

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

```tsx
import IconButton from '@mui/material/IconButton';
import Badge from '@mui/material/Badge';
import MailIcon from '@mui/icons-material/Mail';

const maxVisibleNotifications = 99;
const unreadNotificationsCount = 100;

function getUnreadNotificationsLabel(count: number) {
  if (count === 0) {
    return 'show no unread notifications';
  }
  if (count > maxVisibleNotifications) {
    return `show more than ${maxVisibleNotifications} unread notifications`;
  }
  return `show ${count} unread notification${count === 1 ? '' : 's'}`;
}

export default function BadgeIntro() {
  const label = getUnreadNotificationsLabel(unreadNotificationsCount);

  return (
    <IconButton aria-label={label}>
      <Badge
        badgeContent={unreadNotificationsCount}
        color="secondary"
        max={maxVisibleNotifications}
      >
        <MailIcon />
      </Badge>
    </IconButton>
  );
}

```

## 示例依赖边界

`@mui/material/IconButton`, `@mui/material/Badge`, `@mui/icons-material/Mail`, `@mui/material/List`, `@mui/material/ListItemButton`, `@mui/material/ListItemIcon`, `@mui/material/ListItemText`, `@mui/material/Paper`, `@mui/icons-material/Drafts`, `@mui/icons-material/Inbox`, `@mui/icons-material/Send`, `@mui/icons-material/Notifications`, `react`, `@mui/material/Stack`, `@mui/material/Switch`, `@mui/material/FormControlLabel`, `@mui/icons-material/CalendarMonth`, `@mui/icons-material/FolderOpen`, `@mui/icons-material/ReportProblemOutlined`, `@mui/material/Box`, `@mui/material/Avatar`, `@mui/material/ListItemAvatar`, `@mui/material/styles`

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

## 固定上游 API 元数据参考

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

### Badge

```tsx
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` | 否 | `bool` | `false` |
| `max` | 否 | `number` | `99` |
| `overlap` | 否 | `'circular' \| 'rectangular'` | `'rectangular'` |
| `showZero` | 否 | `bool` | `false` |
| `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/Badge/Badge.d.ts

```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，不标为全文中文化。

---

JGuang 全部资源索引: https://jiguang132.com/llms.txt
