Spinner
An accessible React loading spinner with role="status", a screen-reader label and four sizes — plus five animated loaders built with Motion: bouncing dots, equalizer bars, a gradient ring, an orbit and a radar pulse, all respecting reduced motion.
- role="status"
- Server Component friendly
- currentColor
- prefers-reduced-motion
- Dots loaderDots
- Bars loaderBars
- Ring loaderRing
- Orbit loaderOrbit
- Pulse loaderPulse
"use client";
import {
BarsLoader,
DotsLoader,
OrbitLoader,
PulseLoader,
RingLoader,
} from "@/components/dusk/spinner/loaders";
const loaders = [
{ name: "Dots", Loader: DotsLoader },
{ name: "Bars", Loader: BarsLoader },
{ name: "Ring", Loader: RingLoader },
{ name: "Orbit", Loader: OrbitLoader },
{ name: "Pulse", Loader: PulseLoader },
];
export default function LoadersDemo() {
return (
<ul className="grid w-full max-w-xl grid-cols-2 gap-3 sm:grid-cols-5">
{loaders.map(({ name, Loader }) => (
<li
key={name}
className="flex flex-col items-center gap-4 rounded-xl border bg-card px-4 py-6 last:col-span-2 sm:last:col-span-1"
>
<Loader size={36} label={`${name} loader`} />
<span className="text-xs text-muted-foreground">{name}</span>
</li>
))}
</ul>
);
}Installation
$ npx shadcn@latest add https://duskui.com/r/spinner.json1. Install the dependencies.
npm install class-variance-authority2. Copy the source into
components/ui/spinner.tsx.spinner.tsx import * as React from "react"; import { cva, type VariantProps } from "class-variance-authority"; import { cn } from "@/lib/utils"; const spinnerVariants = cva("relative inline-flex shrink-0 items-center justify-center", { variants: { size: { sm: "size-3.5", default: "size-4", lg: "size-6", xl: "size-10", }, }, defaultVariants: { size: "default" }, }); export interface SpinnerProps extends Omit<React.ComponentProps<"span">, "children">, VariantProps<typeof spinnerVariants> { /** Text announced to screen readers. */ label?: string; } /** * DuskUI Spinner — an accessible loading indicator. * * Like shadcn/ui's Spinner, plus: * - `role="status"` with a screen-reader-only `label` ("Loading" by default), * so the busy state is announced instead of being a silent icon. * - A faint track with a rounded arc. * - `size`: sm, default, lg, xl. Colored with `currentColor` (use `text-*`). * - Under `prefers-reduced-motion` it turns slowly instead of spinning. * CSS only, so it works in Server Components. */ function Spinner({ className, size, label = "Loading", ...props }: SpinnerProps) { return ( <span role="status" data-slot="spinner" className={cn(spinnerVariants({ size }), className)} {...props} > <svg viewBox="0 0 24 24" fill="none" aria-hidden className="size-full animate-spin [animation-duration:0.8s] motion-reduce:[animation-duration:2.4s]" > <circle cx="12" cy="12" r="9.5" stroke="currentColor" strokeWidth="2.5" className="opacity-20" /> <path d="M12 2.5a9.5 9.5 0 0 1 9.5 9.5" stroke="currentColor" strokeWidth="2.5" strokeLinecap="round" /> </svg> <span className="sr-only">{label}</span> </span> ); } export { Spinner, spinnerVariants };
Adds components/ui/spinner.tsx. CSS only, colored with currentColor, so it inherits the text color of buttons and badges.
Usage
import { Spinner } from "@/components/ui/spinner"
export function Loading() {
return <Spinner size="lg" className="text-primary" label="Loading projects" />
}Examples
Sizes & colors
size is sm, default, lg or xl, and text-* classes recolor it.
import { Spinner } from "@/components/ui/spinner";
export default function SpinnerBasicDemo() {
return (
<div className="flex items-end gap-6">
<Spinner size="sm" />
<Spinner />
<Spinner size="lg" className="text-primary" />
<Spinner size="xl" className="text-primary" label="Loading dashboard" />
<Spinner size="lg" className="text-sky-300" />
</div>
);
}In buttons, badges & rows
When the surrounding text already says what's happening ("Saving…"), add aria-hidden to the spinner so it isn't announced twice. Keep busy buttons focusable with aria-disabled and aria-busy.
import { Badge } from "@/components/ui/badge";
import { Button } from "@/components/ui/button";
import { Spinner } from "@/components/ui/spinner";
export default function SpinnerUsageDemo() {
return (
<div className="flex flex-col items-center gap-5">
<div className="flex flex-wrap items-center justify-center gap-3">
{/* The button text already says what's happening, so the spinner is hidden from screen readers. */}
<Button aria-disabled aria-busy>
<Spinner aria-hidden />
Saving…
</Button>
<Button variant="outline" aria-disabled aria-busy>
<Spinner aria-hidden className="text-primary" />
Syncing
</Button>
<Badge variant="soft" size="lg">
<Spinner size="sm" aria-hidden />
Deploying
</Badge>
</div>
<div className="flex w-full max-w-xs items-center gap-3 rounded-lg border bg-card/40 px-3 py-2.5 text-sm">
<Spinner className="text-primary" label="Fetching messages" />
<span className="text-muted-foreground">Fetching new messages…</span>
</div>
</div>
);
}Blocks
Ready-made variations composed from the base component with Motion. Each installs on its own and pulls in its dependencies automatically.
Animated Loaders
Five loaders from one file: DotsLoader, BarsLoader, RingLoader, OrbitLoader and PulseLoader. Each is a role="status" box with a hidden label, drawn in currentColor and sized in pixels. Under reduced motion they stop moving and fade in turn instead.
- Dots loaderDots
- Bars loaderBars
- Ring loaderRing
- Orbit loaderOrbit
- Pulse loaderPulse
"use client";
import {
BarsLoader,
DotsLoader,
OrbitLoader,
PulseLoader,
RingLoader,
} from "@/components/dusk/spinner/loaders";
const loaders = [
{ name: "Dots", Loader: DotsLoader },
{ name: "Bars", Loader: BarsLoader },
{ name: "Ring", Loader: RingLoader },
{ name: "Orbit", Loader: OrbitLoader },
{ name: "Pulse", Loader: PulseLoader },
];
export default function LoadersDemo() {
return (
<ul className="grid w-full max-w-xl grid-cols-2 gap-3 sm:grid-cols-5">
{loaders.map(({ name, Loader }) => (
<li
key={name}
className="flex flex-col items-center gap-4 rounded-xl border bg-card px-4 py-6 last:col-span-2 sm:last:col-span-1"
>
<Loader size={36} label={`${name} loader`} />
<span className="text-xs text-muted-foreground">{name}</span>
</li>
))}
</ul>
);
}$ npx shadcn@latest add https://duskui.com/r/loaders.json1. Install the dependencies.
npm install motion2. Copy the source into
components/dusk/spinner/loaders.tsx.loaders.tsx "use client"; import * as React from "react"; import { motion, useReducedMotion, type Transition } from "motion/react"; import { cn } from "@/lib/utils"; export interface LoaderProps extends Omit<React.ComponentProps<"span">, "children"> { /** Width and height in pixels. */ size?: number; /** Text announced to screen readers. */ label?: string; /** Seconds per loop. Lower is faster. */ speed?: number; } // Every animated element sets an explicit `initial` that doesn't depend on // reduced motion, so the server HTML and the first client render always match. /** * Shared wrapper: an accessible `role="status"` box with a screen-reader-only * label and the loader drawn in `currentColor` (color it with `text-*`). */ function LoaderRoot({ size, label = "Loading", className, style, children, slot, ...props }: Omit<LoaderProps, "speed"> & { slot: string; children: React.ReactNode }) { return ( <span role="status" data-slot={slot} className={cn("relative inline-flex shrink-0 items-center justify-center", className)} style={{ width: size, height: size, ...style }} {...props} > <span aria-hidden className="relative block size-full"> {children} </span> <span className="sr-only">{label}</span> </span> ); } /** Reduced-motion fallback for every loader: elements fade in turn instead of moving. */ function fade(index: number, count: number, speed: number): Transition { return { duration: speed, repeat: Infinity, ease: "easeInOut", delay: (index / count) * speed }; } /** Three dots that hop one after another, like a typing indicator. */ export function DotsLoader({ size = 32, speed = 1.1, ...props }: LoaderProps) { const reduceMotion = useReducedMotion() ?? false; return ( <LoaderRoot slot="dots-loader" size={size} {...props}> <span className="absolute inset-0 flex items-center justify-between px-[6%]"> {[0, 1, 2].map((i) => ( <motion.span key={i} className="block aspect-square w-[22%] rounded-full bg-current" initial={{ y: "0%", opacity: 0.5 }} animate={reduceMotion ? { opacity: [0.3, 1, 0.3] } : { y: ["0%", "-50%", "0%"], opacity: [0.5, 1, 0.5] }} transition={ reduceMotion ? fade(i, 3, speed) : { duration: speed * 0.6, repeat: Infinity, repeatDelay: speed * 0.4, ease: "easeInOut", delay: i * speed * 0.15 } } /> ))} </span> </LoaderRoot> ); } /** Equalizer bars that rise and fall in a wave. */ export function BarsLoader({ size = 32, speed = 1, bars = 5, ...props }: LoaderProps & { bars?: number }) { const reduceMotion = useReducedMotion() ?? false; return ( <LoaderRoot slot="bars-loader" size={size} {...props}> <span className="absolute inset-0 flex items-center justify-between gap-[8%] py-[10%]"> {Array.from({ length: bars }, (_, i) => ( <motion.span key={i} className="block h-full flex-1 origin-center rounded-full bg-current" initial={{ scaleY: 0.6, opacity: 1 }} animate={reduceMotion ? { opacity: [0.3, 1, 0.3] } : { scaleY: [0.35, 1, 0.35] }} transition={ reduceMotion ? fade(i, bars, speed) : { duration: speed, repeat: Infinity, ease: "easeInOut", delay: (i / bars) * speed * 0.6 } } /> ))} </span> </LoaderRoot> ); } /** A ring whose arc fades out along a conic gradient, with a rounded head. */ export function RingLoader({ size = 32, speed = 0.9, thickness = 3, ...props }: LoaderProps & { thickness?: number }) { const reduceMotion = useReducedMotion() ?? false; const mask = `radial-gradient(farthest-side, transparent calc(100% - ${thickness}px), #000 calc(100% - ${thickness}px + 0.5px))`; return ( <LoaderRoot slot="ring-loader" size={size} {...props}> {/* Track */} <span className="absolute inset-0 rounded-full bg-current opacity-15" style={{ mask, WebkitMask: mask }} /> <motion.span className="absolute inset-0 rounded-full" initial={{ rotate: 0, opacity: 1 }} animate={reduceMotion ? { opacity: [0.4, 1, 0.4] } : { rotate: 360 }} transition={ reduceMotion ? { duration: speed * 2, repeat: Infinity, ease: "easeInOut" } : { duration: speed, repeat: Infinity, ease: "linear" } } > <span className="absolute inset-0 rounded-full bg-[conic-gradient(from_0deg,transparent_0deg,currentColor_300deg,transparent_301deg)]" style={{ mask, WebkitMask: mask }} /> {/* Rounded head at the end of the arc (300deg), half the ring thickness from the edge. */} <span className="absolute top-1/2 left-1/2 rounded-full bg-current" style={{ width: thickness, height: thickness, transform: `translate(-50%, -50%) rotate(300deg) translateY(calc(-${size / 2}px + ${thickness / 2}px))`, }} /> </motion.span> </LoaderRoot> ); } /** Two dots orbiting a core at different distances and speeds. */ export function OrbitLoader({ size = 32, speed = 1.4, ...props }: LoaderProps) { const reduceMotion = useReducedMotion() ?? false; const orbits = [ { inset: "0%", duration: speed, dot: "18%" }, { inset: "22%", duration: speed * 0.6, dot: "20%", reverse: true }, ]; return ( <LoaderRoot slot="orbit-loader" size={size} {...props}> <motion.span className="absolute top-1/2 left-1/2 block size-[22%] -translate-x-1/2 -translate-y-1/2 rounded-full bg-current" initial={{ opacity: 0.6 }} animate={{ opacity: [0.6, 1, 0.6] }} transition={{ duration: speed, repeat: Infinity, ease: "easeInOut" }} /> {orbits.map((orbit, i) => ( <motion.span key={i} className="absolute rounded-full border border-current/15" style={{ inset: orbit.inset }} initial={{ rotate: 0, opacity: 1 }} animate={reduceMotion ? { opacity: [0.35, 1, 0.35] } : { rotate: orbit.reverse ? -360 : 360 }} transition={ reduceMotion ? fade(i, orbits.length, speed) : { duration: orbit.duration, repeat: Infinity, ease: "linear" } } > <span className="absolute top-0 left-1/2 block aspect-square -translate-x-1/2 -translate-y-1/2 rounded-full bg-current" style={{ width: orbit.dot }} /> </motion.span> ))} </LoaderRoot> ); } /** A core with rings that ripple outward and fade, like a radar ping. */ export function PulseLoader({ size = 32, speed = 1.6, rings = 2, ...props }: LoaderProps & { rings?: number }) { const reduceMotion = useReducedMotion() ?? false; return ( <LoaderRoot slot="pulse-loader" size={size} {...props}> {Array.from({ length: rings }, (_, i) => ( <motion.span key={i} className="absolute inset-0 rounded-full border-2 border-current" initial={{ scale: 0.3, opacity: 0 }} animate={reduceMotion ? { scale: 0.8, opacity: [0, 0.5, 0] } : { scale: [0.3, 1], opacity: [0.9, 0] }} transition={{ duration: speed, repeat: Infinity, ease: "easeOut", delay: (i / rings) * speed, ...(reduceMotion && { scale: { duration: 0 } }), }} /> ))} <motion.span className="absolute inset-[34%] rounded-full bg-current" initial={{ scale: 0.85, opacity: 0.6 }} animate={reduceMotion ? { opacity: [0.6, 1, 0.6] } : { scale: [0.85, 1, 0.85] }} transition={{ duration: speed / 2, repeat: Infinity, ease: "easeInOut" }} /> </LoaderRoot> ); }
| Prop | Type | Default | Description |
|---|---|---|---|
| size | number | 32 | Width and height in pixels. |
| label | string | "Loading" | Text announced to screen readers. |
| speed | number | — | Seconds per loop; lower is faster. Each loader has its own default. |
| bars | number | 5 | BarsLoader only. Number of bars. |
| thickness | number | 3 | RingLoader only. Ring thickness in pixels. |
| rings | number | 2 | PulseLoader only. Number of ripples. |
Loaders in context
Dots for a typing indicator, a pulse for a live connection, a thin ring inside a button. Recolor any loader with text-*.
"use client";
import { Button } from "@/components/ui/button";
import { DotsLoader, PulseLoader, RingLoader } from "@/components/dusk/spinner/loaders";
export default function LoadersInlineDemo() {
return (
<div className="flex w-full max-w-sm flex-col gap-3">
<div className="flex items-center gap-3 rounded-xl border bg-card/40 px-4 py-3">
<span className="grid size-8 place-items-center rounded-full bg-muted text-xs font-medium text-muted-foreground">AI</span>
<DotsLoader size={28} className="text-muted-foreground" label="Assistant is typing" />
</div>
<div className="flex items-center justify-between rounded-xl border bg-card/40 px-4 py-3 text-sm">
<span className="flex items-center gap-3">
<PulseLoader size={20} className="text-primary" label="Live" />
Listening for events
</span>
<span className="text-xs text-muted-foreground">live</span>
</div>
<Button variant="outline" aria-disabled aria-busy className="self-start">
<RingLoader size={16} thickness={2} aria-hidden />
Processing payment
</Button>
</div>
);
}$ npx shadcn@latest add https://duskui.com/r/loaders.json1. Install the dependencies.
npm install motion2. Copy the source into
components/dusk/spinner/loaders.tsx.loaders.tsx "use client"; import * as React from "react"; import { motion, useReducedMotion, type Transition } from "motion/react"; import { cn } from "@/lib/utils"; export interface LoaderProps extends Omit<React.ComponentProps<"span">, "children"> { /** Width and height in pixels. */ size?: number; /** Text announced to screen readers. */ label?: string; /** Seconds per loop. Lower is faster. */ speed?: number; } // Every animated element sets an explicit `initial` that doesn't depend on // reduced motion, so the server HTML and the first client render always match. /** * Shared wrapper: an accessible `role="status"` box with a screen-reader-only * label and the loader drawn in `currentColor` (color it with `text-*`). */ function LoaderRoot({ size, label = "Loading", className, style, children, slot, ...props }: Omit<LoaderProps, "speed"> & { slot: string; children: React.ReactNode }) { return ( <span role="status" data-slot={slot} className={cn("relative inline-flex shrink-0 items-center justify-center", className)} style={{ width: size, height: size, ...style }} {...props} > <span aria-hidden className="relative block size-full"> {children} </span> <span className="sr-only">{label}</span> </span> ); } /** Reduced-motion fallback for every loader: elements fade in turn instead of moving. */ function fade(index: number, count: number, speed: number): Transition { return { duration: speed, repeat: Infinity, ease: "easeInOut", delay: (index / count) * speed }; } /** Three dots that hop one after another, like a typing indicator. */ export function DotsLoader({ size = 32, speed = 1.1, ...props }: LoaderProps) { const reduceMotion = useReducedMotion() ?? false; return ( <LoaderRoot slot="dots-loader" size={size} {...props}> <span className="absolute inset-0 flex items-center justify-between px-[6%]"> {[0, 1, 2].map((i) => ( <motion.span key={i} className="block aspect-square w-[22%] rounded-full bg-current" initial={{ y: "0%", opacity: 0.5 }} animate={reduceMotion ? { opacity: [0.3, 1, 0.3] } : { y: ["0%", "-50%", "0%"], opacity: [0.5, 1, 0.5] }} transition={ reduceMotion ? fade(i, 3, speed) : { duration: speed * 0.6, repeat: Infinity, repeatDelay: speed * 0.4, ease: "easeInOut", delay: i * speed * 0.15 } } /> ))} </span> </LoaderRoot> ); } /** Equalizer bars that rise and fall in a wave. */ export function BarsLoader({ size = 32, speed = 1, bars = 5, ...props }: LoaderProps & { bars?: number }) { const reduceMotion = useReducedMotion() ?? false; return ( <LoaderRoot slot="bars-loader" size={size} {...props}> <span className="absolute inset-0 flex items-center justify-between gap-[8%] py-[10%]"> {Array.from({ length: bars }, (_, i) => ( <motion.span key={i} className="block h-full flex-1 origin-center rounded-full bg-current" initial={{ scaleY: 0.6, opacity: 1 }} animate={reduceMotion ? { opacity: [0.3, 1, 0.3] } : { scaleY: [0.35, 1, 0.35] }} transition={ reduceMotion ? fade(i, bars, speed) : { duration: speed, repeat: Infinity, ease: "easeInOut", delay: (i / bars) * speed * 0.6 } } /> ))} </span> </LoaderRoot> ); } /** A ring whose arc fades out along a conic gradient, with a rounded head. */ export function RingLoader({ size = 32, speed = 0.9, thickness = 3, ...props }: LoaderProps & { thickness?: number }) { const reduceMotion = useReducedMotion() ?? false; const mask = `radial-gradient(farthest-side, transparent calc(100% - ${thickness}px), #000 calc(100% - ${thickness}px + 0.5px))`; return ( <LoaderRoot slot="ring-loader" size={size} {...props}> {/* Track */} <span className="absolute inset-0 rounded-full bg-current opacity-15" style={{ mask, WebkitMask: mask }} /> <motion.span className="absolute inset-0 rounded-full" initial={{ rotate: 0, opacity: 1 }} animate={reduceMotion ? { opacity: [0.4, 1, 0.4] } : { rotate: 360 }} transition={ reduceMotion ? { duration: speed * 2, repeat: Infinity, ease: "easeInOut" } : { duration: speed, repeat: Infinity, ease: "linear" } } > <span className="absolute inset-0 rounded-full bg-[conic-gradient(from_0deg,transparent_0deg,currentColor_300deg,transparent_301deg)]" style={{ mask, WebkitMask: mask }} /> {/* Rounded head at the end of the arc (300deg), half the ring thickness from the edge. */} <span className="absolute top-1/2 left-1/2 rounded-full bg-current" style={{ width: thickness, height: thickness, transform: `translate(-50%, -50%) rotate(300deg) translateY(calc(-${size / 2}px + ${thickness / 2}px))`, }} /> </motion.span> </LoaderRoot> ); } /** Two dots orbiting a core at different distances and speeds. */ export function OrbitLoader({ size = 32, speed = 1.4, ...props }: LoaderProps) { const reduceMotion = useReducedMotion() ?? false; const orbits = [ { inset: "0%", duration: speed, dot: "18%" }, { inset: "22%", duration: speed * 0.6, dot: "20%", reverse: true }, ]; return ( <LoaderRoot slot="orbit-loader" size={size} {...props}> <motion.span className="absolute top-1/2 left-1/2 block size-[22%] -translate-x-1/2 -translate-y-1/2 rounded-full bg-current" initial={{ opacity: 0.6 }} animate={{ opacity: [0.6, 1, 0.6] }} transition={{ duration: speed, repeat: Infinity, ease: "easeInOut" }} /> {orbits.map((orbit, i) => ( <motion.span key={i} className="absolute rounded-full border border-current/15" style={{ inset: orbit.inset }} initial={{ rotate: 0, opacity: 1 }} animate={reduceMotion ? { opacity: [0.35, 1, 0.35] } : { rotate: orbit.reverse ? -360 : 360 }} transition={ reduceMotion ? fade(i, orbits.length, speed) : { duration: orbit.duration, repeat: Infinity, ease: "linear" } } > <span className="absolute top-0 left-1/2 block aspect-square -translate-x-1/2 -translate-y-1/2 rounded-full bg-current" style={{ width: orbit.dot }} /> </motion.span> ))} </LoaderRoot> ); } /** A core with rings that ripple outward and fade, like a radar ping. */ export function PulseLoader({ size = 32, speed = 1.6, rings = 2, ...props }: LoaderProps & { rings?: number }) { const reduceMotion = useReducedMotion() ?? false; return ( <LoaderRoot slot="pulse-loader" size={size} {...props}> {Array.from({ length: rings }, (_, i) => ( <motion.span key={i} className="absolute inset-0 rounded-full border-2 border-current" initial={{ scale: 0.3, opacity: 0 }} animate={reduceMotion ? { scale: 0.8, opacity: [0, 0.5, 0] } : { scale: [0.3, 1], opacity: [0.9, 0] }} transition={{ duration: speed, repeat: Infinity, ease: "easeOut", delay: (i / rings) * speed, ...(reduceMotion && { scale: { duration: 0 } }), }} /> ))} <motion.span className="absolute inset-[34%] rounded-full bg-current" initial={{ scale: 0.85, opacity: 0.6 }} animate={reduceMotion ? { opacity: [0.6, 1, 0.6] } : { scale: [0.85, 1, 0.85] }} transition={{ duration: speed / 2, repeat: Infinity, ease: "easeInOut" }} /> </LoaderRoot> ); }
API Reference
Spinner renders a <span role="status"> and forwards span props.
| Prop | Type | Default | Description |
|---|---|---|---|
| size | "sm" | "default" | "lg" | "xl" | "default" | Spinner size. |
| label | string | "Loading" | Screen-reader text inside the status region. |
Accessibility
- Every spinner and loader is a
role="status"live region with visually hidden text, so the loading state is announced instead of being a silent icon. - Pass a specific
label("Loading invoices") when several things can load at once. - Mark the busy container with
aria-busy="true"while content loads, and remove the spinner when it's done. - Under prefers-reduced-motion the Spinner turns slowly and the loaders fade instead of moving, so progress is still visible without motion.