Table of Contents (TOC)

Table of content component is a simple component that displays the current page's headings in a table of content format.

Examples Info

TOC component uses the IntersectionObserver Api. You can use all the options of the IntersectionObserver with the same attributes.

Default TOC

Default TOC works with native html heading elements (h1, h2, h3, etc..) with the usage of the dds-anchor class. dap-ds-typography heading variants (variant="h3" etc..) can be used also with the anchor attribute to generate the anchor links. You can see the example of this component on the right side of the page.

<dap-ds-toc header="Table of contents"></dap-ds-toc>
Heading levels

The TOC component can be configured to display only specific heading levels. The minHeadingLevel and maxHeadingLevel attribute can be used to set the levels. minHeadingLevel default value is 2, maxHeadingLevel default value is 3.

<dap-ds-toc header="Table of contents" minHeadingLevel="3" maxHeadingLevel="6"></dap-ds-toc>
Custom element text

The TOC elements text can be customized with the data-text attribute. The order of the text validation is:

  1. data-text attribute
  2. innerText
  3. textContent
<dap-ds-typography variant="h2" anchor data-text="very good h2 type">Section h2</dap-ds-typography>
<h4 class="dds-anchor" data-text="very good h4">native h4</h4>
Indentation

The TOC component items are indented by default. Setting the noIndent attribute to true will remove the indentation.

<dap-ds-toc header="Table of contents" noIndent></dap-ds-toc>
Importing
import { DapDSTOC } from 'dap-design-system'
Importing React
import { DapDSTOCReact } from 'dap-design-system/react'
Tree-Shakeable Imports

For optimal bundle sizes, use the tree-shakeable import syntax:

import { DapDSTOC } from 'dap-design-system/components'
Attributes
PropertyTypeDefaultDescription
rootstring''The root element to observe IntersectionObserver
rootMarginstring'0px 0px -80% 0px'The margin around the root element to observe IntersectionObserver
lastAnchorRootMarginstring'0px 0px -50% 0px'The margin around the last anchor to observe IntersectionObserver
thresholdnumber[][0.1]The threshold to trigger IntersectionObserver
minHeadingLevelnumber2The minimum heading level to include in the TOC. The default value is 2.
maxHeadingLevelnumber3The maximum heading level to include in the TOC. The default value is 3.
noIndentbooleanfalseWhether to indent the TOC items.
headerstringThe header of the TOC.
size'sm', 'lg''lg'The size of the toc. Default is lg.
Slots

No slots available.

Events
Event NameDescriptionType
dds-anchor-changeEvent that is triggered when the anchor changes.{selectedItem: HTMLAnchorElement, anchorElement: IntersectionObserverEntry , HTMLElement }
CSS Parts
Part NameDescription
baseThe main container of the toc.
How to Use CSS Parts

You can style CSS parts using the ::part() pseudo-element selector:

/* Target a specific part */
.my-custom-dap-ds-toc::part(base) {
  /* Your custom styles */
}

Example usage:

<dap-ds-toc class="my-custom-dap-ds-toc">
  Table of Contents (TOC)
</dap-ds-toc>
.my-custom-dap-ds-toc::part(base) {
  border-radius: 12px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

CSS parts allow you to style internal elements of the component while maintaining encapsulation. Learn more in our styling guide.

CSS Custom Properties
Property NameDescription
--dds-toc-border-widthWidth of the TOC border. Default: var(--dds-border-width-base)
--dds-toc-border-colorColor of the TOC border. Default: var(--dds-border-neutral-divider)
--dds-toc-title-colorColor of the TOC title. Default: var(--dds-text-neutral-strong)
--dds-toc-title-font-sizeFont size of the TOC title. Default: var(--dds-font-base)
--dds-toc-title-font-weightFont weight of the TOC title. Default: var(--dds-font-weight-bold)
--dds-toc-title-line-heightLine height of the TOC title. Default: 1.25
--dds-toc-link-colorColor of the TOC links. Default: var(--dds-button-subtle-text-neutral-enabled)
--dds-toc-link-active-colorColor of the active TOC link. Default: var(--dds-button-subtle-text-enabled)
--dds-toc-link-border-colorBorder color of the active TOC link. Default: var(--dds-border-brand-base)
--dds-toc-link-line-heightLine height of the TOC links. Default: 1.2
--dds-toc-link-paddingPadding of the TOC links. Default: var(--dds-spacing-200)
How to Use CSS Custom Properties

CSS custom properties (CSS variables) can be set directly on the component or in your stylesheet:

Method 1: Inline styles (Quick customization)

<dap-ds-toc
  style="--dds-toc-border-width: value; --dds-toc-border-color: value;">
  Table of Contents (TOC)
</dap-ds-toc>

Method 2: CSS classes (Reusable styles)

.my-custom-dap-ds-toc {
  --dds-toc-border-width: value;
  --dds-toc-border-color: value;
  --dds-toc-title-color: value;
}
<dap-ds-toc class="my-custom-dap-ds-toc">
  Table of Contents (TOC)
</dap-ds-toc>

Method 3: Global theme customization

/* Apply to all instances */
dap-ds-toc {
  --dds-toc-border-width: value;
  --dds-toc-border-color: value;
}

CSS custom properties inherit through the Shadow DOM, making them perfect for theming. Changes apply immediately without rebuilding.