{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"sparkles-text","type":"registry:ui","title":"SparklesText","description":"Text overlaid with small SVG star sparkles that scale, rotate, and fade in a continuous loop, each one regenerating at a random position once its lifespan expires, cycling between two configurable colors","author":"fancy-ui <https://fancy-ui.rama.app>","categories":["text"],"registryDependencies":["https://fancy-ui.rama.app/r/internals-dom.json","https://fancy-ui.rama.app/r/utils.json"],"files":[{"path":"react/src/components/sparkles-text/SparklesText.tsx","content":"\"use client\";\n\nimport { useEffect, useState, type CSSProperties } from \"react\";\nimport { useLiveRef } from \"../../internals/dom/use-live-ref\";\nimport { cn } from \"../../utils\";\nimport \"./sparkles-text.css\";\n\n/**\n * SparklesText - Text with animated sparkle stars\n *\n * Renders text with SVG sparkle stars overlaid that animate with\n * fade/scale/rotation effects. Sparkles regenerate periodically.\n */\nexport interface SparklesTextProps {\n\t/** Text to display */\n\ttext: string;\n\t/** Number of sparkle stars */\n\tsparklesCount?: number;\n\t/** Two colors for sparkle stars */\n\tcolors?: { first: string; second: string };\n\t/** Additional CSS classes */\n\tclassName?: string;\n\t/**\n\t * Seed for the sparkle field. The same seed always produces the same\n\t * initial field, which is what keeps a server render and its hydration\n\t * identical. Change it to give two fields on one page different layouts —\n\t * the default is shared, so two `<SparklesText />` with no seed start\n\t * with the same field.\n\t */\n\tseed?: number;\n}\n\ninterface Sparkle {\n\tid: string;\n\tx: string;\n\ty: string;\n\tcolor: string;\n\tdelay: number;\n\tscale: number;\n\tlifespan: number;\n}\n\n/**\n * mulberry32 — a tiny deterministic PRNG. `Math.random()` cannot be used\n * here: the initializer runs once on the server and again during client\n * hydration, and the two would disagree on every value, which React reports\n * as a hydration mismatch and may resolve by keeping the SERVER attributes.\n * A seed keeps both renders identical AND keeps the sparkles in the markup.\n */\nfunction mulberry32(seed: number): () => number {\n\tlet state = seed >>> 0;\n\treturn () => {\n\t\tstate = (state + 0x6d2b79f5) >>> 0;\n\t\tlet t = state;\n\t\tt = Math.imul(t ^ (t >>> 15), t | 1);\n\t\tt ^= t + Math.imul(t ^ (t >>> 7), t | 61);\n\t\treturn ((t ^ (t >>> 14)) >>> 0) / 4294967296;\n\t};\n}\n\nfunction generateStar(\n\trandom: () => number,\n\tcolors: { first: string; second: string }\n): Sparkle {\n\tconst x = `${random() * 100}%`;\n\tconst y = `${random() * 100}%`;\n\tconst color = random() > 0.5 ? colors.first : colors.second;\n\tconst delay = random() * 2;\n\tconst scale = random() * 1 + 0.3;\n\tconst lifespan = random() * 10 + 5;\n\t// The source ids in `Date.now()` plus a `Math.random()` draw; both are\n\t// forbidden in a render path, so the id takes another PRNG draw instead —\n\t// still unique within a field for keying purposes.\n\tconst id = `${x}-${y}-${random()}`;\n\treturn { id, x, y, color, delay, scale, lifespan };\n}\n\nexport function SparklesText({\n\ttext,\n\tsparklesCount = 10,\n\tcolors = { first: \"#9E7AFF\", second: \"#FE8BBB\" },\n\tclassName,\n\tseed = 1,\n}: SparklesTextProps) {\n\t// Initialized once at mount, like the source's `initializeStars()` call at\n\t// component init — a later `sparklesCount` change does not regenerate the\n\t// field there either. The lazy initializer builds its own PRNG stream, so\n\t// it is pure and produces the same field on the server and on hydration.\n\tconst [sparkles, setSparkles] = useState<Sparkle[]>(() => {\n\t\tconst random = mulberry32(seed);\n\t\treturn Array.from({ length: sparklesCount }, () => generateStar(random, colors));\n\t});\n\n\t// Regenerated stars read the CURRENT colors, as the source's reactive\n\t// closure does, without restarting the interval when colors change.\n\tconst colorsRef = useLiveRef(colors);\n\n\tuseEffect(() => {\n\t\t// Regeneration happens only after mount, so it never has to agree with\n\t\t// the server; a second seeded stream keeps it deterministic anyway.\n\t\tconst random = mulberry32(seed + 1);\n\t\tconst intervalId = setInterval(() => {\n\t\t\tsetSparkles((prev) =>\n\t\t\t\tprev.map((star) => {\n\t\t\t\t\tif (star.lifespan <= 0) {\n\t\t\t\t\t\treturn generateStar(random, colorsRef.current);\n\t\t\t\t\t}\n\t\t\t\t\treturn { ...star, lifespan: star.lifespan - 0.1 };\n\t\t\t\t})\n\t\t\t);\n\t\t}, 100);\n\t\treturn () => clearInterval(intervalId);\n\t}, [seed]);\n\n\treturn (\n\t\t<div className={cn(\"sparkles-text text-6xl font-bold\", className)}>\n\t\t\t<span className=\"relative inline-block\">\n\t\t\t\t{sparkles.map((sparkle) => (\n\t\t\t\t\t<svg\n\t\t\t\t\t\tkey={sparkle.id}\n\t\t\t\t\t\tclassName=\"sparkles-star pointer-events-none absolute z-20\"\n\t\t\t\t\t\tstyle={\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\tleft: sparkle.x,\n\t\t\t\t\t\t\t\ttop: sparkle.y,\n\t\t\t\t\t\t\t\t\"--sparkle-scale\": `${sparkle.scale}`,\n\t\t\t\t\t\t\t\tanimation: `sparkleAnim 0.8s ease-in-out ${sparkle.delay}s infinite`,\n\t\t\t\t\t\t\t\ttransform: \"scale(0) rotate(75deg)\",\n\t\t\t\t\t\t\t\topacity: 0,\n\t\t\t\t\t\t\t} as CSSProperties\n\t\t\t\t\t\t}\n\t\t\t\t\t\twidth=\"21\"\n\t\t\t\t\t\theight=\"21\"\n\t\t\t\t\t\tviewBox=\"0 0 21 21\"\n\t\t\t\t\t>\n\t\t\t\t\t\t<path\n\t\t\t\t\t\t\td=\"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\"\n\t\t\t\t\t\t\tfill={sparkle.color}\n\t\t\t\t\t\t/>\n\t\t\t\t\t</svg>\n\t\t\t\t))}\n\t\t\t\t{text}\n\t\t\t</span>\n\t\t</div>\n\t);\n}\n","type":"registry:ui","target":"@ui/fancy/components/sparkles-text/SparklesText.tsx"},{"path":"react/src/components/sparkles-text/index.ts","content":"\"use client\";\n\nexport { SparklesText } from \"./SparklesText\";\nexport type { SparklesTextProps } from \"./SparklesText\";\n","type":"registry:ui","target":"@ui/fancy/components/sparkles-text/index.ts"},{"path":"react/src/components/sparkles-text/sparkles-text.css","content":"/* Keyframes referenced by the inline `animation` on each .sparkles-star.\n   Keyframe names are global by nature; the name is kept identical to the\n   Svelte source's emitted name. */\n@keyframes sparkleAnim {\n\t0% {\n\t\topacity: 0;\n\t\ttransform: scale(0) rotate(75deg);\n\t}\n\t50% {\n\t\topacity: 1;\n\t\ttransform: scale(var(--sparkle-scale, 1)) rotate(120deg);\n\t}\n\t100% {\n\t\topacity: 0;\n\t\ttransform: scale(0) rotate(150deg);\n\t}\n}\n","type":"registry:ui","target":"@ui/fancy/components/sparkles-text/sparkles-text.css"}],"meta":{"license":"MIT","package":"fancy-ui-react","version":"0.7.0","source":"https://fancy-ui.rama.app/docs/components/sparkles-text?framework=react"}}