A layout-text-flip component from Aceternity UI.
From Aceternity UI
1"use client";
2import React, { useState, useEffect } from "react";
3import { motion, AnimatePresence } from "motion/react";
4import { cn } from "@/lib/utils";
5
6export const LayoutTextFlip = ({
7 text = "Build Amazing",
8 words = ["Landing Pages", "Component Blocks", "Page Sections", "3D Shaders"],
9 duration = 3000,
10}: {
11 text: string;
12 words: string[];
13 duration?: number;
14}) => {
15 const [currentIndex, setCurrentIndex] = useState(0);
16
17 useEffect(() => {
18 const interval = setInterval(() => {
19 setCurrentIndex((prevIndex) => (prevIndex + 1) % words.length);
20 }, duration);
21
22 return () => clearInterval(interval);
23 }, []);
24
25 return (
26 <>
27 <motion.span
28 layoutId="subtext"
29 className="text-2xl font-bold tracking-tight drop-shadow-lg md:text-4xl"
30 >
31 {text}
32 </motion.span>
33
34 <motion.span
35 layout
36 className="relative w-fit overflow-hidden rounded-md border border-transparent bg-white px-4 py-2 font-sans text-2xl font-bold tracking-tight text-black shadow-sm ring shadow-black/10 ring-black/10 drop-shadow-lg md:text-4xl dark:bg-neutral-900 dark:text-white dark:shadow-sm dark:ring-1 dark:shadow-white/10 dark:ring-white/10"
37 >
38 <AnimatePresence mode="popLayout">
39 <motion.span
40 key={currentIndex}
41 initial={{ y: -40, filter: "blur(10px)" }}
42 animate={{
43 y: 0,
44 filter: "blur(0px)",
45 }}
46 exit={{ y: 50, filter: "blur(10px)", opacity: 0 }}
47 transition={{
48 duration: 0.5,
49 }}
50 className={cn("inline-block whitespace-nowrap")}
51 >
52 {words[currentIndex]}
53 </motion.span>
54 </AnimatePresence>
55 </motion.span>
56 </>
57 );
58};
59