On hover, each letter rolls over on a little 3D cube to reveal a fresh copy, one after another. Made for links and buttons.
On hover or focus, the letters swap places a few times and slide home. Made for links and buttons.
Customize
Usage
import { ShuffleText } from "@/components/motionkit/shuffle-text";
<a href="/about">
<ShuffleText text="About us" />
</a>Motion
The exact curves this component runs, plotted with Motion's own maths.
Swaps
Hover or focus
Slide to new slot
Each swap
Navigation links, buttons and footer links, a small reward on hover that works for keyboard focus as well.
On hover or focus, the letters are given a shuffled flex order a few times in a row, then put back. Each letter uses Motion's layout animation, so every swap slides rather than jumps. It can listen to its enclosing link or button, so the whole target triggers it.
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/shuffle-text.jsonnpm install motionSave as components/motionkit/shuffle-text.tsx.
"use client";
import { useEffect, useRef, useState } from "react";
import { motion, useReducedMotion } from "motion/react";
type ShuffleTextProps = {
text: string;
/** How many times the letters swap places before settling. */
rounds?: number;
/** Milliseconds between swaps. */
speed?: number;
/**
* "parent" plays when the pointer enters (or focus lands on) the enclosing element, such as a
* link or button; "self" listens to the text only.
*/
target?: "parent" | "self";
className?: string;
};
function shuffled(count: number) {
const order = Array.from({ length: count }, (_, i) => i);
for (let i = count - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[order[i], order[j]] = [order[j], order[i]];
}
return order;
}
export function ShuffleText({ text, rounds = 3, speed = 90, target = "parent", className = "" }: ShuffleTextProps) {
const ref = useRef<HTMLSpanElement>(null);
const reduceMotion = useReducedMotion();
const letters = Array.from(text);
// order[i] is the slot letter i occupies; the identity order spells the word
const [order, setOrder] = useState<number[] | null>(null);
useEffect(() => {
const self = ref.current;
const area = target === "parent" ? self?.parentElement : self;
if (!area || reduceMotion) return;
let timers: number[] = [];
function play() {
if (timers.length) return;
for (let round = 0; round <= rounds; round++) {
timers.push(
window.setTimeout(() => {
// the last round puts every letter home
setOrder(round === rounds ? null : shuffled(letters.length));
if (round === rounds) timers = [];
}, round * speed),
);
}
}
area.addEventListener("pointerenter", play);
area.addEventListener("focusin", play);
return () => {
timers.forEach(window.clearTimeout);
area.removeEventListener("pointerenter", play);
area.removeEventListener("focusin", play);
};
}, [target, rounds, speed, reduceMotion, letters.length]);
return (
<span ref={ref} className={`relative inline-flex ${className}`}>
<span className="sr-only">{text}</span>
{letters.map((char, i) => (
// flex order moves a letter to another slot; layout animation slides it there
<motion.span
key={i}
aria-hidden
layout="position"
transition={{ type: "spring", stiffness: 500, damping: 32 }}
style={{ order: order ? order[i] : i }}
className="inline-block whitespace-pre"
>
{char}
</motion.span>
))}
</span>
);
}
| Prop | Type | Default | Description |
|---|---|---|---|
| text | string | — | The word or short label. |
| rounds | number | 3 | Swaps before settling. |
| speed | number | 90 | Milliseconds between swaps. |
| target | "parent" | "self" | "parent" | Listen to the enclosing link or button, or to the text only. |
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/shuffle-text.json in any project with a components.json. It saves the file to components/motionkit and installs its dependencies.
With reduced motion on, the letters never move.
On hover, each letter rolls over on a little 3D cube to reveal a fresh copy, one after another. Made for links and buttons.
A link or button whose letters roll up on hover, each replaced by a copy from below, one after another. Pure CSS.
Text that decodes itself character by character, terminal style.