目录
链接到标题并跟踪可见章节。
用法
传入标题 ID、可选正文 ID 或受控的活动 ID。使用原生片段链接或由使用方处理导航。
无障碍
保留章节 ID,标记当前位置,并允许移动端用户将焦点返回展开按钮或标题。
| 属性 | 类型 | 默认值 |
|---|---|---|
| headings | readonly TocHeading[] | Required |
| label | string | Required |
| contentId | string | Document body |
| offset | number | 0 |
| activeId | string | null | Observed |
| mobileLabel | string | None |
| onNavigate | function | Native anchor |
<>
<TableOfContents label="On this page" mobileLabel="Browse sections"
contentId="om-toc-demo-article"
headings={[
{ id: 'om-toc-demo-setup', label: 'Installation', level: 2 },
{ id: 'om-toc-demo-usage', label: 'Usage guide', level: 3 }
]}
onNavigate={(heading, event) => {
event.preventDefault();
document.getElementById(heading.id)?.scrollIntoView({ block: 'start' });
}}
/>
<article id="om-toc-demo-article">
<h3 id="om-toc-demo-setup" tabIndex={-1}>Installation</h3>
<p>Install the package.</p>
<h3 id="om-toc-demo-usage" tabIndex={-1}>Usage guide</h3>
<p>Learn how to use the library.</p>
</article>
</>