Scroll Spy
An "On this page" table of contents that highlights the section you're reading.
<arc-scroll-spy>Overview
ScrollSpy builds a table of contents from <arc-spy-link> children and marks the entry for the section you're reading. Put it in a sticky sidebar or aside next to long content. Each link's target is the id of a section, and its text is the label. ScrollSpy hides the links you write and renders its own list under an "On this page" heading. Clicking the heading scrolls back to the top.
The current section is the last one whose top has scrolled past offset pixels from the top of the viewport (80 by default). Raise offset to clear a taller sticky header. When the page reaches the bottom, the last section counts as current even if it's too short to reach that line. ScrollSpy listens for scroll events on the whole document in the capture phase, so sections inside a scrolling container are tracked as well as ones in the window.
The current link gets aria-current="location" and the accent background, and the component fires arc-change with { value } set to the section id when scrolling changes the current section. Clicking a link marks it current without firing arc-change, scrolls its section into view, smoothly unless the reader has asked for reduced motion, and writes #id to the URL with history.replaceState.
Guidelines
Do
- Give every section a unique `id` that matches a spy link's `target`
- Set `offset` to the height of your sticky header or TopBar
- Listen for `arc-change` to follow the section being read as the reader scrolls
- Keep link labels short, matching or abbreviating the section headings
Don't
- Don't use it for site navigation. It only tracks sections on the current page
- Don't forget to import `arc-spy-link`. ScrollSpy reads its links from it
- Don't give it more than 10 to 12 links. A longer list is hard to scan
Features
- `level` on a spy link indents it and sets it in a smaller font, for nested headings
- A link whose `target` matches no element on the page still shows in the list but is never marked current
Preview
Usage
This component needs JavaScript. Use the Web Component or a framework wrapper.
<arc-scroll-spy>
<arc-spy-link target="section-1">Section 1</arc-spy-link>
<arc-spy-link target="section-2">Section 2</arc-spy-link>
</arc-scroll-spy>import { ScrollSpy, SpyLink } from '@arclux/arc-ui-react';
export default function Example() {
return (
<ScrollSpy>
<SpyLink target="section-1">Section 1</SpyLink>
<SpyLink target="section-2">Section 2</SpyLink>
</ScrollSpy>
);
}<script setup>
import { ScrollSpy, SpyLink } from '@arclux/arc-ui-vue';
</script>
<template>
<ScrollSpy>
<SpyLink target="section-1">Section 1</SpyLink>
<SpyLink target="section-2">Section 2</SpyLink>
</ScrollSpy>
</template><script>
import { ScrollSpy, SpyLink } from '@arclux/arc-ui-svelte';
</script>
<ScrollSpy>
<SpyLink target="section-1">Section 1</SpyLink>
<SpyLink target="section-2">Section 2</SpyLink>
</ScrollSpy>import { Component } from '@angular/core';
import { ScrollSpy, SpyLink } from '@arclux/arc-ui-angular';
@Component({
imports: [ScrollSpy, SpyLink],
template: `
<arc-scroll-spy>
<arc-spy-link target="section-1">Section 1</arc-spy-link>
<arc-spy-link target="section-2">Section 2</arc-spy-link>
</arc-scroll-spy>
`,
})
export class MyComponent {}import { ScrollSpy, SpyLink } from '@arclux/arc-ui-solid';
export default function Example() {
return (
<ScrollSpy>
<SpyLink target="section-1">Section 1</SpyLink>
<SpyLink target="section-2">Section 2</SpyLink>
</ScrollSpy>
);
}import { ScrollSpy, SpyLink } from '@arclux/arc-ui-preact';
export default function Example() {
return (
<ScrollSpy>
<SpyLink target="section-1">Section 1</SpyLink>
<SpyLink target="section-2">Section 2</SpyLink>
</ScrollSpy>
);
}API
ringRadiusnumber6- Ring geometry. r drives the dasharray, so the two cannot drift apart.
activestring''- The id of the currently active section. Reflects to an attribute and updates automatically as the user scrolls.
offsetnumber80- Pixel distance from the top of the viewport at which a section counts as current. Increase it to account for taller sticky headers.
progress'none' | 'ring' | 'read' | 'both''none'- How the reader's position through the document is shown.
ringdraws a progress arc beside the heading;readrecedes the entries already scrolled past;bothdoes each. Defaults tonone, and an unrecognized value lands there too.
Events
arc-change- Fired when the active spy target changes during scroll
SpyLink
<arc-spy-link>One entry in a ScrollSpy. `target` names the section id, `level` sets the indent, and the text is the label.
labeltargetstring''- ID of the section to observe
levelnumber0- Nesting depth for visual indentation. Level 0 links render at default size; level 1+ links are indented and use a smaller font size.
See also
- Scroll To TopA floating button that shows up once you've scrolled down and takes you back to the top of the page.
- TabsA row of tab buttons that shows one of several panels at a time.
- Navigation MenuHorizontal navigation bar whose top-level items open dropdowns of links on hover or click. Each link can carry a one-line description.