# Spinner

> Show that work is happening when its length and shape are unknown.

Source: https://www.honestui.com/docs/components/spinner

```tsx
import { Spinner } from "@/components/honest-ui/ui/spinner"

export function SpinnerDemo() {
  return (
    <div className="flex items-center gap-[var(--hui-space-3)] text-[var(--hui-color-foreground-base-secondary)] [font-size:var(--hui-font-size-control,13px)]">
      <Spinner label="Loading recent activity" />
      <span aria-hidden="true">Loading recent activity…</span>
    </div>
  )
}

```

## Overview [#overview]

Use Spinner for short waits when there is nothing better to show: a button saving a form, a panel waiting for a first response, a list loading more rows. A spinner shows only that something is happening. It gives no idea how long the wait will be or what the result will look like, so pick it after the other options.

Choose by what you know:

* If you know the layout of the content that is coming, use [Skeleton](/docs/components/skeleton). The page keeps its shape, and people can see what will appear.
* If you know how far along the work is, use [Progress](/docs/components/progress). A bar that moves tells people how long to wait. A spinner can't.
* If you know neither, and the wait is a few seconds at most, use Spinner.

For waits longer than a few seconds, add text that says what is happening, such as "Uploading 3 of 12 files", next to the spinner or instead of it.

## Anatomy [#anatomy]

A spinner is a status element that holds a rotating arc and a visually hidden label. The arc is the same loader icon used by [Button](/docs/components/button) loading states and the toast loading state, so loading looks the same everywhere. It uses `currentColor`, so it takes the color of the text around it, and its size follows the `size` prop: `sm`, `default`, `lg`, or a number of pixels.

## Accessibility [#accessibility]

By default the spinner has `role="status"` and a visually hidden label, "Loading". Screen readers announce the label politely when the spinner appears. Replace the label with what is loading, such as "Loading invoices", so people who can't see the screen know what they're waiting for.

When visible text next to the spinner already says what is happening, such as a button that reads "Saving changes…", set `decorative`. The spinner is then hidden from assistive technology, so the status isn't announced twice. Put `aria-busy` on the region or button that is updating.

The arc spins under `prefers-reduced-motion` too, but much more slowly. A frozen arc looks like a broken icon, while a slow turn still shows that work is happening without fast motion. In forced-colors mode the arc uses the system text color.

## Installation [#installation]


  

  
    <CliBlock commands="[&#x22;spinner&#x22;]" />
  

  
    
      
        Install the following dependencies:
      

      ```bash
      npm install honestui
      ```

      
        Copy and paste the following code into your project.
      

      ### components/ui/spinner.tsx

```tsx
import * as React from "react"
import { LoaderCircle as LoaderCircleIcon } from "honestui/icons"

import { cn } from "@/lib/utils"

const sizes = {
  sm: "size-3.5",
  default: "size-4",
  lg: "size-5",
} as const

type SpinnerProps = Omit<React.ComponentProps<"span">, "children"> & {
  /** Announced to assistive technology. Ignored when `decorative`. */
  label?: string
  /**
   * Hides the spinner from assistive technology. Use it when nearby text
   * already says what is loading, such as a button that reads "Saving…".
   */
  decorative?: boolean
  size?: keyof typeof sizes | number
}

function Spinner({
  className,
  label = "Loading",
  decorative = false,
  size = "default",
  style,
  ...props
}: SpinnerProps) {
  const numeric = typeof size === "number"

  return (
    <span
      data-slot="spinner"
      role={decorative ? undefined : "status"}
      aria-hidden={decorative ? true : undefined}
      className={cn(
        "inline-flex shrink-0 items-center justify-center text-current",
        !numeric && sizes[size],
        className
      )}
      style={numeric ? { width: size, height: size, ...style } : style}
      {...props}
    >
      <LoaderCircleIcon
        aria-hidden="true"
        // The spin carries meaning, so reduced motion slows it rather than
        // stopping it: a frozen arc reads as a broken icon.
        className="size-full motion-safe:animate-spin motion-reduce:animate-[spin_2.4s_linear_infinite] forced-colors:text-[CanvasText]"
      />
      {!decorative && <span className="sr-only">{label}</span>}
    </span>
  )
}

export { Spinner }

```

      
        Update the import paths to match your project setup.
      
    
  


## Usage [#usage]

```tsx
import { Spinner } from "@/components/ui/spinner"
```

```tsx
<Spinner label="Loading invoices" />
```

```tsx
<Button disabled aria-busy>
  <Spinner decorative />
  Saving changes…
</Button>
```

