Skip to content

GhostScrollSpy ​

GhostScrollSpy keeps an in-page table of contents in step with the reader's scroll position. Import it from @ghostvue/themes for a styled vertical section rail. The rail marks the section being read, scrolls to a section when a link is activated, and moves focus there.

Teams that bring their own design system can compose the same behavior from @ghostvue/vue; see Advanced: custom layout.

Navigate an article ​

Themed article rail

Themed section rail that tracks a short article scrolling in its own panel.

Roll out a feature flag

Plan the rollout

Write down who gets the feature first, which metric proves it works, and the number that would make you roll back.

Agree on an owner for each stage so nobody waits for a sign-off that was never assigned.

Ship behind a flag

Merge the code with the flag off. The release goes out like any other deploy, and nothing changes for customers yet.

Turn the flag on for your own team first and use the feature for a day.

Ramp up traffic

Raise exposure in steps: 1%, 10%, 50%, then everyone. Hold each step long enough to see a full day of traffic.

Pause the ramp, rather than pushing through, when a step looks worse than the one before it.

Watch the metrics

Compare error rates and latency between the flagged and unflagged groups, not against last week.

If the rollback number from your plan is crossed, turn the flag off first and investigate second.

Clean up the flag

Once everyone has had the feature for a full release cycle, delete the flag and the old code path in one change.

The example imports every part from @ghostvue/themes and renders the rail beside a short feature-flag rollout article. The article scrolls inside its own panel, so the example passes that panel's template ref as container.

The ScrollSpy parts make up the navigation only. Each GhostScrollSpyItem wraps one GhostScrollSpyLink, and the link's target names a section's id. The sections are plain elements with ids, such as <section id="rollout-ramp">, and need no ScrollSpy part.

Try it:

  • Jump to a section. Tab to a link such as Ramp up traffic and press Enter. The panel scrolls the section to its top, focus moves into the section, the URL hash becomes #rollout-ramp, and the link turns active.
  • Keep reading. Scroll the panel with the wheel, or focus it and use the arrow keys. The active link follows the section under the reading line.
  • Narrow the preview. Below 34rem the rail stacks above the panel, so every link stays on screen while the article scrolls.

The panel is a named role="region" with tabindex="0", so keyboard users can scroll it. Each section has tabindex="-1" so link activation can focus it. :probe-offset="64" puts the reading line 64px below the top of the panel, and the last section is at least one panel tall, so a jump to any section brings it to the top and keeps its link active.

Without container, ScrollSpy tracks and scrolls the window. Pass an element or a template ref for a nested scroll area; tracking follows the ref when it resolves or changes. Navigation honors the container's vertical scroll padding and the section's scroll margin, so the example's scroll-padding-block-start keeps each heading clear of the panel edge.

Active section behavior ​

The active link has aria-current="location" and data-state="active". The themed recipe paints it in the default text color at semibold weight, while the other links stay muted.

  • Reading line. The active section is the last one, in link order, whose start edge has crossed the probe line. probeOffset defaults to 0.4: values in (0, 1] are a fraction of the container height, and 0 or values above 1 are pixel offsets from the start edge.
  • Before scrolling. The first enabled link is active, including in server-rendered markup.
  • At the end. When overall scroll progress reaches endThreshold (default 0.995, the very end of the container), the last section becomes active even if its start never reaches the probe line, so short final sections still get their turn. A jump to an earlier section that cannot scroll to the top of the container also lands at the end, so the last link is marked. Leave enough content after the sections, as the example does, when every jump should mark the chosen link.
  • During a jump. Activating a link or calling scrollTo() marks the chosen section at once and keeps it active while the container scrolls, so sections passed on the way are never marked. Progress and data-travel still update. Tracking resumes when the scroll ends, after 200ms without a scroll event where scrollend is unsupported, or as soon as the reader starts their own scroll with the wheel, touch, a key, or a pointer press.
  • Disabled and shared targets. Disabled links are skipped when choosing the active section. If several links share a target, the first enabled link in DOM order owns the active state and the section counts once toward progress.
  • Refreshes. Viewport and observed content resizes refresh tracking, and so does adding or removing a target section in the scroll container. Changing probeOffset or endThreshold refreshes the active section at the current scroll position.
  • Controlled state. With a controlled modelValue, ScrollSpy requests a selection change once for consecutive measurements of the same section while the current value stays the same.

Keyboard and focus ​

  • Tab / Shift+Tab move between section links.
  • Enter scrolls to the target, updates the URL hash (historyMode defaults to replace; see Routers and the URL hash), and moves focus into the section (focusTarget defaults to true).

Link activation and scrollTo() jump to the section immediately when reduced motion is requested, even if the scroll container uses CSS smooth scrolling.

Routers and the URL hash ​

