Text Flip
ncdai/text-flipUnverifiedComponent
Animated text that cycles through items with a smooth flip transition.
Install it
npx shadcn@latest add https://raw.githubusercontent.com/exekartik/exeKartik/main/text-flip.jsonSource
1"use client"23import { Children, useEffect, useState } from "react"4import type { Transition, Variants } from "motion/react"5import { AnimatePresence, motion } from "motion/react"67import { cn } from "@/lib/utils"89const defaultVariants: Variants = {10 initial: { y: "-20%", opacity: 0, filter: "blur(1px)" },11 animate: {12 y: "0%",13 opacity: 1,14 filter: "blur(0px)",15 },16 exit: {17 y: "40%",18 opacity: 0,19 filter: "blur(1px)",20 transition: { ease: "easeOut" },21 },22}2324type MotionElement = typeof motion.p | typeof motion.span | typeof motion.code2526export type TextFlipProps = {27 /**28 * Motion element to render.29 * @defaultValue motion.p30 * */31 as?: MotionElement32 className?: string33 /** Array of children to cycle through. */34 children: React.ReactNode[]3536 /**37 * Time in seconds between each flip.38 * @defaultValue 239 * */40 interval?: number41 /**42 * Motion transition configuration.43 * @defaultValue { duration: 0.3 }44 * */45 transition?: Transition46 /** Motion variants for enter/exit animations. */47 variants?: Variants4849 /** Controls whether the flip animation runs. */50 play?: boolean5152 /** Called with the new index after each flip. */53 onIndexChange?: (index: number) => void54}5556export function TextFlip({57 as: Component = motion.p,58 className,59 children,6061 interval = 2,62 transition = { duration: 0.3 },63 variants = defaultVariants,64 play = true,6566 onIndexChange,67}: TextFlipProps) {68 const [currentIndex, setCurrentIndex] = useState(0)6970 const items = Children.toArray(children)7172 useEffect(() => {73 if (!play) return7475 const timer = setInterval(() => {76 setCurrentIndex((prev) => {77 const next = (prev + 1) % items.length78 onIndexChange?.(next)79 return next80 })81 }, interval * 1000)8283 return () => clearInterval(timer)84 }, [play, interval, items.length, onIndexChange])8586 return (87 <AnimatePresence mode="wait" initial={false}>88 <Component89 key={currentIndex}90 className={cn("inline-block", className)}91 initial="initial"92 animate="animate"93 exit="exit"94 transition={transition}95 variants={variants}96 >97 {items[currentIndex]}98 </Component>99 </AnimatePresence>100 )101}
What it pulls in
npm packages
Files it writes
More from ncdai
All 17 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| Apple Hello Effectapple-hello-effect | ncdai | Components | Unverified | 1 dep | |
| Apple Hello Effect - Hindiapple-hello-effect-hindi | ncdai | Components | Unverified | 1 dep | |
| Apple Hello Effect - Spanishapple-hello-effect-spanish | ncdai | Components | Unverified | 1 dep | |
| Apple Hello Effect - Vietnameseapple-hello-effect-vietnamese | ncdai | Components | Unverified | 1 dep | |
| Brand Assets Menubrand-assets-menu | ncdai | Components | Unverified | 1 dep | |
| chevrons-up-down-iconchevrons-up-down-icon | ncdai | Components | Unverified | 1 dep | |
| Code Block Commandcode-block-command | ncdai | Components | Unverified | 4 deps · 3 files | |
| Consent Managerconsent-manager | ncdai | Components | Unverified | 1 dep · 2 files |
