Scroll Tracker

Track scroll progress with a visual indicator.

Scroll this page to see the scroll tracker at the top.
TSX
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
"use client"; import { motion, useScroll } from "motion/react"; import { cn } from "@/lib/utils"; interface ScrollTrackerProps extends React.ComponentProps<typeof motion.div> { /** * Thickness of the progress indicator * @default 4 */ thickness?: number; /** * Tailwind color class for the tracker * @default "bg-primary" */ trackerColor?: string; /** * Where to position the tracker * @default "top" */ position?: "top" | "bottom"; /** * Reference to scrollable element * If not specified, uses window */ targetContainer?: React.RefObject<HTMLElement | null>; } function ScrollTracker({ thickness = 4, trackerColor = "bg-primary", position = "top", className, targetContainer, ...rest }: ScrollTrackerProps) { const { scrollYProgress } = useScroll({ container: targetContainer as any, }); const positionalClasses = cn( "absolute left-0 right-0 z-50 origin-left", !targetContainer && "fixed", position === "top" ? "top-0" : "bottom-0", trackerColor, className, ); const inlineStyles = { scaleX: scrollYProgress, height: `${thickness}px`, }; return ( <motion.div data-ui="scroll-tracker" className={positionalClasses} style={inlineStyles} {...rest} /> ); } export { ScrollTracker };

Installation

pnpm dlx shadcn@latest add @satoriui/scroll-tracker