Skip to main content

useIsVisible

The useIsVisible hook returns an object that provides information about the visibility of a component. More Info...

This hook can be used for lazy loading images, run expensive animations on hidden components or tracking visibility of user content. It integrates the Intersection Observer API in the background.

import { useIsVisible } from '@archibald/client';

const RETURN_VALUE = useIsVisible(PARAMETERS);

Parameters

NameTypeOptionalDescription
passedOptionsCustomIntersectionObserverInit✔️An object holding configuration that will be used in the hook.

passedOptions

CustomIntersectionObserverInit

Interface CustomIntersectionObserverInit extends IntersectionObserverInit object and adds additional properties.

PropertyTypeDescriptionDefault
rootElement|DocumentAncestor of the target element which is used to check target visibility.null
elementHTMLElementA custom target for the hook to track.device viewport
rootMarginstringRoot element's marginal bounding box necessary to compute intersections.100px 0px 0px 0px
thresholdnumber|number[]% of the target's visibility before isVisible becomes true. Range 0.0 - 1.00
multiplebooleanWhen false, stop observing target element as soon as isVisible is true.false
defaultValuebooleanDefault value of isVisible on first render.false

Return value

HookResults

PropertyTypeDescription
isVisiblebooleantrue when DOM element is visible. Defaults to passed defaultValue.
setReffunction|nullCallback passed to ref attribute of the HTML element that needs to be observed. null if element was provided
refMutableRefObject<ObservedHTMLElement>Reference object of the targeted HTML element which can be accessed in useEffect.