Menu

Components

Loader

A moving mark that says something is loading, in five looks and five sizes, in the color of the text around it.

Introduction

Loader tells people that something is on its way: a page fetching its data, a button saving a form, a list refreshing. It moves, so it is noticed, and it is small enough to sit beside text or inside a button.

  • Five looks: ring, spinner, dots, bars and pulse. Five sizes, from 12 to 48 pixels.
  • It draws in the color of the text around it, so it follows the theme, and a text-* class changes it.
  • It is a status for screen readers and says "Loading", or whatever you set with label. When the text next to it already says it, turn the label off with label={false} so it is not read twice.
  • For people who prefer reduced motion it does not spin or bounce. It breathes in and out, which still shows that something is going on.
  • It has no client code, so it works in Server Components, for example in a loading.tsx file.

For content that has a shape to show before it arrives, a skeleton is better than a loader. Use a loader when there is nothing to draw yet, or when the wait is for an action.

Installation

centerUI has no CLI or registry yet: components are copied into your project.

# bun
bun add tailwind-variants tailwind-merge clsx

# pnpm
pnpm add tailwind-variants tailwind-merge clsx

# npm
npm install tailwind-variants tailwind-merge clsx

Manual installation

Copy these files into your project and keep the paths, or adjust the imports.

1. Add the styles

src/types/loader-styles.tsShow code
import { tv } from "tailwind-variants";

// Styles for Loader. It draws in `currentColor`, so its color is the text
// color around it: set it with a `text-*` class. The animations are named in
// globals.css (`animate-loader-*`), and turn into a gentle fade for people who
// prefer reduced motion.
export const loaderStyles = tv({
  slots: {
    root: "inline-flex shrink-0 items-center justify-center text-current",
    // The parts that are not an svg: a row of dots or bars.
    row: "flex size-full items-center justify-between",
  },
  variants: {
    size: {
      xs: { root: "size-3" },
      sm: { root: "size-4" },
      md: { root: "size-6" },
      lg: { root: "size-8" },
      xl: { root: "size-12" },
    },
  },
  defaultVariants: { size: "md" },
});

2. Add the Loader file

src/components/ui/loader/loader.tsxShow code
import { cn } from "@/lib/cn";
import { loaderStyles } from "@/types/loader-styles";

export interface LoaderProps {
  /**
   * The look:
   * - `ring`: a circle with a gap that turns.
   * - `spinner`: twelve spokes that fade in turn.
   * - `dots`: three dots that swell one after the other.
   * - `bars`: four bars that rise and fall.
   * - `pulse`: circles that grow out and fade.
   */
  variant?: "ring" | "spinner" | "dots" | "bars" | "pulse";
  size?: "xs" | "sm" | "md" | "lg" | "xl";
  /**
   * What a screen reader hears. Defaults to "Loading". Pass `false` when the
   * text next to the loader already says it, for example a button that reads
   * "Saving…", and the loader is only decoration.
   */
  label?: string | false;
  className?: string;
}

const spokes = Array.from({ length: 12 }, (_, i) => i);
const bars = [0, 1, 2, 3];
const dots = [0, 1, 2];

/**
 * A moving mark that says something is loading. It takes the color of the text
 * around it and comes in five looks and five sizes. It announces itself to
 * screen readers as a status, and stops spinning, and only breathes, for
 * people who prefer reduced motion.
 *
 * @example
 * <Loader />
 * <Loader variant="dots" size="sm" className="text-muted-fg" />
 * <Button isPending><Loader size="sm" label={false} /> Saving…</Button>
 */