Link activation scrolls to the section without following the link, then writes the section id to the URL hash. historyMode controls that write:

  • replace (default) swaps the current history entry for the current path and query plus #section-id, so Back does not step through sections.
  • push adds a history entry for each activation.
  • none leaves the URL alone. Scrolling, focus, and the active link work the same.

A hash-mode router, such as vue-router's createWebHashHistory(), keeps its route in the hash (/#/settings). The default write replaces that route with the section id, so the app loses its route. In those apps, set history-mode="none":

vue
<GhostScrollSpy history-mode="none">
  <!-- list, items, and links as usual -->
</GhostScrollSpy>

Path-based routing, such as createWebHistory(), keeps the route in the path, so the default only changes the hash after it.

Advanced: custom layout ​

This section is for teams that bring their own design system and style the navigation themselves. Import the same parts from @ghostvue/vue; they render semantic markup and state attributes but no visual styles.

Headless list markup ​

Headless list markup Headless

Headless section navigation with CSS progress variables.

Introduction

Intro paragraph 1 with enough copy to scroll this demo page.

Intro paragraph 2 with enough copy to scroll this demo page.

Intro paragraph 3 with enough copy to scroll this demo page.

Intro paragraph 4 with enough copy to scroll this demo page.

Intro paragraph 5 with enough copy to scroll this demo page.

Intro paragraph 6 with enough copy to scroll this demo page.

Details

Details paragraph 1 describing implementation notes.

Details paragraph 2 describing implementation notes.

Details paragraph 3 describing implementation notes.

Details paragraph 4 describing implementation notes.

Details paragraph 5 describing implementation notes.

Details paragraph 6 describing implementation notes.

Details paragraph 7 describing implementation notes.

Details paragraph 8 describing implementation notes.

Summary

Summary paragraph 1 wrapping up the scroll-spy walkthrough.

Summary paragraph 2 wrapping up the scroll-spy walkthrough.

Summary paragraph 3 wrapping up the scroll-spy walkthrough.

Summary paragraph 4 wrapping up the scroll-spy walkthrough.

The compound parts render <nav> → <ol> → <li> → <a href="#..."> via GhostScrollSpyLink. Style them through data-part and data-state. Progress is exposed as --ghost-scroll-spy-progress and --ghost-scroll-spy-section-progress on the root without per-frame Vue re-renders. Both progress values stay at zero when there are no measurable targets.

When enabled, GhostScrollSpyLink forwards its default fragment or explicit href to a custom link component passed through as.

Composable layout ​

Composable layout Headless

Custom markup powered by useGhostScrollSpy.

custom-a

Section content 1.

Section content 2.

Section content 3.

Section content 4.

Section content 5.

Section content 6.

Section content 7.

Section content 8.

custom-b

Section content 1.

Section content 2.

Section content 3.

Section content 4.

Section content 5.

Section content 6.

Section content 7.

Section content 8.

Use the root scoped slot (activeId, scrollTo, travelDirection, itemCount) or useGhostScrollSpy when the default list markup is not enough.

The docs homepage uses --ghost-scroll-spy-section-progress to move a ghost between markers on the right-hand rail. The active section also drives the split-flap number. With reduced motion, the active ring and number update immediately and the traveling ghost is hidden.

API ​

See the specification.

PropTypeDefaultDescription
asGhostScrollSpyElement"nav"Declared prop as.
containerHTMLElement | null | Window—Declared prop container.
defaultValuestring—Declared prop defaultValue.
endThresholdnumber—Declared prop endThreshold.
focusTargetbooleantrueDeclared prop focusTarget.
historyMode'none' | 'push' | 'replace'"replace"Declared prop historyMode.
labelstring"Page sections"Declared prop label.
modelValuestring—Declared prop modelValue.
orientationGhostScrollSpyOrientation"vertical"Declared prop orientation.
probeOffsetnumber—Declared prop probeOffset.
scrollBehaviorScrollBehavior"smooth"Declared prop scrollBehavior.
travelSettleMsnumber180Declared prop travelSettleMs.
EventPayloadDescription
activate{ id: string; element: HTMLElement | null; event: MouseEvent | KeyboardEvent; }Emitted as activate.
SlotSlot propsDescription
defaultactiveId: string | undefined, itemCount: number, scrollTo: (targetId: string, options?: { behavior?: ScrollBehavior; focus?: boolean; }) => void, travelDirection: GhostScrollSpyTravelDirection | undefinedPrimary content.

@ghostvue/themes adds no props, events, or slots to the headless GhostScrollSpy API, which it forwards.

PartElementKey semantics
GhostScrollSpynavaria-label, CSS variables
GhostScrollSpyListolordered section sequence
GhostScrollSpyItemlilist item wrapper
GhostScrollSpyLinkaaria-current="location"