# Scroll Reveal

> Elements animate into view when they enter the viewport. Supports any Motion variant, configurable thresholds, and a once-only mode.

Docs: https://ui.cnippet.dev/motion/scroll-animations/scroll-reveal

```tsx
import { ScrollReveal } from "@/registry/default/motion/scroll-reveal";

const cards = [
  {
    description: "Composable components that snap together cleanly.",
    icon: "◈",
    title: "Composable",
  },
  {
    description: "Every animation respects prefers-reduced-motion.",
    icon: "⬡",
    title: "Accessible",
  },
  {
    description: "Zero runtime overhead — pure CSS where possible.",
    icon: "◎",
    title: "Performant",
  },
];

export default function ScrollRevealCards() {
  return (
    <div className="flex min-h-50 items-center justify-center px-6">
      <div className="grid w-full max-w-2xl grid-cols-1 gap-4 sm:grid-cols-3">
        {cards.map((card, i) => (
          <ScrollReveal
            key={card.title}
            transition={{ delay: i * 0.12, duration: 0.5, ease: "easeOut" }}
            variants={{
              hidden: { opacity: 0, y: 24 },
              visible: { opacity: 1, y: 0 },
            }}
            viewOptions={{ amount: 0.3 }}
          >
            <div className="rounded-xl border border-border bg-card p-5">
              <p className="mb-2 text-2xl">{card.icon}</p>
              <h3 className="font-semibold text-foreground">{card.title}</h3>
              <p className="mt-1 text-muted-foreground text-sm">
                {card.description}
              </p>
            </div>
          </ScrollReveal>
        ))}
      </div>
    </div>
  );
}
```

## Installation

**CLI**

```bash
npx shadcn@latest add @cnippet/scroll-reveal
```

**Manual**

1. Install the following dependencies:

```bash
npm install motion
```

2. Copy and paste the following code into your project.

`components/motion/scroll-reveal.tsx`

```tsx
"use client";

import {
  motion,
  type Transition,
  type UseInViewOptions,
  useInView,
  useReducedMotion,
  type Variant,
} from "motion/react";
import { type ReactNode, useRef, useState } from "react";

export type ScrollRevealProps = {
  children: ReactNode;
  variants?: {
    hidden: Variant;
    visible: Variant;
  };
  transition?: Transition;
  viewOptions?: UseInViewOptions;
  as?: React.ElementType;
  once?: boolean;
};

const defaultVariants = {
  hidden: { opacity: 0 },
  visible: { opacity: 1 },
};

export function ScrollReveal({
  children,
  variants = defaultVariants,
  transition,
  viewOptions,
  as = "div",
  once,
}: ScrollRevealProps) {
  const reduceMotion = useReducedMotion();
  const ref = useRef(null);
  const isInView = useInView(ref, viewOptions);
  const [isViewed, setIsViewed] = useState(false);

  const MotionComponent = motion[
    as as keyof typeof motion
  ] as typeof motion.div;

  return (
    <MotionComponent
      animate={isInView || isViewed ? "visible" : "hidden"}
      initial="hidden"
      onAnimationComplete={() => {
        if (once) setIsViewed(true);
      }}
      ref={ref}
      transition={reduceMotion ? { duration: 0 } : transition}
      variants={variants}
    >
      {children}
    </MotionComponent>
  );
}
```

3. Update the import paths to match your project setup.

## Usage

```tsx
import { ScrollReveal } from "@/components/motion/scroll-reveal";
```

```tsx
<ScrollReveal
  variants={{ hidden: { opacity: 0, y: 24 }, visible: { opacity: 1, y: 0 } }}
  transition={{ duration: 0.5, ease: "easeOut" }}
>
  <YourContent />
</ScrollReveal>
```

## Anatomy

```tsx
<ScrollReveal once> {/* motion element that animates from "hidden" to "visible" */}
  <YourContent />
</ScrollReveal>
```

## Good to know

