Spinner
A turning ring for a wait whose length is not known: a query, a save, a first load. Where the shape of the content is known, a skeleton holds its place better. Where the progress is known, use the progress bar.
Install
npx shadcn@latest add https://thrumui.dev/r/spinner.jsonAdds components/ui/spinner.tsx. Nothing else in your project changes.
import { Spinner } from "@/components/ui/spinner";Props
| Prop | Type | Default | Description |
|---|---|---|---|
size | "sm" | "md" | "lg" | Default "md" | sm is 16 pixels, md 20, lg 32. A size class in className, such as size-12, sets any other size. |
label | string | Default "Loading" | What is being waited for. It is visually hidden text: screen readers announce it when the spinner appears. |
paused | boolean | Default false | Freeze the ring where it is. |
className | string | – | Extra classes, merged last. A text colour class recolours the ring. |
...props | ComponentProps<"span"> | – | Every other span attribute is passed through. The spinner takes no children. |
Motion and sound
Motion
- The ring turns once a second at constant speed. It is a CSS animation and needs no script.
- paused freezes it where it is.
- Reduced motion: the ring does not turn. It fades between full and half strength every 2 seconds, so it still shows that something is going on.
Sound
Silent. This component makes no sound.
Accessibility
- The spinner has the status role. A screen reader announces its label when it is added to the page, and not again while it turns.
- The ring is hidden from screen readers. When the same words are also printed beside it, hide the printed copy with aria-hidden so they are read once.
- The arc is drawn in the text colour around it, over a track at a quarter of that strength: the arc keeps 3 to 1 wherever the text is readable.
- It takes no focus. Mark the region that is loading with aria-busy.
Source
"use client";
import type { ComponentProps } from "react";
import { cn } from "@/lib/utils";
export type SpinnerSize = "sm" | "md" | "lg";
export type SpinnerProps = Omit<ComponentProps<"span">, "children"> & {
/** `sm` is 16 pixels, `md` 20, `lg` 32. A `size-*` class in `className` sets any other size. */
size?: SpinnerSize;
/** What is being waited for. Screen readers announce it when the spinner appears; it is not drawn. */
label?: string;
/** Freeze the ring where it is. */
paused?: boolean;
};
const SIZE: Record<SpinnerSize, string> = { sm: "size-4", md: "size-5", lg: "size-8" };
/**
* A turning ring for a wait of unknown length. It takes the colour of the text around it.
* It is a status region: a screen reader announces `label` when the spinner is added to the page.
*/
export function Spinner({ size = "md", label = "Loading", paused = false, className, ...props }: SpinnerProps) {
return (
<span data-slot="spinner" role="status" className={cn("inline-flex shrink-0 align-middle", SIZE[size], className)} {...props}>
<svg
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
strokeWidth={2.5}
strokeLinecap="round"
aria-hidden="true"
// Under reduced motion the ring stops turning and fades between full and half strength instead.
// Important, because the reduced-motion animation is declared later and would start it again.
className={cn("size-full animate-spin motion-reduce:animate-pulse", paused && "[animation-play-state:paused]!")}
>
<circle cx={12} cy={12} r={9} opacity={0.25} />
<path d="M12 3a9 9 0 0 1 9 9" />
</svg>
<span className="sr-only">{label}</span>
</span>
);
}Files and dependencies
- Adds
components/ui/spinner.tsx- Packages
- None
- Brings along
- Nothing
Changelog
- 1.0.0 · 2026-12-03First release.