useSticky

Commons
stable

Implements the "sticky-while-scrolling" pattern for taller-than-viewport navigation drawers. Tracks scroll direction and element bounding rect to decide whether the sidebar is stuck at the top, stuck at the bottom, or free-floating mid-page. Returns stickyStyles with the appropriate top/bottom offset so the sidebar scrolls naturally through long content without jumping.

API

Signature

Signature TypeScript complète extraite du fichier du composable.

typescript

useSticky mounts a passive scroll listener on window when isSticky is true and removes it when false or on beforeUnmount. The scroll handler reads getBoundingClientRect() and a --v-body-scroll-y CSS custom property (set by the layout manager) to compute the final offset.

Interfaces consommées

ISticky

API

Paramètres

Paramètres d'entrée acceptés par ce composable.

rootEl
requis
Ref<HTMLElement | undefined>
options
requis
ISticky
Configuration object containing rootEl (template ref to the sticky element), isSticky (Ref<boolean> — feature toggle; when false the scroll listener is removed), and layoutItemStyles (ComputedRef of the layout item styles, providing the top offset imposed by the app-bar).
isSticky
requis
Ref<boolean>
layoutItemStyles
requis
Ref<CSSProperties>

API

Valeurs de retour

Propriétés et méthodes exposées par l'objet retourné par le composable.

isStuck
Current stuck state. "top" = anchored at the top of the viewport (sidebar shorter than viewport or scrolled to top), "bottom" = anchored at the bottom edge, true = stuck mid-page (sidebar taller than viewport, user scrolled past the top edge), false = not sticky (isSticky prop is false).
stickyStyles
Two-element style array to spread on the element. The first resets top/bottom/height when isSticky is enabled. The second applies the computed positional offset: the layoutItem top when not yet stuck, or the scrolled stuckPosition when stuck mid-page or at the bottom.

Usage

Exemples

Extraits de code prêts à coller.

Sticky navigation drawer

vue