UI Library for Design Engineers. Animated components and effects you can copy and paste into your apps. Free. Open Source.
fix: aurora text gradient css approach (#606)
Arghya Das8f7c7e4bdb9c
diff --git a/content/docs/components/aurora-text.mdx b/content/docs/components/aurora-text.mdxindex 7180c0e1..44a85bbe 100644--- a/content/docs/components/aurora-text.mdx+++ b/content/docs/components/aurora-text.mdx@@ -42,9 +42,9 @@ npx shadcn@latest add "https://magicui.design/r/aurora-text"## Props-| Prop | Type | Default | Description |-| ----------- | ----------- | -------------------------------------------------------------------- | ------------------------------------------------------------- |-| `className` | `string` | `""` | The class name to be applied to the component |-| `children` | `ReactNode` | `-` | Children elements |-| `colors` | `string[]` | `["#FF0080", "#7928CA", "#0070F3", "#38bdf8", "#a855f7", "#2dd4bf"]` | Array of colors used for the aurora effect |-| `speed` | `number` | `1` | Animation speed multiplier (1 is default, 2 is twice as fast) |+| Prop | Type | Default | Description |+| ----------- | ----------- | ----------------------------------------------- | ------------------------------------------------------------- |+| `className` | `string` | `""` | The class name to be applied to the component |+| `children` | `ReactNode` | `-` | Children elements |+| `colors` | `string[]` | ` ["#FF0080", "#7928CA", "#0070F3", "#38bdf8"]` | Array of colors used for the aurora effect |+| `speed` | `number` | `1` | Animation speed multiplier (1 is default, 2 is twice as fast) |diff --git a/public/r/aurora-text.json b/public/r/aurora-text.jsonindex 0844d392..f5887a68 100644--- a/public/r/aurora-text.json+++ b/public/r/aurora-text.json@@ -7,9 +7,44 @@"files": [{"path": "registry/magicui/aurora-text.tsx",- "content": "\"use client\";\n\nimport type { CSSProperties } from \"react\";\nimport React, { useEffect, useRef } from \"react\";\n\ninterface AuroraTextProps {\n children: React.ReactNode;\n className?: string;\n colors?: string[];\n speed?: number; // 1 is default speed, 2 is twice as fast, 0.5 is half speed\n}\n\nexport function AuroraText({\n children,\n className = \"\",\n colors = [\"#FF0080\", \"#7928CA\", \"#0070F3\", \"#38bdf8\", \"#a855f7\", \"#2dd4bf\"],\n speed = 1,\n}: AuroraTextProps) {\n const canvasRef = useRef<HTMLCanvasElement>(null);\n const textRef = useRef<SVGTextElement>(null);\n const containerRef = useRef<HTMLSpanElement>(null);\n const [fontSize, setFontSize] = React.useState(0);\n const [dimensions, setDimensions] = React.useState({ width: 0, height: 0 });\n const [isReady, setIsReady] = React.useState(false);\n const [textStyle, setTextStyle] = React.useState<\n Partial<CSSStyleDeclaration>\n >({});\n\n // Updated effect to compute all text styles from parent\n useEffect(() => {\n if (containerRef.current) {\n const computedStyle = window.getComputedStyle(containerRef.current);\n\n // Extract text-related styles\n const relevantStyles = {\n fontSize: computedStyle.fontSize,\n fontFamily: computedStyle.fontFamily,\n fontWeight: computedStyle.fontWeight,\n fontStyle: computedStyle.fontStyle,\n letterSpacing: computedStyle.letterSpacing,\n lineHeight: computedStyle.lineHeight,\n textTransform: computedStyle.textTransform,\n fontVariant: computedStyle.fontVariant,\n fontStretch: computedStyle.fontStretch,\n fontFeatureSettings: computedStyle.fontFeatureSettings,\n };\n\n requestAnimationFrame(() => {\n setTextStyle(relevantStyles);\n });\n }\n }, [className]);\n\n // Updated effect to compute font size from both inline and class styles\n useEffect(() => {\n const updateFontSize = () => {\n if (containerRef.current) {\n const computedStyle = window.getComputedStyle(containerRef.current);\n const computedFontSize = parseFloat(computedStyle.fontSize);\n\n requestAnimationFrame(() => {\n setFontSize(computedFontSize);\n });\n }\n };\n\n updateFontSize();\n window.addEventListener(\"resize\", updateFontSize);\n\n return () => window.removeEventListener(\"resize\", updateFontSize);\n }, [className]);\n\n // Update effect to set ready state after dimensions are computed\n useEffect(() => {\n const updateDimensions = () => {\n if (textRef.current) {\n const bbox = textRef.current.getBBox();\n setDimensions({\n width: bbox.width,\n height: bbox.height,\n });\n setIsReady(true);\n }\n };\n\n updateDimensions();\n window.addEventListener(\"resize\", updateDimensions);\n\n return () => window.removeEventListener(\"resize\", updateDimensions);\n }, [children, fontSize]);\n\n useEffect(() => {\n const canvas = canvasRef.current;\n if (!canvas) return;\n\n const ctx = canvas.getContext(\"2d\");\n if (!ctx) return;\n\n // Set canvas size\n canvas.width = dimensions.width;\n canvas.height = dimensions.height;\n\n let time = 0;\n const baseSpeed = 0.008; // Original speed as base unit\n\n function animate() {\n if (!ctx || !canvas) return;\n ctx.clearRect(0, 0, canvas.width, canvas.height);\n\n time += baseSpeed * speed;\n\n colors.forEach((color, i) => {\n const x =\n canvas.width *\n (0.5 +\n Math.cos(time * 0.8 + i * 1.3) * 0.4 +\n Math.sin(time * 0.5 + i * 0.7) * 0.2);\n const y =\n canvas.height *\n (0.5 +\n Math.sin(time * 0.7 + i * 1.5) * 0.4 +\n Math.cos(time * 0.6 + i * 0.8) * 0.2);\n\n const gradient = ctx.createRadialGradient(\n x,\n y,\n 0,\n x,\n y,\n canvas.width * 0.4,\n );\n\n gradient.addColorStop(0, `${color}99`);\n gradient.addColorStop(0.5, `${color}33`);\n gradient.addColorStop(1, \"#00000000\");\n\n ctx.fillStyle = gradient;\n ctx.fillRect(0, 0, canvas.width, canvas.height);\n });\n\n requestAnimationFrame(animate);\n }\n animate();\n }, [dimensions, colors, speed]);\n\n return (\n <span\n ref={containerRef}\n className={`relative inline-block align-middle ${className}`}\n style={{\n width: dimensions.width || \"auto\",\n height: dimensions.height || \"auto\",\n }}\n >\n {/* Hidden text for SEO */}\n <span className=\"sr-only\">{children}</span>\n\n {/* Visual placeholder while canvas loads */}\n <span\n style={{\n opacity: isReady ? 0 : 1,\n transition: \"opacity 0.2s ease-in\",\n position: isReady ? \"absolute\" : \"relative\",\n display: \"inline-block\",\n whiteSpace: \"nowrap\",\n }}\n aria-hidden=\"true\"\n >\n {children}\n </span>\n\n <div\n className=\"absolute inset-0\"\n style={{\n opacity: isReady ? 1 : 0,\n transition: \"opacity 0.2s ease-in\",\n }}\n aria-hidden=\"true\"\n >\n <svg\n width={dimensions.width}\n height={dimensions.height}\n className=\"absolute inset-0\"\n >\n <defs>\n <mask id={`text-mask-${children}`}>\n <rect width=\"100%\" height=\"100%\" fill=\"black\" />\n <text\n ref={textRef}\n x=\"50%\"\n y=\"50%\"\n dominantBaseline=\"middle\"\n textAnchor=\"middle\"\n fill=\"white\"\n style={textStyle as CSSProperties}\n >\n {children}\n </text>\n </mask>\n </defs>\n </svg>\n\n <canvas\n ref={canvasRef}\n style={{\n maskImage: `url(#text-mask-${children})`,\n WebkitMaskImage: `url(#text-mask-${children})`,\n }}\n className=\"h-full w-full\"\n />\n </div>\n </span>\n );\n}\n",+ "content": "\"use client\";\n\nimport React, { memo } from \"react\";\n\ninterface AuroraTextProps {\n children: React.ReactNode;\n className?: string;\n colors?: string[];\n speed?: number;\n}\n\nexport const AuroraText = memo(\n ({\n children,\n className = \"\",\n colors = [\"#FF0080\", \"#7928CA\", \"#0070F3\", \"#38bdf8\"],\n speed = 1,\n }: AuroraTextProps) => {\n const gradientStyle = {\n backgroundImage: `linear-gradient(135deg, ${colors.join(\", \")}, ${colors[0]})`,\n WebkitBackgroundClip: \"text\",\n WebkitTextFillColor: \"transparent\",\n animationDuration: `${10 / speed}s`,\n };\n\n return (\n <span className={`relative inline-block ${className}`}>\n <span className=\"sr-only\">{children}</span>\n <span\n className=\"relative animate-aurora bg-[length:200%_auto] bg-clip-text text-transparent\"\n style={gradientStyle}\n aria-hidden=\"true\"\n >\n {children}\n </span>\n </span>\n );\n }\n);\n\nAuroraText.displayName = \"AuroraText\";\n","type": "registry:ui","target": "components/magicui/aurora-text.tsx"}- ]+ ],+ "tailwind": {+ "config": {+ "theme": {+ "extend": {+ "keyframes": {+ "aurora": {+ "0%": {+ "backgroundPosition": "0% 50%",+ "transform": "rotate(-5deg) scale(0.9)"+ },+ "25%": {+ "backgroundPosition": "50% 100%",+ "transform": "rotate(5deg) scale(1.1)"+ },+ "50%": {+ "backgroundPosition": "100% 50%",+ "transform": "rotate(-3deg) scale(0.95)"+ },+ "75%": {+ "backgroundPosition": "50% 0%",+ "transform": "rotate(3deg) scale(1.05)"+ },+ "100%": {+ "backgroundPosition": "0% 50%",+ "transform": "rotate(-5deg) scale(0.9)"+ }+ }+ },+ "animation": {+ "aurora": "aurora 8s ease-in-out infinite alternate"+ }+ }+ }+ }+ }}\ No newline at end of filediff --git a/registry.json b/registry.jsonindex 0b993636..998dbd3e 100644--- a/registry.json+++ b/registry.json@@ -95,7 +95,42 @@"type": "registry:ui","target": "components/magicui/aurora-text.tsx"}- ]+ ],+ "tailwind": {+ "config": {+ "theme": {+ "extend": {+ "keyframes": {+ "aurora": {+ "0%": {+ "backgroundPosition": "0% 50%",+ "transform": "rotate(-5deg) scale(0.9)"+ },+ "25%": {+ "backgroundPosition": "50% 100%",+ "transform": "rotate(5deg) scale(1.1)"+ },+ "50%": {+ "backgroundPosition": "100% 50%",+ "transform": "rotate(-3deg) scale(0.95)"+ },+ "75%": {+ "backgroundPosition": "50% 0%",+ "transform": "rotate(3deg) scale(1.05)"+ },+ "100%": {+ "backgroundPosition": "0% 50%",+ "transform": "rotate(-5deg) scale(0.9)"+ }+ }+ },+ "animation": {+ "aurora": "aurora 8s ease-in-out infinite alternate"+ }+ }+ }+ }+ }},{"name": "morphing-text",diff --git a/registry/magicui/aurora-text.tsx b/registry/magicui/aurora-text.tsxindex ff7b265c..6f89b571 100644--- a/registry/magicui/aurora-text.tsx+++ b/registry/magicui/aurora-text.tsx@@ -1,213 +1,41 @@"use client";-import type { CSSProperties } from "react";-import React, { useEffect, useId, useRef } from "react";+import React, { memo } from "react";interface AuroraTextProps {children: React.ReactNode;className?: string;colors?: string[];- speed?: number; // 1 is default speed, 2 is twice as fast, 0.5 is half speed+ speed?: number;}-export function AuroraText({- children,- className = "",- colors = ["#FF0080", "#7928CA", "#0070F3", "#38bdf8", "#a855f7", "#2dd4bf"],- speed = 1,-}: AuroraTextProps) {- const canvasRef = useRef<HTMLCanvasElement>(null);- const textRef = useRef<SVGTextElement>(null);- const containerRef = useRef<HTMLSpanElement>(null);- const [fontSize, setFontSize] = React.useState(0);- const [dimensions, setDimensions] = React.useState({ width: 0, height: 0 });- const [isReady, setIsReady] = React.useState(false);- const [textStyle, setTextStyle] = React.useState<- Partial<CSSStyleDeclaration>- >({});- const maskId = useId();-- // Updated effect to compute all text styles from parent- useEffect(() => {- if (containerRef.current) {- const computedStyle = window.getComputedStyle(containerRef.current);-- // Extract text-related styles- const relevantStyles = {- fontSize: computedStyle.fontSize,- fontFamily: computedStyle.fontFamily,- fontWeight: computedStyle.fontWeight,- fontStyle: computedStyle.fontStyle,- letterSpacing: computedStyle.letterSpacing,- lineHeight: computedStyle.lineHeight,- textTransform: computedStyle.textTransform,- fontVariant: computedStyle.fontVariant,- fontStretch: computedStyle.fontStretch,- fontFeatureSettings: computedStyle.fontFeatureSettings,- };-- requestAnimationFrame(() => {- setTextStyle(relevantStyles);- });- }- }, [className]);-- // Updated effect to compute font size from both inline and class styles- useEffect(() => {- const updateFontSize = () => {- if (containerRef.current) {- const computedStyle = window.getComputedStyle(containerRef.current);- const computedFontSize = parseFloat(computedStyle.fontSize);-- requestAnimationFrame(() => {- setFontSize(computedFontSize);- });- }- };-- updateFontSize();- window.addEventListener("resize", updateFontSize);-- return () => window.removeEventListener("resize", updateFontSize);- }, [className]);-- // Update effect to set ready state after dimensions are computed- useEffect(() => {- const updateDimensions = () => {- if (textRef.current) {- const bbox = textRef.current.getBBox();- setDimensions({- width: bbox.width,- height: bbox.height,- });- setIsReady(true);- }+export const AuroraText = memo(+ ({+ children,+ className = "",+ colors = ["#FF0080", "#7928CA", "#0070F3", "#38bdf8"],+ speed = 1,+ }: AuroraTextProps) => {+ const gradientStyle = {+ backgroundImage: `linear-gradient(135deg, ${colors.join(", ")}, ${colors[0]})`,+ WebkitBackgroundClip: "text",+ WebkitTextFillColor: "transparent",+ animationDuration: `${10 / speed}s`,};- updateDimensions();- window.addEventListener("resize", updateDimensions);-- return () => window.removeEventListener("resize", updateDimensions);- }, [children, fontSize]);-- useEffect(() => {- const canvas = canvasRef.current;- if (!canvas) return;-- const ctx = canvas.getContext("2d");- if (!ctx) return;-- // Set canvas size- canvas.width = dimensions.width;- canvas.height = dimensions.height;-- let time = 0;- const baseSpeed = 0.008; // Original speed as base unit-- function animate() {- if (!ctx || !canvas) return;- ctx.clearRect(0, 0, canvas.width, canvas.height);-- time += baseSpeed * speed;-- colors.forEach((color, i) => {- const x =- canvas.width *- (0.5 +- Math.cos(time * 0.8 + i * 1.3) * 0.4 +- Math.sin(time * 0.5 + i * 0.7) * 0.2);- const y =- canvas.height *- (0.5 +- Math.sin(time * 0.7 + i * 1.5) * 0.4 +- Math.cos(time * 0.6 + i * 0.8) * 0.2);-- const gradient = ctx.createRadialGradient(- x,- y,- 0,- x,- y,- canvas.width * 0.4,- );-- gradient.addColorStop(0, `${color}99`);- gradient.addColorStop(0.5, `${color}33`);- gradient.addColorStop(1, "#00000000");-- ctx.fillStyle = gradient;- ctx.fillRect(0, 0, canvas.width, canvas.height);- });-- requestAnimationFrame(animate);- }- animate();- }, [dimensions, colors, speed]);-- return (- <span- ref={containerRef}- className={`relative inline-block align-middle ${className}`}- style={{- width: dimensions.width || "auto",- height: dimensions.height || "auto",- }}- >- {/* Hidden text for SEO */}- <span className="sr-only">{children}</span>-- {/* Visual placeholder while canvas loads */}- <span- style={{- opacity: isReady ? 0 : 1,- transition: "opacity 0.2s ease-in",- position: isReady ? "absolute" : "relative",- display: "inline-block",- whiteSpace: "nowrap",- }}- aria-hidden="true"- >- {children}- </span>-- <div- className="absolute inset-0"- style={{- opacity: isReady ? 1 : 0,- transition: "opacity 0.2s ease-in",- }}- aria-hidden="true"- >- <svg- width={dimensions.width}- height={dimensions.height}- className="absolute inset-0"+ return (+ <span className={`relative inline-block ${className}`}>+ <span className="sr-only">{children}</span>+ <span+ className="relative animate-aurora bg-[length:200%_auto] bg-clip-text text-transparent"+ style={gradientStyle}+ aria-hidden="true">- <defs>- <clipPath id={maskId}>- <text- ref={textRef}- x="50%"- y="50%"- dominantBaseline="middle"- textAnchor="middle"- style={textStyle as CSSProperties}- >- {children}- </text>- </clipPath>- </defs>- </svg>+ {children}+ </span>+ </span>+ );+ },+);- <canvas- ref={canvasRef}- style={{- clipPath: `url(#${maskId})`,- WebkitClipPath: `url(#${maskId})`,- }}- className="h-full w-full"- />- </div>- </span>- );-}+AuroraText.displayName = "AuroraText";diff --git a/registry/registry-ui.ts b/registry/registry-ui.tsindex 2ae2e724..3e692103 100644--- a/registry/registry-ui.ts+++ b/registry/registry-ui.ts@@ -69,6 +69,41 @@ export const ui: Registry["items"] = [target: "components/magicui/aurora-text.tsx",},],+ tailwind: {+ config: {+ theme: {+ extend: {+ keyframes: {+ aurora: {+ "0%": {+ backgroundPosition: "0% 50%",+ transform: "rotate(-5deg) scale(0.9)",+ },+ "25%": {+ backgroundPosition: "50% 100%",+ transform: "rotate(5deg) scale(1.1)",+ },+ "50%": {+ backgroundPosition: "100% 50%",+ transform: "rotate(-3deg) scale(0.95)",+ },+ "75%": {+ backgroundPosition: "50% 0%",+ transform: "rotate(3deg) scale(1.05)",+ },+ "100%": {+ backgroundPosition: "0% 50%",+ transform: "rotate(-5deg) scale(0.9)",+ },+ },+ },+ animation: {+ aurora: "aurora 8s ease-in-out infinite alternate",+ },+ },+ },+ },+ },},{name: "morphing-text",diff --git a/tailwind.config.cjs b/tailwind.config.cjsindex d3b34203..9075a756 100644--- a/tailwind.config.cjs+++ b/tailwind.config.cjs@@ -86,8 +86,31 @@ module.exports = {pulse: "pulse var(--duration) ease-out infinite",rainbow: "rainbow var(--speed, 2s) infinite linear","line-shadow": "line-shadow 15s linear infinite",+ aurora: "aurora 8s ease-in-out infinite alternate",},keyframes: {+ aurora: {+ "0%": {+ backgroundPosition: "0% 50%",+ transform: "rotate(-5deg) scale(0.9)",+ },+ "25%": {+ backgroundPosition: "50% 100%",+ transform: "rotate(5deg) scale(1.1)",+ },+ "50%": {+ backgroundPosition: "100% 50%",+ transform: "rotate(-3deg) scale(0.95)",+ },+ "75%": {+ backgroundPosition: "50% 0%",+ transform: "rotate(3deg) scale(1.05)",+ },+ "100%": {+ backgroundPosition: "0% 50%",+ transform: "rotate(-5deg) scale(0.9)",+ },+ },"accordion-down": {from: { height: "0" },to: { height: "var(--radix-accordion-content-height)" },