# Spinner

> A CSS-only loading indicator that renders in Server Components, announces an optional label once, and covers content or a positioned parent while it loads.

Canonical: https://salmonui.dev/docs/notifications/spinner

```tsx
import { Spinner } from '@salmonui/notifications/spinner';
import '@salmonui/notifications/spinner/styles.css';

<Spinner label="Loading orders" />;
```

`Spinner` is a server component: the entry has no `"use client"` directive,
no hooks and no client JavaScript, so a React Server Component can render it
directly. The ring is one CSS animation (the shared `salmon-spin` keyframes),
stopped under `prefers-reduced-motion`.

### Live demo: Spinner sizes and overlays

Live demo: https://salmonui.dev/demos/notifications/spinner-overlay

Labelled spinners in every size and a pixel diameter, a custom indicator, a form covered by a spinner while it saves, and an overlay on a positioned panel while it refreshes.

`Demo.tsx`:

```tsx
'use client';

import { useState } from 'react';
import { Button } from '@salmonui/buttons/button';
import { Spinner } from '@salmonui/notifications/spinner';
import { TextField } from '@salmonui/inputs/text-field';

const dots = (
  <svg viewBox="0 0 24 24" width="24" height="24">
    {[4, 12, 20].map((cx, i) => (
      <circle key={cx} cx={cx} cy="12" r="3" fill="currentColor" opacity="0.3">
        <animate attributeName="opacity" values="0.3;1;0.3" dur="0.9s" begin={`${i * 0.3}s`} repeatCount="indefinite" />
      </circle>
    ))}
  </svg>
);

// Spinners: labelled sizes, a form covered while it saves, an overlay on a
// positioned panel, and a custom indicator.
export default function SpinnerOverlayDemo() {
  const [saving, setSaving] = useState(false);
  const [refreshing, setRefreshing] = useState(false);
  const [rate, setRate] = useState(1.08);
  const save = () => {
    setSaving(true);
    setTimeout(() => setSaving(false), 1500);
  };
  return (
    <div style={{ display: 'grid', gap: 20, maxInlineSize: 480 }}>
      <div className="demo-toolbar">
        <Spinner size="sm" label="Loading, small" />
        <Spinner label="Loading, medium" />
        <Spinner size="lg" label="Loading, large" />
        <Spinner size={48} label="Loading, 48 pixels" style={{ color: 'var(--salmon-color-success)' }} />
        <Spinner indicator={dots} label="Loading with a custom indicator" />
      </div>
      <Spinner loading={saving} label="Saving the customer" style={{ border: '1px solid var(--salmon-border-subtle)', borderRadius: 'var(--salmon-radius-lg)' }}>
        <form
          onSubmit={(e) => {
            e.preventDefault();
            save();
          }}
          style={{ display: 'grid', gap: 8, padding: 12 }}
        >
          <TextField name="customer" label="Customer" defaultValue="Ada Lovelace" />
          <Button type="submit" tone="primary" style={{ justifySelf: 'start' }}>
            Save
          </Button>
        </form>
      </Spinner>
      <div style={{ position: 'relative', padding: 12, border: '1px solid var(--salmon-border-subtle)', borderRadius: 'var(--salmon-radius-lg)' }} aria-busy={refreshing}>
        <p style={{ marginBlockStart: 0 }}>Exchange rate: 1 EUR = {rate.toFixed(2)} USD</p>
        <Button
          variant="outline"
          onClick={() => {
            setRefreshing(true);
            setTimeout(() => {
              setRefreshing(false);
              setRate((r) => (r >= 1.1 ? 1.07 : r + 0.01));
            }, 1500);
          }}
        >
          Refresh
        </Button>
        {refreshing ? <Spinner overlay label="Refreshing rates" /> : null}
      </div>
    </div>
  );
}
```

## Label and accessibility

- With `label` the spinner is a `role="status"` element whose visually hidden
  text is the label. Render it when loading starts so the label is
  announced once.
- Without `label` the spinner is decorative (`aria-hidden="true"`). Put
  `aria-busy="true"` on the region that is loading and announce the state once
  elsewhere.

## Size, class and style

- `size`: `'sm'` (16 px), `'md'` (24 px, default), `'lg'` (32 px), or a number
  for a diameter in pixels.
- `className` and `style` reach the root; every other DOM prop (`id`,
  `data-*`, `aria-*`, handlers) is spread onto it. `ref` points at the root element.
- `indicator` replaces the built-in ring with any node, for example an inline
  SVG. The node is shown as is: it brings
  its own animation, if any, and should stop it under
  `prefers-reduced-motion`.

## Covering content

```tsx
// Covers its children while loading.
<Spinner loading={saving} label="Saving">
  <OrderForm />
</Spinner>

// Covers the nearest positioned ancestor.
<div style={{ position: 'relative' }} aria-busy={loading}>
  <Spinner overlay />
  …
</div>
```

- With `children`, the root is a wrapper `<div>` with `aria-busy` while
  `loading`. The children keep their size and layout, but they are hidden and
  `inert` (no focus, no pointer input), and the spinner is centered over them
  on a translucent scrim.
- `loading` defaults to `true`; toggle it to show and hide the spinner.
`loading={false}` renders the children alone; without children it
  renders nothing.
- `overlay` positions a standalone spinner over its nearest positioned
  ancestor.

## Design notes

There is one spinner design, colored by the theme:
Salmon UI has one visual language driven by the five theme
seeds, so a per-spinner design-system style would draw a ring that does not
match the rest of the interface; use `indicator` for a different shape.

There is no global function that changes every spinner:
it would need module-level state, which would leak
between server requests and break the React Compiler rules. An application
component such as
`function AppSpinner(props) { return <Spinner size="lg" indicator={<Logo />} {...props} />; }`
gives the same app-wide default.

API reference: [`@salmonui/notifications/spinner`](https://salmonui.dev/docs/reference/notifications/spinner).
