UI Library for Design Engineers. Animated components and effects you can copy and paste into your apps. Free. Open Source.
fix: updated animated beam component and lint fix (#812)
Arghya Dasdd0e6b753876
diff --git a/apps/www/public/r/animated-beam.json b/apps/www/public/r/animated-beam.jsonindex 70f3367e..4cff7436 100644--- a/apps/www/public/r/animated-beam.json+++ b/apps/www/public/r/animated-beam.json@@ -10,7 +10,7 @@"files": [{"path": "registry/magicui/animated-beam.tsx",- "content": "\"use client\";\n\nimport { motion } from \"motion/react\";\nimport { RefObject, useEffect, useId, useState } from \"react\";\n\nimport { cn } from \"@/lib/utils\";\n\nexport interface AnimatedBeamProps {\n className?: string;\n containerRef: RefObject<HTMLElement | null>; // Container ref\n fromRef: RefObject<HTMLElement | null>;\n toRef: RefObject<HTMLElement | null>;\n curvature?: number;\n reverse?: boolean;\n pathColor?: string;\n pathWidth?: number;\n pathOpacity?: number;\n gradientStartColor?: string;\n gradientStopColor?: string;\n delay?: number;\n duration?: number;\n startXOffset?: number;\n startYOffset?: number;\n endXOffset?: number;\n endYOffset?: number;\n}\n\nexport const AnimatedBeam: React.FC<AnimatedBeamProps> = ({\n className,\n containerRef,\n fromRef,\n toRef,\n curvature = 0,\n reverse = false, // Include the reverse prop\n duration = Math.random() * 3 + 4,\n delay = 0,\n pathColor = \"gray\",\n pathWidth = 2,\n pathOpacity = 0.2,\n gradientStartColor = \"#ffaa40\",\n gradientStopColor = \"#9c40ff\",\n startXOffset = 0,\n startYOffset = 0,\n endXOffset = 0,\n endYOffset = 0,\n}) => {\n const id = useId();\n const [pathD, setPathD] = useState(\"\");\n const [svgDimensions, setSvgDimensions] = useState({ width: 0, height: 0 });\n\n // Calculate the gradient coordinates based on the reverse prop\n const gradientCoordinates = reverse\n ? {\n x1: [\"90%\", \"-10%\"],\n x2: [\"100%\", \"0%\"],\n y1: [\"0%\", \"0%\"],\n y2: [\"0%\", \"0%\"],\n }\n : {\n x1: [\"10%\", \"110%\"],\n x2: [\"0%\", \"100%\"],\n y1: [\"0%\", \"0%\"],\n y2: [\"0%\", \"0%\"],\n };\n\n useEffect(() => {\n const updatePath = () => {\n if (containerRef.current && fromRef.current && toRef.current) {\n const containerRect = containerRef.current.getBoundingClientRect();\n const rectA = fromRef.current.getBoundingClientRect();\n const rectB = toRef.current.getBoundingClientRect();\n\n const svgWidth = containerRect.width;\n const svgHeight = containerRect.height;\n setSvgDimensions({ width: svgWidth, height: svgHeight });\n\n const startX =\n rectA.left - containerRect.left + rectA.width / 2 + startXOffset;\n const startY =\n rectA.top - containerRect.top + rectA.height / 2 + startYOffset;\n const endX =\n rectB.left - containerRect.left + rectB.width / 2 + endXOffset;\n const endY =\n rectB.top - containerRect.top + rectB.height / 2 + endYOffset;\n\n const controlY = startY - curvature;\n const d = `M ${startX},${startY} Q ${\n (startX + endX) / 2\n },${controlY} ${endX},${endY}`;\n setPathD(d);\n }\n };\n\n // Initialize ResizeObserver\n const resizeObserver = new ResizeObserver((entries) => {\n // For all entries, recalculate the path\n for (let entry of entries) {\n updatePath();\n }\n });\n\n // Observe the container element\n if (containerRef.current) {\n resizeObserver.observe(containerRef.current);\n }\n\n // Call the updatePath initially to set the initial path\n updatePath();\n\n // Clean up the observer on component unmount\n return () => {\n resizeObserver.disconnect();\n };\n }, [\n containerRef,\n fromRef,\n toRef,\n curvature,\n startXOffset,\n startYOffset,\n endXOffset,\n endYOffset,\n ]);\n\n return (\n <svg\n fill=\"none\"\n width={svgDimensions.width}\n height={svgDimensions.height}\n xmlns=\"http://www.w3.org/2000/svg\"\n className={cn(\n \"pointer-events-none absolute left-0 top-0 transform-gpu stroke-2\",\n className,\n )}\n viewBox={`0 0 ${svgDimensions.width} ${svgDimensions.height}`}\n >\n <path\n d={pathD}\n stroke={pathColor}\n strokeWidth={pathWidth}\n strokeOpacity={pathOpacity}\n strokeLinecap=\"round\"\n />\n <path\n d={pathD}\n strokeWidth={pathWidth}\n stroke={`url(#${id})`}\n strokeOpacity=\"1\"\n strokeLinecap=\"round\"\n />\n <defs>\n <motion.linearGradient\n className=\"transform-gpu\"\n id={id}\n gradientUnits={\"userSpaceOnUse\"}\n initial={{\n x1: \"0%\",\n x2: \"0%\",\n y1: \"0%\",\n y2: \"0%\",\n }}\n animate={{\n x1: gradientCoordinates.x1,\n x2: gradientCoordinates.x2,\n y1: gradientCoordinates.y1,\n y2: gradientCoordinates.y2,\n }}\n transition={{\n delay,\n duration,\n ease: [0.16, 1, 0.3, 1], // https://easings.net/#easeOutExpo\n repeat: Infinity,\n repeatDelay: 0,\n }}\n >\n <stop stopColor={gradientStartColor} stopOpacity=\"0\"></stop>\n <stop stopColor={gradientStartColor}></stop>\n <stop offset=\"32.5%\" stopColor={gradientStopColor}></stop>\n <stop\n offset=\"100%\"\n stopColor={gradientStopColor}\n stopOpacity=\"0\"\n ></stop>\n </motion.linearGradient>\n </defs>\n </svg>\n );\n};\n",+ "content": "\"use client\";\n\nimport { motion } from \"motion/react\";\nimport { RefObject, useEffect, useId, useState } from \"react\";\n\nimport { cn } from \"@/lib/utils\";\n\nexport interface AnimatedBeamProps {\n className?: string;\n containerRef: RefObject<HTMLElement | null>; // Container ref\n fromRef: RefObject<HTMLElement | null>;\n toRef: RefObject<HTMLElement | null>;\n curvature?: number;\n reverse?: boolean;\n pathColor?: string;\n pathWidth?: number;\n pathOpacity?: number;\n gradientStartColor?: string;\n gradientStopColor?: string;\n delay?: number;\n duration?: number;\n startXOffset?: number;\n startYOffset?: number;\n endXOffset?: number;\n endYOffset?: number;\n}\n\nexport const AnimatedBeam: React.FC<AnimatedBeamProps> = ({\n className,\n containerRef,\n fromRef,\n toRef,\n curvature = 0,\n reverse = false, // Include the reverse prop\n duration = Math.random() * 3 + 4,\n delay = 0,\n pathColor = \"gray\",\n pathWidth = 2,\n pathOpacity = 0.2,\n gradientStartColor = \"#ffaa40\",\n gradientStopColor = \"#9c40ff\",\n startXOffset = 0,\n startYOffset = 0,\n endXOffset = 0,\n endYOffset = 0,\n}) => {\n const id = useId();\n const [pathD, setPathD] = useState(\"\");\n const [svgDimensions, setSvgDimensions] = useState({ width: 0, height: 0 });\n\n // Calculate the gradient coordinates based on the reverse prop\n const gradientCoordinates = reverse\n ? {\n x1: [\"90%\", \"-10%\"],\n x2: [\"100%\", \"0%\"],\n y1: [\"0%\", \"0%\"],\n y2: [\"0%\", \"0%\"],\n }\n : {\n x1: [\"10%\", \"110%\"],\n x2: [\"0%\", \"100%\"],\n y1: [\"0%\", \"0%\"],\n y2: [\"0%\", \"0%\"],\n };\n\n useEffect(() => {\n const updatePath = () => {\n if (containerRef.current && fromRef.current && toRef.current) {\n const containerRect = containerRef.current.getBoundingClientRect();\n const rectA = fromRef.current.getBoundingClientRect();\n const rectB = toRef.current.getBoundingClientRect();\n\n const svgWidth = containerRect.width;\n const svgHeight = containerRect.height;\n setSvgDimensions({ width: svgWidth, height: svgHeight });\n\n const startX =\n rectA.left - containerRect.left + rectA.width / 2 + startXOffset;\n const startY =\n rectA.top - containerRect.top + rectA.height / 2 + startYOffset;\n const endX =\n rectB.left - containerRect.left + rectB.width / 2 + endXOffset;\n const endY =\n rectB.top - containerRect.top + rectB.height / 2 + endYOffset;\n\n const controlY = startY - curvature;\n const d = `M ${startX},${startY} Q ${\n (startX + endX) / 2\n },${controlY} ${endX},${endY}`;\n setPathD(d);\n }\n };\n\n // Initialize ResizeObserver\n const resizeObserver = new ResizeObserver(() => {\n updatePath();\n });\n\n // Observe the container element\n if (containerRef.current) {\n resizeObserver.observe(containerRef.current);\n }\n\n // Call the updatePath initially to set the initial path\n updatePath();\n\n // Clean up the observer on component unmount\n return () => {\n resizeObserver.disconnect();\n };\n }, [\n containerRef,\n fromRef,\n toRef,\n curvature,\n startXOffset,\n startYOffset,\n endXOffset,\n endYOffset,\n ]);\n\n return (\n <svg\n fill=\"none\"\n width={svgDimensions.width}\n height={svgDimensions.height}\n xmlns=\"http://www.w3.org/2000/svg\"\n className={cn(\n \"pointer-events-none absolute left-0 top-0 transform-gpu stroke-2\",\n className,\n )}\n viewBox={`0 0 ${svgDimensions.width} ${svgDimensions.height}`}\n >\n <path\n d={pathD}\n stroke={pathColor}\n strokeWidth={pathWidth}\n strokeOpacity={pathOpacity}\n strokeLinecap=\"round\"\n />\n <path\n d={pathD}\n strokeWidth={pathWidth}\n stroke={`url(#${id})`}\n strokeOpacity=\"1\"\n strokeLinecap=\"round\"\n />\n <defs>\n <motion.linearGradient\n className=\"transform-gpu\"\n id={id}\n gradientUnits={\"userSpaceOnUse\"}\n initial={{\n x1: \"0%\",\n x2: \"0%\",\n y1: \"0%\",\n y2: \"0%\",\n }}\n animate={{\n x1: gradientCoordinates.x1,\n x2: gradientCoordinates.x2,\n y1: gradientCoordinates.y1,\n y2: gradientCoordinates.y2,\n }}\n transition={{\n delay,\n duration,\n ease: [0.16, 1, 0.3, 1], // https://easings.net/#easeOutExpo\n repeat: Infinity,\n repeatDelay: 0,\n }}\n >\n <stop stopColor={gradientStartColor} stopOpacity=\"0\"></stop>\n <stop stopColor={gradientStartColor}></stop>\n <stop offset=\"32.5%\" stopColor={gradientStopColor}></stop>\n <stop\n offset=\"100%\"\n stopColor={gradientStopColor}\n stopOpacity=\"0\"\n ></stop>\n </motion.linearGradient>\n </defs>\n </svg>\n );\n};\n","type": "registry:ui"}]diff --git a/apps/www/public/r/animated-theme-toggler.json b/apps/www/public/r/animated-theme-toggler.jsonindex 4791310d..5f2321c3 100644--- a/apps/www/public/r/animated-theme-toggler.json+++ b/apps/www/public/r/animated-theme-toggler.json@@ -10,7 +10,7 @@"files": [{"path": "registry/magicui/animated-theme-toggler.tsx",- "content": "\"use client\";\n\nimport { Moon, SunDim } from \"lucide-react\";\nimport { useState, useRef } from \"react\";\nimport { flushSync } from \"react-dom\";\nimport { cn } from \"@/lib/utils\";\n\ntype props = {\n className?: string;\n};\n\nexport const AnimatedThemeToggler = ({ className }: props) => {\n const [isDarkMode, setIsDarkMode] = useState<boolean>(false);\n const buttonRef = useRef<HTMLButtonElement | null>(null);\n const changeTheme = async () => {\n if (!buttonRef.current) return;\n\n await document.startViewTransition(() => {\n flushSync(() => {\n const dark = document.documentElement.classList.toggle(\"dark\");\n setIsDarkMode(dark);\n });\n }).ready;\n\n const { top, left, width, height } =\n buttonRef.current.getBoundingClientRect();\n const y = top + height / 2;\n const x = left + width / 2;\n\n const right = window.innerWidth - left;\n const bottom = window.innerHeight - top;\n const maxRad = Math.hypot(Math.max(left, right), Math.max(top, bottom));\n\n document.documentElement.animate(\n {\n clipPath: [\n `circle(0px at ${x}px ${y}px)`,\n `circle(${maxRad}px at ${x}px ${y}px)`,\n ],\n },\n {\n duration: 700,\n easing: \"ease-in-out\",\n pseudoElement: \"::view-transition-new(root)\",\n },\n );\n };\n return (\n <button ref={buttonRef} onClick={changeTheme} className={cn(className)}>\n {isDarkMode ? <SunDim /> : <Moon />}\n </button>\n );\n};\n",+ "content": "\"use client\";\n\nimport { Moon, Sun } from \"lucide-react\";\nimport { useState, useEffect, useRef, useCallback } from \"react\";\nimport { flushSync } from \"react-dom\";\nimport { cn } from \"@/lib/utils\";\n\ntype Props = {\n className?: string;\n};\n\nexport const AnimatedThemeToggler = ({ className }: Props) => {\n const [isDark, setIsDark] = useState(false);\n const buttonRef = useRef<HTMLButtonElement>(null);\n\n useEffect(() => {\n const updateTheme = () => {\n setIsDark(document.documentElement.classList.contains(\"dark\"));\n };\n\n updateTheme();\n\n const observer = new MutationObserver(updateTheme);\n observer.observe(document.documentElement, {\n attributes: true,\n attributeFilter: [\"class\"],\n });\n\n return () => observer.disconnect();\n }, []);\n\n const toggleTheme = useCallback(async () => {\n if (!buttonRef.current) return;\n\n await document.startViewTransition(() => {\n flushSync(() => {\n const newTheme = !isDark;\n setIsDark(newTheme);\n document.documentElement.classList.toggle(\"dark\");\n localStorage.setItem(\"theme\", newTheme ? \"dark\" : \"light\");\n });\n }).ready;\n\n const { top, left, width, height } =\n buttonRef.current.getBoundingClientRect();\n const x = left + width / 2;\n const y = top + height / 2;\n const maxRadius = Math.hypot(\n Math.max(left, window.innerWidth - left),\n Math.max(top, window.innerHeight - top),\n );\n\n document.documentElement.animate(\n {\n clipPath: [\n `circle(0px at ${x}px ${y}px)`,\n `circle(${maxRadius}px at ${x}px ${y}px)`,\n ],\n },\n {\n duration: 700,\n easing: \"ease-in-out\",\n pseudoElement: \"::view-transition-new(root)\",\n },\n );\n }, [isDark]);\n\n return (\n <button ref={buttonRef} onClick={toggleTheme} className={cn(className)}>\n {isDark ? <Sun /> : <Moon />}\n </button>\n );\n};\n","type": "registry:ui"}],diff --git a/apps/www/registry/magicui/animated-beam.tsx b/apps/www/registry/magicui/animated-beam.tsxindex 4dd04a70..e824264c 100644--- a/apps/www/registry/magicui/animated-beam.tsx+++ b/apps/www/registry/magicui/animated-beam.tsx@@ -92,11 +92,8 @@ export const AnimatedBeam: React.FC<AnimatedBeamProps> = ({};// Initialize ResizeObserver- const resizeObserver = new ResizeObserver((entries) => {- // For all entries, recalculate the path- for (let entry of entries) {- updatePath();- }+ const resizeObserver = new ResizeObserver(() => {+ updatePath();});// Observe the container element