ComponentsuseState + useEffect setInterval. Pure CSS keyframe per l'enter, niente exit (sostituzione diretta).
TextRotateClient
Esempio01
word-rotate.tsx 001Default · 3 words002Hero · profession statementCostruisco interfacce
Niente template, niente kit. Tutto su misura.
003Faster · 4 words at 1.4sDisponibile per contratti
tsxsrc/components/word-rotate.tsx
"use client";
import { useEffect, useState } from "react";
import { cn } from "@/lib/utils";
export type WordRotateProps = {
words: string[];
/** Milliseconds each word stays visible. Default 2500. */
duration?: number;
className?: string;
};
/**
* <WordRotate/> — cycles through `words`, swapping each with a translateY +
* opacity transition. Inherits font sizing from the caller.
*/
export function WordRotate({ words, duration = 2500, className }: WordRotateProps) {
const [index, setIndex] = useState(0);
useEffect(() => {
if (words.length < 2) return;
const id = setInterval(() => {
setIndex((i) => (i + 1) % words.length);
}, Math.max(800, duration));
return () => clearInterval(id);
}, [words.length, duration]);
return (
<>
<style>{`
@keyframes word-rotate-in {
from { transform: translate3d(0, 0.6em, 0); opacity: 0; filter: blur(2px); }
to { transform: translate3d(0, 0, 0); opacity: 1; filter: blur(0); }
}
`}</style>
<span className={cn("relative inline-block overflow-hidden align-baseline", className)}>
<span
key={index}
className="inline-block"
style={{ animation: "word-rotate-in 380ms var(--ease-out) both" }}
>
{words[index]}
</span>
</span>
</>
);
}
Note — Wrapper inline-block con overflow-hidden per evitare reflow durante l'enter. Min duration 800ms per evitare flicker.
Prompt LLM02
Incolla in Claude o ChatGPT per generare la tua variante. Include il contesto del brand, i token e i vincoli del progetto.
Sei un senior frontend engineer. Stai lavorando su un sito Next.js 16 + React 19 + Tailwind v4 in italiano, look chanhdai-inspired: colonna stretta 672px, Geist Sans + Geist Mono, hairline 1px, divisori a stripe diagonale, palette zinc.
Token CSS disponibili: --bg, --bg-alt, --fg, --fg-muted, --fg-soft, --border, --border-strong, --accent. Usa SEMPRE queste variabili tramite le utility tailwind generate (bg-bg, text-fg-muted, border-border, ecc.). Helper "cn" da "@/lib/utils". Niente librerie UI extra: solo lucide-react e tailwind-merge.
Genera un componente <WordRotate> per ciclare parole.
Props:
- words: string[].
- duration?: number (ms per parola, default 2500).
- className?: string.
Implementazione:
- "use client". useState<number> index. useEffect: setInterval che fa setIndex(i => (i+1) % words.length).
- Wrapper inline-block overflow-hidden align-baseline.
- Span chiave key={index} con animation word-rotate-in 380ms var(--ease-out) both. Keyframe da 0% (translateY 0.6em, opacity 0, blur 2px) a 100% normale.
Output: file completo src/components/word-rotate.tsx.Uso tipico03
<WordRotate words={["a", "b", "c"]} duration={2500} />
Dipendenze04
Ti è servito? Dimmelo, oppure proponi il prossimo componente.