Gravity Text Swap
ui-beats/gravity-text-swapFreeComponent
The GravityTextSwap component drops characters into place under a gravity-like fall, so one word gives way to the next instead of simply being replaced.
Install it
npx shadcn@latest add https://uibeats.com/r/gravity-text-swap.jsonSource
1"use client";2import React, { useState, useEffect } from "react";3import { motion, AnimatePresence } from "motion/react";4import { cn } from "@/lib/utils";56interface GravityTextSwapProps {7 textArray: string[];8 duration?: number;9 pauseDuration?: number;10 className?: string;11}1213const GravityTextSwap: React.FC<GravityTextSwapProps> = ({14 textArray,15 duration = 0.5,16 pauseDuration = 2,17 className = "",18}) => {19 const [, setCurrentIndex] = useState(0);20 const [currentText, setCurrentText] = useState(textArray[0]);2122 useEffect(() => {23 if (textArray.length <= 1) return;2425 const intervalId = setInterval(26 () => {27 setCurrentIndex((prevIndex) => {28 const nextIndex = (prevIndex + 1) % textArray.length;29 setCurrentText(textArray[nextIndex] ?? "");30 return nextIndex;31 });32 },33 (duration + pauseDuration) * 1000,34 );3536 return () => clearInterval(intervalId);37 }, [textArray, duration, pauseDuration]);3839 return (40 <div className={cn("inline-block overflow-hidden", className)}>41 <AnimatePresence mode="wait">42 <motion.span43 key={currentText}44 className="inline-block"45 initial={{ opacity: 0 }}46 animate={{ opacity: 1 }}47 exit={{ opacity: 0 }}48 transition={{ duration: duration / 2 }}49 >50 {(currentText ?? "").split("").map((char, index) => (51 <motion.span52 key={index}53 className="inline-block"54 initial={{ y: -20, opacity: 0 }}55 animate={{56 y: 0,57 opacity: 1,58 transition: {59 type: "spring",60 damping: 12,61 stiffness: 200,62 delay: index * 0.03,63 },64 }}65 exit={{66 y: 20,67 opacity: 0,68 transition: {69 duration: duration / 2,70 delay: index * 0.02,71 },72 }}73 >74 {char}75 </motion.span>76 ))}77 </motion.span>78 </AnimatePresence>79 </div>80 );81};8283export default GravityTextSwap;
What it pulls in
npm packages
Other registry items
Files it writes
More from ui-beats
All 34 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| Animated Beamanimated-beam | ui-beats | Components | Free | 1 dep | |
| Animated Listanimated-list | ui-beats | Components | Free | 1 dep | |
| Border Beamborder-beam | ui-beats | Components | Free | 1 dep | |
| Bouncebounce | ui-beats | Components | Free | 1 dep | |
| Card Stackcard-stack | ui-beats | Components | Free | 1 dep | |
| Dockdock | ui-beats | Components | Free | 1 dep | |
| Fade Infade-in | ui-beats | Components | Free | 1 dep | |
| Fade In Unblurfade-in-unblur | ui-beats | Components | Free | 1 dep |
