ContentToc
A table of contents that follows the heading being read, and collapses behind its title on small screens.
On this page
Examples
Scroll-spy
An IntersectionObserver in the browser marks the link of the heading being read. The right column of every component page is a ContentToc.
Introduction
A section with enough height for the next heading to be out of sight, so the table of contents has something to follow while you scroll.
Usage
A section with enough height for the next heading to be out of sight, so the table of contents has something to follow while you scroll.
Props
A section with enough height for the next heading to be out of sight, so the table of contents has something to follow while you scroll.
Theming
A section with enough height for the next heading to be out of sight, so the table of contents has something to follow while you scroll.
@* Scroll this article: the link of the heading being read is marked (aria-current="location"). The scroll-spy runs in
the browser with an IntersectionObserver, and the list collapses behind its title below the lg breakpoint. *@
<div class="grid w-full gap-8 lg:grid-cols-[minmax(0,1fr)_14rem]" data-testid="content-toc-example">
<article class="min-w-0 text-sm text-(--klyroo-text-muted)">
@foreach (var link in _links)
{
<section class="min-h-[45vh]">
<h3 id="@link.Id" class="scroll-mt-24 text-base font-semibold text-(--klyroo-text-highlighted)" data-testid="@("toc-heading-" + link.Id)">@link.Text</h3>
<p class="mt-2 max-w-prose">A section with enough height for the next heading to be out of sight, so the table of contents has something to follow while you scroll.</p>
</section>
}
</article>
<aside class="lg:sticky lg:top-24 lg:self-start">
<KlyContentToc Links="_links" Title="In this article" data-testid="content-toc-example-toc" />
</aside>
</div>
@code {
private static readonly KlyContentTocLink[] _links =
[
new("toc-intro", "Introduction"),
new("toc-usage", "Usage"),
new("toc-props", "Props") { Level = 3 },
new("toc-theme", "Theming"),
];
}API
KlyContentToc
Table of contents of a page that highlights the heading being read.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
Breakpoint |
KlyContent |
KlyContent |
Screen width from which the table of contents is a sticky list instead of a collapsible block ( KlyContentTocBreakpoint ). Values: Lg, Xl, Xxl. |
Class |
string |
Classes added to the root element, merged last so they win over the theme. | |
Collapsible |
bool |
true |
Collapses the list behind its title below Breakpoint , as a disclosure button. Above it, and without this, the list is always shown. |
Color |
KlyColor |
"primary" |
The color of the link of the heading being read. Values: primary, secondary, success, info, warning, error, neutral. |
Links (required) |
IReadOnlyList |
The headings of the page, in document order. Each Id must be the id of an element on the page. | |
Title |
string |
The title, and the accessible name of the navigation. Defaults to the built-in text. | |
Ui |
ContentTocUi |
Per-instance class overrides of the slots of the content toc, see ContentTocUi . They are merged after the theme and the Class parameter wins on the root slot. |
Ui slots
Ui takes a ContentTocUi with one class string per slot, merged after the theme.
| Slot | Styles |
|---|---|
Root |
Classes merged into the root slot of KlyContentToc , after the theme and the app configuration. |
Trigger |
Classes merged into the trigger slot of KlyContentToc , after the theme and the app configuration. |
Title |
Classes merged into the title slot of KlyContentToc , after the theme and the app configuration. |
Chevron |
Classes merged into the chevron slot of KlyContentToc , after the theme and the app configuration. |
Content |
Classes merged into the content slot of KlyContentToc , after the theme and the app configuration. |
List |
Classes merged into the list slot of KlyContentToc , after the theme and the app configuration. |
Item |
Classes merged into the item slot of KlyContentToc , after the theme and the app configuration. |
Link |
Classes merged into the link slot of KlyContentToc , after the theme and the app configuration. |