Anchor
FreeAn "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.
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 demoMore examples
Horizontal pill row
<sv-anchor [items]="sections" orientation="horizontal" />
Custom scroll container + offset
<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
Changeevents to store what the user does.
Inputs
| Name | Type | Default | Description |
|---|---|---|---|
| itemsrequired | readonly AnchorItem[] | — | The sections to list and track, in document order. |
| root | HTMLElement | null | null | Scroll container the sections live in. `null` = the window/viewport. |
| orientation | AnchorOrientation | 'vertical' | Layout axis. |
| ariaLabel | string | '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. |
| offset | number | 0 | Extra 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. |
| offsetFrom | HTMLElement | null | null | A sticky header to measure instead of using a fixed `offset` — its current height becomes the offset, and follows it as it changes. |
| activation | AnchorActivation | '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). |
| urlKey | string | null | null | Reflect the section being read into the URL under this key — pair it with `svHashNavLocation('section')` for `#installation`. Needs `provideSvNavLocation()` at the app root. |
| urlWriteMode | SvNavWriteMode | '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. |
| appearance | AnchorAppearance | 'rail' | The list's look. Vertical: `'rail'`, `'compact'`, `'minimal'`, `'card'`. Horizontal: `'minimal'` draws plain text tabs instead of pills. |
| marker | AnchorMarker | 'slide' | How the rail marks the active item. |
| numbered | boolean | false | Prefix each item with its outline number — 1, 1.1, 1.2, 2. |
| collapsible | boolean | false | Show sub-sections only under the section being read, plus any the reader opened with the toggle. |
| expandedIds | readonly string[] (two-way) | [] | With `collapsible`, the parents the reader opened. The active trail is always open on top of these. |
| progress | boolean | false | A reading-progress bar above the list. |
| activationOffset | number | null | null | Where 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. |
| hashSync | AnchorHashSync | '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. |
| wrap | boolean | true | Horizontal only: wrap the items onto more lines. `false` keeps one line, for a bar that scrolls sideways. |
| labels | Partial<SvAnchorLabels> | {} | Every string the anchor renders. |
Outputs
| Event | Payload type | Description |
|---|---|---|
| progressChange | number | How far the container has scrolled, 0–1, whenever it changes. |
| activeIdChange | string | null | Emits whenever the active section changes, including on click-to-jump. |
| unresolved | SvUnresolvedValue<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. |
| expandedIdsChange | readonly 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
| Name | Type | Default | Description |
|---|---|---|---|
| itemsrequired | readonly AnchorItem[] | — | The sections, top level — `children` are tracked and light their parent's tab. |
| root | HTMLElement | null | null | Scroll container the sections live in. `null` = the window. |
| sticky | boolean | true | Pin the bar while the page scrolls under it. |
| stickyTop | number | 0 | Where the bar pins, in px from the top — below an app header. |
| offset | number | 0 | Px kept above a jumped-to section. |
| offsetFrom | HTMLElement | null | null | A sticky header to measure instead of `offset`. |
| activation | AnchorActivation | 'last-passed' | Which reading of "current" to highlight — see `sv-anchor`. |
| activationOffset | number | null | null | The activation line, apart from the jump offset. |
| hashSync | AnchorHashSync | 'off' | Reflect the section into `location.hash`. |
| numbered | boolean | false | Prefix each tab with its number. |
| ariaLabel | string | 'On this page' | The `<nav>` landmark's name. |
| loading | boolean | false | The 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. |
| error | SvDataError | string | null | null | The sections failed to load. `toSvDataError()` normalises an HTTP error. |
| labels | Partial<SvAnchorBarLabels> | {} | The bar's own strings. |
Outputs
| Event | Payload type | Description |
|---|---|---|
| activeIdChange | string | null | Emits whenever the active section changes. |
| retry | void | The 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
| Name | Type | Default | Description |
|---|---|---|---|
| sourcerequired | string | HTMLElement | null | — | The article whose headings become the outline: a CSS selector or the element. |
| headingLevels | readonly number[] | [2, 3] | Which heading levels to list. |
| affix | boolean | false | Pin the rail while its column scrolls. |
| affixTop | number | 0 | With `affix`, where it pins, in px from the top. |
| affixBottom | number | 16 | With `affix`, room kept below it, in px. |
| root | HTMLElement | null | null | Scroll container the article lives in. `null` = the window. |
| offset | number | 0 | Px kept above a jumped-to heading. |
| offsetFrom | HTMLElement | null | null | A sticky header to measure instead of `offset`. |
| activation | AnchorActivation | 'last-passed' | Which reading of "current" to highlight. |
| activationOffset | number | null | null | The activation line, apart from the jump offset. |
| appearance | AnchorAppearance | 'rail' | The rail's look. |
| marker | AnchorMarker | 'slide' | How the active item is marked. |
| numbered | boolean | false | Outline numbers. |
| collapsible | boolean | false | Sub-headings only under the section being read. |
| progress | boolean | false | A reading-progress bar. |
| hashSync | AnchorHashSync | 'off' | Reflect the heading into `location.hash`. |
| ariaLabel | string | 'On this page' | The `<nav>` landmark's name. |
Outputs
| Event | Payload type | Description |
|---|---|---|
| activeIdChange | string | null | Emits whenever the active heading changes. |
| itemsChange | readonly 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.