1"use client"
2
3import { CSSProperties, ReactElement, useEffect, useState } from "react"
4import { motion } from "motion/react"
5
6import { cn } from "@/lib/utils"
7
8interface Sparkle {
9 id: string
10 x: string
11 y: string
12 color: string
13 delay: number
14 scale: number
15 lifespan: number
16}
17
18const Sparkle: React.FC<Sparkle> = ({ id, x, y, color, delay, scale }) => {
19 return (
20 <motion.svg
21 key={id}
22 className="pointer-events-none absolute z-20"
23 initial={{ opacity: 0, left: x, top: y }}
24 animate={{
25 opacity: [0, 1, 0],
26 scale: [0, scale, 0],
27 rotate: [75, 120, 150],
28 }}
29 transition={{ duration: 0.8, repeat: Infinity, delay }}
30 width="21"
31 height="21"
32 viewBox="0 0 21 21"
33 >
34 <path
35 d="M9.82531 0.843845C10.0553 0.215178 10.9446 0.215178 11.1746 0.843845L11.8618 2.72026C12.4006 4.19229 12.3916 6.39157 13.5 7.5C14.6084 8.60843 16.8077 8.59935 18.2797 9.13822L20.1561 9.82534C20.7858 10.0553 20.7858 10.9447 20.1561 11.1747L18.2797 11.8618C16.8077 12.4007 14.6084 12.3916 13.5 13.5C12.3916 14.6084 12.4006 16.8077 11.8618 18.2798L11.1746 20.1562C10.9446 20.7858 10.0553 20.7858 9.82531 20.1562L9.13819 18.2798C8.59932 16.8077 8.60843 14.6084 7.5 13.5C6.39157 12.3916 4.19225 12.4007 2.72023 11.8618L0.843814 11.1747C0.215148 10.9447 0.215148 10.0553 0.843814 9.82534L2.72023 9.13822C4.19225 8.59935 6.39157 8.60843 7.5 7.5C8.60843 6.39157 8.59932 4.19229 9.13819 2.72026L9.82531 0.843845Z"
36 fill={color}
37 />
38 </motion.svg>
39 )
40}
41
42interface SparklesTextProps {
43 /**
44 * @default <div />
45 * @type ReactElement
46 * @description
47 * The component to be rendered as the text
48 * */
49 as?: ReactElement
50
51 /**
52 * @default ""
53 * @type string
54 * @description
55 * The className of the text
56 */
57 className?: string
58
59 /**
60 * @required
61 * @type ReactNode
62 * @description
63 * The content to be displayed
64 * */
65 children: React.ReactNode
66
67 /**
68 * @default 10
69 * @type number
70 * @description
71 * The count of sparkles
72 * */
73 sparklesCount?: number
74
75 /**
76 * @default "{first: '#9E7AFF', second: '#FE8BBB'}"
77 * @type string
78 * @description
79 * The colors of the sparkles
80 * */
81 colors?: {
82 first: string
83 second: string
84 }
85}
86
87export const SparklesText: React.FC<SparklesTextProps> = ({
88 children,
89 colors = { first: "#9E7AFF", second: "#FE8BBB" },
90 className,
91 sparklesCount = 10,
92 ...props
93}) => {
94 const [sparkles, setSparkles] = useState<Sparkle[]>([])
95
96 useEffect(() => {
97 const generateStar = (): Sparkle => {
98 const starX = `${Math.random() * 100}%`
99 const starY = `${Math.random() * 100}%`
100 const color = Math.random() > 0.5 ? colors.first : colors.second
101 const delay = Math.random() * 2
102 const scale = Math.random() * 1 + 0.3
103 const lifespan = Math.random() * 10 + 5
104 const id = `${starX}-${starY}-${Date.now()}`
105 return { id, x: starX, y: starY, color, delay, scale, lifespan }
106 }
107
108 const initializeStars = () => {
109 const newSparkles = Array.from({ length: sparklesCount }, generateStar)
110 setSparkles(newSparkles)
111 }
112
113 const updateStars = () => {
114 setSparkles((currentSparkles) =>
115 currentSparkles.map((star) => {
116 if (star.lifespan <= 0) {
117 return generateStar()
118 } else {
119 return { ...star, lifespan: star.lifespan - 0.1 }
120 }
121 })
122 )
123 }
124
125 initializeStars()
126 const interval = setInterval(updateStars, 100)
127
128 return () => clearInterval(interval)
129 }, [colors.first, colors.second, sparklesCount])
130
131 return (
132 <div
133 className={cn("text-6xl font-bold", className)}
134 {...props}
135 style={
136 {
137 "--sparkles-first-color": `${colors.first}`,
138 "--sparkles-second-color": `${colors.second}`,
139 } as CSSProperties
140 }
141 >
142 <span className="relative inline-block">
143 {sparkles.map((sparkle) => (
144 <Sparkle key={sparkle.id} {...sparkle} />
145 ))}
146 <strong>{children}</strong>
147 </span>
148 </div>
149 )
150}
151