Skip to content

Scroll Reveal Section

Content blurs in on scroll with a progress bar.

Installation

CLI

npx shadcn@latest add https://hantui-theta.vercel.app/r/scroll-reveal.json

Manual

  1. Install the dependencies

    npm install clsx motion tailwind-merge
  2. Add the shared helpers

    Components use cn() from lib/utils.ts and the motion tokens below. See Installation if you have not set them up yet.

    lib/motion.ts
    import type { Transition, Variants } from "motion/react";
    
    /** Durations in seconds. */
    export const duration = {
      instant: 0.1,
      quick: 0.18,
      base: 0.28,
      slow: 0.45,
    } as const;
    
    /** Cubic-bezier curves. */
    export const ease = {
      /** Entering, UI response. */
      out: [0.22, 1, 0.36, 1],
      /** Moving on screen. */
      inOut: [0.65, 0, 0.35, 1],
      /** Sheets and the mobile menu. */
      drawer: [0.32, 0.72, 0, 1],
    } as const;
    
    /** Everyday UI: presses, icon swaps, popovers and modals. Exits run at `exitFactor` of the enter. */
    export const ui = {
      press: { scale: 0.96, duration: 0.12 },
      icon: { type: "spring", duration: 0.3, bounce: 0 },
      popover: { duration: 0.18, ease: ease.out },
      modal: { duration: 0.24, ease: ease.out },
      exitFactor: 0.6,
    } as const;
    
    /** Once-per-visit motion on the landing page: hero letters, the reel and the afterimage. */
    export const kinetic = {
      heroLetter: { duration: 0.6, ease: ease.out },
      /** Delay per letter, eased so the wave slows toward the end. */
      heroStep: 0.035,
      reelSwap: { type: "spring", duration: 0.5, bounce: 0.1 },
      /** Two afterimages: lag in seconds and opacity of each. */
      ghost: { lag: [0.04, 0.09], opacity: [0.14, 0.06] },
    } as const;
    
    /** Spring presets. */
    export const spring = {
      snappy: { type: "spring", stiffness: 500, damping: 30 },
      smooth: { type: "spring", stiffness: 260, damping: 26 },
      bouncy: { type: "spring", stiffness: 300, damping: 15 },
    } as const satisfies Record<string, Transition>;
    
    /** Stagger between siblings, in seconds (0.03–0.06). */
    export const stagger = {
      tight: 0.03,
      base: 0.045,
      loose: 0.06,
    } as const;
    
    /** Blur + fade + y-offset. Falls back to a plain fade under reduced motion. */
    export function enterVariants(reduceMotion?: boolean | null, y = 8): Variants {
      if (reduceMotion) {
        return {
          hidden: { opacity: 0 },
          visible: { opacity: 1, transition: { duration: duration.base } },
          exit: { opacity: 0, transition: { duration: duration.quick } },
        };
      }
      return {
        hidden: { opacity: 0, y, filter: "blur(4px)" },
        visible: {
          opacity: 1,
          y: 0,
          filter: "blur(0px)",
          transition: { duration: duration.base, ease: ease.out },
        },
        exit: {
          opacity: 0,
          y: -y / 2,
          filter: "blur(4px)",
          transition: { duration: duration.quick, ease: ease.out },
        },
      };
    }
    
    /** Scale 0.96 → 1 for popovers, menus and dialogs. */
    export function overlayVariants(reduceMotion?: boolean | null): Variants {
      if (reduceMotion) {
        return {
          hidden: { opacity: 0 },
          visible: { opacity: 1, transition: { duration: duration.quick } },
          exit: { opacity: 0, transition: { duration: duration.instant } },
        };
      }
      return {
        hidden: { opacity: 0, scale: 0.96, filter: "blur(2px)" },
        visible: {
          opacity: 1,
          scale: 1,
          filter: "blur(0px)",
          transition: { duration: duration.quick, ease: ease.out },
        },
        exit: {
          opacity: 0,
          scale: 0.96,
          filter: "blur(2px)",
          transition: { duration: duration.instant + 0.02, ease: ease.out },
        },
      };
    }
    
    /** Parent variants that stagger their children in. */
    export function staggerVariants(gap: number = stagger.base, delayChildren = 0): Variants {
      return {
        hidden: {},
        visible: { transition: { staggerChildren: gap, delayChildren } },
        exit: { transition: { staggerChildren: gap / 2, staggerDirection: -1 } },
      };
    }
  3. Copy the source

    components/ui/scroll-reveal/scroll-reveal.tsx
    "use client";
    
    import { Children, useRef, type ComponentProps, type ReactNode, type RefObject } from "react";
    import { motion, useReducedMotion, useScroll, useSpring, type Variants } from "motion/react";
    import { cn } from "@/lib/utils";
    import { duration, ease } from "@/lib/motion";
    
    export interface ScrollRevealProps
      extends Omit<ComponentProps<"section">, "children" | "onAnimationStart" | "onDrag" | "onDragStart" | "onDragEnd"> {
      /** Blocks to reveal; each direct child blurs in on its own as it enters view. */
      children: ReactNode;
      /** Show a reading-progress bar pinned to the top of the section. @default true */
      progress?: boolean;
      /** Scrollable element that contains the section, if it isn't the page. Give it position: relative so offsets measure correctly. */
      container?: RefObject<HTMLElement | null>;
      /** Reveal each block only the first time. @default true */
      once?: boolean;
      /** Fraction of a block that must be visible to reveal it. @default 0.35 */
      amount?: number;
      /** Starting blur in px. @default 8 */
      blur?: number;
    }
    
    export function ScrollReveal({
      children,
      progress = true,
      container,
      once = true,
      amount = 0.35,
      blur = 8,
      className,
      ...props
    }: ScrollRevealProps) {
      const reduceMotion = useReducedMotion();
      const ref = useRef<HTMLElement>(null);
      // 0 when the section's top reaches the top of the viewport, 1 when its bottom reaches the bottom.
      const { scrollYProgress } = useScroll({ target: ref, container, offset: ["start start", "end end"] });
      const scaleX = useSpring(scrollYProgress, { stiffness: 300, damping: 40, restDelta: 0.001 });
    
      const variants: Variants = {
        hidden: reduceMotion ? { opacity: 0 } : { opacity: 0, y: 24, filter: `blur(${blur}px)` },
        visible: {
          opacity: 1,
          y: 0,
          filter: "blur(0px)",
          transition: { duration: reduceMotion ? duration.base : duration.slow + 0.2, ease: ease.out },
          transitionEnd: { filter: "none" },
        },
      };
    
      return (
        <section ref={ref} className={cn("relative", className)} {...props}>
          {progress ? (
            <motion.div
              aria-hidden="true"
              className="sticky top-0 z-10 h-0.5 origin-left rounded-full bg-primary"
              style={{ scaleX: reduceMotion ? scrollYProgress : scaleX }}
            />
          ) : null}
          {Children.toArray(children).map((child, i) => (
            <motion.div key={i} variants={variants} initial="hidden" whileInView="visible" viewport={{ root: container, once, amount }}>
              {child}
            </motion.div>
          ))}
        </section>
      );
    }

