Table of Contents

The sections of a page, as links beside it: listed from its headings or written by you, the section read marked as it scrolls.

NavigationExperimental

Usage

import { Component, ViewEncapsulation } from '@angular/core';import {  ScToc,  ScTocHeading,  ScTocLink,  ScTocList,} from '@semantic-components/ui';
@Component({  selector: 'app-toc-usage-demo',  imports: [ScToc, ScTocHeading, ScTocLink, ScTocList],  template: `    <div class="mx-auto flex max-w-3xl gap-10 p-6">      <article class="min-w-0 flex-1">        @for (section of sections; track section) {          <h2 scTocHeading class="text-lg font-semibold">{{ section }}</h2>          <p class="text-muted-foreground mt-2 mb-8 min-h-72">            The page's headings, listed beside it. The link of the section you            read is marked as you scroll.          </p>        }      </article>      <nav        scToc        #toc="scToc"        aria-label="On this page"        class="sticky top-6 hidden w-40 self-start sm:block"      >        <p class="mb-3 text-sm font-medium">On this page</p>        <ul scTocList>          @for (item of toc.items(); track item.id) {            <li>              <a [scTocLink]="item.id" [depth]="item.depth">{{ item.text }}</a>            </li>          }        </ul>      </nav>    </div>  `,  encapsulation: ViewEncapsulation.None,})export class TocUsageDemo {  private readonly sections = [    'Introduction',    'Installation',    'Usage',    'Examples',    'API reference',  ];}

Anatomy

Every part is a directive on your element: the headings in your content, a nav named by you, a list and its links. The nav lists the headings (items()); links written by hand need no headings part. A scope is only for a table of contents apart from the page's.

import {  ScToc, ScTocList, ScTocLink, ScTocHeading, ScTocScope,} from '@semantic-components/ui';
<h2 scTocHeading>…</h2>
<nav scToc #toc="scToc" aria-label="…">  <ul scTocList>    @for (item of toc.items(); track item.id) {      <li>        <a [scTocLink]="item.id" [depth]="item.depth">{{ item.text }}</a>      </li>    }  </ul></nav>

Notes

  • The section read is the last one whose heading reached the top of the page — or of the box that scrolls it — and the last one, scrolled to the end. A heading's scroll margin is where it counts as reached: under a sticky header, the header's height (scroll-mt-20).
  • Its link is aria-current="location": a place in this page, not another page.
  • A link followed scrolls to its section and moves the focus to its heading, for the keyboard and screen readers. In a box, the box scrolls, not the page.
  • A link's address is this page at the section, not #id alone (which <base href="/"> makes the home page's): a new tab or a copied link opens there.
  • Listed from the headings, the list is filled in the browser: a heading's text is read once rendered. Links written by hand are rendered on the server too.
  • A list taller than its place scrolls (overflow-y-auto on the nav) and keeps the active link in view.

Examples

Basic

Getting started

Install the package, then import the parts you need. Every part is a directive on your own element: the markup and the text stay yours.

Headings

Mark the headings to list. Each gets an id from its text, unique on the page, unless it has one of its own.

Links

The links go to the sections: their address is this page at the section, so a new tab or a copied link opens there too.

Scrolling

As the box scrolls, the link of the section read is marked: the last heading to reach the top, or the last one at the end.

Focus

A link followed moves the focus to its heading, for the keyboard and screen readers: the next Tab goes on from there.

import { Component, ViewEncapsulation } from '@angular/core';import {  ScFrame,  ScToc,  ScTocHeading,  ScTocLink,  ScTocList,  ScTocScope,} from '@semantic-components/ui';

Levels

Install

Add the package to your app with your package manager.

npm

One command, then import what you use.

pnpm

The same package, the same imports.

Configure

Choose a style and a theme for the whole app.

Presets

Eight styles, from the most compact to the roundest.

Themes

Light, dark, or the system's, with your colours.

import { Component, ViewEncapsulation } from '@angular/core';import {  ScFrame,  ScToc,  ScTocHeading,  ScTocLink,  ScTocList,  ScTocScope,} from '@semantic-components/ui';

Hand-written links

Added

A table of contents, from the headings or from your links.

Changed

The docs pages list their sections with it.

Fixed

The last sections are marked too, scrolled to the end.

import { Component, ViewEncapsulation } from '@angular/core';import { ScFrame, ScToc, ScTocLink, ScTocList } from '@semantic-components/ui';
/** * Links written by hand, to your sections' ids: no headings to read, so the * list is rendered on the server too. */@Component({  selector: 'app-links-toc-demo',  imports: [ScFrame, ScToc, ScTocLink, ScTocList],

RTL

التثبيت

أضف الحزمة إلى تطبيقك باستخدام مدير الحزم.

npm

أمر واحد، ثم استورد ما تستخدمه.

الإعداد

اختر نمطًا وسمة للتطبيق كله.

الأنماط

ثمانية أنماط، من الأكثر إحكامًا إلى الأكثر استدارة.

السمات

فاتحة أو داكنة أو حسب النظام، بألوانك.

import { Dir } from '@angular/cdk/bidi';import { Component, LOCALE_ID, ViewEncapsulation } from '@angular/core';import {  ScFrame,  ScToc,  ScTocHeading,  ScTocLink,  ScTocList,  ScTocScope,} from '@semantic-components/ui';

API reference

Each part takes class, merged over its own (cn: a later class wins).

PartSelectorDescription
ScTocnav[scToc] The navigation, named by you (aria-label): lists the headings and marks the section read.
ScTocListul[scTocList], ol[scTocList] The list of links.
ScTocLinka[scTocLink] A link to a section, by its id; aria-current="location" when it is the one read.
ScTocHeading[scTocHeading] A heading listed: your id, or one from its text; its scroll margin is where it counts as reached.
ScTocScope[scTocScope] A table of contents of its own: the headings and the nav inside, apart from the page’s.

ScToc

Exported as scToc.

MemberTypeDescription
items()ScTocItem[] The headings in document order: id, text, level, and depth under the shallowest.
activeId()string The id of the section read; empty on the server.
goTo(id)boolean Goes to the section: scrolled there, focus on its heading, its link marked.

ScTocLink

InputTypeDefaultDescription
scTocLinkstring— The id of the section it goes to (required).
depthnumber0 Indented by it: an item’s depth (up to 3).