Skip to content
UI Brok

Loading…

No results

Browse catalog

Scroll Spy

A navigation list that highlights the link for the page section currently in view via IntersectionObserver.

Preview

Introduction

Scroll Spy tracks which section of the page is currently in view and highlights the matching navigation link automatically. Scroll down to see the active item change.

The component uses an IntersectionObserver under the hood so there is no scroll event polling — it is efficient and accurate even at high frame rates.

Features

Supports an optional pixel offset to account for sticky headers, full RTL layout via logical CSS properties, and reduced-motion-safe smooth scrolling.

The active section is exposed as aria-current="location" for assistive technologies and the start border indicator provides a clear visual cue.

Conclusion

Drop the component into any multi-section page and pass the matching ids and labels. The observer wires everything up automatically on mount and tears itself down on destroy.

Click any link in the navigation to smooth-scroll directly to that section.

Controls

Theme
Surface
Direction
Viewport

Device view renders in an isolated frame so real responsive breakpoints apply. Switch to Full for overflowing popovers.

Installation

terminal
php artisan ui:add scroll-spy

Behavior layer (Alpine)

This component installs a self-registering Alpine module to resources/js/ui/scroll-spy.js. Import the behavior layer once from your bundle so it loads (it hooks alpine:init):

resources/js/ui/index.js JS
import './scroll-spy.js';

Registry contract

Install confidence

php artisan ui:add scroll-spy writes only the generated targets below. The CLI validates each file hash before writing and prompts before replacing local changes unless --force is used.

Version
1.0.0
License
open
Stability
stable
Contract
v2
Foundation
≥ 1.0.0
Type Generated target
blade resources/views/components/ui/scroll-spy.blade.php
js resources/js/ui/scroll-spy.js

Registry dependencies

None.

Package dependencies

composer: jml/brok:^1.0

npm: alpinejs

Guidance

Navigation and orientation

Orient users and move between destinations.

Use when

  • Use to orient users and help them move across pages, sections, or commands. Do not hide primary wayfinding in novelty interactions or deep nested structures if straightforward navigation would be clearer.

Avoid when

  • Avoid using scroll-spy as a default simply because it is available in the library. Choose it only when its interaction cost, visibility, and behavior match the job to be done.
Task length
micro
Interaction cost
low
Interruptiveness
none
Input precision
none
Mobile / touch risk
low
Accessibility risk
medium
Needs background context
No
Comparison needed
No

Preferred alternatives

  • Visible links and local navigation

Anti-patterns

  • Hiding primary wayfinding in novelty interactions

API

manifest knowledge + registry-derived coverage

Props

Prop Type / values Default Description
items array [] Declared by @props in the registry Blade source.
offset int 0 Declared by @props in the registry Blade source.

Slots

  • default — Primary Blade slot rendered by the component.

Data slots

scroll-spy scroll-spy-heading scroll-spy-item scroll-spy-list

Capabilities

wcag-2.2-aa-target interactive responsive rtl darkMode localized

Behavior

  • Uses server-rendered Blade and semantic HTML.
  • Installs a JavaScript behavior module when the registry item includes resources/js/ui files.
  • Declares registry capability flags: a11y, interactive, responsive, rtl, darkMode, localized.

Usage

previews.components.scroll-spy.default.blade.php Blade
@php
    $items = [
        ['id' => 'spy-introduction', 'label' => __('Introduction')],
        ['id' => 'spy-features',     'label' => __('Features')],
        ['id' => 'spy-conclusion',   'label' => __('Conclusion')],
    ];
@endphp

<div class="grid gap-8 md:grid-cols-[1fr_200px]">
    {{-- Scrollable content pane --}}
    <div class="space-y-0 overflow-auto" style="max-height: 320px;" tabindex="0" role="region" aria-label="Sections">
        <section id="spy-introduction" class="min-h-[200px] space-y-2 py-4">
            <h2 class="text-xl font-semibold text-foreground">{{ __('Introduction') }}</h2>
            <p class="text-muted-foreground">{{ __('Scroll Spy tracks which section of the page is currently in view and highlights the matching navigation link automatically. Scroll down to see the active item change.') }}</p>
            <p class="text-muted-foreground">{{ __('The component uses an IntersectionObserver under the hood so there is no scroll event polling — it is efficient and accurate even at high frame rates.') }}</p>
        </section>

        <section id="spy-features" class="min-h-[200px] space-y-2 py-4">
            <h2 class="text-xl font-semibold text-foreground">{{ __('Features') }}</h2>
            <p class="text-muted-foreground">{{ __('Supports an optional pixel offset to account for sticky headers, full RTL layout via logical CSS properties, and reduced-motion-safe smooth scrolling.') }}</p>
            <p class="text-muted-foreground">{{ __('The active section is exposed as aria-current="location" for assistive technologies and the start border indicator provides a clear visual cue.') }}</p>
        </section>

        <section id="spy-conclusion" class="min-h-[200px] space-y-2 py-4">
            <h2 class="text-xl font-semibold text-foreground">{{ __('Conclusion') }}</h2>
            <p class="text-muted-foreground">{{ __('Drop the component into any multi-section page and pass the matching ids and labels. The observer wires everything up automatically on mount and tears itself down on destroy.') }}</p>
            <p class="text-muted-foreground">{{ __('Click any link in the navigation to smooth-scroll directly to that section.') }}</p>
        </section>
    </div>

    {{-- Scroll Spy navigation --}}
    <x-ui.scroll-spy :items="$items" class="md:sticky md:top-4 md:self-start">
        {{ __('On this page') }}
    </x-ui.scroll-spy>
</div>

Accessibility & localization

WCAG 2.2 AA RTL-ready data-slot Keyboard focus-visible Localized labels
  • Accessibility target: wcag-2.2-aa-target.
  • Uses semantic HTML and a stable data-slot attribute for styling/scripting hooks.
  • Focus-visible rings use the ring token so keyboard focus is always visible.
  • Disabled and invalid states are conveyed to assistive tech, not by color alone.
  • Targets WCAG 2.2 AA; verify contrast in light, dark, admin and customer surfaces above.
  • Labels go through __() and the component uses logical properties (ms-*, text-start, start-*) so it mirrors correctly — flip the preview to RTL to confirm.

Source

The exact, editable file ui:add writes into your app — copy it or own the whole thing. Previews render this same code; there are no preview-only components.

Changelog

Current version: v1.0.0. See the site changelog for the full release history.