Waypoint / Scrollspy

Highlights the navigation entry for the section currently in view as the user scrolls, driven by the useWaypoint composable. useScrollspy is the id-list helper: scrollspy.

Sidebar Navigation Scrollspy

Introduction

Waypoint automatically highlights navigation links as the user scrolls through corresponding content sections. It observes section visibility and updates the .is-active class on the matching nav link.

Setup

Add data-vd-waypoint-nav to the nav container and point it to the scrollable parent. Each link href should reference a section ID within the scroll container.

Usage

Waypoint supports vertical scroll containers of any height. Links are highlighted top-down - the topmost visible section wins. Works with both window scroll and overflow containers.

API

Call useWaypoint(root) once in setup — it wires every [data-vd-waypoint-nav] inside the root and tears down its observers automatically on unmount; there are no manual refresh/destroy calls.

Underline Variant

Overview of the current section with a compact underline indicator.

Pill Variant

Getting started stays selected while the card remains anchored in place.

API Reference

Wiring

import { ref } from 'vue';
import { useScrollspy, useWaypoint } from "@vanduo-oss/vd3";

const root = ref<HTMLElement | null>(null);
useWaypoint(root);   // wires [data-vd-waypoint-nav] inside root; cleanup on unmount

// Or observe section IDs directly with useScrollspy:
const { activeId } = useScrollspy(['overview', 'features', 'pricing'], {
  offset: 96,
});

// react to the active section changing on a waypoint nav
root.value?.addEventListener('waypoint:change', (e) => {
  console.log('active', e.detail.activeId);
});

CSS Classes

ClassDescriptionType
.vd-waypoint-navBase scrollspy navigation container — requiredBase
.vd-waypoint-borderAdds a left border highlight on the active link (sidebar style)Modifier
.vd-waypoint-pillRounded pill-shaped background on the active linkModifier
.vd-waypoint-underlineBottom underline indicator on the active link (tab style)Modifier
.is-activeApplied automatically to the currently visible section's linkState

Data Attributes

AttributeDescriptionDefault
data-vd-waypoint-navSelector for the scrollable container to observe (e.g. "#content" or "window")window
data-vd-waypoint-offsetPixel offset from the top to trigger activation earlier or later80
data-vd-scrollspy-navAlias for data-vd-waypoint-nav—

Composable API

SymbolDescription
useWaypoint(root)Composable — wires every [data-vd-waypoint-nav] inside the root ref; highlights the link of the topmost visible section. Call once in setup().
useScrollspy(ids, options?)Composable — observes document elements matching ids (string[]) via IntersectionObserver and returns { activeId: Ref<string | null> }.
options.offset / options.rootMarginOptional useScrollspy options: offset (number, default 96px) or custom IntersectionObserver rootMargin (default `-${offset}px 0px -60% 0px`).
(automatic cleanup)The IntersectionObserver and click listeners are removed on component unmount.

Events

EventDescriptionDetail
waypoint:changeFired on the nav element when the active section changes{ activeId, link }
Accessibility
  • Wrap the links in a <nav> with a descriptive aria-label (for example aria-label="Table of contents") — the composable does not add a landmark role for you
  • Active link receives aria-current="true" in addition to the .is-active class
  • Links use standard anchor elements for native keyboard navigation and focus management
  • Smooth scrolling respects prefers-reduced-motion media query
  • Section targets use semantic heading hierarchy for screen reader landmark navigation