Anchor

Scroll-spy in-page navigation. Click a link to smooth-scroll; the closest visible heading auto-highlights.

Installation

import { WrAnchor } from 'ngwr/anchor';

@Component({ imports: [WrAnchor] })
export class MyComponent {}

Demo

The anchor below tracks the headings as you scroll the showcase page. Spacers between sections give the scroll-spy something to react to.

<wr-anchor [links]="links" [offset]="80" />

Introduction

Anchor watches the page scroll and highlights the closest section.

Usage

Basic

Pass an array of { id, label } and the component does the rest.

Advanced

Children render one level of nested links.

API

See the component reference below.

API

Inputs of <wr-anchor>.

NameDescriptionTypeDefault
linksreadonly WrAnchorLink[][]
ariaLabelAccessible name for the navigation landmark. Falls back to anchor.label, then 'Table of contents' — it used to be that English string, hard-coded on the host, in a landmark a screen reader reads out on every page.string | nullnull
offsetPixel offset subtracted on scroll — for sticky headers.number0
hitAreaHighlight an item when its target's top is within this many px of the offset line.number80

Types

Data shapes used by the inputs above.

interface WrAnchorLink {
  id: string;
  label: string;
  children?: readonly WrAnchorLink[];
}
NameDescriptionTypeDefault
WrAnchorLinkOne entry in the rail.interface
idrequiredTarget element id (without #).string
labelrequiredVisible text.string
childrenOne level of nested links.readonly WrAnchorLink[]