# LoadingSpinner (/components/loading-spinner)

<ReferenceLinks
  links={[
    { label: "Source", href: meta.links.source, iconName: "code" },
    { label: "Stories", href: meta.links.stories },
  ]}
/>

<Stage code={"<LoadingSpinner size=\"lg\" />\n<LoadingSpinner size=\"md\" />\n<LoadingSpinner size=\"md\" variant=\"bars\" />\n<LoadingSpinner size=\"sm\" variant=\"bars\" />"}>
  <LoadingSpinner size="lg" />
  <LoadingSpinner size="md" />
  <LoadingSpinner size="md" variant="bars" />
  <LoadingSpinner size="sm" variant="bars" />
</Stage>

## Installation [#installation]

1. Install the package.

```sh
npm install @hashintel/ds-components
```

2. Import the component.

```tsx
import { LoadingSpinner } from "@hashintel/ds-components";

export const EntityTableLoading = () => <LoadingSpinner size="md" />;
```

### When to use [#when-to-use]

<DoList
  items={[
    "Use a spinner for indeterminate waits — fetching entities, syncing a type, running a flow — where progress can't be quantified.",
    "Place it where the loaded content will appear, so the user's eye is already there when it lands.",
    "Use one spinner per loading region — simultaneous spinners compete for attention and imply separate processes.",
  ]}
/>

### When not to use [#when-not-to-use]

<DontList
  items={[
    "Don't show a spinner for sub-second work — render the result directly; a spinner that flashes in and out reads as flicker.",
    'Don\'t use a spinner when progress is measurable — state it as text, like "3 of 5 imports complete", so users can gauge the wait.',
    "Don't leave a spinner running after a request fails — swap in an error state, such as a Banner, that says what went wrong.",
  ]}
/>

## Examples [#examples]

### Basic [#basic]

The default spinner is a smooth circular sweep at `md`. Drop it into
the space the pending content will fill.

<Stage code={"<LoadingSpinner />"}>
  <LoadingSpinner />
</Stage>

### Sizes [#sizes]

Five fixed sizes from `xxs` to `lg`, sharing the scale Button and
the form inputs use — match the control or region the spinner stands in for.

<Stage code={"<LoadingSpinner size=\"xxs\" />\n<LoadingSpinner size=\"xs\" />\n<LoadingSpinner size=\"sm\" />\n<LoadingSpinner size=\"md\" />\n<LoadingSpinner size=\"lg\" />"}>
  <LoadingSpinner size="xxs" />
  <LoadingSpinner size="xs" />
  <LoadingSpinner size="sm" />
  <LoadingSpinner size="md" />
  <LoadingSpinner size="lg" />
</Stage>

### Bars variant [#bars-variant]

`bars` replaces the smooth sweep with twelve spokes that tick in discrete
steps.

<Stage code={"<LoadingSpinner variant=\"default\" size=\"md\" />\n<LoadingSpinner variant=\"bars\" size=\"md\" />"}>
  <LoadingSpinner variant="default" size="md" />
  <LoadingSpinner variant="bars" size="md" />
</Stage>

### Color [#color]

Both variants draw entirely with `currentColor`, so the spinner takes the CSS
`color` of its container.

<Stage code={"<LoadingSpinner size=\"md\" />\n<span style={{ color: \"#0d74ce\", display: \"inline-flex\" }}>\n  <LoadingSpinner size=\"md\" />\n</span>\n<div\n  style={{\n    backgroundColor: \"#1d1d1d\",\n    color: \"#ffffff\",\n    padding: 12,\n    borderRadius: 8,\n    display: \"inline-flex\",\n  }}\n>\n  <LoadingSpinner size=\"md\" />\n</div>"}>
  <LoadingSpinner size="md" />
  <span style={{ color: "#0d74ce", display: "inline-flex" }}>
    <LoadingSpinner size="md" />
  </span>
  <div
    style={{
      backgroundColor: "#1d1d1d",
      color: "#ffffff",
      padding: 12,
      borderRadius: 8,
      display: "inline-flex",
    }}
  >
    <LoadingSpinner size="md" />
  </div>
</Stage>

### With a label [#with-a-label]

A spinner alone doesn't say what's loading. Pair it with text naming the work,
and wrap both in `role="status"` so screen readers announce it too.

<Stage code={"<div role=\"status\" style={{ display: \"flex\", alignItems: \"center\", gap: 8 }}>\n  <LoadingSpinner size=\"sm\" variant=\"bars\" />\n  <span>Loading entities…</span>\n</div>"}>
  <div role="status" style={{ display: "flex", alignItems: "center", gap: 8 }}>
    <LoadingSpinner size="sm" variant="bars" />
    <span>Loading entities…</span>
  </div>
</Stage>

## Behavior [#behavior]

- The animation keeps running under `prefers-reduced-motion` — a frozen
  spinner reads as a stalled process.
- Sizes are fixed sizes; the spinner doesn't scale with
  surrounding text or its container.

## Accessibility [#accessibility]

LoadingSpinner provides a decorative SVG with no role, accessible name, or live region. You must set these yourself.

### What you must do [#what-you-must-do]

- Announce loading in text: wrap the spinner and a label in an element with
  `role="status"` so screen readers hear the state change.
- Name the work in the label — "Loading entities…", not "Please wait".

## API reference [#api-reference]

<PropsTable
  of={LoadingSpinner}
  defaults={{
    size: '"md"',
    variant: '"default"',
  }}
/>
