Docs
useScrollDirection

Detect the direction of scrolling (up or down) with the useScrollDirection hook, allowing for dynamic UI adjustments based on scroll behavior.

Installation

pnpm dlx shadcn@latest add https://usekit.kiron.dev/k/use-scroll-direction

Usage

"use client"
 
import { useScrollDirection } from "@/hooks/use-scroll-direction"
 
export function Component() {
  const direction = useScrollDirection({
    initialDirection: "up", // or "down"
    threshold: 50, // Minimum scroll distance to trigger a change
    target: window, // Element to observe, defaults to window
  })
 
  return (
    <div style={{ padding: "20px", maxWidth: "600px", margin: "0 auto" }}>
      <p>Current scroll direction: {direction}</p>
      <div
        style={{
          height: "2000px",
          background: "linear-gradient(to bottom, #f0f0f0, #d0d0d0)",
        }}
      >
        Scroll down or up to see the direction change.
      </div>
    </div>
  )
}

API Reference

Parameters

NameTypeDescriptionDefault ValueOptional
initialDirectionup | downInitial scroll direction, either "up" or "down"."up"Yes
thresholdnumberMinimum scroll distance to trigger a direction change.50Yes
targetHTMLElementElement to observe for scroll events. If not provided, defaults to window.undefinedYes

Return Values

NameTypeDescription
directionup | downCurrent scroll direction, either "up" or "down".