Skip to content

UI Library for Design Engineers. Animated components and effects you can copy and paste into your apps. Free. Open Source.

feat: sparkles-gradient-text component (#110)

William62141b899a08
diff --git a/config/docs.ts b/config/docs.ts
index dea111df..091276c9 100644
--- a/config/docs.ts
+++ b/config/docs.ts
@@ -270,6 +270,12 @@ export const docsConfig: DocsConfig = {
items: [],
label: "",
},
+ {
+ title: "Sparkles Text",
+ href: `/docs/components/sparkles-text`,
+ items: [],
+ label: "New",
+ },
],
},
{
diff --git a/content/docs/components/sparkles-text.mdx b/content/docs/components/sparkles-text.mdx
new file mode 100644
index 00000000..ddd54f55
--- /dev/null
+++ b/content/docs/components/sparkles-text.mdx
@@ -0,0 +1,36 @@
+---
+title: Sparkles Text
+date: 2024-05-30
+description: A dynamic text that generates continuous sparkles with smooth transitions, perfect for highlighting text with animated stars.
+author: Draxx0
+published: true
+---
+
+<ComponentPreview name="sparkles-text-demo" />
+
+<Steps>
+
+### Installation
+
+Copy and paste the following code into your project.
+
+```text
+components/magicui/sparkles-text.tsx
+```
+
+<ComponentSource name="sparkles-text" />
+
+</Steps>
+
+## Props
+
+| Prop | Type | Description | Default |
+| ------------- | ------ | -------------------------------------------------- | ---------------------------------------- |
+| text | string | The text to display | - |
+| className | string | The class name to be applied to the sparkles text. | - |
+| sparklesCount | number | sparkles count that appears on the text | 10 |
+| colors | object | the sparkles colors | `{first: '#A07CFE'; second: '#FE8FB5';}` |
+
+## Credits
+
+- Credit to [@simonlejeune](https://www.simlej.dev/)
diff --git a/registry/components/example/sparkles-text-demo.tsx b/registry/components/example/sparkles-text-demo.tsx
new file mode 100644
index 00000000..d09b9bf9
--- /dev/null
+++ b/registry/components/example/sparkles-text-demo.tsx
@@ -0,0 +1,5 @@
+import SparklesText from "@/registry/components/magicui/sparkles-text";
+
+export default async function SparklesTextDemo() {
+ return <SparklesText text="Magic UI" />;
+}
diff --git a/registry/components/magicui/sparkles-text.tsx b/registry/components/magicui/sparkles-text.tsx
new file mode 100644
index 00000000..e25d048a
--- /dev/null
+++ b/registry/components/magicui/sparkles-text.tsx
@@ -0,0 +1,153 @@
+"use client";
+
+import { cn } from "@/lib/utils";
+import { motion } from "framer-motion";
+import { CSSProperties, ReactElement, useEffect, useState } from "react";
+
+interface Sparkle {
+ id: string;
+ x: string;
+ y: string;
+ color: string;
+ delay: number;
+ scale: number;
+ lifespan: number;
+}
+
+interface SparklesTextProps {
+ /**
+ * @default <div />
+ * @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 string
+ * @description
+ * The text to be displayed
+ * */
+ text: string;
+
+ /**
+ * @default 10
+ * @type number
+ * @description
+ * The count of sparkles
+ * */
+ sparklesCount?: number;
+
+ /**
+ * @default "{first: '#A07CFE', second: '#FE8FB5'}"
+ * @type string
+ * @description
+ * The colors of the sparkles
+ * */
+ colors?: {
+ first: string;
+ second: string;
+ };
+}
+
+const SparklesText: React.FC<SparklesTextProps> = ({
+ text,
+ colors = { first: "#A07CFE", second: "#FE8FB5" },
+ className,
+ sparklesCount = 10,
+ ...props
+}) => {
+ const [sparkles, setSparkles] = useState<Sparkle[]>([]);
+
+ 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]);
+
+ return (
+ <div
+ className={cn("text-6xl font-bold", className)}
+ {...props}
+ style={
+ {
+ "--sparkles-first-color": `${colors.first}`,
+ "--sparkles-second-color": `${colors.second}`,
+ } as CSSProperties
+ }
+ >
+ <span className="relative inline-block">
+ {sparkles.map((sparkle) => (
+ <Sparkle key={sparkle.id} {...sparkle} />
+ ))}
+ <strong className="bg-gradient-to-r from-[var(--sparkles-first-color)] to-[var(--sparkles-second-color)] bg-clip-text text-transparent">
+ {text}
+ </strong>
+ </span>
+ </div>
+ );
+};
+
+const Sparkle: React.FC<Sparkle> = ({ id, x, y, color, delay, scale }) => {
+ return (
+ <motion.svg
+ key={id}
+ className="pointer-events-none absolute z-20"
+ initial={{ opacity: 0, left: x, top: y }}
+ animate={{
+ opacity: [0, 1, 0],
+ scale: [0, scale, 0],
+ rotate: [75, 120, 150],
+ }}
+ transition={{ duration: 0.8, repeat: Infinity, delay }}
+ width="21"
+ height="21"
+ viewBox="0 0 21 21"
+ >
+ <path
+ 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"
+ fill={color}
+ />
+ </motion.svg>
+ );
+};
+
+export default SparklesText;
diff --git a/registry/index.tsx b/registry/index.tsx
index c6f0b6af..2c46e4e6 100644
--- a/registry/index.tsx
+++ b/registry/index.tsx
@@ -182,6 +182,11 @@ const ui: Registry = {
type: "components:ui",
files: ["registry/components/magicui/letter-pullup.tsx"],
},
+ "sparkles-text": {
+ name: "sparkles-text",
+ type: "components:ui",
+ files: ["registry/components/magicui/sparkles-text.tsx"],
+ },
"flip-text": {
name: "flip-text",
type: "components:ui",
@@ -745,6 +750,14 @@ const example: Registry = {
() => import("@/registry/components/example/flip-text-demo"),
),
},
+ "sparkles-text-demo": {
+ name: "sparkles-text-demo",
+ type: "components:example",
+ files: ["registry/components/example/sparkles-text-demo.tsx"],
+ component: React.lazy(
+ () => import("@/registry/components/example/sparkles-text-demo"),
+ ),
+ },
"icon-cloud-demo": {
name: "icon-cloud-demo",
type: "components:example",