~/components/reveal
Reveal
Fade and slide content in as it scrolls into view. Stays still under reduced motion.
$ reveal --from up
rises into place
$ reveal --from left
slides in from the left
$ reveal --from right
slides in from the right
$ reveal --from scale
grows from 94%
"use client";
import { MotionConfig, motion, type Variants } from "framer-motion";
import type { ReactNode } from "react";
type RevealFrom = "up" | "left" | "right" | "scale";
export type RevealProps = {
/** Content to reveal. */
children: ReactNode;
/** Direction the content comes in from. @default "up" */
from?: RevealFrom;
/** Delay before the animation starts, in seconds. @default 0 */
delay?: number;
/** Animate only the first time it scrolls into view. @default true */
once?: boolean;
/** Animate even when the user prefers reduced motion (e.g. behind an explicit "play" button). Use sparingly. @default false */
ignoreReducedMotion?: boolean;
/** Extra classes for the wrapper div. */
className?: string;
};
type Custom = { delay: number; from: RevealFrom };
const offset: Record<RevealFrom, { x?: number; y?: number; scale?: number }> = {
up: { y: 28 },
left: { x: -32 },
right: { x: 32 },
scale: { scale: 0.94, y: 12 },
};
const variants: Variants = {
hidden: (c: Custom) => ({ opacity: 0, ...offset[c.from] }),
show: (c: Custom) => ({
opacity: 1,
x: 0,
y: 0,
scale: 1,
transition: { duration: 0.7, ease: [0.22, 1, 0.36, 1], delay: c.delay },
}),
};
/**
* Fades and slides its children in when they scroll into view.
* Under prefers-reduced-motion nothing moves: position and scale snap into place
* and only the opacity fades. The server and client render the same markup, so
* there is no hydration mismatch.
*/
export function Reveal({
children,
from = "up",
delay = 0,
once = true,
ignoreReducedMotion = false,
className,
}: RevealProps) {
return (
<MotionConfig reducedMotion={ignoreReducedMotion ? "never" : "user"}>
<motion.div
className={className}
initial="hidden"
whileInView="show"
viewport={{ once, amount: 0.2 }}
variants={variants}
custom={{ delay, from } satisfies Custom}
>
{children}
</motion.div>
</MotionConfig>
);
}Installation
npx mpkcn add reveal01Install the dependencies
npm i framer-motion02Add mpkcn-theme.css if you haven't yet (see Installation)
03Copy the source into your components folder
"use client";
import { MotionConfig, motion, type Variants } from "framer-motion";
import type { ReactNode } from "react";
type RevealFrom = "up" | "left" | "right" | "scale";
export type RevealProps = {
/** Content to reveal. */
children: ReactNode;
/** Direction the content comes in from. @default "up" */
from?: RevealFrom;
/** Delay before the animation starts, in seconds. @default 0 */
delay?: number;
/** Animate only the first time it scrolls into view. @default true */
once?: boolean;
/** Animate even when the user prefers reduced motion (e.g. behind an explicit "play" button). Use sparingly. @default false */
ignoreReducedMotion?: boolean;
/** Extra classes for the wrapper div. */
className?: string;
};
type Custom = { delay: number; from: RevealFrom };
const offset: Record<RevealFrom, { x?: number; y?: number; scale?: number }> = {
up: { y: 28 },
left: { x: -32 },
right: { x: 32 },
scale: { scale: 0.94, y: 12 },
};
const variants: Variants = {
hidden: (c: Custom) => ({ opacity: 0, ...offset[c.from] }),
show: (c: Custom) => ({
opacity: 1,
x: 0,
y: 0,
scale: 1,
transition: { duration: 0.7, ease: [0.22, 1, 0.36, 1], delay: c.delay },
}),
};
/**
* Fades and slides its children in when they scroll into view.
* Under prefers-reduced-motion nothing moves: position and scale snap into place
* and only the opacity fades. The server and client render the same markup, so
* there is no hydration mismatch.
*/
export function Reveal({
children,
from = "up",
delay = 0,
once = true,
ignoreReducedMotion = false,
className,
}: RevealProps) {
return (
<MotionConfig reducedMotion={ignoreReducedMotion ? "never" : "user"}>
<motion.div
className={className}
initial="hidden"
whileInView="show"
viewport={{ once, amount: 0.2 }}
variants={variants}
custom={{ delay, from } satisfies Custom}
>
{children}
</motion.div>
</MotionConfig>
);
}04Update the import paths to match your project
Usage
Wrap anything that should appear as it scrolls into view:
import { Reveal } from "@/components/mpkcn/reveal";
export function Features() {
return (
<section className="grid gap-4 sm:grid-cols-3">
<Reveal>
<Card title="Copy" />
</Reveal>
<Reveal delay={0.1}>
<Card title="Paste" />
</Reveal>
<Reveal delay={0.2}>
<Card title="Own" />
</Reveal>
</section>
);
}The content fades in over 0.7s with the ease [0.22, 1, 0.36, 1] once 20% of it is visible. Reveal is a client component, but its children can be server components.
Examples
Directions
from sets where the content comes from: up (default), left, right or scale.
$ reveal --from up
rises into place
$ reveal --from left
slides in from the left
$ reveal --from right
slides in from the right
$ reveal --from scale
grows from 94%
<Reveal from="up">…</Reveal>
<Reveal from="left" delay={0.12}>…</Reveal>
<Reveal from="right" delay={0.24}>…</Reveal>
<Reveal from="scale" delay={0.36}>…</Reveal>Stagger a list
Give each item a growing delay to cascade them:
{items.map((item, i) => (
<Reveal key={item.id} delay={i * 0.08}>
<Row item={item} />
</Reveal>
))}Replay on every scroll
By default an element animates only the first time it enters the viewport. Set once={false} to animate it every time it comes back into view.
<Reveal from="scale" once={false}>
<Stat value="99.9%" label="uptime" />
</Reveal>Props
| Prop | Type | Default | Description |
|---|---|---|---|
| children | React.ReactNode | — | Content to reveal. |
| from | "up" | "left" | "right" | "scale" | "up" | Direction the content comes in from. |
| delay | number | 0 | Delay before the animation starts, in seconds. |
| once | boolean | true | Animate only the first time it scrolls into view. |
| ignoreReducedMotion | boolean | false | Animate even when the user prefers reduced motion. Only use it behind an explicit action, like a "play anyway" button. |
| className | string | — | Extra classes for the wrapper div. |
Accessibility
- Under
prefers-reduced-motion: reducenothing moves: position and scale snap into place and only the opacity fades.ignoreReducedMotionoverrides this; keep it behind an explicit user action. - The wrapper is a plain
divwith no role, so the content is read exactly as written. - Server and client render the same markup, so there is no hydration mismatch.