Anchor

Free

An "on this page" navigation list that tracks scroll position with an IntersectionObserver and smooth-scrolls to a section on click. Vertical (side-rail) and horizontal (pill-row) layouts, nested and collapsible sections, outline numbers, a sliding marker and reading progress. Pro adds a sticky underline bar and an outline written from the article's headings.

</>sv-anchor anchorscrollspytable of contentstocnavigationon this page
import { SvAnchorComponent } from '@styloviz/anchor';

Demo & usage

Basic usage

Usage example — open the gallery for the full interactive demo.

An interactive demo for this component lives in the gallery.

Open live demo

More examples

Horizontal pill row

example.htmlAngular
<sv-anchor [items]="sections" orientation="horizontal" />

Custom scroll container + offset

example.htmlAngular
<sv-anchor [items]="sections" [root]="scrollContainer()" [offset]="72" (activeIdChange)="onSectionChange($event)" />

When to use

  • Job: document position — renders real hash links and reports where the reader is via `activeIdChange` (compare all seven in the “Choosing a navigation pattern” guide under Guides).
  • A long docs or article page where readers benefit from a persistent "on this page" outline.
  • You want the outline to highlight itself as the reader scrolls, without wiring up scroll listeners by hand.
  • For a fixed set of in-page tabs that switch content rather than scroll to it, use Tabs instead.

Compare with

  • Jump Menu — Jump Menu is one native select for moving between destinations — the compact form when a rail does not fit, with the platform picker on touch.

Async states

None — this component has nothing to load, so it has no loading, empty or error state to drive.

Item identity & owned state

Restorable state
  • expandedIds

Bind these to put a saved view back — the open sections, the hidden columns, the selected rows — and read their Change events to store what the user does.

Inputs

NameTypeDefaultDescription
itemsrequiredreadonly AnchorItem[]—The sections to list and track, in document order.
rootHTMLElement | nullnullScroll container the sections live in. `null` = the window/viewport.
orientationAnchorOrientation'vertical'Layout axis.
ariaLabelstring'On this page'Accessible name of the `<nav>` landmark. Two rails on one page need two names, or a screen reader's landmark list shows "On this page" twice.
offsetnumber0Extra pixels kept between a jumped-to section and the top of the scroll container — room for a sticky header the section would otherwise land underneath.
offsetFromHTMLElement | nullnullA sticky header to measure instead of using a fixed `offset` — its current height becomes the offset, and follows it as it changes.
activationAnchorActivation'last-passed'Which reading of "current" to highlight: `'last-passed'` (the last heading scrolled past), `'first-visible'`, `'most-visible'`, or `'nearest-offset'` (the heading closest to the sticky-header line).
urlKeystring | nullnullReflect the section being read into the URL under this key — pair it with `svHashNavLocation('section')` for `#installation`. Needs `provideSvNavLocation()` at the app root.
urlWriteModeSvNavWriteMode'replace'What a CLICK on a nav link does to history — `'push'` makes back return to the section the reader came from. Scrolling always replaces, whatever this says.
appearanceAnchorAppearance'rail'The list's look. Vertical: `'rail'`, `'compact'`, `'minimal'`, `'card'`. Horizontal: `'minimal'` draws plain text tabs instead of pills.
markerAnchorMarker'slide'How the rail marks the active item.
numberedbooleanfalsePrefix each item with its outline number — 1, 1.1, 1.2, 2.
collapsiblebooleanfalseShow sub-sections only under the section being read, plus any the reader opened with the toggle.
expandedIdsreadonly string[] (two-way)[]With `collapsible`, the parents the reader opened. The active trail is always open on top of these.
progressbooleanfalseA reading-progress bar above the list.
activationOffsetnumber | nullnullWhere a section counts as being read, in px from the top of the scroll container — the activation line, set apart from the jump's `offset`. Null uses the same number as the jump.
hashSyncAnchorHashSync'off'`'auto'` reflects the section being read into `location.hash`, and jumps to the hash on load, with no `provideSvNavLocation()` — `urlKey` wins when both are set.
wrapbooleantrueHorizontal only: wrap the items onto more lines. `false` keeps one line, for a bar that scrolls sideways.
labelsPartial<SvAnchorLabels>{}Every string the anchor renders.

Outputs

EventPayload typeDescription
progressChangenumberHow far the container has scrolled, 0–1, whenever it changes.
activeIdChangestring | nullEmits whenever the active section changes, including on click-to-jump.
unresolvedSvUnresolvedValue<string>Emitted when an item points at a section this page does not have exactly one of. `'unknown'` for no element, `'blocked'` for several with the same id.
expandedIdsChangereadonly string[]Emits the new `expandedIds` whenever it changes — the output half of `[(expandedIds)]`.

<sv-anchor-bar>

Also in this family — SvAnchorBarComponent from @styloviz-pro/layout.

Async states

  • loading
  • error
  • retry

Inputs

