use-scroll
efferd-/use-scrollFreeHook
Use scroll hook.
Live preview
live · rendered by the registry
Rendered by efferd on their own page — this is the component running, not a screenshot of it.Install it
npx shadcn@latest add http://efferd.com/r/use-scroll.jsonSource
1"use client";2import { useEffect, useState } from "react";34export function useScroll(downThreshold: number, upThreshold?: number) {5 const [scrolled, setScrolled] = useState(false);6 const scrollUpThreshold = upThreshold ?? downThreshold / 2;78 useEffect(() => {9 const handleScroll = () => {10 const y = window.scrollY;11 // Hysteresis: different thresholds for up/down to prevent flickering12 setScrolled((prev) => {13 if (prev) {14 // Currently scrolled - only unscroll when below lower threshold15 return y > scrollUpThreshold;16 }17 // Currently not scrolled - only scroll when above higher threshold18 return y > downThreshold;19 });20 };2122 window.addEventListener("scroll", handleScroll, { passive: true });23 handleScroll();24 return () => window.removeEventListener("scroll", handleScroll);25 }, [downThreshold, scrollUpThreshold]);2627 return scrolled;28}
Files it writes
More from efferd
All 201 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| use-copy-to-clipboarduse-copy-to-clipboard | efferd | Hooks | Free | no deps | |
| use-keypressuse-keypress | efferd | Hooks | Free | no deps | |
| use-media-queryuse-media-query | efferd | Hooks | Free | no deps |