## Don't do this [#dont-do-this]

### A spinner where the layout is known [#a-spinner-where-the-layout-is-known]

```tsx
// Bad
{isLoading ? <Spinner /> : <InvoiceTable rows={rows} />}
```

```tsx
// Good
{isLoading ? <Skeleton className="h-64 w-full" /> : <InvoiceTable rows={rows} />}
```

Swapping a whole table for a spinner makes the page collapse and then jump when the data arrives. A skeleton of the table's shape keeps the layout steady and shows what is coming.

### Announcing the same thing twice [#announcing-the-same-thing-twice]

```tsx
// Bad
<Button disabled>
  <Spinner label="Saving" />
  Saving…
</Button>
```

```tsx
// Good
<Button disabled aria-busy>
  <Spinner decorative />
  Saving…
</Button>
```

The button text already says what is happening. A second status announcement only repeats it.

## Examples [#examples]

### In a button [#in-a-button]

The label changes while the work runs, so the spinner is decorative.

```tsx
"use client"

import * as React from "react"

import { Button } from "@/components/honest-ui/ui/button"
import { Spinner } from "@/components/honest-ui/ui/spinner"

export function SpinnerButton() {
  const [saving, setSaving] = React.useState(false)
  const [saved, setSaved] = React.useState(false)

  const save = () => {
    setSaved(false)
    setSaving(true)
    window.setTimeout(() => {
      setSaving(false)
      setSaved(true)
    }, 1600)
  }

  return (
    <div className="flex flex-col items-center gap-[var(--hui-space-3)]">
      <Button onClick={save} disabled={saving} aria-busy={saving}>
        {/* The label already says what is happening, so the spinner is
            decorative here. */}
        {saving && <Spinner decorative />}
        {saving ? "Saving changes…" : "Save changes"}
      </Button>
      <p
        aria-live="polite"
        className="m-0 min-h-[var(--hui-space-5)] text-[var(--hui-color-foreground-base-secondary)] [font-size:var(--hui-font-size-small)]"
      >
        {saved ? "All changes saved." : ""}
      </p>
    </div>
  )
}

```

### Sizes [#sizes]

The three preset sizes and a numeric size, each taking the color of its text.

```tsx
import { Spinner } from "@/components/honest-ui/ui/spinner"

export function SpinnerSizes() {
  return (
    <div className="flex items-center gap-[var(--hui-space-6)] text-[var(--hui-color-foreground-base-primary)]">
      <Spinner size="sm" label="Loading, small" />
      <Spinner label="Loading, default" />
      <Spinner size="lg" label="Loading, large" />
      <Spinner
        size={32}
        label="Loading, 32 pixels"
        className="text-[var(--hui-color-foreground-accent-primary)]"
      />
    </div>
  )
}

```

### Waiting for a stream [#waiting-for-a-stream]

An empty region waiting for its first data, where the shape of the content is not yet known.

```tsx
import {
  Card,
  CardDescription,
  CardHeader,
  CardPanel,
  CardTitle,
} from "@/components/honest-ui/ui/card"
import { Spinner } from "@/components/honest-ui/ui/spinner"

export function SpinnerCard() {
  return (
    <Card className="w-full max-w-sm">
      <CardHeader>
        <CardTitle>Deployment logs</CardTitle>
        <CardDescription>Build #1842 on main</CardDescription>
      </CardHeader>
      <CardPanel>
        <div
          aria-busy="true"
          className="flex min-h-32 flex-col items-center justify-center gap-[var(--hui-space-3)] rounded-[var(--hui-radius-2)] border-[0.5px] border-dashed border-[var(--hui-color-border-base-secondary)] text-[var(--hui-color-foreground-base-secondary)] [font-size:var(--hui-font-size-small)]"
        >
          <Spinner label="Streaming build logs" />
          <span aria-hidden="true">Waiting for the first log line…</span>
        </div>
      </CardPanel>
    </Card>
  )
}

```

## API reference [#api-reference]

`Spinner` renders a `span` and forwards native span props.

| Prop         | Values                        | Default     |
| ------------ | ----------------------------- | ----------- |
| `label`      | string                        | `"Loading"` |
| `decorative` | boolean                       | `false`     |
| `size`       | `sm`, `default`, `lg`, number | `default`   |

Without `decorative`, the root has `role="status"` and a visually hidden label. With `decorative`, it has `aria-hidden="true"` and no label. The arc uses `currentColor`; set a text color on the spinner or its parent to change it.
