# Sticky (`<z-sticky>`)

> Sticks an element to the viewport edge on scroll, with start/end boundaries, show-on-up reveal, overflow handling, and native-sticky fallback.

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

## Attributes

| Attribute | Type | Default | From | Description |
| --- | --- | --- | --- | --- |
| `position` | `'top' \| 'bottom'` | `'top'` | — | Edge the element sticks to: `top` once scrolled past its natural position, or `bottom` of the viewport. |
| `offset` | `string` | `'0'` | — | Distance from the sticking edge. Accepts a length (`px`, `rem`, `em`, viewport units, or `%` of the viewport height), a CSS selector whose visible height is used, `selector + length` / `selector - length`, or `!selector` for the nearest matching ancestor. |
| `offset-end` | `string` | `'0'` | — | Distance from the opposite edge, used when computing overflow and the bottom sticking position; accepts the same syntax as `offset`. |
| `start` | `string` | `''` | — | Scroll boundary where sticking begins: a length past the natural position (`%` of the element height), `true`/`parent` for the parent's inner bottom edge, or a selector for an element's edge. |
| `end` | `string` | `''` | — | Scroll boundary where sticking stops: empty/`true`/`parent` for the parent's inner bottom, `false`/`none` for the document end, a length past the element, or a selector for an element's bottom edge. |
| `show-on-up` | `boolean` | `false` | — | Only sticks while scrolling up; scrolling down hides the fixed element, playing the leave animation when `animation` is set. |
| `overflow-flip` | `boolean` | `false` | — | When the element is taller than the viewport, flips it to stick from the opposite edge so its far end stays reachable instead of scrolling the overflow. |
| `animation` | `string` | `''` | — | Animation preset played when the fixed element enters or leaves (with `show-on-up`, or when scrolling below its natural position). |
| `duration` | `number` | `150` | — | Animation duration in milliseconds; non-finite or negative values fall back to 150. |
| `media` | `string` | `''` | — | Media query that enables sticking; accepts a full query, a breakpoint shorthand (`sm`, `md`, `lg`, `xl`, `2xl`), or a bare pixel number. When it stops matching, the element returns to its natural position and is marked inactive. |
| `target-offset` | `string` | `''` | — | Enables hash-navigation compensation: after jumping to an anchor, scrolls back so the sticky element does not cover the target. `true` adds no extra gap, a length adds clearance, and `false` disables it. |
| `sel-target` | `string` | `''` | — | CSS selector for a descendant that receives the active/inactive classes instead of the host. |

### 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: `active`. Default `stl` target: `host-inner`.

| Key | Default class | cls | stl | Description |
| --- | --- | --- | --- | --- |
| `active` | `z-active` | ✓ | — | Class applied while the element is sticking (or to `sel-target`); the default `cls` target for this component. |
| `below` | `z-sticky-below` | ✓ | — | Class applied once the page has scrolled past the element's natural position. |
| `fixed` | `z-sticky-fixed` | ✓ | — | Class applied while the element is `position: fixed` rather than native sticky. |
| `inactive` | — | ✓ | — | Class applied while the element is not sticking; empty by default. |

## Events

| Event | Cancelable | Detail | Description |
| --- | --- | --- | --- |
| `z-sticky:active` | — | `{ fixed: boolean; below: boolean; scroll: number; target: HTMLElement }` | Fires when the element starts sticking. Detail carries `fixed`, `below`, `scroll`, and `target`. |
| `z-sticky:change` | — | `{ active: boolean; fixed: boolean; below: boolean; scroll: number; target: HTMLElement }` | Fires on any active-state change; detail adds an `active` boolean to the `fixed`, `below`, `scroll`, and `target` fields. |
| `z-sticky:inactive` | — | `{ fixed: boolean; below: boolean; scroll: number; target: HTMLElement }` | Fires when the element stops sticking, with the same detail shape as `active`. |
| `z-sticky:mount` | — | `Record<string, unknown>` | Fires when deferred template content is mounted into the container. |

## Methods

| Method | Params | Returns | Description |
| --- | --- | --- | --- |
| `isActive()` | — | `boolean` | Returns whether the element is currently sticking. |
| `isBelow()` | — | `boolean` | Returns whether the page has scrolled past the element's natural position. |
| `isFixed()` | — | `boolean` | Returns whether the element is currently `position: fixed` (as opposed to native sticky or static). |
| `refresh()` | — | `void` | Re-measures the element and viewport and re-syncs the sticky state. |

## 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. |

## CSS Classes

| Class | Role | Applies to | Description |
| --- | --- | --- | --- |
| `.z-sticky` | `root` | `z-sticky` | Root class for a sticky element, also styled on the `z-sticky` host; creates the positioning and stacking context. |
| `.z-sticky-fixed` | `state` | `.z-sticky` | State class applied while the element is fixed; zeroes its margin so the fixed position matches the layout slot. |
| `.z-sticky-placeholder` | `element` | `.z-sticky` | Spacer inserted after the element while it is fixed to preserve the layout slot; pointer events are disabled so content beneath stays clickable. |

## Accessibility

> The layout placeholder is marked `aria-hidden="true"` and `hidden` when not in use, so assistive technology ignores it.

## Notes

- Uses native `position: sticky` when possible (top position, no `show-on-up`, no animation, default end, and no overflow-clipping ancestor); otherwise it fixes the element and inserts a placeholder.
- When the element is taller than the viewport, scrolling accumulates overflow so both ends stay reachable.
- A sticky element only sticks within its parent's box; the default `end` boundary is the parent's inner bottom edge.

## 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

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