# Text Cube

> 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.

Category: Text · Free · Updated 2026-11-10 · Live demo: https://www.motionkitui.com/components/text-cube

## When to use it

Hero headlines with a rotating keyword, feature lists, taglines and anywhere one word should change on a beat.

## How it works

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.

## Installation

```bash
npx shadcn@latest add https://www.motionkitui.com/r/text-cube.json
```

Or copy `components/motionkit/text-cube.tsx` below. Requires react, motion and Tailwind CSS v4. Free for personal projects; commercial use needs MotionKit Plus (https://www.motionkitui.com/pricing).

## Props

| 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. |

## Accessibility

- **Keyboard: Not applicable.** Text, not a control.
- **Screen readers: Pass.** Every word is read once as a list; the turning faces are hidden, so nothing is announced every few seconds.
- **Reduced motion: Pass.** With reduced motion on, the words swap with a plain fade instead of turning.
- **Colour contrast: Pass.** It keeps your text colour; ivory on the site's dark measures 17:1.

## Source

```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>
  );
}
```