- The animation plays when the element enters the viewport. Tune when that happens with `viewOptions` (Motion's `useInView` options, e.g. `{ amount: 0.5 }`).
- Without `once`, it animates out again when it leaves the viewport and back in on return.
- `variants` needs both a `hidden` and a `visible` state. The default is a simple fade.
- When the user has turned on reduced motion, content appears in its `visible` state straight away when it enters the viewport.

## Props

| Prop | Type | Default | Description |
|------|------|---------|-------------|
| `children` | `ReactNode` | — | Content to reveal |
| `variants` | `{ hidden: Variant; visible: Variant }` | `{ hidden: { opacity: 0 }, visible: { opacity: 1 } }` | Motion variants for the hidden and visible states |
| `transition` | `Transition` | — | Transition applied to the visible state |
| `viewOptions` | `UseInViewOptions` | — | Options passed to Motion's `useInView` (e.g. `amount`, `margin`) |
| `as` | `React.ElementType` | `"div"` | HTML element or component to render as |
| `once` | `boolean` | — | When `true`, the element stays visible after the first reveal and never hides again |

## Examples

### Feature Cards

Three cards slide up and fade in with staggered `delay` values as they enter the viewport. Each card is its own `ScrollReveal` instance.

```tsx
import { ScrollReveal } from "@/registry/default/motion/scroll-reveal";

const cards = [
  {
    description: "Composable components that snap together cleanly.",
    icon: "◈",
    title: "Composable",
  },
  {
    description: "Every animation respects prefers-reduced-motion.",
    icon: "⬡",
    title: "Accessible",
  },
  {
    description: "Zero runtime overhead — pure CSS where possible.",
    icon: "◎",
    title: "Performant",
  },
];

export default function ScrollRevealCards() {
  return (
    <div className="flex min-h-50 items-center justify-center px-6">
      <div className="grid w-full max-w-2xl grid-cols-1 gap-4 sm:grid-cols-3">
        {cards.map((card, i) => (
          <ScrollReveal
            key={card.title}
            transition={{ delay: i * 0.12, duration: 0.5, ease: "easeOut" }}
            variants={{
              hidden: { opacity: 0, y: 24 },
              visible: { opacity: 1, y: 0 },
            }}
            viewOptions={{ amount: 0.3 }}
          >
            <div className="rounded-xl border border-border bg-card p-5">
              <p className="mb-2 text-2xl">{card.icon}</p>
              <h3 className="font-semibold text-foreground">{card.title}</h3>
              <p className="mt-1 text-muted-foreground text-sm">
                {card.description}
              </p>
            </div>
          </ScrollReveal>
        ))}
      </div>
    </div>
  );
}
```

### Feature Section (Split Layout)

A two-column layout where the image slides in from the left and the text from the right, creating a balanced split reveal.

```tsx
import { ScrollReveal } from "@/registry/default/motion/scroll-reveal";

export default function ScrollRevealFeatureSection() {
  return (
    <div className="flex min-h-50 items-center justify-center px-6">
      <div className="flex w-full max-w-2xl flex-col items-center gap-6 sm:flex-row sm:gap-10">
        <ScrollReveal
          transition={{ duration: 0.6, ease: "easeOut" }}
          variants={{
            hidden: { opacity: 0, x: -32 },
            visible: { opacity: 1, x: 0 },
          }}
          viewOptions={{ amount: 0.4 }}
        >
          <div className="flex h-36 w-full items-center justify-center rounded-2xl border border-border bg-gradient-to-br from-violet-500/10 to-purple-500/5 sm:h-44 sm:w-56">
            <span className="text-5xl">✦</span>
          </div>
        </ScrollReveal>

        <ScrollReveal
          transition={{ delay: 0.15, duration: 0.6, ease: "easeOut" }}
          variants={{
            hidden: { opacity: 0, x: 32 },
            visible: { opacity: 1, x: 0 },
          }}
          viewOptions={{ amount: 0.4 }}
        >
          <div className="flex-1">
            <p className="font-semibold text-primary text-xs uppercase tracking-widest">
              New
            </p>
            <h2 className="mt-1 font-bold text-2xl text-foreground">
              Motion-first design system
            </h2>
            <p className="mt-2 text-muted-foreground text-sm leading-relaxed">
              164 animated components built on Motion and Tailwind — ready to
              drop into your next project and customise in seconds.
            </p>
          </div>
        </ScrollReveal>
      </div>
    </div>
  );
}
```

### Stepped List

Each list item slides in from the left with an incrementing delay, building a step-by-step visual sequence on scroll.

```tsx
import { ScrollReveal } from "@/registry/default/motion/scroll-reveal";

const items = [
  "Pick a component",
  "Run the CLI command",
  "Paste into your project",
  "Customise props & styles",
  "Ship to production",
];

export default function ScrollRevealList() {
  return (
    <div className="flex min-h-50 items-center justify-center px-6">
      <ol className="w-full max-w-xs space-y-2">
        {items.map((item, i) => (
          <ScrollReveal
            as="li"
            key={item}
            transition={{ delay: i * 0.1, duration: 0.45, ease: "easeOut" }}
            variants={{
              hidden: { opacity: 0, x: -20 },
              visible: { opacity: 1, x: 0 },
            }}
            viewOptions={{ amount: 0.5 }}
          >
            <div className="flex items-center gap-3 rounded-lg border border-border bg-card px-4 py-3">
              <span className="flex h-5 w-5 shrink-0 items-center justify-center rounded-full bg-primary font-bold text-[10px] text-primary-foreground">
                {i + 1}
              </span>
              <span className="font-medium text-foreground text-sm">
                {item}
              </span>
            </div>
          </ScrollReveal>
        ))}
      </ol>
    </div>
  );
}
```
