# Scrollspy (`<z-scrollspy>`)

> Scroll-driven behavior in two modes: nav highlights links whose sections are in view, and reveal animates elements as they enter the viewport.

- **Kind:** controller
- **Category:** navigation
- **Inherits:** `Controller`, `Base`

## Attributes

| Attribute | Type | Default | From | Description |
| --- | --- | --- | --- | --- |
| `mode` | `'' \| 'nav' \| 'reveal'` | `''` | — | Forces a mode: `nav` highlights links whose targets scroll into view, `reveal` animates elements as they enter the viewport. When empty, the mode is inferred from the attributes and markup. |
| `nav` | `boolean` | `false` | — | Shorthand for `mode="nav"`. |
| `target` | `string` | `''` | — | CSS selector for the elements to watch: the animated targets in reveal mode (default: the host), or the links in nav mode (default: owned `a[href]`). |
| `animation` | `string` | `''` | — | Animation preset(s) applied to reveal targets as they enter view; its presence also helps infer reveal mode. |
| `no-hidden` | `boolean` | `false` | — | Keeps reveal targets visible before they enter view instead of hiding them with `opacity: 0`. |
| `margin` | `string` | `'-1px'` | — | IntersectionObserver root margin for reveal mode; the default `-1px` shrinks the viewport by a pixel. |
| `threshold` | `number` | `0` | — | IntersectionObserver threshold (0–1) for reveal mode: the fraction of the target that must be visible to trigger. |
| `repeat` | `boolean` | `false` | — | Re-runs the reveal animation every time a target re-enters view, instead of revealing once. |
| `delay` | `number` | `0` | — | Delay in milliseconds before each target reveals; targets reveal in sequence through a queue. |
| `duration` | `number` | `0` | — | Overrides the reveal animation duration in milliseconds; `0` uses the animation's own duration. |
| `closest` | `string` | `''` | — | CSS selector for the nav item that receives the active class, resolved with `closest()` from each link. `self`/`false` targets the link itself; by default the parent `<li>` is used when present. |
| `offset` | `string` | `'0'` | — | Offset from the viewport top used to decide which nav target is active and where smooth scrolling lands; accepts a length, `%` of the viewport height, a selector, or `selector ± length`. |
| `scroll` | `boolean` | `false` | — | Enables smooth scrolling to a nav target on click and updates the URL hash; falls back to instant scrolling when the user prefers reduced motion. |
| `active-parent` | `boolean` | `false` | — | Also applies the active class to ancestor `li.z-parent` items that contain the active link. |

### Inherited from `Base`

| Attribute | Type | Default | From | Description |
| --- | --- | --- | --- | --- |
| `cls` | `string` | `''` | `Base` | Adds classes to named parts as `key: classes` pairs; a plain string targets the default part. |
| `stl` | `string` | `''` | `Base` | Adds inline styles to named parts as `key: styles` pairs; a plain string targets the default part. |

## Styling Parts (`cls` / `stl`)

Default `cls` target: `default`. Default `stl` target: `host-inner`.

| Key | Default class | cls | stl | Description |
| --- | --- | --- | --- | --- |
| `active` | `z-active` | ✓ | — | Class applied to the active nav item when the `default` part is empty. |
| `default` | — | ✓ | — | Default `cls` target. In nav mode its classes mark the active item (falling back to `active`); in reveal mode it is a fallback source of animation classes. |
| `inview` | `z-scrollspy-inview` | ✓ | — | Class applied to reveal targets while they are in view. |
| `reveal` | — | ✓ | — | Source of animation classes for reveal targets, used when `animation` is empty; per-element `data-scrollspy-class` overrides both. |

## Events

| Event | Cancelable | Detail | Description |
| --- | --- | --- | --- |
| `z-scrollspy:active` | — | `{ index: number; previousIndex: number; item: HTMLElement \| null; link: HTMLAnchorElement \| null; target: HTMLElement \| null; id: string \| null }` | Fires when the active nav target changes. Detail carries `index`, `previousIndex`, `item`, `link`, `target`, and `id`. |
| `z-scrollspy:before-scroll` | ✓ | `{ index: number; item: HTMLElement; link: HTMLAnchorElement; target: HTMLElement; id: string }` | Fires before smooth-scrolling to a nav target on click; `preventDefault()` cancels the scroll. Detail carries `index`, `item`, `link`, `target`, and `id`. |
| `z-scrollspy:change` | — | `{ index: number; previousIndex: number; item: HTMLElement \| null; link: HTMLAnchorElement \| null; target: HTMLElement \| null; id: string \| null }` | Fires alongside `active` on every nav active-state change, with the same detail. |
| `z-scrollspy:inview` | — | `{ index: number; target: HTMLElement }` | Fires when a reveal target enters view. Detail carries `index` and `target`. |
| `z-scrollspy:mount` | — | `Record<string, unknown>` | Fires when deferred template content is mounted into the container. |
| `z-scrollspy:outview` | — | `{ index: number; target: HTMLElement }` | Fires when a reveal target leaves view (with `repeat`). Detail carries `index` and `target`. |
| `z-scrollspy:scrolled` | — | `{ index: number; item: HTMLElement; link: HTMLAnchorElement; target: HTMLElement; id: string }` | Fires after a smooth scroll to a nav target is initiated, with the same detail as `before-scroll`. |

## Methods

| Method | Params | Returns | Description |
| --- | --- | --- | --- |
| `activeIndex()` | — | `number` | Returns the index of the active nav target, or `-1` in reveal mode or when none is active. |
| `inviewElements()` | — | `HTMLElement[]` | Returns the reveal targets currently in view; empty in nav mode. |
| `refresh()` | — | `void` | Tears down and re-initializes the observers and listeners after the DOM or attributes change. |

## Data Hooks

| Attribute | Values | Applies to | Role | Description |
| --- | --- | --- | --- | --- |
| `data-fn` | `template` | `descendant` | `config` | Marks a deferred `<template>` child whose content is mounted into the container on demand. |
| `data-scrollspy-class` | — | `descendant` | `config` | Per-element override of the reveal animation classes, replacing the `animation` attribute and the `reveal`/`default` parts for that target. |

## Notes

- With `mode`/`nav` unset, reveal signals (`repeat`, `delay`, `no-hidden`, `animation`, or `z-animation-*` classes) select reveal mode; nav attributes (`closest`, `scroll`, `offset`, `active-parent`), a nav container (`.z-nav`, `.z-tab`, `.z-dotnav`, `.z-thumbnav`, etc.), or owned hash links select nav mode; otherwise reveal.
- Reveal targets are hidden with `opacity: 0` until they enter view, unless `no-hidden` is set or the user prefers reduced motion, which reveals instantly.
- Nav mode watches the nearest scrollable ancestor of the targets, or the window; the last target becomes active when the scroll reaches the bottom.

## Guides

- [Styling with cls and stl](https://next.0build.dev/docs/guides/styling.md)
- [Translating Components](https://next.0build.dev/docs/guides/i18n.md)
- [Icon Resolution](https://next.0build.dev/docs/guides/icons.md)
- [The data-source Convention](https://next.0build.dev/docs/guides/data-source.md)
- [Openers & Triggers](https://next.0build.dev/docs/guides/triggers.md)
- [Dialog Behavior](https://next.0build.dev/docs/guides/dialogs.md)
- [Utilities](https://next.0build.dev/docs/guides/utilities.md)
- [Runtime](https://next.0build.dev/docs/guides/runtime.md)

## Related

- [Sticky](https://next.0build.dev/docs/components/sticky.md)
