Vitral 0.2.18
Menu

TableOfContents

The outline of a page, read from its headings, with the section being read marked as the reader scrolls — the “On this page” beside long documents.

Import

main.ts
import { TableOfContents } from '@vitral/vue';

Read from the headings

`source` is where to look and `selector` which headings to take; an h3 sits under the h2 before it. Scroll the page: the link of the section at the top of the view is marked, and `offset` is the height of a sticky header that covers it.

Install

A few paragraphs stand in for the section's text. The outline beside it follows as the page scrolls past each heading.

Headings keep their own ids; the outline links to them, so a link to a section still works when shared.

With a package manager

A subsection, drawn one step in.

From a CDN

A subsection, drawn one step in.

Pick a theme

A few paragraphs stand in for the section's text. The outline beside it follows as the page scrolls past each heading.

Headings keep their own ids; the outline links to them, so a link to a section still works when shared.

Tokens

A subsection, drawn one step in.

Use a component

A few paragraphs stand in for the section's text. The outline beside it follows as the page scrolls past each heading.

Headings keep their own ids; the outline links to them, so a link to a section still works when shared.

Ship it

A few paragraphs stand in for the section's text. The outline beside it follows as the page scrolls past each heading.

Headings keep their own ids; the outline links to them, so a link to a section still works when shared.

<script setup lang="ts">import { TableOfContents } from '@vitral/vue'; const sections = [    { id: 'toc-install', title: 'Install', sub: [{ id: 'toc-npm', title: 'With a package manager' }, { id: 'toc-cdn', title: 'From a CDN' }] },    { id: 'toc-theme', title: 'Pick a theme', sub: [{ id: 'toc-tokens', title: 'Tokens' }] },    { id: 'toc-use', title: 'Use a component', sub: [] },    { id: 'toc-ship', title: 'Ship it', sub: [] }];</script> <template>    <div style="display: grid; grid-template-columns: minmax(0, 1fr) 13rem; gap: 2rem; align-items: start">        <article id="toc-demo-article" style="line-height: 1.7">            <template v-for="section in sections" :key="section.id">                <h2 :id="section.id" style="margin: 1.5rem 0 0.5rem; font-size: 1.25rem">{{ section.title }}</h2>                <p>A few paragraphs stand in for the section's text. The outline beside it follows as the page scrolls past each heading.</p>                <p>Headings keep their own ids; the outline links to them, so a link to a section still works when shared.</p>                <template v-for="sub in section.sub" :key="sub.id">                    <h3 :id="sub.id" style="margin: 1rem 0 0.25rem; font-size: 1.05rem">{{ sub.title }}</h3>                    <p>A subsection, drawn one step in.</p>                </template>            </template>        </article>        <TableOfContents source="#toc-demo-article" selector="h2, h3" :offset="72" style="position: sticky; top: 5rem" />    </div></template>

An outline of your own

`items` gives the outline outright — ids, labels and levels — for headings drawn later or labels shorter than the headings. `v-model:active` is the section being read.

Reading: toc-install

<script setup lang="ts">import { TableOfContents } from '@vitral/vue';import { ref } from 'vue'; const active = ref<string | null>(null);const items = [    { id: 'toc-install', label: 'Install' },    { id: 'toc-npm', label: 'npm, pnpm, yarn', level: 2 },    { id: 'toc-theme', label: 'Theme' },    { id: 'toc-use', label: 'Usage' },    { id: 'toc-ship', label: 'Ship' }];</script> <template>    <div style="display: flex; flex-wrap: wrap; gap: 2rem; align-items: start">        <TableOfContents v-model:active="active" :items="items" title="Contents" />        <p>Reading: <strong>{{ active ?? '—' }}</strong></p>    </div></template>

API

Read from packages/vue/src/components/TableOfContents/types.ts, so it says what the component actually accepts.

Props

NameTypeDescription
items TableOfContentsItem[]The outline, when you have it; otherwise it is read from `source`.
source string | HTMLElementWhere to read headings from: a selector or an element. Defaults to the document.
selector stringWhich headings to read, by selector. Defaults to `'h2, h3'`.
offset numberPixels from the top of the view where a heading counts as reached: the height of a sticky header. Defaults to 80.
title string | falseA heading over the list; the locale's "On this page" otherwise. `false` for none.
smooth booleanScroll to a heading smoothly when its link is followed. Defaults to true.

Plus pt, dt and unstyled from BaseProps, see pass-through and unstyled mode.

Emits

EventPayloadDescription
navigate id: stringA link was followed.