Text Flip
lyminhnghia/text-flipFreeComponent
Animated text that cycles through items with a smooth flip transition.
Install it
npx shadcn@latest add https://lyminhnghia.github.io/r/text-flip.jsonSource
1"use client"23import type { Transition, Variants } from "motion/react"4import { AnimatePresence, motion } from "motion/react"5import { Children, useEffect, useState } from "react"67import { cn } from "@/lib/utils"89const defaultVariants: Variants = {10 initial: { y: -8, opacity: 0 },11 animate: { y: 0, opacity: 1 },12 exit: { y: 8, opacity: 0 },13}1415type MotionElement = typeof motion.p | typeof motion.span | typeof motion.code1617export type TextFlipProps = {18 /**19 * Motion element to render.20 * @defaultValue motion.p21 * */22 as?: MotionElement23 className?: string24 /** Array of children to cycle through. */25 children: React.ReactNode[]2627 /**28 * Time in seconds between each flip.29 * @defaultValue 230 * */31 interval?: number32 /**33 * Motion transition configuration.34 * @defaultValue { duration: 0.3 }35 * */36 transition?: Transition37 /** Motion variants for enter/exit animations. */38 variants?: Variants3940 /** Called with the new index after each flip. */41 onIndexChange?: (index: number) => void42}4344export function TextFlip({45 as: Component = motion.p,46 className,47 children,4849 interval = 2,50 transition = { duration: 0.3 },51 variants = defaultVariants,5253 onIndexChange,54}: TextFlipProps) {55 const [currentIndex, setCurrentIndex] = useState(0)5657 const items = Children.toArray(children)5859 useEffect(() => {60 const timer = setInterval(() => {61 setCurrentIndex((prev) => {62 const next = (prev + 1) % items.length63 onIndexChange?.(next)64 return next65 })66 }, interval * 1000)6768 return () => clearInterval(timer)69 }, [items.length, interval, onIndexChange])7071 return (72 <AnimatePresence mode="wait" initial={false}>73 <Component74 key={currentIndex}75 className={cn("inline-block", className)}76 initial="initial"77 animate="animate"78 exit="exit"79 transition={transition}80 variants={variants}81 >82 {items[currentIndex]}83 </Component>84 </AnimatePresence>85 )86}
What it pulls in
npm packages
Files it writes
More from lyminhnghia
All 26 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| Apple Hello Effectapple-hello-effect | lyminhnghia | Components | Free | 1 dep | |
| chevrons-up-down-iconchevrons-up-down-icon | lyminhnghia | Components | Free | no deps | |
| Code Block Commandcode-block-command | lyminhnghia | Components | Free | 4 deps · 3 files | |
| Consent Managerconsent-manager | lyminhnghia | Components | Free | 1 dep | |
| Copy Buttoncopy-button | lyminhnghia | Components | Free | 2 deps · 2 files | |
| GitHub Starsgithub-stars | lyminhnghia | Components | Free | no deps | |
| Scroll Fade Effectscroll-fade-effect | lyminhnghia | Components | Free | no deps | |
| Shimmering Textshimmering-text | lyminhnghia | Components | Free | 1 dep |
