useInViewport
Detects when an element is visible within the viewport.
Installation
npx hookcn add useInViewportDescription
The useInViewport hook checks if an element is visible in the viewport. You can adjust how much of the element needs to be visible or add a margin around the viewport to control when the element is considered visible.
Usage
import useInViewport from "src/hooks/useInViewport";
//..
const isInViewport = useInViewport(ref, 0.1, 0);Parameters
Name
Type
Default value
Description
ref
RefObject<HTMLElement | null>
-
The ref of the HTML element to observe.
threshold
number
0.1
Percentage of the element that needs to be visible.
rootMargin
number
0
Margin around the viewport to consider.
Return Value
Name
Type
Description
isInViewport
boolean
true if the element is in the viewport, otherwise false.
Example
Last updated