"use client"; import { motion } from "motion/react"; import { CSSProperties, ReactElement, useEffect, useState } from "react"; import { cn } from "@/lib/utils"; interface Sparkle { id: string; x: string; y: string; color: string; delay: number; scale: number; lifespan: number; } const Sparkle: React.FC = ({ id, x, y, color, delay, scale }) => { return ( ); }; interface SparklesTextProps { /** * @default
* @type ReactElement * @description * The component to be rendered as the text * */ as?: ReactElement; /** * @default "" * @type string * @description * The className of the text */ className?: string; /** * @required * @type ReactNode * @description * The content to be displayed * */ children: React.ReactNode; /** * @default 10 * @type number * @description * The count of sparkles * */ sparklesCount?: number; /** * @default "{first: '#9E7AFF', second: '#FE8BBB'}" * @type string * @description * The colors of the sparkles * */ colors?: { first: string; second: string; }; } export const SparklesText: React.FC = ({ children, colors = { first: "#9E7AFF", second: "#FE8BBB" }, className, sparklesCount = 10, ...props }) => { const [sparkles, setSparkles] = useState([]); useEffect(() => { const generateStar = (): Sparkle => { const starX = `${Math.random() * 100}%`; const starY = `${Math.random() * 100}%`; const color = Math.random() > 0.5 ? colors.first : colors.second; const delay = Math.random() * 2; const scale = Math.random() * 1 + 0.3; const lifespan = Math.random() * 10 + 5; const id = `${starX}-${starY}-${Date.now()}`; return { id, x: starX, y: starY, color, delay, scale, lifespan }; }; const initializeStars = () => { const newSparkles = Array.from({ length: sparklesCount }, generateStar); setSparkles(newSparkles); }; const updateStars = () => { setSparkles((currentSparkles) => currentSparkles.map((star) => { if (star.lifespan <= 0) { return generateStar(); } else { return { ...star, lifespan: star.lifespan - 0.1 }; } }), ); }; initializeStars(); const interval = setInterval(updateStars, 100); return () => clearInterval(interval); }, [colors.first, colors.second, sparklesCount]); return (
{sparkles.map((sparkle) => ( ))} {children}
); };