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

Full TypeScript signature sourced from the composable file.

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.

Consumed interfaces

ISticky

API

Parameters

Input parameters accepted by this composable.

rootEl
required
Ref<HTMLElement | undefined>
options
required
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
required
Ref<boolean>
layoutItemStyles
required
Ref<CSSProperties>

API

Return values

Properties and methods exposed by the composable return object.

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

Examples

Ready-to-paste usage snippets.

Sticky navigation drawer

vue