Flip Sentences
ncdai-chirags/flip-sentencesFreeComponent
ncdai publishes no description for this item.
Install it
npx shadcn@latest add https://www.chirags.dev/r/flip-sentences.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.code;1617type Props = {18 as?: MotionElement;19 className?: string;20 children: React.ReactNode[];2122 interval?: number;23 transition?: Transition;24 variants?: Variants;2526 onIndexChange?: (index: number) => void;27};2829export function FlipSentences({30 as: Component = motion.p,31 className,32 children,3334 interval = 2,35 transition = { duration: 0.3 },36 variants = defaultVariants,3738 onIndexChange,39}: Props) {40 const [currentIndex, setCurrentIndex] = useState(0);4142 const items = Children.toArray(children);4344 useEffect(() => {45 const timer = setInterval(() => {46 setCurrentIndex((prev) => {47 const next = (prev + 1) % items.length;48 onIndexChange?.(next);49 return next;50 });51 }, interval * 1000);5253 return () => clearInterval(timer);54 }, [items.length, interval, onIndexChange]);5556 return (57 <AnimatePresence mode="wait" initial={false}>58 <Component59 key={currentIndex}60 className={cn("inline-block", className)}61 initial="initial"62 animate="animate"63 exit="exit"64 transition={transition}65 variants={variants}66 >67 {items[currentIndex]}68 </Component>69 </AnimatePresence>70 );71}
What it pulls in
npm packages
Other registry items
Files it writes
More from ncdai
All 28 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| Apple Hello Effectapple-hello-effect | ncdai | Components | Free | 1 dep | |
| Consent Managerconsent-manager | ncdai | Components | Free | 1 dep · 2 files | |
| GitHub Starsgithub-stars | ncdai | Components | Free | no deps | |
| Scroll Fade Effectscroll-fade-effect | ncdai | Components | Free | no deps | |
| Shimmering Textshimmering-text | ncdai | Components | Free | 1 dep | |
| Slide to Unlockslide-to-unlock | ncdai | Components | Free | 1 dep | |
| Testimonials Marqueetestimonials-marquee | ncdai | Components | Free | no deps | |
| Theme Switchertheme-switcher | ncdai | Components | Free | 3 deps |