Usage

import { ScrollReveal } from "@/components/ui/scroll-reveal/scroll-reveal";

export function Article() {
  return (
    <ScrollReveal className="space-y-8">
      <h2>Designing motion people don't notice</h2>
      <p>…</p>
      <img src="/figure.jpg" alt="…" />
    </ScrollReveal>
  );
}

API reference

PropTypeDefaultDescription
children*ReactNode–Blocks to reveal; each direct child blurs in on its own as it enters view.
progressbooleantrueShow a reading-progress bar pinned to the top of the section.
containerRefObject<HTMLElement | null>–Scrollable element that contains the section, if it isn't the page. Give it position: relative so offsets measure correctly.
oncebooleantrueReveal each block only the first time.
amountnumber0.35Fraction of a block that must be visible to reveal it.
blurnumber8Starting blur in px.

Also accepts every native <section> attribute, including ref and className.

  • Each direct child reveals on its own as it enters view (blur, fade and a short rise), once by default.
  • The bar is pinned to the top of the section and fills from the moment the section reaches the top until its end reaches the bottom.
  • Pass `container` when the section scrolls inside an element rather than the page, and make that element positioned (e.g. relative); otherwise the section's position is measured from the wrong ancestor and the bar lags.

Accessibility

  • Content is real, in-order markup; only its appearance animates. The progress bar is decorative and aria-hidden.
  • With reduced motion enabled, blocks fade in without blur or movement, and the bar tracks scroll directly.