@fluid-topics/ftds-toc

中文文档
2.2.52发布于 2026/11/14ISCTypeScriptCommonJS近 30 天下载 1.9万

The design system table of contents (TOC) component

原始使用文档
文档目录5 节
  1. Install
  2. Usage
  3. Keyboard navigation
  4. Events
  5. Building the items from the page

Install

npm install @fluid-topics/ftds-toc
yarn add @fluid-topics/ftds-toc

Usage

The design system table of contents (TOC). It is a navigation component with two levels (section and sub-section) that links to anchors of a page.

The link matching the current page location is flagged with aria-current="page". It is updated on click and on hashchange, or can be forced with the current property.

It is sticky, and hidden on screens up to 1440px wide.

import { html } from "lit"
import "@fluid-topics/ftds-toc"

function render() {
    return html`
        <ftds-toc
            label="Content"
            .items=${ [
                { value: "general", label: "General", href: "#general" },
                {
                    value: "provider",
                    label: "Provider",
                    href: "#provider",
                    children: [
                        { value: "settings", label: "Settings", href: "#settings" },
                    ],
                },
            ] }
        ></ftds-toc>`
}

Keyboard navigation

It follows the APG treeview navigation pattern, without expand and collapse. Only one item is in the tab sequence (the current one, or the first).

KeyAction
ArrowDown / UpMoves the focus to the next / previous item
ArrowRightMoves the focus to the first child of the item
ArrowLeftMoves the focus to the parent of a child item
Home / EndMoves the focus to the first / last item
A letterMoves the focus to the next item starting with that letter
Space / EnterActivates the link

Events

item-selected (TocItemSelectedEvent, detail is the selected item) is dispatched when an item is clicked or activated with the keyboard. It is cancelable: calling preventDefault() on it cancels the native navigation of the link, so the host can scroll to the section itself.

Building the items from the page

Instead of writing the items by hand, FtTocController (from @fluid-topics/ft-wc-utils) can build them from the sections of a host component and track the section currently in view.

Mark each section of the host's light DOM with data-toc-label. Add data-toc-level="1" to make a sub-section of the previous section. An element without an id is given one. The sections are rescanned whenever the host's light DOM changes.

import { html } from "lit"
import { FtLitElement, FtTocController } from "@fluid-topics/ft-wc-utils"
import { TocItemSelectedEvent } from "@fluid-topics/ftds-toc"
import "@fluid-topics/ftds-toc"

class MyPage extends FtLitElement {
    private readonly toc = new FtTocController(this, {
        scrollContainer: () => this.renderRoot.querySelector("main"),
    })

    protected render() {
        return html`
            <ftds-toc label="Content"
                      .items=${ this.toc.items }
                      .current=${ this.toc.current }
                      @item-selected=${ this.onItemSelected }>
            </ftds-toc>
            <main>
                <slot></slot>
            </main>`
    }

    private onItemSelected(e: TocItemSelectedEvent) {
        if (this.toc.scrollTo(e.detail.value)) {
            e.preventDefault()
        }
    }
}
html`
    <my-page>
        <section data-toc-label="General">...</section>
        <section data-toc-label="Provider">...</section>
        <section data-toc-label="Settings" data-toc-level="1">...</section>
    </my-page>`
MemberDescription
scrollContainer (option)The element scrolling the sections.
isVisible (option)Returns whether the TOC is visible. The scroll is not tracked while it returns false. Defaults to always visible.
scrollOffset (option)Distance in pixels from the top of the container at which a section is reached. Defaults to 24.
scrollSettleDelay (option)Milliseconds without scroll event after which a scroll triggered by scrollTo is over. Defaults to 150.
itemsThe items to give to ftds-toc.
currentThe value of the section in view, to give to ftds-toc.
scrollTo(value)Scrolls to the section, makes it current and keeps it current until the scroll ends.
refresh()Rescans the sections.

ftds-page-layout does this for you with its withToc property.

版本与发布记录

@fluid-topics/ftds-toc 共发布 26 个版本,首次发布于 2026/10/15;过去 12 个月发布了 26 个版本。

版本发布日期距今
2.2.52latest2026/11/141 天前
2.2.512026/11/133 天前
2.2.502026/11/114 天前
2.2.492026/11/115 天前
2.2.482026/11/105 天前
2.2.472026/11/078 天前

关于 @fluid-topics/ftds-toc 的常见问题

@fluid-topics/ftds-toc 是什么?

The design system table of contents (TOC) component

如何安装 @fluid-topics/ftds-toc?

在项目目录运行 npm install @fluid-topics/ftds-toc,或使用 pnpm add / yarn add / bun add。

@fluid-topics/ftds-toc 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@fluid-topics/ftds-toc 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@fluid-topics/ftds-toc 可以商用吗?

@fluid-topics/ftds-toc 使用 ISC 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@fluid-topics/ftds-toc 的下载量有多少?

根据 npm 官方下载统计,@fluid-topics/ftds-toc 在 2026/10/15 – 2026/11/13 这 30 天内被下载 18,708 次,日均约 624 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@fluid-topics/ftds-toc 还在维护吗?

最新版本 2.2.52 发布于 2026/11/14(1 天前);至今共发布 26 个版本,其中过去 12 个月发布了 26 个;首次发布于 2026/10/15。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。