Animated Beam
An animated SVG beam for React that connects two elements with a curved path and sends a thin gradient pulse along it. Ideal for integration diagrams, data-flow illustrations and "how it works" sections; it measures the elements itself and redraws whenever the layout changes.
- Connects any two refs
- Curved paths
- ResizeObserver
- No re-renders while animating
- prefers-reduced-motion
"use client";
import * as React from "react";
import { Cloud, Database, GitBranch, Mail, MessageSquare, Webhook, Zap } from "lucide-react";
import { AnimatedBeam } from "@/components/dusk/animated-beam/animated-beam";
import { cn } from "@/lib/utils";
function Node({
ref,
label,
className,
children,
}: {
ref: React.Ref<HTMLDivElement>;
label: string;
className?: string;
children: React.ReactNode;
}) {
return (
<div
ref={ref}
title={label}
className={cn(
"relative z-10 flex size-12 items-center justify-center rounded-full border border-white/10 bg-card text-white/80 [&_svg]:size-5",
className
)}
>
{children}
<span className="sr-only">{label}</span>
</div>
);
}
export default function AnimatedBeamDemo() {
const containerRef = React.useRef<HTMLDivElement>(null);
const hubRef = React.useRef<HTMLDivElement>(null);
const r1 = React.useRef<HTMLDivElement>(null);
const r2 = React.useRef<HTMLDivElement>(null);
const r3 = React.useRef<HTMLDivElement>(null);
const r4 = React.useRef<HTMLDivElement>(null);
const r5 = React.useRef<HTMLDivElement>(null);
const r6 = React.useRef<HTMLDivElement>(null);
return (
<div
ref={containerRef}
className="relative flex h-80 w-full max-w-lg items-center justify-between overflow-hidden px-6 sm:px-10"
>
<div className="flex flex-col gap-8">
<Node ref={r1} label="Email"><Mail /></Node>
<Node ref={r2} label="Webhooks"><Webhook /></Node>
<Node ref={r3} label="Git"><GitBranch /></Node>
</div>
<Node
ref={hubRef}
label="Automation hub"
className="size-16 border-white/20 text-foreground [&_svg]:size-7"
>
<Zap />
</Node>
<div className="flex flex-col gap-8">
<Node ref={r4} label="Chat"><MessageSquare /></Node>
<Node ref={r5} label="Cloud storage"><Cloud /></Node>
<Node ref={r6} label="Database"><Database /></Node>
</div>
<AnimatedBeam containerRef={containerRef} fromRef={r1} toRef={hubRef} curvature={-30} delay={0} />
<AnimatedBeam containerRef={containerRef} fromRef={r2} toRef={hubRef} delay={0.4} />
<AnimatedBeam containerRef={containerRef} fromRef={r3} toRef={hubRef} curvature={30} delay={0.8} />
<AnimatedBeam containerRef={containerRef} fromRef={r4} toRef={hubRef} curvature={30} delay={0.2} />
<AnimatedBeam containerRef={containerRef} fromRef={r5} toRef={hubRef} delay={0.6} />
<AnimatedBeam containerRef={containerRef} fromRef={r6} toRef={hubRef} curvature={-30} delay={1} />
</div>
);
}Installation
$ npx shadcn@latest add https://duskui.com/r/animated-beam.json1. Install the dependencies.
npm install motion2. Copy the source into
components/dusk/animated-beam/animated-beam.tsx.animated-beam.tsx "use client"; import * as React from "react"; import { animate, motion, useMotionValue, useReducedMotion, useTransform, type MotionValue, } from "motion/react"; import { cn } from "@/lib/utils"; type Offset = { x?: number; y?: number }; export interface AnimatedBeamProps extends Omit<React.SVGProps<SVGSVGElement>, "ref" | "from" | "to"> { /** The positioned element both ends live in. The beam's SVG fills it. */ containerRef: React.RefObject<HTMLElement | null>; /** Element the beam starts from (its centre, plus `fromOffset`). */ fromRef: React.RefObject<HTMLElement | null>; /** Element the beam ends at (its centre, plus `toOffset`). */ toRef: React.RefObject<HTMLElement | null>; /** Bend of the curve in px, perpendicular to the straight line. Negative bends the other way. */ curvature?: number; /** Send the pulse from `to` back to `from`. */ reverse?: boolean; /** Seconds for the pulse to travel the whole beam. */ duration?: number; /** Seconds before the first pulse. Stagger several beams with it. */ delay?: number; /** Seconds of rest between pulses. */ repeatDelay?: number; /** Color of the resting track. */ pathColor?: string; /** Stroke width of the track and the pulse. */ pathWidth?: number; /** Opacity of the resting track. */ pathOpacity?: number; /** Pulse color at the `from` end. */ gradientStartColor?: string; /** Pulse color at the `to` end. */ gradientStopColor?: string; /** Length of the pulse as a fraction of the beam (0.05–0.6). */ pulseLength?: number; /** Nudge the start point, in px from the centre of `fromRef`. */ fromOffset?: Offset; /** Nudge the end point, in px from the centre of `toRef`. */ toOffset?: Offset; } type Geometry = { w: number; h: number; d: string; sx: number; sy: number; ex: number; ey: number }; // The pulse is drawn as a few stacked dashes that share a head, so the tail fades out along the curve. const TAIL = [1, 0.7, 0.45, 0.25]; function PulseDash({ d, head, length, reverse, stroke, width, opacity, }: { d: string; head: MotionValue<number>; length: number; reverse: boolean; stroke: string; width: number; opacity: number; }) { // With pathLength=1 a dash of `length` followed by a gap of 2 means only one dash is ever visible. // Forward: the dash covers [head - length, head]. Reverse: it covers [head, head + length]. const offset = useTransform(head, (h) => (reverse ? -h : length - h)); return ( <motion.path d={d} pathLength={1} fill="none" stroke={stroke} strokeWidth={width} strokeLinecap="round" strokeDasharray={`${length} 2`} style={{ strokeDashoffset: offset, opacity }} /> ); } /** * An SVG beam that links two elements with a gentle curve and sends a thin * pulse along it. Place it inside the positioned container that holds both * elements; it measures them and redraws whenever any of the three resize. * * The pulse is a motion value, so it never re-renders React. The SVG is * decorative (`aria-hidden`) and ignores the pointer. Under reduced motion * the beam is drawn as a still gradient line. */ export function AnimatedBeam({ containerRef, fromRef, toRef, curvature = 0, reverse = false, duration = 3, delay = 0, repeatDelay = 0.6, pathColor = "#ffffff", pathWidth = 1.5, pathOpacity = 0.1, gradientStartColor = "var(--color-primary)", gradientStopColor = "#e4e4e7", pulseLength = 0.25, fromOffset, toOffset, className, ...props }: AnimatedBeamProps) { const reduceMotion = useReducedMotion(); const rawId = React.useId(); const gradientId = `beam-${rawId.replace(/[^a-zA-Z0-9_-]/g, "")}`; const [geo, setGeo] = React.useState<Geometry | null>(null); const length = Math.min(0.6, Math.max(0.05, pulseLength)); const head = useMotionValue(reverse ? 1 : 0); const fx = fromOffset?.x ?? 0; const fy = fromOffset?.y ?? 0; const tx = toOffset?.x ?? 0; const ty = toOffset?.y ?? 0; React.useEffect(() => { const container = containerRef.current; const from = fromRef.current; const to = toRef.current; if (!container || !from || !to) return; const measure = () => { const c = container.getBoundingClientRect(); const a = from.getBoundingClientRect(); const b = to.getBoundingClientRect(); const sx = a.left - c.left + a.width / 2 + fx; const sy = a.top - c.top + a.height / 2 + fy; const ex = b.left - c.left + b.width / 2 + tx; const ey = b.top - c.top + b.height / 2 + ty; // Control point: the midpoint pushed sideways along the line's normal. const dx = ex - sx; const dy = ey - sy; const len = Math.hypot(dx, dy) || 1; const cx = (sx + ex) / 2 + (-dy / len) * curvature; const cy = (sy + ey) / 2 + (dx / len) * curvature; const round = (n: number) => Math.round(n * 100) / 100; const next: Geometry = { w: round(c.width), h: round(c.height), d: `M ${round(sx)},${round(sy)} Q ${round(cx)},${round(cy)} ${round(ex)},${round(ey)}`, sx: round(sx), sy: round(sy), ex: round(ex), ey: round(ey), }; setGeo((prev) => (prev && prev.d === next.d && prev.w === next.w && prev.h === next.h ? prev : next)); }; measure(); const observer = new ResizeObserver(measure); observer.observe(container); observer.observe(from); observer.observe(to); window.addEventListener("resize", measure); return () => { observer.disconnect(); window.removeEventListener("resize", measure); }; }, [containerRef, fromRef, toRef, curvature, fx, fy, tx, ty]); React.useEffect(() => { if (reduceMotion || !geo) return; // The head travels far enough past the end for the whole tail to leave the beam. const keyframes = reverse ? [1, -length] : [0, 1 + length]; head.set(keyframes[0]); const controls = animate(head, keyframes, { duration, delay, repeat: Infinity, repeatDelay, ease: [0.45, 0.05, 0.4, 1], }); return () => controls.stop(); }, [head, reverse, length, duration, delay, repeatDelay, reduceMotion, geo]); return ( <svg aria-hidden focusable="false" data-slot="animated-beam" width={geo?.w ?? 0} height={geo?.h ?? 0} viewBox={geo ? `0 0 ${geo.w} ${geo.h}` : undefined} className={cn("pointer-events-none absolute left-0 top-0 overflow-visible", className)} {...props} > {geo && ( <> <defs> <linearGradient id={gradientId} gradientUnits="userSpaceOnUse" x1={geo.sx} y1={geo.sy} x2={geo.ex} y2={geo.ey} > <stop offset="0" stopColor={gradientStartColor} /> <stop offset="1" stopColor={gradientStopColor} /> </linearGradient> </defs> <path d={geo.d} fill="none" stroke={pathColor} strokeOpacity={pathOpacity} strokeWidth={pathWidth} strokeLinecap="round" /> {reduceMotion ? ( <path d={geo.d} fill="none" stroke={`url(#${gradientId})`} strokeOpacity={0.4} strokeWidth={pathWidth} strokeLinecap="round" /> ) : ( <g> {TAIL.map((share, i) => ( <PulseDash key={i} d={geo.d} head={head} length={length * share} reverse={reverse} stroke={`url(#${gradientId})`} width={pathWidth} opacity={i === TAIL.length - 1 ? 0.85 : 0.3} /> ))} </g> )} </> )} </svg> ); }
Usage
"use client"
import { useRef } from "react"
import { AnimatedBeam } from "@/components/dusk/animated-beam/animated-beam"
export function Integration() {
const containerRef = useRef<HTMLDivElement>(null)
const appRef = useRef<HTMLDivElement>(null)
const apiRef = useRef<HTMLDivElement>(null)
return (
<div ref={containerRef} className="relative flex justify-between p-10">
<div ref={appRef} className="relative z-10 size-12 rounded-full bg-card" />
<div ref={apiRef} className="relative z-10 size-12 rounded-full bg-card" />
<AnimatedBeam containerRef={containerRef} fromRef={appRef} toRef={apiRef} curvature={-40} />
</div>
)
}Examples
Two-way traffic
Two beams between the same pair: one bends up, one bends down with reverse and its own colors.fromOffset / toOffset aim at the icons rather than the centre of each block.
"use client";
import * as React from "react";
import { Laptop, Server } from "lucide-react";
import { AnimatedBeam } from "@/components/dusk/animated-beam/animated-beam";
export default function AnimatedBeamBidirectional() {
const containerRef = React.useRef<HTMLDivElement>(null);
const clientRef = React.useRef<HTMLDivElement>(null);
const serverRef = React.useRef<HTMLDivElement>(null);
return (
<div ref={containerRef} className="relative flex h-56 w-full max-w-md items-center justify-between px-8">
<div ref={clientRef} className="relative z-10 flex flex-col items-center gap-2">
<span className="flex size-14 items-center justify-center rounded-2xl border border-white/10 bg-card text-white/80">
<Laptop className="size-6" aria-hidden />
</span>
<span className="text-xs text-white/60">Client</span>
</div>
<div ref={serverRef} className="relative z-10 flex flex-col items-center gap-2">
<span className="flex size-14 items-center justify-center rounded-2xl border border-white/10 bg-card text-white/80">
<Server className="size-6" aria-hidden />
</span>
<span className="text-xs text-white/60">Server</span>
</div>
{/* Request goes over the top, response comes back underneath. */}
<AnimatedBeam
containerRef={containerRef}
fromRef={clientRef}
toRef={serverRef}
fromOffset={{ y: -12 }}
toOffset={{ y: -12 }}
curvature={-50}
duration={2.4}
/>
<AnimatedBeam
containerRef={containerRef}
fromRef={clientRef}
toRef={serverRef}
fromOffset={{ y: -12 }}
toOffset={{ y: -12 }}
curvature={50}
duration={2.4}
delay={1.2}
reverse
gradientStartColor="#a1a1aa"
gradientStopColor="#e4e4e7"
/>
</div>
);
}Fan-out
One source, several targets. Stagger the pulses with delay and give each beam a different curvature.
"use client";
import * as React from "react";
import { Bell, CreditCard, Package, User } from "lucide-react";
import { AnimatedBeam } from "@/components/dusk/animated-beam/animated-beam";
function Service({ ref, label, children }: { ref: React.Ref<HTMLDivElement>; label: string; children: React.ReactNode }) {
return (
<div
ref={ref}
className="relative z-10 flex flex-col items-center gap-2 rounded-xl border border-white/10 bg-card px-3 py-2.5 text-xs text-white/70 [&_svg]:size-5 [&_svg]:text-white/80"
>
{children}
{label}
</div>
);
}
const beam = {
duration: 2,
repeatDelay: 1.2,
pulseLength: 0.35,
};
export default function AnimatedBeamFanOut() {
const containerRef = React.useRef<HTMLDivElement>(null);
const userRef = React.useRef<HTMLDivElement>(null);
const a = React.useRef<HTMLDivElement>(null);
const b = React.useRef<HTMLDivElement>(null);
const c = React.useRef<HTMLDivElement>(null);
return (
<div ref={containerRef} className="relative flex h-72 w-full max-w-md flex-col items-center justify-between py-6">
<div
ref={userRef}
className="relative z-10 flex items-center gap-2 rounded-full border border-white/10 bg-card px-4 py-2 text-sm text-white/80"
>
<User className="size-4" aria-hidden />
Checkout
</div>
<div className="flex w-full justify-around">
<Service ref={a} label="Billing"><CreditCard aria-hidden /></Service>
<Service ref={b} label="Fulfilment"><Package aria-hidden /></Service>
<Service ref={c} label="Notifications"><Bell aria-hidden /></Service>
</div>
<AnimatedBeam containerRef={containerRef} fromRef={userRef} toRef={a} curvature={40} {...beam} />
<AnimatedBeam containerRef={containerRef} fromRef={userRef} toRef={b} delay={0.25} {...beam} />
<AnimatedBeam containerRef={containerRef} fromRef={userRef} toRef={c} curvature={-40} delay={0.5} {...beam} />
</div>
);
}API Reference
Render the beam inside the positioned container that holds both elements, and give the elements relative z-10 so they sit above it. Also accepts SVG attributes for the root <svg>.
| Prop | Type | Default | Description |
|---|---|---|---|
| containerRef | RefObject<HTMLElement> | — | The positioned element both ends live in. The SVG fills it. |
| fromRef | RefObject<HTMLElement> | — | Element the beam starts from (its centre). |
| toRef | RefObject<HTMLElement> | — | Element the beam ends at (its centre). |
| curvature | number | 0 | Bend in px, perpendicular to the straight line. Negative bends the other way. |
| reverse | boolean | false | Send the pulse from to back to from. |
| duration | number | 3 | Seconds for the pulse to travel the beam. |
| delay | number | 0 | Seconds before the first pulse. |
| repeatDelay | number | 0.6 | Seconds of rest between pulses. |
| pathColor | string | "#ffffff" | Color of the resting track. |
| pathWidth | number | 1.5 | Stroke width of the track and pulse. |
| pathOpacity | number | 0.1 | Opacity of the resting track. |
| gradientStartColor | string | var(--color-primary) | Pulse color at the from end. |
| gradientStopColor | string | "#e4e4e7" | Pulse color at the to end. |
| pulseLength | number | 0.25 | Pulse length as a fraction of the beam (0.05–0.6). |
| fromOffset | { x?: number; y?: number } | — | Nudge the start point in px. |
| toOffset | { x?: number; y?: number } | — | Nudge the end point in px. |
Accessibility
- The SVG is aria-hidden and ignores the pointer; it never sits on top of interactive content.
- A beam is decoration. Label the connected elements themselves (visible text, or sr-only text inside icon nodes) so the relationship is conveyed without it.
- The pulse is driven by a motion value, so React does not re-render while it travels.
- With prefers-reduced-motion there is no pulse; the beam is drawn as a still gradient line.