Breadcrumb
通过链接、路由参数和菜单通过部分层次结构的路径。
何时使用
链条和链节
结构由 items 设置。标题可以是文本或 Svelte 片段; href 创建一个真正的链接。最后一段通常保留为文本以显示当前页面。
| 脚本 | 机会 | 示例 |
|---|---|---|
| 正常的层次结构 | items、href、title |
节链 |
| 文本旁边的图标 | title 中的片段 |
图标 |
| 路径选项 | path、params |
选项 |
| 路由器的转换 | itemRender |
自定义渲染 |
| 替代子页面 | item.menu |
项目菜单 |
path 收集哈希地址,例如#/workspace/project。对于 SvelteKit 路由,请勿将 href 替换为它。对于常规寻址,设置 href;对于特殊的路由器合约,请使用 itemRender 和自定义 a.
itemRender - 带有参数 item、params、所有 items 和累积路径的片段。返回的标记负责地址、处理程序和可用名称。最后一个元素可以通过它在传递的数组中的位置来识别。
分隔符和子菜单
separator 指定通用分隔符并接受文本或片段。 type:separator 的项目输出一个单独的分隔符。如果手动指定整个分隔符序列,请将公共 separator 设置为空字符串:否则它将继续出现在常规子句之间。
父项的菜单通过 menu.items 指定。在当前适配器中,子级的 path 将添加到父级的 href 中。尽管 BreadcrumbMenuItem 类型包含 href,但此适配器不使用它。菜单示例有意传递 path;不要指望此类项目中有独立的 href。
DropdownProps 允许您配置支持的 Dropdown 属性,dropdownIcon 允许您替换菜单指示器。 dropdownIcon 和 title 有片段变体;装饰图标不得与可用链接名称重复。
语义和设计
根包含 nav 和有序列表。该组件不会分配 aria-current 本身。传递最后一个项目 'aria-current': 'page' 或向 itemRender 添加属性。保留父项的链接语义:没有 href 的 onclick 处理程序不会替换导航地址。
classNames/styles 与 root、item 和 separator 插槽一起使用,包括来自 props 的函数。 Svelte styles 包含 CSS 字符串。常规 separator、dropdownIcon 和语义默认值由 ConfigProvider.breadcrumb 设置。 Breadcrumb 的组件设计令牌(design tokens)独立于其他示例来控制颜色、字体和间距。
prefixCls 指定路径、子句、链接和分隔符的完整本地前缀。空字符串和 undefined 使用带有 -breadcrumb 后缀的 ConfigProvider.prefixCls。type:separator 分隔符继承相同的所有者前缀。语义回调获取原始本地 props.prefixCls; theme.cssVar.prefix 独立设置主题变量的命名空间。 前缀示例 显示具有不同边距、RTL 的相邻区域,并在切换时保持菜单打开。
下拉菜单和菜单使用自己的样式:本地 Breadcrumb.prefixCls 不会重命名弹出类。在 Ant Design 6.6.8 中,具有本地前缀的分隔符可能会保留提供者前缀并丢失侧边填充。 Svelte 为它们提供了所有者的前缀;这是与标准行为的有意识的区别。
示例
面包屑
这些链接指向网站的现有部分。最后一项表示当前位置。
<script lang="ts">
import { Breadcrumb } from '@woodger/antd';
</script>
<Breadcrumb aria-label={("基本路径")} items={([
{ key: 'home', href: '/', title: "家" },
{ key: 'components', href: '/ant-design', title: "组件" },
{ key: 'current', title: 'Breadcrumb', 'aria-current': 'page' }
])} />
以点为单位的图标
title 接受片段。装饰性 SVG 从可访问的树中隐藏;链接标签保留文本。
<script lang="ts">
import { Breadcrumb } from '@woodger/antd';
</script>
<style>
.crumb {
display: inline-flex;
align-items: center;
gap: 6px;
}
</style>
{#snippet home()}
<span class="crumb"><svg aria-hidden="true" width="14" height="14" viewBox="0 0 24 24"
fill="none" stroke="currentColor" stroke-width="1.8">
<path d="m3 10 9-7 9 7v11h-7v-7h-4v7H3Z" />
</svg><span>{("家")}</span></span>
{/snippet}
{#snippet components()}
<span class="crumb"><svg aria-hidden="true" width="14" height="14" viewBox="0 0 24 24"
fill="none" stroke="currentColor" stroke-width="1.8">
<path d="M3 3h7v7H3Zm11 0h7v7h-7ZM3 14h7v7H3Zm11 0h7v7h-7Z" />
</svg><span>{("组件")}</span></span>
{/snippet}
<Breadcrumb aria-label={("带图标的路径")} items={([
{ key: 'home', href: '/', title: home },
{ key: 'components', href: '/ant-design', title: components },
{ key: 'current', title: "文档" }
])} />
路线选择
params 将值替换为 title 和 path。每个点的路径都是从之前的段中收集的。
<script lang="ts">
import { Breadcrumb } from '@woodger/antd';
let project = $state('svelte');
</script>
<style>
label {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 8px;
margin-bottom: 16px;
}
input {
min-width: 0;
max-width: 100%;
box-sizing: border-box;
}
</style>
<label>{("项目名称 面包屑 ")}<input bind:value={project} /></label>
<Breadcrumb aria-label={("带参数的路径")} params={{ project }} items={([
{ key: 'workspace', path: 'workspace', title: "项目" },
{ key: 'project', path: ':project', title: ':project' },
{ key: 'docs', path: 'docs', title: "文档" }
])} />
公共分隔符
separator 接受字符串或片段。空字符串删除公共分隔符。
<script lang="ts">
import { Breadcrumb } from '@woodger/antd';
let separator = $state('›');
</script>
<style>
label {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 8px;
margin-bottom: 16px;
}
</style>
<label>{("面包屑分离器 ")}<select bind:value={separator}>
<option value="›">›</option><option value="/">/</option>
<option value="→">→</option><option value="">{("不带分隔符")}</option>
</select></label>
<Breadcrumb aria-label={("分隔路径")} {separator} items={([
{ key: 'workspace', title: "项目" },
{ key: 'library', title: "图书馆" },
{ key: 'current', title: 'API' }
])} />
<script lang="ts">
import { Breadcrumb, Checkbox } from '@woodger/antd';
let hideIcon = $state(false);
let selected = $state(("未选择部分"));
</script>
<style>
.controls { margin-bottom: 16px; }
output {
display: block;
margin-top: 16px;
}
</style>
<div class="controls">
<Checkbox bind:checked={hideIcon}>
{("隐藏面包屑菜单图标")}
</Checkbox>
</div>
<Breadcrumb
aria-label={("菜单路径")}
dropdownIcon={hideIcon ? false : undefined}
items={([
{ key: 'home', path: 'workspace', title: "项目" },
{
key: 'project', path: 'svelte', title: 'Svelte',
menu: {
items: [
{ key: 'components', path: '/components', title: "组件" },
{ key: 'themes', path: '/themes', title: "主题" },
{ key: 'archive', title: "档案", disabled: true }
],
onClick: info => { selected = String(info.key); }
},
dropdownProps: { trigger: 'click', 'aria-label': "面包屑导航的相邻部分" }
},
{ key: 'current', title: "概览" }
])}
/>
<output aria-label={("选定的面包屑部分")}>{selected}</output>
单独的分隔符
type=separator 子句允许您混合 separator。通用分隔符被禁用。
<script lang="ts">
import { Breadcrumb } from '@woodger/antd';
</script>
{#snippet arrow()}<span aria-hidden="true">→</span>{/snippet}
<Breadcrumb
aria-label={("单独分隔的路径")}
separator=""
items={([
{ key: 'home', title: "目录" },
{ key: 'first-divider', type: 'separator', separator: ':' },
{ key: 'project', title: 'Svelte' },
{ key: 'second-divider', type: 'separator', separator: arrow },
{ key: 'current', title: "组件" }
])}
/>
您的 itemRender
片段接收 item、params、路线和累积路径。应用程序本身选择祖先的链接和当前项目的文本。
<script lang="ts">
import { Breadcrumb, type BreadcrumbItem } from '@woodger/antd';
const items: readonly BreadcrumbItem[] = [
{ key: 'home', path: 'workspace', title: "项目" },
{ key: 'project', path: ':project', title: "项目" },
{ key: 'current', path: 'docs', title: "文档" }
];
</script>
{#snippet itemRender(
item: BreadcrumbItem,
params: Record<string, unknown>,
routes: readonly BreadcrumbItem[],
paths: readonly string[]
)}
{#if item === routes.at(-1)}
<span
aria-current="page"
>{(typeof item.title === 'string' ? item.title : '')}</span>
{:else}
<a href={(`#/${paths.join('/')}`)}>
{(item.key === 'project' ? String(params.project) : String(item.title ?? ''))}
</a>
{/if}
{/snippet}
<Breadcrumb
aria-label={("渲染器的路径")}
items={(items)}
params={{ project: 'antd-svelte' }}
{itemRender}
/>
Semantic styles
classNames 对象和 styles 函数设置 root、item 和 separator 的样式。局部RTL显示方向继承。
<script lang="ts">
import { Breadcrumb, Checkbox, ConfigProvider } from '@woodger/antd';
let rtl = $state(false);
</script>
<style>
.controls { margin-bottom: 16px; }
</style>
<div class="controls"><Checkbox bind:checked={rtl}>RTL Breadcrumb</Checkbox></div>
<ConfigProvider direction={rtl ? 'rtl' : 'ltr'}>
<Breadcrumb aria-label={("装饰路径")} separator="›"
classNames={{ root: 'semantic-breadcrumb-root', item: 'semantic-breadcrumb-item' }}
styles={({ props }) => ({
root: `
padding: 12px;
border: 1px solid var(--ant-color-primary-border);
border-radius: 8px;
`,
item: 'font-weight:600',
separator: props.separator === '›' ? 'color:var(--ant-color-success)' : ''
})}
items={([
{ key: 'home', title: "项目" },
{ key: 'project', title: "图书馆" },
{ key: 'current', title: 'API' }
])}
/>
</ConfigProvider>
组件代币
本地主题更改项目颜色和分隔符间距。数值控制更新 separatorMargin 而不重新加载。
<script lang="ts">
import { Breadcrumb, ConfigProvider, theme } from '@woodger/antd';
const tokens = theme.useToken();
let margin = $state(12);
</script>
<style>
label {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 8px;
margin-bottom: 16px;
}
</style>
<label>{("分隔符间隔 ")}<input
type="range"
min="4"
max="20"
bind:value={margin}
/>
<output>{margin}px</output>
</label>
<ConfigProvider theme={{
cssVar: {}, components: {
Breadcrumb: {
separatorMargin: margin,
itemColor: tokens.token.colorTextSecondary,
lastItemColor: tokens.token.colorSuccess,
separatorColor: tokens.token.colorWarning
}
}
}}>
<Breadcrumb aria-label={("具有您自己主题的路径")} items={([
{ key: 'home', title: "目录" },
{ key: 'current', title: "组件" }
])} />
</ConfigProvider>
相同的本地类与不同的分隔符缩进一起使用。更改前缀会保留打开的菜单和链接;下拉列表是独立处理的。
路径前缀和分隔符
通用和本地 prefixCls、独立邻居区域和RTL。切换时打开的菜单和缩进将被保留。
<script lang="ts">
import { Breadcrumb, Checkbox, ConfigProvider, type BreadcrumbItem } from '@woodger/antd';
let custom = $state(true);
let local = $state(true);
let rtl = $state(false);
let visits = $state(0);
let selected = $state(("未选择部分"));
const prefix = $derived(custom ? 'suite' : 'ant');
const items: BreadcrumbItem[] = [
{
key: 'catalog', title: "目录", href: '#breadcrumb-prefix-destination',
onClick: () => { visits += 1; }
},
{
key: 'sections', title: "部分", href: '#breadcrumb-prefix-destination',
dropdownProps: { trigger: 'click', 'aria-label': "带前缀的面包屑部分" },
menu: {
items: [
{ key: 'guide', title: "管理" },
{ key: 'api', title: "API参考" }
],
onClick: info => { selected = String(info.key); }
}
},
{ key: 'current', title: "概览", 'aria-current': 'page' }
];
</script>
<style>
.example {
display: grid;
gap: 20px;
min-width: 0;
}
.controls {
display: flex;
flex-wrap: wrap;
gap: 12px;
}
p { margin: 0; }
output { overflow-wrap: anywhere; }
</style>
<div class="example">
<div class="controls">
<Checkbox bind:checked={custom}>{("通用前缀面包屑套件")}</Checkbox>
<Checkbox bind:checked={local}>{("面包屑本地前缀")}</Checkbox>
<Checkbox bind:checked={rtl}>{("带前缀的 RTL 面包屑导航")}</Checkbox>
</div>
<ConfigProvider prefixCls={prefix} direction={rtl ? 'rtl' : 'ltr'}
theme={{
cssVar: { prefix: 'breadcrumb-theme' }, components: {
Breadcrumb: {
separatorMargin: 12
}
}
}}>
<Breadcrumb items={(items)} prefixCls={local ? 'project-breadcrumb' : ''}
class="prefix-breadcrumb-main" aria-label={("带前缀的路径")}
styles={({ props }) => ({
root: `outline: 1px ${props.prefixCls ? 'solid' : 'dashed'} var(--ant-color-primary-border)`
})} />
</ConfigProvider>
<ConfigProvider prefixCls="neighbor"
theme={{ cssVar: {}, components: { Breadcrumb: { separatorMargin: 6 } } }}>
<Breadcrumb
prefixCls={local ? 'project-breadcrumb' : ''}
separator=""
class="prefix-breadcrumb-neighbor"
aria-label={("带前缀的相邻路径")}
items={([
{ key: 'catalog', title: "目录" },
{ key: 'separator', type: 'separator', separator: '›' },
{ key: 'current', title: 'Svelte', 'aria-current': 'page' }
])}
/>
</ConfigProvider>
<output id="breadcrumb-prefix-destination" aria-label={("带前缀的面包屑状态")}>{("\n 一般的: ")}{prefix}{(";班级: ")}{(local ? 'project-breadcrumb' : prefix + '-breadcrumb')}{(";过渡: ")}{visits}{(";章: ")}{selected}
</output>
<p>
{("相同的本地类与不同的分隔符缩进一起使用。更改前缀会保留打开的菜单和链接;下拉列表是独立处理的。")}
</p>
</div>
项目路径
保存了带有项目参数和真实菜单链接的查看示例。
<script lang="ts">
import { Breadcrumb } from '@woodger/antd';
let project = $state('svelte');
</script>
<style>
label {
display: flex;
align-items: center;
flex-wrap: wrap;
gap: 8px;
margin-bottom: 16px;
}
</style>
<label>{("项目 ")}<input bind:value={project} /></label>
<Breadcrumb
aria-label={("项目路径")}
params={{ project }}
items={([
{ key: 'home', path: 'workspace', title: "工作空间" },
{
key: 'project',
path: ':project',
title: ':project',
menu: {
items: [
{ key: 'components', path: '/components', title: "组件" },
{ key: 'themes', path: '/themes', title: "主题" }
]
},
dropdownProps: { trigger: 'click', 'aria-label': "项目部分" }
},
{ key: 'current', title: "图书馆" }
])}
/>
API
这些示例是根据当前的 ant.design 页面和已安装包的公共 API 进行检查的。 Props 的完整定义位于 @woodger/antd 中。
| 主题 | 示例或限制 |
|---|---|
| Basic Usage | 祖先链接和当前项目。 |
| With an Icon | 使用 SVG 的公共 title 片段。 |
| With Params | 替换 params 和累积 path。 |
| Configuring the Separator | 常用分离器和断开器。 |
| Bread crumbs with drop down menu | 相邻部分的菜单和键盘。 |
| Configuring the Separator Independently | 单独的 separator items。 |
| Custom semantic dom styling | 类和函数形式的 styles。 |
| CSS prefix | 链接前缀、常规分隔符和单独分隔符;保存打开的菜单。 |
| 属性 | 描述 | 类型 | 默认 |
|---|---|---|---|
prefixCls | 路径、子句和分隔符的完整本地 CSS 前缀。空字符串和 undefined 使用提供者前缀。 | string | ConfigProvider.prefixCls + "-breadcrumb" |
items | 子句和单个分隔符的现代描述。 | readonly BreadcrumbItem[] | — |
params | 将参数替换为字符串 title 和 path。 | Record<string, unknown> | {} |
separator | 文本、数字或片段;空行删除公共分隔符。 | BreadcrumbContent | / |
dropdownIcon | 通用菜单图标。 false 或空行隐藏它; null/undefined 使用标准的。 | BreadcrumbContent | 向下图标 |
itemRender | 完全设置项目的内容。 Links 和 aria-current 创建应用程序渲染器。 | Snippet<[item, params, routes, paths]> | — |
classNames | root 类、item 类和 separator 类。 | BreadcrumbSlots | (info) => BreadcrumbSlots | — |
styles | CSS 行语义槽;函数接收道具。 | BreadcrumbSlots | (info) => BreadcrumbSlots | — |
class | 外部 nav 类; rootClassName 添加到同一节点。 | string | — |
nativeElement | bind:nativeElement 在挂载后返回 nav。 | HTMLElement | — |
项目参数
BreadcrumbItem 支持 key、title、href 或 path、menu、dropdownProps、onClick、class/style 和 aria/data 属性。对于单独的分隔符,请指定 type:separator 和 separator。 menu.items 接受 title/label、path、disabled、danger 和 onClick;title 可以是片段。
Semantic DOM
根是 nav 和 ol 列表。每个项目创建一个 li;链接使用a,没有 href 的操作使用 button,常规标题使用 span。使用 aria-hidden 隐藏分隔符。可用的导航名称由本机 aria-label 设置。
Semantic slots: root, item, separator。单个项目的 class/style 是指其内容; styles.item - 用于 li。 item 中的 aria-* 和 data-* 被传递到内容。 ConfigProvider.breadcrumb 设置常规 separator、dropdownIcon、class/style 和语义默认值。
prefixCls 还适用于常规和单独的分隔符、链接和菜单项触发器。语义回调接收原始本地 props.prefixCls。命名空间 theme.cssVar.prefix 单独设置;弹出设计仍然是下拉菜单和菜单。
该菜单使用公共下拉菜单及其焦点管理。原生链接保留 href 和常规导航; itemRender 负责其语义。
注册令牌
七个组件令牌通过 ConfigProvider theme.components.Breadcrumb。卡片的本地主题与邻近的示例相隔离;共享令牌遵循部分设置。
| 代币 | 目的 |
|---|---|
iconFontSize | 图标大小。 |
itemColor | 常规物品颜色。 |
lastItemColor | 最后一个项目的颜色。 |
linkColor | 链接颜色。 |
linkHoverColor | 链接悬停颜色。 |
separatorColor | 分隔符颜色。 |
separatorMargin | 每侧都有分隔符缩进。 |
| 代币 | 当前值 |
|---|---|
colorBgTextHover | rgba(0,0,0,0.06) |
colorPrimaryBorder | #adc6ff |
colorText | rgba(0,0,0,0.88) |
borderRadiusSM | 4 |
fontFamily | -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial,
'Noto Sans', sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol',
'Noto Color Emoji' |
fontSize | 14 |
fontSizeIcon | 12 |
lineHeight | 1.5714285714285714 |
lineWidthFocus | 3 |
marginXXS | 4 |
motionDurationMid | 0.2s |
paddingXXS | 4 |
FAQ
是否需要 React Router?
不需要。对于常规链接,href 就足够了。 itemRender 允许您在不使用 React API 的情况下嵌入应用程序路由。
如何指示当前页面?
将最后一段保留为文本并设置 aria-current:page。该属性不会自动出现。
额外的分隔符从哪里来?
通用 separator 对于各个分隔符项目保持启用状态。对于完全手动排序,请传递空字符串。
为什么子菜单项的 href 不起作用?
在当前适配器中,菜单链接是由父级的 href 和子级的 path 组装而成的。使用示例中所示的结构。