Rainbow Button
A React rainbow button with a slowly scrolling gradient border, built on the DuskUI Button with Motion and Tailwind CSS. Pick a dark-filled or outline style, pass your own colors, and keep the width-stable loading state and every shadcn/ui Button prop.
- Built on DuskUI Button
- Animated gradient border
- Custom colors
- Loading state
- prefers-reduced-motion
import { ArrowRight, Github } from "lucide-react";
import { RainbowButton } from "@/components/dusk/rainbow-button/rainbow-button";
export default function RainbowButtonDemo() {
return (
<>
<RainbowButton size="lg">
Get started
<ArrowRight />
</RainbowButton>
<RainbowButton size="lg" variant="outline">
<Github />
Star on GitHub
</RainbowButton>
</>
);
}Installation
$ npx shadcn@latest add https://duskui.com/r/rainbow-button.json1. Install the dependencies.
npm install motion2. Copy the source into
components/dusk/rainbow-button/rainbow-button.tsx.rainbow-button.tsx "use client"; import * as React from "react"; import { animate, useInView, useMotionTemplate, useMotionValue, useReducedMotion, } from "motion/react"; import { MotionButton, type MotionButtonProps } from "@/components/ui/button"; import { cn } from "@/lib/utils"; export interface RainbowButtonProps extends Omit<MotionButtonProps, "variant" | "asChild"> { /** `default` has a dark fill; `outline` is see-through. */ variant?: "default" | "outline"; /** Gradient colors, in order. The loop is seamless, so the last color blends back into the first. */ colors?: string[]; /** Seconds for the gradient to scroll one full cycle. */ duration?: number; /** Border width in px. */ borderWidth?: number; } const RAINBOW = ["#fb7185", "#fbbf24", "#a3e635", "#38bdf8", "#a78bfa"]; /* * The border is the button's own ::before, not a child, so it stays put while * the base Button crossfades its label to a spinner. It reads the gradient * and its scroll position from CSS variables. */ /** Button with a slowly scrolling rainbow gradient border. */ export function RainbowButton({ variant = "default", colors = RAINBOW, duration = 6, borderWidth = 1, className, style, children, ...props }: RainbowButtonProps) { const sentinel = React.useRef<HTMLSpanElement>(null); const reduceMotion = useReducedMotion(); const inView = useInView(sentinel); const x = useMotionValue(0); const position = useMotionTemplate`${x}%`; React.useEffect(() => { if (reduceMotion || !inView) return; // The gradient repeats twice across a 200%-wide image, so 0% → 100% is one seamless cycle. const controls = animate(x, [x.get() % 100, (x.get() % 100) + 100], { duration: Math.max(0.5, duration), ease: "linear", repeat: Infinity, }); return () => controls.stop(); }, [duration, inView, reduceMotion, x]); const palette = colors.length ? colors : RAINBOW; const loop = [...palette, ...palette, palette[0]].join(", "); const vars = { "--rb-gradient": `linear-gradient(90deg, ${loop})`, "--rb-x": position, "--rb-w": `${borderWidth}px`, } as unknown as MotionButtonProps["style"]; return ( <MotionButton variant="default" data-rainbow-variant={variant} className={cn( "group/rainbow isolate bg-transparent text-white shadow-none", // Border layer (::before). "before:pointer-events-none before:absolute before:inset-0 before:-z-10 before:rounded-[inherit]", variant === "default" ? [ // Fill and border in one layer: a dark padding-box over the gradient border-box. "[--rb-fill:oklch(0.13_0.02_260)] hover:bg-transparent hover:[--rb-fill:oklch(0.18_0.02_260)]", "before:border-(length:--rb-w) before:border-transparent", "before:[background:linear-gradient(var(--rb-fill),var(--rb-fill))_padding-box,var(--rb-gradient)_var(--rb-x)_0/200%_100%_repeat-x_border-box]", ] : [ // Gradient masked down to a ring, so the fill stays see-through. "hover:bg-white/[0.04]", "before:p-(--rb-w) before:[background:var(--rb-gradient)_var(--rb-x)_0/200%_100%_repeat-x]", "before:[mask-image:linear-gradient(#000_0_0),linear-gradient(#000_0_0)] before:[mask-clip:content-box,border-box] before:[mask-composite:exclude]", ], className )} style={{ ...vars, ...style } as MotionButtonProps["style"]} {...props} > {/* Off-screen detection only: the gradient pauses when the button isn't visible. */} <span ref={sentinel} aria-hidden className="pointer-events-none absolute inset-0" /> {children} </MotionButton> ); }
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 { RainbowButton } from "@/components/dusk/rainbow-button/rainbow-button"
export function HeroCta() {
return <RainbowButton size="lg">Get started</RainbowButton>
}Examples
Colors & shape
Pass any list of CSS colors to colors — the loop is seamless. duration sets the seconds per cycle, borderWidth the line, and className can make it a pill.
import { RainbowButton } from "@/components/dusk/rainbow-button/rainbow-button";
export default function RainbowButtonColorsDemo() {
return (
<>
<RainbowButton colors={["#5eead4", "#7dd3fc", "#c4b5fd"]}>
Cool
</RainbowButton>
<RainbowButton colors={["#fda4af", "#fcd34d"]} duration={4}>
Warm
</RainbowButton>
<RainbowButton variant="outline" colors={["#f9a8d4", "#a5b4fc"]} className="rounded-full">
Pill outline
</RainbowButton>
</>
);
}Loading
The border is drawn on the button itself, so it keeps moving while loading crossfades the label to a spinner without changing the width.
"use client";
import * as React from "react";
import { RainbowButton } from "@/components/dusk/rainbow-button/rainbow-button";
export default function RainbowButtonLoadingDemo() {
const [loading, setLoading] = React.useState(false);
const timer = React.useRef<ReturnType<typeof setTimeout>>(undefined);
React.useEffect(() => () => clearTimeout(timer.current), []);
const publish = () => {
setLoading(true);
clearTimeout(timer.current);
timer.current = setTimeout(() => setLoading(false), 2000);
};
return (
<RainbowButton size="lg" loading={loading} loadingText="Publishing…" onClick={publish}>
Publish site
</RainbowButton>
);
}API Reference
Also accepts every DuskUI Button prop (size, loading, loadingText, loaderIcon) and Motion props such as whileTap. asChild is not supported.
| Prop | Type | Default | Description |
|---|---|---|---|
| variant | "default" | "outline" | "default" | Dark fill, or a see-through fill with a subtle hover background. |
| colors | string[] | 5-color rainbow | Gradient colors in order; the last blends back into the first. |
| duration | number | 6 | Seconds for the gradient to scroll one full cycle. |
| borderWidth | number | 1 | Border width in px. |
| className | string | — | Merged with tailwind-merge, so overrides such as rounded-full win. |
Accessibility
- Renders a native <button>, so Enter, Space and focus order work as usual, with the standard DuskUI focus-visible ring.
- Keyboard focus shows the standard focus ring.
- The border is a pseudo-element, so they never reach screen readers or shift layout.
- Loading keeps the button focusable (aria-busy + aria-disabled) instead of disabling it.
- With prefers-reduced-motion the gradient stays still; it also pauses whenever the button is off-screen.