Pools of colour swirl and flow inside the letters, for headings that feel alive without moving an inch.
Words on the faces of a cube that rolls forward to the next one, the old word tipping away as the new one rises to face you.
Ship
Customize
Usage
import { TextCube } from "@/components/motionkit/text-cube";
<h1 className="text-6xl font-semibold">
Ship{" "}
<TextCube
words={["faster", "bolder", "smoother", "brighter"]}
/>
</h1>Motion
The exact curves this component runs, plotted with Motion's own maths.
Cube turns
Runs on its own
Hero headlines with a rotating keyword, feature lists, taglines and anywhere one word should change on a beat.
Four faces sit round a box with preserve-3d, each turned 90 degrees further and pushed out half a line, and the box springs round by 90 degrees for every new word. Each face shows the word that's due when it comes round to the front, so any number of words cycle on four faces. Invisible copies of every word size the cube to the longest, and the visible faces are hidden from screen readers in favour of one list.
Works in the App Router as is. Requires Tailwind CSS v4.
One command adds the file and installs its dependencies. Needs a components.json (run npx shadcn@latest init once if you don't have one).
npx shadcn@latest add https://www.motionkitui.com/r/text-cube.jsonnpm install motionSave as components/motionkit/text-cube.tsx.
"use client";
import { useEffect, useState, useSyncExternalStore, type ElementType } from "react";
import { AnimatePresence, motion, useReducedMotion } from "motion/react";
type TextCubeProps = {
/** The words on the cube's faces, in order. */
words: string[];
/** Seconds each word stays facing you. */
interval?: number;
/** The element to render, e.g. "span" inside a heading. */
as?: ElementType;
/** Colour of the soft glow behind the words. */
accent?: string;
className?: string;
};
const FACE = 1.15; // a face's height in em, roughly one line of text
const noSubscribe = () => () => {};
/**
* Words on the faces of a cube that rolls forward to the next one: the old word tips away over the top edge while
* the new one rises to face you, with light catching the edges. Sized to the longest word, so nothing jumps.
*/
export function TextCube({ words, interval = 2.4, as: Tag = "span", accent = "#B9C7FF", className = "" }: TextCubeProps) {
// the server can't know the visitor's motion setting, so the first render in the browser matches the server's
// cube and the reduced-motion version takes over once hydrated
const hydrated = useSyncExternalStore(noSubscribe, () => true, () => false);
const reduceMotion = useReducedMotion() && hydrated;
const [step, setStep] = useState(0);
useEffect(() => {
if (words.length < 2) return;
const timer = window.setInterval(() => setStep((current) => current + 1), interval * 1000);
return () => window.clearInterval(timer);
}, [interval, words.length]);
const word = (index: number) => words[((index % words.length) + words.length) % words.length];
// each of the four faces shows the word due when it turns to face you: this one, the next, and the one before
const faceWord = (face: number) => {
const offset = (((face - step) % 4) + 4) % 4;
return word(step + (offset === 3 ? -1 : offset));
};
return (
<Tag className={`relative inline-grid ${className}`}>
<span className="sr-only">{words.join(", ")}</span>
{/* every word stacked invisibly in one cell sizes the cube to the longest */}
{words.map((w) => (
<span key={w} aria-hidden className="invisible col-start-1 row-start-1 whitespace-nowrap" style={{ lineHeight: `${FACE}em` }}>
{w}
</span>
))}
{reduceMotion ? (
<span aria-hidden className="absolute inset-0">
<AnimatePresence mode="wait" initial={false}>
<motion.span key={step} className="absolute inset-0 whitespace-nowrap" style={{ lineHeight: `${FACE}em` }} initial={{ opacity: 0 }} animate={{ opacity: 1 }} exit={{ opacity: 0 }} transition={{ duration: 0.25 }}>
{word(step)}
</motion.span>
</AnimatePresence>
</span>
) : (
<span aria-hidden className="absolute inset-0 [perspective:700px]">
<motion.span
className="absolute inset-0 [transform-style:preserve-3d]"
style={{ translateZ: `-${FACE / 2}em` }}
animate={{ rotateX: step * 90 }}
transition={{ type: "spring", stiffness: 140, damping: 18, mass: 0.9 }}
>
{[0, 1, 2, 3].map((face) => (
<span
key={face}
className="absolute inset-0 whitespace-nowrap [backface-visibility:hidden]"
// a soft glow in the accent colour lifts each word off the page as it turns
style={{ lineHeight: `${FACE}em`, transform: `rotateX(${-face * 90}deg) translateZ(${FACE / 2}em)`, textShadow: `0 0 28px color-mix(in srgb, ${accent} 45%, transparent)` }}
>
{faceWord(face)}
</span>
))}
</motion.span>
</span>
)}
</Tag>
);
}
| Prop | Type | Default | Description |
|---|---|---|---|
| words | string[] | — | The words on the faces, in order. |
| interval | number | 2.4 | Seconds each word stays facing you. |
| accent | string | "#B9C7FF" | Colour of the soft glow behind the words. |
| as | ElementType | "span" | Element to render. |
How we audit: the WCAG 2.2 animation checklist and respecting reduced motion in React.
Yes. The file starts with "use client", so you can import it straight into a Server Component page or layout. It works in Vite as is, and in Astro as a React island.
Only the motion package, beside React and Tailwind CSS v4. There is no MotionKit package and no Tailwind config to add.
Yes. Run npx shadcn@latest add https://www.motionkitui.com/r/text-cube.json in any project with a components.json. It saves the file to components/motionkit and installs its dependencies.
With reduced motion on, the words swap with a plain fade instead of turning.
Pools of colour swirl and flow inside the letters, for headings that feel alive without moving an inch.
A headline folded like paper that opens out as it scrolls into view: each line swings down from its crease, concertina-style, with a shadow that lifts as it flattens.
Headline text with a slowly flowing, seamlessly looping gradient.