Moving Border
A React moving border: a soft beam that travels along the rounded edge of any element, at constant speed around straight sides and corners alike. Use the MovingBorder wrapper for cards and badges, or the ready-made MovingBorderButton built on the DuskUI Button. Made with Motion and Tailwind CSS.
- Wrapper + button
- Follows rounded corners
- Pauses off-screen
- No layout shift
- prefers-reduced-motion
import { ArrowRight } from "lucide-react";
import { MovingBorderButton } from "@/components/dusk/moving-border/moving-border";
export default function MovingBorderDemo() {
return (
<>
<MovingBorderButton size="lg">
Book a demo
<ArrowRight />
</MovingBorderButton>
<MovingBorderButton size="lg" color="#c4b5fd" radius={8}>
Rounded corners
</MovingBorderButton>
</>
);
}Installation
$ npx shadcn@latest add https://duskui.com/r/moving-border.json1. Install the dependencies.
npm install motion2. Copy the source into
components/dusk/moving-border/moving-border.tsx.moving-border.tsx "use client"; import * as React from "react"; import { animate, motion, useInView, useMotionValue, useReducedMotion, } from "motion/react"; import { Button, type ButtonProps } from "@/components/ui/button"; import { cn } from "@/lib/utils"; export interface MovingBorderProps extends Omit<React.ComponentProps<"div">, "color"> { /** Any CSS color for the travelling light. Defaults to the theme primary. */ color?: string; /** Seconds for one lap around the border. */ speed?: number; /** Corner radius in px. Large values (e.g. 9999) give a pill. */ radius?: number; /** Border width in px: how much of the light shows around the content. */ borderWidth?: number; /** Diameter of the beam in px. */ beamSize?: number; /** Run the beam counter-clockwise. */ reverse?: boolean; /** Classes for the inner content wrapper, which paints its own background. */ contentClassName?: string; } /** * Point at fraction `t` (0–1) of the way around a rounded rectangle, walking * clockwise from the top-left corner. Moves at constant speed on every side. */ function pointOnRoundedRect(t: number, w: number, h: number, r: number) { const rad = Math.max(0, Math.min(r, w / 2, h / 2)); const sx = w - 2 * rad; const sy = h - 2 * rad; const arc = (Math.PI / 2) * rad; const segments = [sx, arc, sy, arc, sx, arc, sy, arc]; const total = segments.reduce((a, b) => a + b, 0) || 1; let d = (((t % 1) + 1) % 1) * total; for (let i = 0; i < segments.length; i++) { const len = segments[i]; if (d > len && i < segments.length - 1) { d -= len; continue; } const k = len === 0 ? 0 : d / len; const corner = (cx: number, cy: number, start: number) => { const a = start + k * (Math.PI / 2); return { x: cx + rad * Math.cos(a), y: cy + rad * Math.sin(a) }; }; switch (i) { case 0: return { x: rad + k * sx, y: 0 }; case 1: return corner(w - rad, rad, -Math.PI / 2); case 2: return { x: w, y: rad + k * sy }; case 3: return corner(w - rad, h - rad, 0); case 4: return { x: w - rad - k * sx, y: h }; case 5: return corner(rad, h - rad, Math.PI / 2); case 6: return { x: 0, y: h - rad - k * sy }; default: return corner(rad, rad, Math.PI); } } return { x: 0, y: 0 }; } /** Wraps any element in a thin border with a soft beam that travels around its rounded edge. */ export function MovingBorder({ color = "var(--color-primary)", speed = 6, radius = 12, borderWidth = 1, beamSize = 64, reverse = false, className, contentClassName, style, children, ...props }: MovingBorderProps) { const ref = React.useRef<HTMLDivElement>(null); const size = React.useRef({ w: 0, h: 0 }); const reduceMotion = useReducedMotion(); const inView = useInView(ref); const progress = useMotionValue(0); // Start off-canvas so nothing flashes before the first measurement. const x = useMotionValue(-beamSize * 2); const y = useMotionValue(-beamSize * 2); const place = React.useCallback(() => { const { w, h } = size.current; if (!w || !h) return; // Walk the centre line of the border so the beam sits on it at every corner. const inset = borderWidth / 2; const p = pointOnRoundedRect( reverse ? -progress.get() : progress.get(), w - borderWidth, h - borderWidth, Math.max(0, radius - inset) ); x.set(p.x + inset - beamSize / 2); y.set(p.y + inset - beamSize / 2); }, [beamSize, borderWidth, progress, radius, reverse, x, y]); React.useEffect(() => { const el = ref.current; if (!el) return; const observer = new ResizeObserver(() => { size.current = { w: el.offsetWidth, h: el.offsetHeight }; place(); }); observer.observe(el); return () => observer.disconnect(); }, [place]); React.useEffect(() => progress.on("change", place), [place, progress]); React.useEffect(() => { if (reduceMotion || !inView) return; const controls = animate(progress, progress.get() + 1, { duration: Math.max(0.5, speed), ease: "linear", repeat: Infinity, }); return () => controls.stop(); }, [inView, progress, reduceMotion, speed]); return ( <div ref={ref} data-slot="moving-border" className={cn( "relative isolate overflow-hidden", // Reduced motion: no beam, a steadily lit edge instead. "motion-reduce:[&>[data-slot=moving-border-beam]]:hidden motion-reduce:[&>[data-slot=moving-border-track]]:bg-(--mb)/45", className )} style={{ "--mb": color, borderRadius: radius, padding: borderWidth, ...style } as React.CSSProperties} {...props} > {/* Faint resting track. */} <span aria-hidden data-slot="moving-border-track" className="pointer-events-none absolute inset-0 -z-10 rounded-[inherit] bg-white/10" /> <motion.span aria-hidden data-slot="moving-border-beam" className="pointer-events-none absolute top-0 left-0 -z-10 rounded-full" style={{ x, y, width: beamSize, height: beamSize, background: "radial-gradient(closest-side, color-mix(in oklab, var(--mb) 85%, transparent) 0%, color-mix(in oklab, var(--mb) 35%, transparent) 55%, transparent 100%)", }} /> <div data-slot="moving-border-content" className={cn("relative h-full bg-card", contentClassName)} style={{ borderRadius: Math.max(0, radius - borderWidth) }} > {children} </div> </div> ); } export interface MovingBorderButtonProps extends Omit<ButtonProps, "asChild" | "color"> { /** Any CSS color for the travelling light. Defaults to the theme primary. */ color?: string; /** Seconds for one lap around the border. */ speed?: number; /** Corner radius in px. Defaults to a pill. */ radius?: number; /** Border width in px. */ borderWidth?: number; /** Diameter of the beam in px. */ beamSize?: number; /** Classes for the outer border wrapper. `className` goes to the button itself. */ containerClassName?: string; } /** Ready-made DuskUI Button with a soft beam circling its border. */ export function MovingBorderButton({ color, speed = 5, radius = 9999, borderWidth = 1, beamSize = 48, containerClassName, className, style, variant = "ghost", ...props }: MovingBorderButtonProps) { return ( <MovingBorder color={color} speed={speed} radius={radius} borderWidth={borderWidth} beamSize={beamSize} // The wrapper clips its overflow, so it draws the keyboard focus ring itself. className={cn( "inline-flex align-middle has-[:focus-visible]:ring-[3px] has-[:focus-visible]:ring-ring/50", containerClassName )} contentClassName="bg-transparent" > <Button variant={variant} className={cn( "focus-visible:ring-0", // Ghost (the default) gets an opaque fill so the beam only shows on the edge. variant === "ghost" && "bg-background text-foreground hover:bg-[color-mix(in_oklab,white_5%,var(--color-background))] dark:hover:bg-[color-mix(in_oklab,white_5%,var(--color-background))]", className )} style={{ borderRadius: Math.max(0, radius - borderWidth), ...style }} {...props} /> </MovingBorder> ); }
MovingBorderButton builds on the DuskUI Button; the CLI installs it automatically. Installing by hand? Add components/ui/button.tsx from the Button page first.
Usage
import { MovingBorder, MovingBorderButton } from "@/components/dusk/moving-border/moving-border"
export function BookDemo() {
return (
<>
<MovingBorderButton size="lg">Book a demo</MovingBorderButton>
<MovingBorder radius={20} contentClassName="p-6">
Any content
</MovingBorder>
</>
)
}Examples
Card wrapper
Wrap any block in MovingBorder. The content wrapper paints its own background, so the beam only shows through the borderWidth gap. A bigger beamSize gives a longer streak.
Edge functions
Run code close to your users in 30 regions.
import { Cpu } from "lucide-react";
import { MovingBorder } from "@/components/dusk/moving-border/moving-border";
export default function MovingBorderCardDemo() {
return (
<MovingBorder className="w-full max-w-sm" beamSize={100} contentClassName="p-6">
<Cpu className="size-6 text-white/60" aria-hidden />
<p className="mt-4 text-lg font-semibold">Edge functions</p>
<p className="mt-1 text-sm text-white/60">
Run code close to your users in 30 regions.
</p>
</MovingBorder>
);
}Color, speed & direction
color takes any CSS color, speed is the seconds per lap and reverse runs it counter-clockwise.
import { MovingBorder, MovingBorderButton } from "@/components/dusk/moving-border/moving-border";
export default function MovingBorderColorsDemo() {
return (
<>
<MovingBorderButton color="#f9a8d4" speed={3}>
Faster lap
</MovingBorderButton>
<MovingBorderButton color="#7dd3fc" speed={8} beamSize={72}>
Slower, wider beam
</MovingBorderButton>
<MovingBorder radius={9999} color="#fde68a" reverse speed={5} beamSize={40}>
<span className="block px-4 py-1.5 text-sm font-medium">Counter-clockwise badge</span>
</MovingBorder>
</>
);
}API Reference
MovingBorder also accepts every <div> prop. MovingBorderButton accepts the same beam props plus every DuskUI Button prop (except asChild); its className goes to the button and containerClassName to the border wrapper. It defaults to a pill with radius 9999, speed 3 and beamSize 56.
| Prop | Type | Default | Description |
|---|---|---|---|
| color | string | var(--color-primary) | Any CSS color for the travelling light. |
| speed | number | 6 (button: 5) | Seconds for one lap around the border. |
| radius | number | 12 (button: 9999) | Corner radius in px. Large values give a pill. |
| borderWidth | number | 1 | Border width in px: how much of the light shows around the content. |
| beamSize | number | 64 (button: 48) | Diameter of the beam in px. |
| reverse | boolean | false | Run the beam counter-clockwise. |
| contentClassName | string | — | Classes for the inner content wrapper (padding, background). |
| containerClassName | string | — | MovingBorderButton only: classes for the outer border wrapper. |
Accessibility
- The beam and track are aria-hidden and absolutely positioned, so they never reach screen readers or change the content's size.
- MovingBorderButton renders a native <button>; because the wrapper clips the beam, it draws the focus-visible ring on the wrapper so keyboard focus stays clearly visible.
- With prefers-reduced-motion the beam is hidden and the whole edge is lit steadily instead; the animation also pauses whenever the element is off-screen.
- The beam is positioned with Motion values, so it never re-renders React and is cleaned up on unmount.