Pulsating Button
A React pulsating button that sends out soft expanding rings to draw attention to a call to action. Built on the DuskUI Button with Motion and Tailwind CSS: the pulse pauses on hover and keyboard focus, stops off-screen, and every variant, size and the loading state keep working.
- Built on DuskUI Button
- Any pulse color
- Pauses on hover & focus
- No layout shift
- prefers-reduced-motion
import { Bell } from "lucide-react";
import { PulsatingButton } from "@/components/dusk/pulsating-button/pulsating-button";
export default function PulsatingButtonDemo() {
return (
<>
<PulsatingButton size="lg">Join the waitlist</PulsatingButton>
<PulsatingButton size="lg" variant="outline" pulseColor="rgb(255 255 255)">
<Bell />
Enable alerts
</PulsatingButton>
</>
);
}Installation
$ npx shadcn@latest add https://duskui.com/r/pulsating-button.json1. Install the dependencies.
npm install motion2. Copy the source into
components/dusk/pulsating-button/pulsating-button.tsx.pulsating-button.tsx "use client"; import * as React from "react"; import { animate, motion, useInView, useMotionValue, useReducedMotion, useTransform, } from "motion/react"; import { Button, type ButtonProps } from "@/components/ui/button"; import { cn } from "@/lib/utils"; export interface PulsatingButtonProps extends Omit<ButtonProps, "asChild"> { /** Any CSS color for the rings. Defaults to the theme primary. */ pulseColor?: string; /** Seconds for one ring to expand and fade. A second ring follows half a beat later. */ duration?: number; /** How far the rings travel outwards, in px. */ spread?: number; } /** Button that sends out soft expanding rings to draw attention, pausing on hover and focus. */ export function PulsatingButton({ pulseColor = "var(--color-primary)", duration = 2.4, spread = 8, className, style, children, onPointerEnter, onPointerLeave, onFocus, onBlur, ...props }: PulsatingButtonProps) { const rings = React.useRef<HTMLSpanElement>(null); const reduceMotion = useReducedMotion(); const inView = useInView(rings); const [hovered, setHovered] = React.useState(false); const [focused, setFocused] = React.useState(false); // `progress` drives both rings; `level` fades them out when paused. const progress = useMotionValue(0); const level = useMotionValue(1); const boxShadow = useTransform(() => { const t = progress.get(); const l = level.get(); return [t % 1, (t + 0.5) % 1] .map((p) => { // Ease out so rings leave quickly, then drift and fade. const eased = 1 - (1 - p) ** 2; const alpha = Math.round((1 - p) * 28 * l); return `0 0 0 ${(eased * spread).toFixed(2)}px color-mix(in oklab, var(--pulse) ${alpha}%, transparent)`; }) .join(", "); }); const paused = hovered || focused || !inView || reduceMotion === true; React.useEffect(() => { if (paused) { const fade = animate(level, 0, { duration: 0.25, ease: "easeOut" }); return () => fade.stop(); } level.set(1); const controls = animate(progress, progress.get() + 1, { duration: Math.max(0.6, duration), ease: "linear", repeat: Infinity, }); return () => controls.stop(); }, [duration, level, paused, progress]); return ( <Button className={cn("group/pulse", className)} style={{ "--pulse": pulseColor, ...style } as React.CSSProperties} onPointerEnter={(e) => { onPointerEnter?.(e); setHovered(true); }} onPointerLeave={(e) => { onPointerLeave?.(e); setHovered(false); }} onFocus={(e) => { onFocus?.(e); setFocused(true); }} onBlur={(e) => { onBlur?.(e); setFocused(false); }} {...props} > {/* Rings are an outer box-shadow on an empty layer, so they never take up space. */} <motion.span ref={rings} aria-hidden data-slot="pulse-rings" className="pointer-events-none absolute inset-0 rounded-[inherit]" style={{ boxShadow }} /> {/* Reduced motion: a steady thin ring instead of moving rings (CSS-only, so SSR markup matches). */} <span aria-hidden className="pointer-events-none absolute inset-0 hidden rounded-[inherit] shadow-[0_0_0_3px_color-mix(in_oklab,var(--pulse)_18%,transparent)] motion-reduce:block" /> {children} </Button> ); }
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 { PulsatingButton } from "@/components/dusk/pulsating-button/pulsating-button"
export function WaitlistCta() {
return <PulsatingButton size="lg">Join the waitlist</PulsatingButton>
}Examples
Colors, speed & spread
pulseColor takes any CSS color, duration sets the seconds per ring and spread how far the rings travel. Works with every Button variant and size.
import { Mic } from "lucide-react";
import { PulsatingButton } from "@/components/dusk/pulsating-button/pulsating-button";
export default function PulsatingButtonColorsDemo() {
return (
<>
<PulsatingButton variant="destructive" pulseColor="#ef4444" duration={2} className="rounded-full">
<span className="size-2 rounded-full bg-white" aria-hidden />
Live
</PulsatingButton>
<PulsatingButton variant="secondary" pulseColor="#7dd3fc" spread={12} duration={3}>
Wider, slower
</PulsatingButton>
<PulsatingButton size="icon-lg" pulseColor="#c4b5fd" className="rounded-full" aria-label="Start recording">
<Mic />
</PulsatingButton>
</>
);
}Loading
loading comes from the base Button: the label crossfades to a spinner without changing the width, and the rings fade out with it.
"use client";
import * as React from "react";
import { PulsatingButton } from "@/components/dusk/pulsating-button/pulsating-button";
export default function PulsatingButtonLoadingDemo() {
const [loading, setLoading] = React.useState(false);
const timer = React.useRef<ReturnType<typeof setTimeout>>(undefined);
React.useEffect(() => () => clearTimeout(timer.current), []);
const claim = () => {
setLoading(true);
clearTimeout(timer.current);
timer.current = setTimeout(() => setLoading(false), 2000);
};
return (
<PulsatingButton size="lg" loading={loading} loadingText="Claiming…" onClick={claim}>
Claim your spot
</PulsatingButton>
);
}API Reference
Also accepts every DuskUI Button prop (variant, size, loading, loadingText, loaderIcon) except asChild.
| Prop | Type | Default | Description |
|---|---|---|---|
| pulseColor | string | var(--color-primary) | Any CSS color for the rings. |
| duration | number | 2.4 | Seconds for one ring to expand and fade. A second ring follows half a beat later. |
| spread | number | 8 | How far the rings travel outwards, in px. |
| className | string | — | Merged with tailwind-merge, so overrides win. |
Accessibility
- Renders a native <button>, so Enter, Space and focus order work as usual, with the standard focus-visible ring.
- The pulse fades out on hover and on focus, so it never competes with the focus ring or distracts while the user is about to act.
- Rings are an outer box-shadow on an aria-hidden layer, so they take up no space and never reach screen readers.
- With prefers-reduced-motion the rings are replaced by a steady thin ring; the animation also stops whenever the button is off-screen.
- Pulses are slow fades (under one per second by default), well clear of photosensitivity limits.