Eunary UI, a modern component library built with React, Tailwind CSS, and Motion, providing accessible and animated components

Flip Words

A component to flip words with a smooth transition effect.

Build your website with Eunary

modern

Installation

Install dependencies

npm install class-variance-authority motion clsx tailwind-merge

Add util file

lib/utils.ts
import { ClassValue, clsx } from 'clsx';
import { twMerge } from 'tailwind-merge';
 
export function cn(...inputs: ClassValue[]) {
    return twMerge(clsx(inputs));
}

Copy the source code

components/ui/flip-words.tsx
'use client';

import React, { useEffect, useState } from 'react';
import { AnimatePresence, motion } from 'motion/react';
import { cn } from '@/lib/utils';

interface FlipwordsProps {
    words?: string[];
    duration?: number;
    className?: string;
}

export const FlipWords = ({
    words = ['Modern', 'Beautiful', 'Elegant', 'Aesthetic', 'Sleek'],
    duration = 3000,
    className,
}: FlipwordsProps) => {
    const [currentWord, setCurrentWord] = useState(0);

    useEffect(() => {
        const interval = setInterval(() => {
            setCurrentWord((prevWord) => (prevWord + 1) % words.length);
        }, duration);

        return () => clearInterval(interval);
    }, []);

    return (
        <AnimatePresence mode="wait">
            <motion.p
                key={currentWord}
                className={cn('inline-block h-fit py-2', className)}
                initial={{ opacity: 0, y: 10 }}
                animate={{ opacity: 1, y: 0 }}
                exit={{
                    opacity: 0,
                    y: -20,
                    scale: 1.2,
                    filter: 'blur(8px)',
                }}
                transition={{
                    type: 'spring',
                    stiffness: 100,
                    damping: 20,
                }}
            >
                {words[currentWord]}
            </motion.p>
        </AnimatePresence>
    );
};

Examples

Build

Modern


websites with Eunary UI

Props

Use the following props to customize the fliping words.

PropTypeDescription
wordsstring[]Array of words to be displayed.
classNamestringThe CSS class to be applied to the card.
durationnumberInterval duration in milliseconds between word transitions.

Explore more components with Eunary

Discover and experiment with a variety of components to craft a stunning and seamless experience for your product.

Eunary UI, a modern component library built with React, Tailwind CSS, and Motion, providing accessible and animated components

Eunary

UI

Product by Eunary

Building in public by Hemant Sharma