~/components/letter-reveal
Letter Reveal
Splits a heading into letters that spring up and flip into place one after another.
Hello, terminal.
hover the letters
letter-reveal.tsx
"use client";
import { MotionConfig, motion, type Variants } from "framer-motion";
import { Fragment, type ElementType } from "react";
export type LetterRevealProps = {
/** The text to animate. Spaces are kept and words never break mid-word. */
text: string;
/** Element to render, e.g. "h1" for a hero heading. @default "span" */
as?: ElementType;
/** Seconds before the first letter starts. @default 0 */
delay?: number;
/** Seconds between letters. @default 0.035 */
stagger?: number;
/** "mount" plays once on load; "view" replays every time the text scrolls into view. @default "mount" */
trigger?: "mount" | "view";
/** Hold the letters hidden until this turns true (e.g. after an intro screen ends). Only used with trigger "mount". @default true */
play?: boolean;
/** Letters hop up and turn blue on hover. @default true */
hover?: boolean;
/** Animate even when the user prefers reduced motion. Use only behind an explicit action. @default false */
ignoreReducedMotion?: boolean;
/** Extra classes for the root element. */
className?: string;
};
type Custom = { delay: number; stagger: number };
const word: Variants = {
hidden: {},
show: (c: Custom) => ({ transition: { staggerChildren: c.stagger, delayChildren: c.delay } }),
};
const letter: Variants = {
hidden: { opacity: 0, y: "0.7em", rotateX: -80 },
show: { opacity: 1, y: 0, rotateX: 0, transition: { type: "spring", stiffness: 260, damping: 18 } },
};
/**
* Splits text into letters that spring up and flip into place one after another.
* Screen readers get the plain text once; the letter spans are hidden from them.
* Under reduced motion the letters only fade in (no movement).
*/
export function LetterReveal({
text,
as: Tag = "span",
delay = 0,
stagger = 0.035,
trigger = "mount",
play = true,
hover = true,
ignoreReducedMotion = false,
className,
}: LetterRevealProps) {
const on =
trigger === "view"
? { whileInView: "show", viewport: { once: false, amount: 0.8 } }
: { animate: play ? "show" : "hidden" };
const words = text.split(" ");
return (
<Tag className={className}>
<span className="sr-only">{text}</span>
<MotionConfig reducedMotion={ignoreReducedMotion ? "never" : "user"}>
<motion.span
aria-hidden="true"
className="inline"
initial="hidden"
variants={word}
custom={{ delay, stagger } satisfies Custom}
{...on}
>
{words.map((w, wi) => (
<Fragment key={wi}>
{wi > 0 && " "}
<span className="inline-block whitespace-nowrap [perspective:600px]">
{[...w].map((ch, i) => (
<motion.span
key={i}
className="inline-block"
variants={letter}
whileHover={hover ? { y: "-0.18em", color: "var(--mpk-blue)", transition: { duration: 0.15 } } : undefined}
>
{ch}
</motion.span>
))}
</span>
</Fragment>
))}
</motion.span>
</MotionConfig>
</Tag>
);
}Installation
terminal
npx mpkcn add letter-reveal01Install the dependencies
terminal
npm i framer-motion02Add mpkcn-theme.css if you haven't yet (see Installation)
03Copy the source into your components folder
components/letter-reveal.tsx
"use client";
import { MotionConfig, motion, type Variants } from "framer-motion";
import { Fragment, type ElementType } from "react";
export type LetterRevealProps = {
/** The text to animate. Spaces are kept and words never break mid-word. */
text: string;
/** Element to render, e.g. "h1" for a hero heading. @default "span" */
as?: ElementType;
/** Seconds before the first letter starts. @default 0 */
delay?: number;
/** Seconds between letters. @default 0.035 */
stagger?: number;
/** "mount" plays once on load; "view" replays every time the text scrolls into view. @default "mount" */
trigger?: "mount" | "view";
/** Hold the letters hidden until this turns true (e.g. after an intro screen ends). Only used with trigger "mount". @default true */
play?: boolean;
/** Letters hop up and turn blue on hover. @default true */
hover?: boolean;
/** Animate even when the user prefers reduced motion. Use only behind an explicit action. @default false */
ignoreReducedMotion?: boolean;
/** Extra classes for the root element. */
className?: string;
};
type Custom = { delay: number; stagger: number };
const word: Variants = {
hidden: {},
show: (c: Custom) => ({ transition: { staggerChildren: c.stagger, delayChildren: c.delay } }),
};
const letter: Variants = {
hidden: { opacity: 0, y: "0.7em", rotateX: -80 },
show: { opacity: 1, y: 0, rotateX: 0, transition: { type: "spring", stiffness: 260, damping: 18 } },
};
/**
* Splits text into letters that spring up and flip into place one after another.
* Screen readers get the plain text once; the letter spans are hidden from them.
* Under reduced motion the letters only fade in (no movement).
*/
export function LetterReveal({
text,
as: Tag = "span",
delay = 0,
stagger = 0.035,
trigger = "mount",
play = true,
hover = true,
ignoreReducedMotion = false,
className,
}: LetterRevealProps) {
const on =
trigger === "view"
? { whileInView: "show", viewport: { once: false, amount: 0.8 } }
: { animate: play ? "show" : "hidden" };
const words = text.split(" ");
return (
<Tag className={className}>
<span className="sr-only">{text}</span>
<MotionConfig reducedMotion={ignoreReducedMotion ? "never" : "user"}>
<motion.span
aria-hidden="true"
className="inline"
initial="hidden"
variants={word}
custom={{ delay, stagger } satisfies Custom}
{...on}
>
{words.map((w, wi) => (
<Fragment key={wi}>
{wi > 0 && " "}
<span className="inline-block whitespace-nowrap [perspective:600px]">
{[...w].map((ch, i) => (
<motion.span
key={i}
className="inline-block"
variants={letter}
whileHover={hover ? { y: "-0.18em", color: "var(--mpk-blue)", transition: { duration: 0.15 } } : undefined}
>
{ch}
</motion.span>
))}
</span>
</Fragment>
))}
</motion.span>
</MotionConfig>
</Tag>
);
}04Update the import paths to match your project
deps:framer-motion@13.4.4· tested with react@19.3.0
Usage
tsx
import { LetterReveal } from "@/components/mpkcn/letter-reveal";
export function Hero() {
return <LetterReveal as="h1" text="Hello, terminal." className="text-5xl font-semibold" />;
}Each letter starts 0.7em low, tilted back 80°, and springs into place (stiffness 260, damping 18), 35ms after the one before it. Words never break in the middle. Hovering a letter lifts it and turns it blue.
Examples
Slower stagger
one letter at a time
hover the letters
tsx
<LetterReveal text="one letter at a time" stagger={0.08} />Replay on every scroll
trigger="view" plays the reveal each time the text comes back into view.
tsx
<LetterReveal as="h2" text="Selected work" trigger="view" />Wait for an intro
play={false} keeps the letters hidden until you flip it, for example when a Boot Screen finishes:
tsx
"use client";
import { useState } from "react";
import { BootScreen } from "@/components/mpkcn/boot-screen";
import { LetterReveal } from "@/components/mpkcn/letter-reveal";
export function Hero() {
const [booted, setBooted] = useState(false);
return (
<>
<BootScreen onDone={() => setBooted(true)} />
<LetterReveal as="h1" text="Hello, terminal." play={booted} delay={0.2} />
</>
);
}Props
| Prop | Type | Default | Description |
|---|---|---|---|
| text | string | — | The text to animate. Spaces are kept and words never break mid-word. |
| as | React.ElementType | "span" | Element to render, e.g. "h1" for a hero heading. |
| delay | number | 0 | Seconds before the first letter starts. |
| stagger | number | 0.035 | Seconds between letters. |
| trigger | "mount" | "view" | "mount" | "mount" plays once on load; "view" replays every time the text scrolls into view. |
| play | boolean | true | Hold the letters hidden until this turns true. Only used with trigger "mount". |
| hover | boolean | true | Letters hop up and turn blue on hover. |
| 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 root element. |
Accessibility
- Screen readers read the text once, whole, from a visually hidden copy. The letter spans are
aria-hidden, so the heading is never spelled out letter by letter. - Under
prefers-reduced-motion: reducenothing moves or flips: the letters only fade in, and the hover hop is reduced to the color change. - Server and client render the same markup, so there is no hydration mismatch.