Introducing Magic UI Pro - 50+ blocks and templates to build beautiful landing pages in minutes.


Docs
Word Rotate

Word Rotate

An vertical rotation of words

Word

Installation

Copy and paste the following code into your project.

components/magicui/word-rotate.tsx
"use client";
 
import { cn } from "@/lib/utils";
import { AnimatePresence, HTMLMotionProps, motion } from "framer-motion";
import { useEffect, useState } from "react";
 
interface WordRotateProps {
  words: string[];
  duration?: number;
  framerProps?: HTMLMotionProps<"h1">;
  className?: string;
}
 
export default function WordRotate({
  words,
  duration = 2500,
  framerProps = {
    initial: { opacity: 0, y: -50 },
    animate: { opacity: 1, y: 0 },
    exit: { opacity: 0, y: 50 },
    transition: { duration: 0.25, ease: "easeOut" },
  },
  className,
}: WordRotateProps) {
  const [index, setIndex] = useState(0);
 
  useEffect(() => {
    const interval = setInterval(() => {
      setIndex((prevIndex) => (prevIndex + 1) % words.length);
    }, duration);
 
    // Clean up interval on unmount
    return () => clearInterval(interval);
  }, [words, duration]);
 
  return (
    <div className="overflow-hidden py-2">
      <AnimatePresence mode="wait">
        <motion.h1
          key={words[index]}
          className={cn(className)}
          {...framerProps}
        >
          {words[index]}
        </motion.h1>
      </AnimatePresence>
    </div>
  );
}

Props

PropTypeDescriptionDefault
classNamestringThe class name to be applied to the component
durationnumberThe class name to be applied to the shimmer.2500
wordsstring[]An array of words to rotate through""
framerPropsHTMLMotionPropsAn object containing framer-motion animation props