NameTypeDefaultDescription
itemsrequiredreadonly AnchorItem[]—The sections, top level — `children` are tracked and light their parent's tab.
rootHTMLElement | nullnullScroll container the sections live in. `null` = the window.
stickybooleantruePin the bar while the page scrolls under it.
stickyTopnumber0Where the bar pins, in px from the top — below an app header.
offsetnumber0Px kept above a jumped-to section.
offsetFromHTMLElement | nullnullA sticky header to measure instead of `offset`.
activationAnchorActivation'last-passed'Which reading of "current" to highlight — see `sv-anchor`.
activationOffsetnumber | nullnullThe activation line, apart from the jump offset.
hashSyncAnchorHashSync'off'Reflect the section into `location.hash`.
numberedbooleanfalsePrefix each tab with its number.
ariaLabelstring'On this page'The `<nav>` landmark's name.
loadingbooleanfalseThe sections are still loading — a CMS outline, a route's data. Draws a one-line skeleton in the bar's place, so the page does not jump when it arrives.
errorSvDataError | string | nullnullThe sections failed to load. `toSvDataError()` normalises an HTTP error.
labelsPartial<SvAnchorBarLabels>{}The bar's own strings.

Outputs

EventPayload typeDescription
activeIdChangestring | nullEmits whenever the active section changes.
retryvoidThe error state's "Try again" was pressed.

<sv-anchor-outline>

Also in this family — SvAnchorOutlineComponent from @styloviz-pro/layout.

Async states

None — this component has nothing to load, so it has no loading, empty or error state to drive.

Inputs

NameTypeDefaultDescription
sourcerequiredstring | HTMLElement | null—The article whose headings become the outline: a CSS selector or the element.
headingLevelsreadonly number[][2, 3]Which heading levels to list.
affixbooleanfalsePin the rail while its column scrolls.
affixTopnumber0With `affix`, where it pins, in px from the top.
affixBottomnumber16With `affix`, room kept below it, in px.
rootHTMLElement | nullnullScroll container the article lives in. `null` = the window.
offsetnumber0Px kept above a jumped-to heading.
offsetFromHTMLElement | nullnullA sticky header to measure instead of `offset`.
activationAnchorActivation'last-passed'Which reading of "current" to highlight.
activationOffsetnumber | nullnullThe activation line, apart from the jump offset.
appearanceAnchorAppearance'rail'The rail's look.
markerAnchorMarker'slide'How the active item is marked.
numberedbooleanfalseOutline numbers.
collapsiblebooleanfalseSub-headings only under the section being read.
progressbooleanfalseA reading-progress bar.
hashSyncAnchorHashSync'off'Reflect the heading into `location.hash`.
ariaLabelstring'On this page'The `<nav>` landmark's name.

Outputs

EventPayload typeDescription
activeIdChangestring | nullEmits whenever the active heading changes.
itemsChangereadonly AnchorItem[]Emits the generated outline whenever it changes.

Customization & theming

  • Vertical (bordered side-rail) or horizontal (pill-row) orientation.
  • `offset` shifts the smooth-scroll landing position, e.g. to clear a sticky header.
  • `root` scopes both the IntersectionObserver and the scroll-to call to a custom scrollable container instead of the window.
  • `activeIdChange` emits whenever the active section changes, for syncing external UI.
  • `AnchorItem.children` (or a flat `level`) nests sections; `collapsible` shows sub-sections only under the one being read, with `[(expandedIds)]` for the ones the reader opened.
  • `appearance` (`rail`, `compact`, `minimal`, `card`), `marker` (`slide` or `border`), `numbered`, and per-item `icon` and `meta`.
  • `progress` + `progressChange` for reading progress; `activationOffset` moves where a section counts as read apart from the jump `offset`; `hashSync="auto"` mirrors the section into the hash with no app provider.
  • Pro: `sv-anchor-bar` (a sticky tab bar with a sliding underline) and `sv-anchor-outline` (generated from headings, with `affix`), both in `@styloviz-pro/layout`.

Best practices

  • Do: Give every linked section a stable `id` that matches the anchor item's `id` — the component resolves targets with `document.getElementById`.
  • Do: Pass `root` when the page scrolls inside a custom container rather than the window, so the scrollspy observes the right viewport.
  • Tip: Tune `rootMargin` behavior indirectly via `offset` when a sticky header covers the top of the viewport and sections trigger active state too late.
  • Don't: Don't use it for fewer than 3–4 sections — a short page doesn't need an outline, and the scrollspy machinery is wasted.

Accessibility

  • Rendered as a labelled `nav` landmark ("On this page").
  • The active link is marked with `aria-current="true"`; ancestors on its trail are styled but not marked current.
  • Up/Down (Left/Right when horizontal, mirrored in RTL), Home and End move focus between the links; collapse toggles expose `aria-expanded` and a translatable name.
  • Respects `prefers-reduced-motion`: falls back to an instant jump instead of a smooth scroll.
  • Links are real anchors with real `href`s (`#section-id`), so they work without JavaScript and are keyboard-focusable in document order.

Feedback on Anchor

Spotted a gap in this page, or something that didn't work as described? Comments are GitHub Discussions in the public styloviz/community repo, so posting needs a GitHub account.

Loads giscus.app and GitHub — nothing is fetched until you click.