A clean, accessible character-by-character word flip with smooth GSAP-powered motion.
Install
Add it to your project
Run this in a project set up for shadcn/ui. It writes the files and installs the npm packages.
First time: connect the Infinity UI registry
- Create a personal token on your Tokens page.
- Put it in
.env.localasINFINITYUI_TOKEN. - Add the registry to
components.json:
{
"registries": {
"@infinityui": {
"url": "https://infinityui.io/r/{name}.json",
"headers": {
"Authorization": "Bearer ${INFINITYUI_TOKEN}"
}
}
}
}Using Claude Code, Codex, Cursor, Lovable or v0? See CLI & MCP for a setup prompt.
npm packages it adds
- gsap
Code
"use client";
import gsap from "gsap";
import { useEffect, useRef } from "react";
const FIRST_WORD = "COMPONENTRY";
const SECOND_WORD = "CONSISTENCY";
export function FlippingWordSwapCardPreview() {
const previewRef = useRef<HTMLDivElement>(null);
useEffect(() => {
const prefersReducedMotion = window.matchMedia(
"(prefers-reduced-motion: reduce)",
).matches;
const context = gsap.context(() => {
const firstWord = gsap.utils.toArray<HTMLElement>(
'[data-card-flip="first"]',
);
const secondWord = gsap.utils.toArray<HTMLElement>(
'[data-card-flip="second"]',
);
gsap.set(secondWord, {
rotationX: -82,
opacity: 0,
transformOrigin: "center bottom",
});
if (prefersReducedMotion) return;
gsap
.timeline({ repeat: -1, repeatDelay: 1, yoyo: true })
.to(firstWord, {
rotationX: 82,
opacity: 0,
duration: 0.4,
stagger: 0.044,
ease: "power2.in",
transformOrigin: "center top",
})
.to(
secondWord,
{
rotationX: 0,
opacity: 1,
duration: 0.4,
stagger: 0.044,
ease: "power2.out",
},
"<0.25",
);
}, previewRef);
return () => context.revert();
}, []);
const renderCharacters = (word: string, layer: "first" | "second") =>
Array.from(word).map((character, index) => (
<span
key={`${layer}-${character}-${index}`}
data-card-flip={layer}
className="inline-block [backface-visibility:hidden] [will-change:transform,opacity]"
>
{character}
</span>
));
return (
<div
ref={previewRef}
className="pointer-events-none flex size-full items-center justify-center overflow-hidden px-6 text-foreground"
aria-hidden="true"
>
<span className="inline-grid overflow-hidden text-2xl font-bold tracking-tight [perspective:800px]">
<span className="col-start-1 row-start-1 inline-flex justify-center gap-[0.012em] whitespace-nowrap">
{renderCharacters(FIRST_WORD, "first")}
</span>
<span className="col-start-1 row-start-1 inline-flex justify-center gap-[0.012em] whitespace-nowrap">
{renderCharacters(SECOND_WORD, "second")}
</span>
</span>
</div>
);
}
import * as __R from "react";
export default function __InfinityFrame() {
__R.useEffect(() => {
// Simulated cursor: a slow figure-eight, so pointer-driven effects draw something.
let t = 0;
const id = setInterval(() => {
t += 0.05;
const x = innerWidth * (0.5 + 0.32 * Math.sin(t));
const y = innerHeight * (0.5 + 0.22 * Math.sin(2 * t));
const el = document.elementFromPoint(x, y);
if (!el) return;
const init = { clientX: x, clientY: y, pageX: x, pageY: y, bubbles: true, cancelable: true, view: window };
el.dispatchEvent(new PointerEvent("pointermove", { ...init, pointerType: "mouse", isPrimary: true }));
el.dispatchEvent(new MouseEvent("mousemove", init));
}, 30);
return () => clearInterval(id);
}, []);
return (
<div style={{ position: "relative", width: "100vw", height: "100vh", display: "flex", alignItems: "center", justifyContent: "center", overflow: "hidden" }}>
<FlippingWordSwapCardPreview />
</div>
);
}
































