Neon Border
An animated neon border for any React element: a beam of light travels around a thin edge with a faint glow. Built with Motion and Tailwind CSS using a masked conic gradient, so it fits cards, pricing tables, pills and inputs at any size or aspect ratio.
- Wraps any content
- Travelling light beam
- Hover trigger
- Pauses off-screen
- prefers-reduced-motion
Pro
$12/mo
- Unlimited projects
- Priority support
- Custom domains
import { Check } from "lucide-react";
import { NeonBorder } from "@/components/dusk/neon/neon-border";
export default function NeonBorderDemo() {
return (
<NeonBorder className="w-full max-w-xs" contentClassName="bg-card p-6">
<p className="text-sm font-medium text-white">Pro</p>
<p className="mt-1 text-3xl font-bold">
$12<span className="text-base font-normal text-white/50">/mo</span>
</p>
<ul className="mt-4 space-y-2 text-sm text-white/70">
{["Unlimited projects", "Priority support", "Custom domains"].map((item) => (
<li key={item} className="flex items-center gap-2">
<Check className="size-4 text-white/50" aria-hidden />
{item}
</li>
))}
</ul>
</NeonBorder>
);
}Installation
$ npx shadcn@latest add https://duskui.com/r/neon-border.json1. Install the dependencies.
npm install motion2. Copy the source into
components/dusk/neon/neon-border.tsx.neon-border.tsx "use client"; import * as React from "react"; import { animate, motion, useInView, useMotionTemplate, useMotionValue, useReducedMotion, type AnimationPlaybackControls, } from "motion/react"; import { cn } from "@/lib/utils"; export interface NeonBorderProps extends Omit<React.ComponentProps<"div">, "color"> { /** Any CSS color for the travelling light. Defaults to the theme primary. */ color?: string; /** Optional second color the light fades into, for a two-tone beam. */ secondaryColor?: string; /** Seconds for one lap around the border. */ speed?: number; /** Border width in px. */ thickness?: number; /** Corner radius: a number in px or any CSS length. */ radius?: number | string; /** Soft glow around the beam, from 0 (off) to 1. */ glow?: number; /** `always` runs continuously; `hover` lights up only on hover or keyboard focus inside. */ trigger?: "always" | "hover"; /** Classes for the inner content wrapper. */ contentClassName?: string; } /** Masks a layer down to its padding box, leaving only a ring the width of its padding. */ const RING = "pointer-events-none absolute inset-0 rounded-[inherit] [mask-image:linear-gradient(#000_0_0),linear-gradient(#000_0_0)] [mask-clip:content-box,border-box] [mask-composite:exclude]"; /** * Wraps any content in a neon border with a beam of light travelling around it. * * The beam is a conic gradient whose angle is a Motion value, masked to the * border ring, so it works at any aspect ratio without measuring. It pauses * when off-screen. Reduced-motion users get a steady, fully lit border. */ export function NeonBorder({ color = "var(--color-primary)", secondaryColor, speed = 6, thickness = 1, radius = 12, glow = 0.5, trigger = "always", className, contentClassName, style, children, onPointerEnter, onPointerLeave, onFocus, onBlur, ...props }: NeonBorderProps) { const ref = React.useRef<HTMLDivElement>(null); const controls = React.useRef<AnimationPlaybackControls | null>(null); const hovered = React.useRef(false); const reduceMotion = useReducedMotion(); const inView = useInView(ref); const angle = useMotionValue(0); const tail = secondaryColor ?? "var(--neon)"; const beam = useMotionTemplate`conic-gradient(from ${angle}deg, transparent 0%, transparent 60%, color-mix(in oklab, ${tail} 45%, transparent) 80%, var(--neon) 95%, transparent 100%)`; const start = React.useCallback(() => { if (controls.current || reduceMotion) return; controls.current = animate(angle, angle.get() + 360, { duration: Math.max(0.5, speed), ease: "linear", repeat: Infinity, }); }, [angle, reduceMotion, speed]); const stop = React.useCallback(() => { controls.current?.stop(); controls.current = null; }, []); React.useEffect(() => { if (inView && (trigger === "always" || hovered.current)) start(); else stop(); return stop; }, [inView, trigger, start, stop]); const activate = () => { hovered.current = true; if (trigger === "hover" && inView) start(); }; const deactivate = () => { hovered.current = false; if (trigger === "hover") stop(); }; const r = typeof radius === "number" ? `${radius}px` : radius; const vars = { "--neon": color, borderRadius: r } as React.CSSProperties; // Reduced motion: no travelling beam, just a steady lit border. const lit = reduceMotion ? "var(--neon)" : beam; const beamVisibility = trigger === "hover" ? "opacity-0 transition-opacity duration-300 group-hover/neon-border:opacity-100 group-focus-within/neon-border:opacity-100 motion-reduce:transition-none" : undefined; return ( <div ref={ref} data-slot="neon-border" className={cn("group/neon-border relative isolate", className)} style={{ ...vars, ...style }} 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} > {/* Dim resting tube so the shape reads even where the beam isn't. */} <span aria-hidden className={RING} style={{ padding: thickness, background: "color-mix(in oklab, var(--neon) 15%, transparent)", }} /> {glow > 0 && ( // Blur is applied to the wrapper, after the inner ring is masked. <span aria-hidden className={cn("pointer-events-none absolute inset-0 rounded-[inherit]", beamVisibility)} style={{ filter: "blur(6px)" }} > <motion.span className={RING} style={{ padding: thickness * 2, background: lit, opacity: Math.min(0.5, 0.4 * glow) }} /> </span> )} <motion.span aria-hidden className={cn(RING, beamVisibility)} style={{ padding: thickness, background: lit }} /> <div data-slot="neon-border-content" className={cn("relative", contentClassName)} style={{ margin: thickness, borderRadius: `max(0px, calc(${r} - ${thickness}px))` }} > {children} </div> </div> ); }
The border sits outside your content, so give the content its own background (for example contentClassName="bg-card") to keep the glow from tinting it.
Usage
import { NeonBorder } from "@/components/dusk/neon/neon-border"
export function FeaturedPlan() {
return (
<NeonBorder radius={20} contentClassName="bg-card p-6">
<h3>Pro</h3>
<p>Everything in Free, plus…</p>
</NeonBorder>
)
}Examples
On hover
With trigger="hover" the beam only lights up while the pointer is over the element or keyboard focus is inside it — great for link cards.
import { NeonBorder } from "@/components/dusk/neon/neon-border";
const links = [
{ title: "Docs", text: "Guides and API reference." },
{ title: "Templates", text: "Start from a full page." },
];
export default function NeonBorderHoverDemo() {
return (
<div className="grid w-full max-w-lg gap-4 sm:grid-cols-2">
{links.map((link) => (
<NeonBorder key={link.title} trigger="hover" color="#22d3ee" speed={3}>
<a
href="#"
className="block rounded-[inherit] bg-card p-5 outline-none focus-visible:ring-2 focus-visible:ring-[#22d3ee]/50"
>
<span className="font-semibold">{link.title}</span>
<span className="mt-1 block text-sm text-white/60">{link.text}</span>
</a>
</NeonBorder>
))}
</div>
);
}Two-tone & pill
secondaryColor fades the beam's tail into a second color. Set a large radius for pills and badges, and tune thickness, glow and speed.
Two-tone
A second color and a 2px border
import { NeonBorder } from "@/components/dusk/neon/neon-border";
export default function NeonBorderTwoToneDemo() {
return (
<>
<NeonBorder color="#f472b6" secondaryColor="#22d3ee" thickness={2} glow={1} speed={4}>
<div className="rounded-[inherit] bg-card px-8 py-6 text-center">
<p className="text-lg font-semibold">Two-tone</p>
<p className="text-sm text-white/60">A second color and a 2px border</p>
</div>
</NeonBorder>
<NeonBorder radius={9999} glow={0} speed={4} color="#facc15">
<span className="block rounded-full bg-card px-4 py-1.5 text-sm font-medium">Version 2.0 is out</span>
</NeonBorder>
</>
);
}API Reference
Also accepts any <div> prop for the outer wrapper.
| Prop | Type | Default | Description |
|---|---|---|---|
| color | string | var(--color-primary) | Any CSS color for the beam and the dim resting tube. |
| secondaryColor | string | — | Color the beam's tail fades into. |
| speed | number | 6 | Seconds per lap around the border. |
| thickness | number | 1 | Border width in px. |
| radius | number | string | 12 | Corner radius in px or any CSS length. |
| glow | number | 0.5 | Soft glow around the beam, from 0 (off) to 1. |
| trigger | "always" | "hover" | "always" | Run continuously, or only on hover / focus-within. |
| contentClassName | string | — | Classes for the inner content wrapper. |
Accessibility
- All border and glow layers are aria-hidden and pointer-events-none; your content keeps its own semantics and focus order.
- The hover trigger also responds to keyboard focus anywhere inside the wrapper.
- The beam moves smoothly with no flashing, and the animation pauses while the element is off-screen.
- With prefers-reduced-motion the beam stops and the whole border stays steadily lit.