export function Loader({
  variant = "ring",
  size,
  label = "Loading",
  className,
}: LoaderProps) {
  const styles = loaderStyles({ size });

  return (
    <span
      data-loader=""
      role={label === false ? undefined : "status"}
      aria-hidden={label === false ? true : undefined}
      className={cn(styles.root(), className)}
    >
      {variant === "ring" ? (
        <svg
          viewBox="0 0 24 24"
          fill="none"
          aria-hidden="true"
          className="size-full animate-loader-spin"
        >
          <circle
            cx="12"
            cy="12"
            r="9"
            stroke="currentColor"
            strokeWidth="3"
            opacity="0.2"
          />
          <path
            d="M21 12a9 9 0 0 0-9-9"
            stroke="currentColor"
            strokeWidth="3"
            strokeLinecap="round"
          />
        </svg>
      ) : null}

      {variant === "spinner" ? (
        <svg
          viewBox="0 0 24 24"
          fill="none"
          aria-hidden="true"
          className="size-full"
        >
          {spokes.map((i) => (
            <line
              key={i}
              x1="12"
              y1="2.5"
              x2="12"
              y2="6.5"
              stroke="currentColor"
              strokeWidth="2.4"
              strokeLinecap="round"
              transform={`rotate(${i * 30} 12 12)`}
              className="animate-loader-fade"
              style={{ animationDelay: `${(i - 12) * 0.1}s` }}
            />
          ))}
        </svg>
      ) : null}

      {variant === "dots" ? (
        <span aria-hidden="true" className={styles.row()}>
          {dots.map((i) => (
            <span
              key={i}
              className="aspect-square w-[26%] animate-loader-dot rounded-full bg-current"
              style={{ animationDelay: `${i * 0.16}s` }}
            />
          ))}
        </span>
      ) : null}

      {variant === "bars" ? (
        <span aria-hidden="true" className={styles.row()}>
          {bars.map((i) => (
            <span
              key={i}
              className="h-full w-[16%] animate-loader-bar rounded-full bg-current"
              style={{ animationDelay: `${i * 0.12}s` }}
            />
          ))}
        </span>
      ) : null}

      {variant === "pulse" ? (
        <span aria-hidden="true" className="relative size-full">
          {[0, 0.7].map((delay) => (
            <span
              key={delay}
              className="absolute inset-0 animate-loader-pulse rounded-full bg-current"
              style={{ animationDelay: `${delay}s` }}
            />
          ))}
        </span>
      ) : null}

      {label === false ? null : <span className="sr-only">{label}</span>}
    </span>
  );
}

3. Add the animations

In src/app/globals.css copy the five --animate-loader-* lines of the theme, the five loader-* keyframes and loader-soft, and the [data-loader] rule inside the reduced-motion query. Without them the loader is still, so it does not do its job.

4. Use it

import { Loader } from "@/components/ui/loader/loader";

Anatomy

A box that announces itself, and the moving mark inside it.

<Loader variant="ring" size="md" label="Loading your files" />
PartRendered asNotes
Loaderspan with role statusA box the size of the loader. It draws in the color of the text around it, so a text-* class colors it.
Marksvg or spans, hidden from screen readersThe moving part, one of five looks.
Labelspan, screen readers onlyWhat a screen reader hears, "Loading" by default. It is left out with label={false}.

Examples

Usage

Loading

Variants

Five looks. Ring is the default and the most familiar. The others suit different places: dots and bars are calm, pulse is soft, and spinner is the crisp one.

Loading, ringring
Loading, spinnerspinner
Loading, dotsdots
Loading, barsbars
Loading, pulsepulse

Sizes

From 12 to 48 pixels: xs, sm, md, lg and xl.

Loading, ring xsLoading, ring smLoading, ring mdLoading, ring lgLoading, ring xl
Loading, dots xsLoading, dots smLoading, dots mdLoading, dots lgLoading, dots xl

Color

A loader draws in the color of its text, so it is colored like text: with a text-* class on it or on its parent.

Loading, primaryLoading, successLoading, warningLoading, dangerLoading, muted

Label

What a screen reader hears. Say what is loading. When the text next to the loader already says it, pass label={false}: the loader then has no name and is hidden from screen readers.

Loading your files Saving…

In a button

A button that is waiting for a request. The loader takes the color of the button's text.

Next to text

A small loader beside a line of text, for a check that runs in the background.

Checking your address…

Centered

Alone in the middle of the place where the content will be, with a line that says what is loading.

Loading your files…

Over content

Over content that is being refreshed. Mark the content aria-busy, and dim it so the loader stands out.

Order #1041
Order #1042
Order #1043
Loading

Component API

Loader takes the props below.

PropTypeDefault
variant"ring" | "spinner" | "dots" | "bars" | "pulse""ring"
size"xs" | "sm" | "md" | "lg" | "xl""md"
labelstring | false"Loading"
classNamestring-