JGuang

面包屑

Breadcrumb · Ant Design

交互预览
npm install antd@6.6.5 @ant-design/icons@6.3.4 dayjs@1.11.23

面包屑

显示当前页面在系统层级结构中的位置,并能向上返回。

何时使用

  • 当系统拥有超过两级以上的层级结构时;
  • 当需要告知用户『你在哪里』时;
  • 当需要向上导航的功能时。

代码演示

  • 基本 · basic
  • 带有图标的 · withIcon
  • 带有参数的 · withParams
  • 分隔符 · separator
  • 带下拉菜单的面包屑 · overlay
  • 独立的分隔符 · separator-component
  • Debug Routes · debug-routes
  • 自定义语义结构的样式和类 · style-class

API

通用属性参考:通用属性

参数说明类型默认值版本全局配置
classNames用于自定义组件内部各语义化结构的 class,支持对象或函数Record<SemanticDOM, string> | (info: { props })=> Record<SemanticDOM, string>-6.0.06.0.0
dropdownIcon自定义下拉图标ReactNode<DownOutlined />6.2.06.2.0
items路由栈信息(>=5.3.0 推荐使用,旧版请使用 Breadcrumb.Item 子组件方式)ItemType[]-5.3.0×
itemRender自定义链接函数,和 react-router 配合使用,详见示例(route, params, routes, paths) => ReactNode-×
params路由的参数object-×
separator分隔符自定义ReactNode/6.0.0
styles用于自定义组件内部各语义化结构的行内 style,支持对象或函数Record<SemanticDOM, CSSProperties> | (info: { props })=> Record<SemanticDOM, CSSProperties>-6.0.06.0.0

ItemType

type ItemType = Omit<RouteItemType, 'title' | 'path'> | SeparatorType

RouteItemType

参数说明类型默认值版本
className自定义类名string-
dropdownProps弹出下拉菜单的自定义配置Dropdown-
href链接的目的地,不能和 path 共用string-
path拼接路径,每一层都会拼接前一个 path 信息。不能和 href 共用string-
menu菜单配置项MenuProps-4.24.0
onClick单击事件(e:MouseEvent) => void-
title名称ReactNode-5.3.0

SeparatorType

const item = {
  type: 'separator', // Must have
  separator: '/',
};
参数说明类型默认值版本
type标记为分隔符separator5.3.0
separator要显示的分隔符ReactNode/5.3.0

和 browserHistory 配合

和 react-router 一起使用时,默认生成的 url 路径是带有 # 的,如果和 browserHistory 一起使用的话,你可以使用 itemRender 属性定义面包屑链接。

import { Link } from 'react-router';

const items = [
  {
    path: '/index',
    title: 'home',
  },
  {
    path: '/first',
    title: 'first',
    children: [
      {
        path: '/general',
        title: 'General',
      },
      {
        path: '/layout',
        title: 'Layout',
      },
      {
        path: '/navigation',
        title: 'Navigation',
      },
    ],
  },
  {
    path: '/second',
    title: 'second',
  },
];

function itemRender(currentRoute, params, items, paths) {
  const isLast = currentRoute?.path === items[items.length - 1]?.path;

  return isLast ? (
    <span>{currentRoute.title}</span>
  ) : (
    <Link to={`/${paths.join('/')}`}>{currentRoute.title}</Link>
  );
}

return <Breadcrumb itemRender={itemRender} items={items} />;

Semantic DOM

语义节点原生说明版本
root根元素,包含文字颜色、字体大小、图标尺寸等基础样式,内部使用 flex 布局的有序列表6.0.0
itemItem 元素,包含文字颜色、链接的颜色变化、悬浮效果、内边距、圆角、高度、外边距等样式6.0.0
separator分隔符元素,包含分隔符的外边距和颜色样式6.0.0

语义说明来源: vendor/components/breadcrumb/demo/_semantic.tsx

主题变量(Design Token)

类型、继承关系和原生令牌声明保持固定源码原文;默认值由原生主题计算,并可能受 ConfigProvider 覆盖。本表不伪造静态默认值。

令牌类型原生说明
itemColorstring面包屑项文字颜色
iconFontSizenumber图标大小
linkColorstring链接文字颜色
linkHoverColorstring链接文字悬浮颜色
lastItemColorstring最后一项文字颜色
separatorMarginnumber分隔符外间距
separatorColorstring分隔符颜色

声明来源: vendor/components/breadcrumb/style/index.ts

import { unit } from '@ant-design/cssinjs';

import type { CSSObject } from '@ant-design/cssinjs';

import { genFocusStyle, resetComponent } from '../../style';

import type { FullToken, GenerateStyle, GetDefaultToken } from '../../theme/internal';

import { genStyleHooks, mergeToken } from '../../theme/internal';

export interface ComponentToken {
  /**
   * @desc 面包屑项文字颜色
   * @descEN Text color of Breadcrumb item
   */
  itemColor: string;
  /**
   * @desc 图标大小
   * @descEN Icon size
   */
  iconFontSize: number;
  /**
   * @desc 链接文字颜色
   * @descEN Text color of link
   */
  linkColor: string;
  /**
   * @desc 链接文字悬浮颜色
   * @descEN Color of hovered link
   */
  linkHoverColor: string;
  /**
   * @desc 最后一项文字颜色
   * @descEN Text color of the last item
   */
  lastItemColor: string;
  /**
   * @desc 分隔符外间距
   * @descEN Margin of separator
   */
  separatorMargin: number;
  /**
   * @desc 分隔符颜色
   * @descEN Color of separator
   */
  separatorColor: string;
}

interface BreadcrumbToken extends FullToken<'Breadcrumb'> {}

原生通用属性

Tips: 以下通用属性适用于 antd 大部分组件,不支持的组件会单独说明。

参数说明类型默认值
style自定义样式CSSProperties-
className自定义类名string-
rootClassName添加在组件最外层的 classNamestring-
autoFocus自动获取焦点,仅对表单类、链接、交互容器等可聚焦元素生效booleanfalse

安装并复现示例

npm install antd@6.6.5 @ant-design/icons@6.3.4 dayjs@1.11.23

使用 ConfigProvider 和原生 App,并单独配置 dayjs 语言。每个示例在查看代码中列出完整源码文件和额外固定依赖。示例名称用于选择演示,不是组件通用属性。主题、语言与 SSR 初始化见资源安装指南。

原生类型参考

源码查看器包含未修改的原生实现和以下声明文件,继承类型仍位于原生包中。代码中的英文注释保留为源码参考。

  • vendor/components/breadcrumb/Breadcrumb.tsx
  • vendor/components/breadcrumb/BreadcrumbContext.ts
  • vendor/components/breadcrumb/BreadcrumbItem.tsx
  • vendor/components/breadcrumb/BreadcrumbSeparator.tsx
  • vendor/components/breadcrumb/index.tsx
  • vendor/components/breadcrumb/style/index.ts
  • vendor/components/breadcrumb/useItemRender.tsx
  • vendor/components/breadcrumb/useItems.ts

来源与许可

https://github.com/ant-design/ant-design/tree/4a39f54842eade4e565ab336ef6097cd7e723cdd

保留原生中英文正文与可执行示例;官网专用的嵌入章节和令牌表格在本地展开。Ant Design 使用 MIT 许可,另列依赖的许可证也需保留。上传预览禁止向外发起请求,不提供后端服务。