Electric Card
A React card with an electric border effect: procedurally generated SVG lightning crackles around the edge on hover or keyboard focus. Built with Tailwind CSS and Motion, with tunable chaos, arc count, speed and color.
- Procedural SVG lightning
- Hover & focus-within
- No re-renders
- Any CSS color
- prefers-reduced-motion
Edge functions
Run code close to your users with cold starts under 50ms.
import { Zap } from "lucide-react";
import { ElectricCard } from "@/components/dusk/neon/electric-card";
import { Button } from "@/components/ui/button";
export default function ElectricCardDemo() {
return (
<ElectricCard className="w-full max-w-sm">
<div className="flex size-10 items-center justify-center rounded-lg border border-white/10 bg-white/5 text-white/70">
<Zap className="size-5" aria-hidden />
</div>
<h3 className="mt-4 text-lg font-semibold">Edge functions</h3>
<p className="mt-1 text-sm leading-relaxed text-white/60">
Run code close to your users with cold starts under 50ms.
</p>
<Button size="sm" className="mt-5">
Deploy now
</Button>
</ElectricCard>
);
}Installation
$ npx shadcn@latest add https://duskui.com/r/electric-card.json1. Install the dependencies.
npm install motion2. Copy the source into
components/dusk/neon/electric-card.tsx.electric-card.tsx "use client"; import * as React from "react"; import { useInView, useReducedMotion } from "motion/react"; import { cn } from "@/lib/utils"; export interface ElectricCardProps extends Omit<React.ComponentProps<"div">, "color"> { /** Any CSS color for the arcs. Defaults to the theme primary. */ color?: string; /** How far the lightning jumps off the border, in px. */ chaos?: number; /** Number of bright arcs crawling around the border. */ arcs?: number; /** Milliseconds between new lightning shapes. Clamped to at least 60ms. */ interval?: number; /** Laps per second the arcs crawl around the border. */ speed?: number; /** Keep the effect on. By default it runs on hover and keyboard focus inside the card. */ active?: boolean; /** Corner radius in px. The lightning follows this curve. */ radius?: number; } type Size = { w: number; h: number }; type Point = { x: number; y: number; nx: number; ny: number }; /** Point and outward normal at distance `s` along a rounded rectangle's perimeter. */ function perimeterPoint(s: number, { w, h }: Size, r: number): Point { const straightX = Math.max(0, w - 2 * r); const straightY = Math.max(0, h - 2 * r); const corner = (Math.PI * r) / 2; const total = 2 * straightX + 2 * straightY + 4 * corner; let d = ((s % total) + total) % total; const arc = (cx: number, cy: number, from: number): Point => { const a = from + (d / corner) * (Math.PI / 2); const nx = Math.cos(a); const ny = Math.sin(a); return { x: cx + nx * r, y: cy + ny * r, nx, ny }; }; if (d < straightX) return { x: r + d, y: 0, nx: 0, ny: -1 }; d -= straightX; if (d < corner) return arc(w - r, r, -Math.PI / 2); d -= corner; if (d < straightY) return { x: w, y: r + d, nx: 1, ny: 0 }; d -= straightY; if (d < corner) return arc(w - r, h - r, 0); d -= corner; if (d < straightX) return { x: w - r - d, y: h, nx: 0, ny: 1 }; d -= straightX; if (d < corner) return arc(r, h - r, Math.PI / 2); d -= corner; if (d < straightY) return { x: 0, y: h - r - d, nx: -1, ny: 0 }; d -= straightY; return arc(r, r, Math.PI); } const jitter = (amount: number) => (Math.random() * 2 - 1) * amount; const fmt = (n: number) => n.toFixed(1); /** A jagged bolt along the border from `start`, tapering at both ends, plus one forked spark. */ function boltPath(start: number, length: number, size: Size, r: number, chaos: number) { const steps = Math.max(4, Math.round(length / 9)); const fork = 1 + Math.floor(Math.random() * (steps - 2)); let d = ""; let spark = ""; for (let k = 0; k <= steps; k++) { const p = perimeterPoint(start + (k / steps) * length, size, r); const offset = jitter(chaos) * Math.sin((Math.PI * k) / steps); const x = p.x + p.nx * offset; const y = p.y + p.ny * offset; d += `${k === 0 ? "M" : "L"}${fmt(x)} ${fmt(y)}`; if (k === fork) { // A short branch shooting outwards from the bolt. const reach = chaos * (1.2 + Math.random()); spark = `M${fmt(x)} ${fmt(y)}L${fmt(x + p.nx * reach * 0.5 + jitter(chaos * 0.6))} ${fmt( y + p.ny * reach * 0.5 + jitter(chaos * 0.6) )}L${fmt(x + p.nx * reach + jitter(chaos * 0.8))} ${fmt(y + p.ny * reach + jitter(chaos * 0.8))}`; } } return d + spark; } /** A faint electric outline around the whole border. */ function outlinePath(size: Size, r: number, chaos: number, perimeter: number) { const steps = Math.max(12, Math.round(perimeter / 14)); let d = ""; for (let k = 0; k < steps; k++) { const p = perimeterPoint((k / steps) * perimeter, size, r); const offset = jitter(chaos * 0.3); d += `${k === 0 ? "M" : "L"}${fmt(p.x + p.nx * offset)} ${fmt(p.y + p.ny * offset)}`; } return `${d}Z`; } /** * A card whose border crackles with electricity on hover or keyboard focus. * * Lightning is procedurally generated SVG: jagged bolts that crawl around the * rounded border and re-randomize on an interval, with one soft glow. Paths are * written straight to the DOM, so the card never re-renders while it sparks. * The bolts change shape but never blink on and off, and reduced-motion users * get a single still frame instead of the crackle. */ export function ElectricCard({ color = "var(--color-primary)", chaos = 4, arcs = 1, interval = 110, speed = 0.25, active = false, radius = 12, className, style, children, onPointerEnter, onPointerLeave, onFocus, onBlur, ...props }: ElectricCardProps) { const ref = React.useRef<HTMLDivElement>(null); const outlineRef = React.useRef<SVGPathElement>(null); const boltRef = React.useRef<SVGPathElement>(null); const hovered = React.useRef(false); const timer = React.useRef<ReturnType<typeof setInterval> | null>(null); const size = React.useRef<Size>({ w: 0, h: 0 }); const offsets = React.useRef<number[]>([]); const inView = useInView(ref); const reduceMotion = useReducedMotion(); const boltId = `electric-bolt-${React.useId().replace(/[^a-zA-Z0-9_-]/g, "")}`; const tickMs = Math.max(60, interval); const draw = React.useCallback( (advance: boolean) => { const { w, h } = size.current; if (!w || !h) return; const r = Math.min(radius, w / 2, h / 2); // Border-box coordinates; inset half a pixel to sit on the 1px border. const box = { w: w - 1, h: h - 1 }; const perimeter = 2 * (box.w + box.h - 4 * r) + 2 * Math.PI * r; const count = Math.max(1, Math.round(arcs)); if (offsets.current.length !== count) { offsets.current = Array.from({ length: count }, (_, i) => (i / count) * perimeter + Math.random() * 40); } const step = advance ? speed * perimeter * (tickMs / 1000) : 0; offsets.current = offsets.current.map((o) => o + step); const bolts = offsets.current .map((o) => boltPath(o, perimeter * 0.2, box, r, chaos)) .join(""); boltRef.current?.setAttribute("d", bolts); outlineRef.current?.setAttribute("d", outlinePath(box, r, chaos, perimeter)); }, [arcs, chaos, radius, speed, tickMs] ); const start = React.useCallback(() => { draw(false); if (timer.current || reduceMotion) return; timer.current = setInterval(() => draw(true), tickMs); }, [draw, reduceMotion, tickMs]); const stop = React.useCallback(() => { if (timer.current) clearInterval(timer.current); timer.current = null; }, []); // Track the card's size without re-rendering. React.useEffect(() => { const el = ref.current; if (!el) return; const observer = new ResizeObserver(() => { size.current = { w: el.offsetWidth, h: el.offsetHeight }; draw(false); }); observer.observe(el); return () => observer.disconnect(); }, [draw]); React.useEffect(() => { if (inView && (active || hovered.current)) start(); else stop(); return stop; }, [inView, active, start, stop]); const activate = () => { hovered.current = true; if (inView) start(); }; const deactivate = () => { hovered.current = false; if (!active) stop(); }; const visible = cn( "transition-opacity duration-300 motion-reduce:transition-none", active ? "opacity-100" : "opacity-0 group-hover/electric:opacity-100 group-focus-within/electric:opacity-100" ); return ( <div ref={ref} data-slot="electric-card" data-active={active || undefined} className={cn( "group/electric relative isolate border border-white/10 bg-card p-6 text-card-foreground", "transition-colors duration-300 hover:border-(--electric-dim) focus-within:border-(--electric-dim) data-active:border-(--electric-dim)", className )} style={ { "--electric": color, "--electric-dim": "color-mix(in oklab, var(--electric) 30%, transparent)", borderRadius: radius, ...style, } as React.CSSProperties } onPointerEnter={(e) => { onPointerEnter?.(e); activate(); }} onPointerLeave={(e) => { onPointerLeave?.(e); deactivate(); }} onFocus={(e) => { onFocus?.(e); activate(); }} onBlur={(e) => { onBlur?.(e); if (!e.currentTarget.contains(e.relatedTarget as Node | null)) deactivate(); }} {...props} > <svg aria-hidden focusable="false" className={cn("pointer-events-none absolute -inset-px z-10 size-[calc(100%+2px)] overflow-visible", visible)} fill="none" strokeLinecap="round" strokeLinejoin="round" style={{ filter: "drop-shadow(0 0 3px color-mix(in oklab, var(--electric) 45%, transparent))" }} > <defs> <path ref={boltRef} id={boltId} /> </defs> <g transform="translate(0.5 0.5)"> <path ref={outlineRef} style={{ stroke: "var(--electric)" }} strokeOpacity={0.25} strokeWidth={1} /> <use href={`#${boltId}`} style={{ stroke: "var(--electric)" }} strokeOpacity={0.85} strokeWidth={1.5} /> </g> </svg> {children} </div> ); }
Usage
import { ElectricCard } from "@/components/dusk/neon/electric-card"
export function FeatureCard() {
return (
<ElectricCard color="#22d3ee" className="max-w-sm">
<h3 className="font-semibold">Realtime sync</h3>
<p className="text-sm text-white/60">Changes land everywhere in milliseconds.</p>
</ElectricCard>
)
}Examples
Always on
Set active to keep the card charged without hover — useful for live or highlighted states. The lightning follows any radius.
Live
1,284
requests per second
import { ElectricCard } from "@/components/dusk/neon/electric-card";
export default function ElectricCardActiveDemo() {
return (
<ElectricCard active color="#22d3ee" className="w-full max-w-xs text-center">
<p className="text-sm font-medium text-white/60">Live</p>
<p className="mt-1 text-3xl font-bold tabular-nums">1,284</p>
<p className="text-sm text-white/60">requests per second</p>
</ElectricCard>
);
}Calm to storm
chaos sets how far bolts jump off the border, arcs how many crawl around it and speed how fast they travel.
Calm
One arc, low chaos.
Storm
Three arcs, more chaos.
import { ElectricCard } from "@/components/dusk/neon/electric-card";
export default function ElectricCardChaosDemo() {
return (
<>
<ElectricCard color="#a78bfa" chaos={2} speed={0.15} className="w-56">
<p className="font-semibold">Calm</p>
<p className="mt-1 text-sm text-white/60">One arc, low chaos.</p>
</ElectricCard>
<ElectricCard color="#facc15" chaos={7} arcs={3} speed={0.4} className="w-56">
<p className="font-semibold">Storm</p>
<p className="mt-1 text-sm text-white/60">Three arcs, more chaos.</p>
</ElectricCard>
</>
);
}API Reference
Also accepts any <div> prop. Padding, width and background are regular classes.
| Prop | Type | Default | Description |
|---|---|---|---|
| color | string | var(--color-primary) | Any CSS color for the lightning. |
| chaos | number | 4 | How far bolts jump off the border, in px. |
| arcs | number | 1 | Number of bright arcs crawling around the border. |
| interval | number | 110 | Milliseconds between new lightning shapes (min 60). |
| speed | number | 0.25 | Laps per second the arcs crawl. |
| active | boolean | false | Keep the effect on instead of only on hover / focus. |
| radius | number | 12 | Corner radius in px; the lightning follows it. |
Accessibility
- The lightning SVG is aria-hidden and pointer-events-none; the card's content keeps its own semantics.
- The effect starts when keyboard focus moves into the card, not only on hover.
- Bolts change shape but never blink on and off and the overall brightness stays constant, so there is no strobing.
- With prefers-reduced-motion the card shows a single still frame of lightning instead of the crackle.
- Paths are written directly to the DOM and the timer only runs while the card is active and on-screen.