---
title: "Table of Contents · Ark UI — StyleX Blocks"
description: "Scroll-spy page navigation that tracks visible headings with a gliding active indicator."
url: "https://stylexblocks.com/components/ark-ui/toc/"
agent:
  instructions: "Use this search API to find relevant components and blocks across this site with keywords or natural-language queries, instead of crawling individual pages. Results are returned in Markdown."
  endpoint: "https://stylexblocks.com/.well-known/search?q={query}"
  method: "GET"
  documentation: "https://developers.cloudflare.com/ai-search/how-to/search-for-agents/"
---

# Table of Contents

Scroll-spy page navigation that tracks visible headings with a gliding active indicator.

- Behavior engine: Ark UI
- Also available for: [Base UI](https://stylexblocks.com/components/base-ui/toc.md), [UI SDK](https://stylexblocks.com/components/ui-sdk/toc.md)
- Category: Display
- Variants: line, subtle
- Sizes: sm, md
- HTML page: https://stylexblocks.com/components/ark-ui/toc/

## Install

Configure the project once (sets the behavior engine and import aliases):

```sh
npx stylexblocks init
```

Then add the item as source you own:

```sh
npx stylexblocks add toc
```

Equivalent: `npx shadcn add @stylexblocks/toc`. Registry item JSON: https://stylexblocks.com/r/ark-ui/toc.json

## Usage

```tsx
import { Toc } from "#components/ui/ark-ui/toc.tsx";

const items = [
  { value: "introduction", depth: 2 },
  { value: "installation", depth: 2 },
  { value: "requirements", depth: 3 },
];

export function Example() {
  return (
    // Ark's toc machine observes the headings whose ids match items[].value
    // (IntersectionObserver, inside scrollEl or the viewport), marks visible
    // ones active (aria-current="location"), and positions the Indicator
    <Toc.Root items={items} variant="line" size="md">
      <Toc.Nav>
        <Toc.Title>On this page</Toc.Title>
        <Toc.List>
          <Toc.Indicator />
          {items.map((item) => (
            <Toc.Item key={item.value} item={item}>
              <Toc.Link href={`#${item.value}`}>{labels[item.value]}</Toc.Link>
            </Toc.Item>
          ))}
        </Toc.List>
      </Toc.Nav>
    </Toc.Root>
  );
}

// scoped to a panel: <Toc.Root items={items} scrollEl={() => panelRef.current}>
// one active entry: <Toc.Root items={items} multiple={false}>
```

## Source

`components/ui/ark-ui/toc.tsx`

```tsx
import * as stylex from "@stylexjs/stylex";
import { Toc as ArkToc } from "@ark-ui/react/toc";
import { createSlotRecipeContext } from "#lib/ark-ui/create-slot-recipe-context.tsx";
import { defineSlotRecipe } from "#lib/recipe-props.ts";
import { colorPalette } from '@stylexblocks/ui/color-palette.stylex.ts'
import { durations } from '@stylexblocks/ui/tokens/durations.stylex.ts'
import { easings } from '@stylexblocks/ui/tokens/easings.stylex.ts'
import { fontSizes } from '@stylexblocks/ui/tokens/font-sizes.stylex.ts'
import { fontWeights } from '@stylexblocks/ui/tokens/font-weights.stylex.ts'
import { lineHeights } from '@stylexblocks/ui/tokens/line-heights.stylex.ts'
import { radii } from '@stylexblocks/ui/tokens/radii.stylex.ts'
import { semanticColors } from '@stylexblocks/ui/semantic-tokens/colors.stylex.ts'
import { spacing } from '@stylexblocks/ui/tokens/spacing.stylex.ts'

// Table of contents: a scroll-spy link list in the docs-sidebar idiom. The
// root carries `--top` / `--left` / `--width` / `--height` (the active items'
// box, relative to the list) which the indicator reads to glide between
// entries. Items carry `--depth` (the heading level, 2 for h2) and links
// indent by `--depth - 2` steps, so h2 sits flush.

const tocRoot = stylex.create({
  base: {
    display: 'flex',
    flexDirection: 'column',
    gap: spacing._2,
    minWidth: 0,
  },
})

const tocTitle = stylex.create({
  base: {
    margin: 0,
    color: semanticColors.fg,
    fontSize: fontSizes.sm,
    lineHeight: lineHeights.sm,
    fontWeight: fontWeights.semibold,
  },
})

const tocList = stylex.create({
  base: {
    position: 'relative',
    isolation: 'isolate',
    display: 'flex',
    flexDirection: 'column',
    listStyleType: 'none',
    margin: 0,
    padding: 0,
  },
})

const tocListVariants = stylex.create({
  line: {
    borderInlineStartWidth: '1px',
    borderInlineStartStyle: 'solid',
    borderInlineStartColor: semanticColors.border,
  },
  subtle: {},
})

const tocItem = stylex.create({
  base: {
    position: 'relative',
    minWidth: 0,
  },
})

