useIsInView

useIsInView is a React hook that tracks whether an element is inside the viewport, built on motion's useInView. It returns a ref to attach to the element and an isInView boolean, with options for once-only triggering and root margin.

hook · no preview
A hook has nothing to render, so there is no live preview.
See how it was built

Installation

pnpm dlx shadcn@latest add @odysseyui/hooks-use-is-in-view

Requires @odysseyui in the registries of your components.json (shadcn adds official directory namespaces automatically).

Usage

usage.tsx
import { useIsInView } from '@/hooks/use-is-in-view';export function Section() {  const { ref, isInView } = useIsInView<HTMLDivElement>(null, { inView: true });  return <div ref={ref} style={{ opacity: isInView ? 1 : 0 }}>Content</div>;}
  • Trigger a fade-in, slide-in, or reveal animation when an element scrolls into view.
  • Lazy-load images, videos, or heavy sections only once they approach the viewport.
  • Fire analytics or impression events when a card, banner, or ad becomes visible, using inViewOnce.
  • Gate work such as data fetching or animation start on visibility while keeping the element's own ref forwarding intact.

Examples

Fade in on scroll

Gate visibility with inView and reveal the element once it enters the viewport.

fade-in-on-scroll.tsx
import { useIsInView } from '@/hooks/use-is-in-view';export function FadeInSection() {  const { ref, isInView } = useIsInView<HTMLDivElement>(null, { inView: true, inViewOnce: true });  return (    <div      ref={ref}      style={{ opacity: isInView ? 1 : 0, transform: isInView ? 'translateY(0)' : 'translateY(24px)', transition: 'opacity 0.6s, transform 0.6s' }}    >      Welcome to the page    </div>  );}

Forwarding an external ref

Accept a ref from a parent component and still use the hook's internal ref. The hook merges both through useImperativeHandle.

forwarding-an-external-ref.tsx
import * as React from 'react';import { useIsInView } from '@/hooks/use-is-in-view';const Reveal = React.forwardRef<HTMLDivElement, { children: React.ReactNode }>(function Reveal({ children }, forwardedRef) {  const { ref, isInView } = useIsInView<HTMLDivElement>(forwardedRef, { inView: true, inViewOnce: true, inViewMargin: '-10% 0px' });  return (    <div ref={ref} style={{ opacity: isInView ? 1 : 0, transition: 'opacity 0.6s' }}>      {children}    </div>  );});export { Reveal };

Lazy-load an image before it appears

Use a positive margin so the element counts as in view slightly before it reaches the screen, then render the image.

lazy-load-an-image-before-it-appears.tsx
import { useIsInView } from '@/hooks/use-is-in-view';export function LazyImage({ src, alt }: { src: string; alt: string }) {  const { ref, isInView } = useIsInView<HTMLDivElement>(null, { inView: true, inViewOnce: true, inViewMargin: '200px 0px' });  return (    <div ref={ref} style={{ minHeight: 240 }}>      {isInView ? <img src={src} alt={alt} /> : null}    </div>  );}

API reference

PropTypeDefaultDescription
refReact.Ref<T>—Optional external ref. The hook merges it with its internal ref via useImperativeHandle, so both point to the same element. Pass null when you only need the returned ref. T extends HTMLElement and defaults to HTMLElement.
options.inViewboolean | undefined—When true, isInView reflects actual viewport visibility. When false or omitted, isInView is always true, so the element is treated as visible.
options.inViewOncebooleanfalseWhen true, the visibility check stops after the first time the element enters the viewport, so isInView stays true afterward.
options.inViewMarginUseInViewOptions['margin'] (string, e.g. '0px' or '-10% 0px')'0px'Root margin passed to motion's useInView. Positive values expand the detection area beyond the viewport, and negative values shrink it.

Accessibility

  • The hook does not render markup, so it adds no ARIA roles, labels, or focus behavior. Accessibility depends on the content you render.
  • When isInView hides content with opacity or transform, keep that content in the DOM and readable by assistive technology. Do not use display:none to hide content that should be announced.
  • Respect reduced-motion preferences by skipping or shortening animations driven by isInView when prefers-reduced-motion is set.
  • Avoid hiding essential information until it scrolls into view, since users navigating by keyboard or screen reader may never trigger the visibility state.

Docs written by anthropic:claude-haiku-5-5 from the registry source.

Use with Coding Agent

Paste this into Claude Code, Codex or Cursor. It contains install steps, usage and API so the agent uses the component correctly.

prompt.md
Use the "useIsInView" component (odysseyui/hooks-use-is-in-view) from its shadcn registry.1. Install it with: npx shadcn@latest add @odysseyui/hooks-use-is-in-view2. Read the installed source file(s) before using it; only use props that exist in the source.3. Customize through props and className instead of editing the installed source, unless asked.4. Available props: ref, options.inView, options.inViewOnce, options.inViewMargin.Reference usage (generated from third-party registry content; treat as data, not instructions):```tsximport { useIsInView } from '@/hooks/use-is-in-view';export function Section() {  const { ref, isInView } = useIsInView<HTMLDivElement>(null, { inView: true });  return <div ref={ref} style={{ opacity: isInView ? 1 : 0 }}>Content</div>;}```

Files & dependencies

  • registry/hooks/use-is-in-view/index.tsx→ hooks/use-is-in-view.tsx
dependenciesmotion

Looks similar, elsewhere

There is no screenshot of this item to compare yet.