# Spinner

> An indeterminate loading indicator with a required accessible name.

```tsx title="hero.tsx"
import { Spinner } from '@langgenius/dify-ui/spinner'
import * as React from 'react'

export function SpinnerHeroDemo() {
  const labelId = React.useId()

  return (
    <div className="flex items-center gap-2">
      <Spinner aria-labelledby={labelId} />
      <span id={labelId} className="system-sm-regular text-text-secondary">
        Indexing documents…
      </span>
    </div>
  )
}
```

## Usage guidelines

- **Name every spinner**: `Spinner` is a progress bar with no value. Give it exactly one accessible name, through `aria-labelledby` when visible text describes the work, or `aria-label` otherwise. The caller passes localized text.
- **Use Progress when the amount is known**: A spinner says that work is running, not how much is done. Use `Progress` for a task with measurable completion.
- **Keep status announcements with the feature**: `Spinner` does not announce completion or set `aria-busy` on the region that is loading. The feature that owns the request owns those.
- **Use SpinnerIcon inside a control that is already named**: `SpinnerIcon` is the same artwork, always hidden from assistive technology. A `Button` with `loading` draws its own spinner, so do not add a second one inside it.
- **Keep layout outside**: Both parts occupy only their own size, 12, 16 or 20 pixels. Placeholders, centering, delays and page geometry belong to the caller.

## Anatomy

```tsx
import { Spinner, SpinnerIcon } from '@langgenius/dify-ui/spinner'
```

```tsx
<Spinner />
<SpinnerIcon />
```

The artwork is four squares on a two-second opacity cycle, with a still frame when the user prefers reduced motion. It is blue in both themes; `className` can set another text color token.

## Examples

### Sizes

`small`, `medium` (the default) and `large` render 12, 16 and 20 pixels.

```tsx title="sizes.tsx"
import { Spinner } from '@langgenius/dify-ui/spinner'

const sizes = [
  { size: 'small', pixels: '12px' },
  { size: 'medium', pixels: '16px' },
  { size: 'large', pixels: '20px' },
] as const

export function SpinnerSizesDemo() {
  return (
    <div className="flex flex-wrap items-end gap-8">
      {sizes.map(({ size, pixels }) => (
        <div key={size} className="flex flex-col items-center gap-2">
          <Spinner size={size} aria-label={`Loading (${size})`} />
          <span className="system-xs-regular text-text-tertiary tabular-nums">
            {size} · {pixels}
          </span>
        </div>
      ))}
    </div>
  )
}
```

### Icon inside a control

`SpinnerIcon` replaces an icon while the control that owns the loading state stays named and marks itself `aria-busy`. Here the button stays focusable while it is disabled.

```tsx title="icon.tsx"
import { IconButton } from '@langgenius/dify-ui/icon-button'
import { SpinnerIcon } from '@langgenius/dify-ui/spinner'
import * as React from 'react'

export function SpinnerIconDemo() {
  const [loading, setLoading] = React.useState(false)

  React.useEffect(() => {
    if (!loading) return
    const timer = setTimeout(() => setLoading(false), 2000)
    return () => clearTimeout(timer)
  }, [loading])

  return (
    <IconButton
      aria-label={loading ? 'Loading voice preview' : 'Play voice preview'}
      aria-busy={loading || undefined}
      variant="secondary"
      size="lg"
      disabled={loading}
      focusableWhenDisabled
      onClick={() => setLoading(true)}
    >
      {loading ? (
        <SpinnerIcon />
      ) : (
        <span aria-hidden="true" className="i-ri-play-circle-line size-4" />
      )}
    </IconButton>
  )
}
```

## API reference

### Spinner

An indeterminate loading indicator; requires `aria-label` or `aria-labelledby`. Renders a `<div>` element.

**Props**

| Prop               | Type                             | Default    | Description                                                                        |
| ------------------ | -------------------------------- | ---------- | ---------------------------------------------------------------------------------- |
| `size?`            | `'large' \| 'medium' \| 'small'` | `'medium'` | The size of the spinner.                                                           |
| `className?`       | `string`                         | —          | CSS class applied to the element.                                                  |
| `aria-label?`      | `string`                         | —          | Names the element for assistive technology. Pass either this or `aria-labelledby`. |
| `aria-labelledby?` | `string`                         | —          | Identifies the element that names this one. Pass either this or `aria-label`.      |

Valid prop combinations:

- `aria-label: string`, `aria-labelledby?: undefined`
- `aria-label?: undefined`, `aria-labelledby: string`

### SpinnerIcon

A decorative loading spinner graphic hidden from assistive technology. Renders an `<svg>` element.

**Props**

| Prop         | Type                             | Default    | Description                       |
| ------------ | -------------------------------- | ---------- | --------------------------------- |
| `size?`      | `'large' \| 'medium' \| 'small'` | `'medium'` | The size of the spinner.          |
| `className?` | `string`                         | —          | CSS class applied to the element. |