const tocLink = stylex.create({
  base: {
    position: 'relative',
    zIndex: 1,
    display: 'block',
    overflow: 'hidden',
    textOverflow: 'ellipsis',
    whiteSpace: 'nowrap',
    textDecorationLine: 'none',
    fontWeight: fontWeights.medium,
    '--toc-indent': 'calc(max(var(--depth, 2) - 2, 0) * var(--toc-indent-size))',
    paddingInlineStart: 'calc(var(--toc-padding-inline) + var(--toc-indent))',
    paddingInlineEnd: 'var(--toc-padding-inline)',
    paddingBlock: 'var(--toc-padding-block)',
    color: {
      default: semanticColors.fgMuted,
      ':hover': semanticColors.fg,
      ':is([data-active], [aria-current="location"])': colorPalette.fg,
    },
    transitionProperty: 'color, background-color',
    transitionDuration: durations.fast,
    outlineWidth: {
      default: null,
      ':focus-visible': '2px',
    },
    outlineOffset: {
      default: null,
      ':focus-visible': '-2px',
    },
    outlineStyle: {
      default: null,
      ':focus-visible': 'solid',
    },
    outlineColor: {
      default: null,
      ':focus-visible': colorPalette.focusRing,
    },
  },
})

const tocLinkVariants = stylex.create({
  line: {
    borderRadius: 0,
  },
  subtle: {
    borderRadius: radii.md,
    backgroundColor: {
      default: 'transparent',
      ':hover': semanticColors.interactionHover,
      ':is([data-active], [aria-current="location"])': 'transparent',
    },
  },
})

const tocLinkSizes = stylex.create({
  sm: {
    fontSize: fontSizes.xs,
    lineHeight: lineHeights.xs,
    '--toc-padding-inline': spacing._2_5,
    '--toc-padding-block': spacing._1,
    '--toc-indent-size': spacing._3,
  },
  md: {
    fontSize: '0.8125rem',
    lineHeight: lineHeights.sm,
    '--toc-padding-inline': spacing._3,
    '--toc-padding-block': spacing._1_5,
    '--toc-indent-size': spacing._3,
  },
})

const tocIndicator = stylex.create({
  base: {
    position: 'absolute',
    zIndex: 0,
    pointerEvents: 'none',
    top: 'var(--top)',
    height: 'var(--height)',
    transitionProperty: 'top, height, left, width',
    transitionDuration: durations.moderate,
    transitionTimingFunction: easings.standard,
  },
})

const tocIndicatorVariants = stylex.create({
  // A bar riding the list's inline-start rail (over its 1px border).
  line: {
    insetInlineStart: '-1px',
    width: '2px',
    borderRadius: radii.full,
    backgroundColor: colorPalette.solid,
  },
  // A pill behind the active link(s).
  subtle: {
    left: 'var(--left)',
    width: 'var(--width)',
    borderRadius: radii.md,
    backgroundColor: colorPalette.muted,
  },
})

export type TocVariant = keyof typeof tocIndicatorVariants

export type TocSize = keyof typeof tocLinkSizes

const tocRecipe = defineSlotRecipe({
  slots: {
    root: { styles: tocRoot },
    title: { styles: tocTitle },
    list: { styles: tocList, variants: tocListVariants },
    item: { styles: tocItem },
    link: { styles: tocLink, variants: tocLinkVariants, sizes: tocLinkSizes },
    indicator: { styles: tocIndicator, variants: tocIndicatorVariants },
  },
  defaultVariants: { variant: "line", size: "md" },
});

const tocStatic = stylex.create({
  nav: { display: "flex", flexDirection: "column", gap: "inherit", minWidth: 0 },
});

const { withProvider, withContext } = createSlotRecipeContext(tocRecipe);

/**
 * Toc — Ark UI's table-of-contents machine wearing the `toc` recipe.
 * `<Toc.Root items scrollEl variant size>` resolves the recipe once and every
 * part inherits it through context. The machine observes the headings whose
 * ids match `items[].value` with an IntersectionObserver (inside `scrollEl`,
 * or the viewport), marks visible ones active (`data-active`,
 * `aria-current="location"` on the link), and measures their box into the
 * root's `--top` / `--left` / `--width` / `--height`, which `Toc.Indicator`
 * (placed inside `Toc.List`) reads to glide between entries. `multiple={false}`
 * keeps one active entry. Composition per part via `asChild`.
 */
export const Toc = {
  Root: withProvider(ArkToc.Root, "root", "TocRoot"),
  Nav: withContext(ArkToc.Nav, () => [tocStatic.nav], "TocNav"),
  Title: withContext(ArkToc.Title, "title", "TocTitle"),
  List: withContext(ArkToc.List, "list", "TocList"),
  Item: withContext(ArkToc.Item, "item", "TocItem"),
  Link: withContext(ArkToc.Link, "link", "TocLink"),
  Indicator: withContext(ArkToc.Indicator, "indicator", "TocIndicator"),
  Content: ArkToc.Content,
};
```
