# useScrollPosition Tracks the vertical scroll position of the window or a specific scrollable element. ## Signature ```ts function useScrollPosition(options?: { targetRef?: RefObject }): number; ``` ### Options | Option | Type | Default | Description | | ----------- | ------------------------------------ | ---------- | ------------------------------------------------------- | | `targetRef` | `RefObject` | `document` | Ref to a scrollable element; falls back to the document | Returns the current `scrollY` (window) or `scrollTop` (element) as a number. ## Usage ```tsx // Track window scroll function ScrollProgress() { const scrollY = useScrollPosition(); return
Scrolled: {scrollY}px
; } ``` ```tsx // Track a specific scrollable container function VirtualList() { const containerRef = useRef(null); const scrollTop = useScrollPosition({ targetRef: containerRef }); return (
{/* items */}
); } ``` ## Notes - Initial position is read synchronously on mount so there is no flash to `0`. - Automatically switches between `window.scrollY` and `element.scrollTop` based on the target. - Client-only (`'use client'`).