# Spinner

## Usage

`Spinner` shows a loading state when the duration of a process is unknown.

```vue
<script setup lang="ts">
import { Spinner } from '@astryx-vue/core/Spinner'
</script>

<template>
  <Spinner label="Loading data" />
</template>
```

## Best practices

| Guidance | Practices |
| --- | --- |
| Do | Provide a meaningful label to describe what is loading for screen reader users. |
| Do | Use the "onMedia" shade when placed on dark or accent-colored backgrounds. |
| Don't | Use for content areas with known dimensions; use Skeleton to preserve layout instead. |
| Don't | Stack multiple spinners in the same view; use one to represent the overall loading state. |

## Examples

### On media

default sits on a light surface. onMedia sits on a dark surface.

```vue
<script setup lang="ts">
import { Spinner } from '@astryx-vue/core/Spinner'
import { HStack } from '@astryx-vue/core/Stack'
</script>

<template>
  <HStack align="center" :gap="4">
    <Spinner shade="default" />
    <div class="spinner-media">
      <Spinner shade="onMedia" />
    </div>
  </HStack>
</template>

<style scoped>
.spinner-media {
  padding: 16px;
  border-radius: 8px;
  background-color: #1a1a2e;
}
</style>
```

### Sizes

sm, md, lg, and xl.

```vue
<script setup lang="ts">
import { Spinner } from '@astryx-vue/core/Spinner'
import { HStack } from '@astryx-vue/core/Stack'
</script>

<template>
  <HStack align="center" :gap="4">
    <Spinner size="sm" />
    <Spinner size="md" />
    <Spinner size="lg" />
    <Spinner size="xl" />
  </HStack>
</template>
```

### With label

A short label, or a title and a supporting line. The accessible name stays Fetching data when the label is rich.

```vue
<script setup lang="ts">
import { Spinner } from '@astryx-vue/core/Spinner'
import { HStack, VStack } from '@astryx-vue/core/Stack'
import { Text } from '@astryx-vue/core/Text'
</script>

<template>
  <HStack align="start" :gap="8">
    <Spinner label="Loading..." size="lg" />
    <Spinner aria-label="Fetching data" size="lg">
      <template #label>
        <VStack h-align="center" :gap="0">
          <Text type="body" weight="bold">
            Fetching data
          </Text>
          <Text type="supporting" color="secondary">
            This may take a moment
          </Text>
        </VStack>
      </template>
    </Spinner>
  </HStack>
</template>
```

## Accessibility

A `Spinner` has no visible text of its own, so its loading state reaches assistive technology entirely through ARIA attributes. The component sets these for you, and you normally do not need to manage them yourself; the list below covers the attributes it actually uses and what each one does.

- The root element carries `role="status"`, so assistive technology treats it as a status region.
- When you pass a string `label`, it shows as visible text below the spinner and provides the accessible name through `aria-labelledby`.
- Without a `label`, the accessible name defaults to `Loading`; pass `aria-label` to change it to something more specific to the context.
- The ring is `aria-hidden` and carries no text of its own, so it is not read out separately.
- With reduced motion enabled, the arc slows to a 3-second cycle.

## Spinner Props

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `size` | `'sm' \| 'md' \| 'lg' \| 'xl'` | `'md'` | Ring diameter: 10px, 14px, 18px, or 28px. |
| `shade` | `'default' \| 'subtle' \| 'onMedia' \| 'inherit'` | `'default'` | Color shade for light backgrounds, dark backgrounds, or inherited text color. |
| `label` | `string` | — | Text shown below the spinner. Also used as the accessible name. |

## Slots

- `label`: rich content below the spinner. Replaces the `label` prop text. With no visible text and no `aria-label`, the accessible name follows the locale: Loading in English, 加载中 in Simplified Chinese.

## Theme

```css
.my-spinner {
  --astryx-spinner-color-accent: #0074e2;
}
```

- `--astryx-spinner-diameter-{sm|md|lg|xl}`: ring diameter per size.
- `--astryx-spinner-stroke-width-{sm|md|lg|xl}`: stroke width per size.
- `--astryx-spinner-arc-fraction`: fraction of the ring covered by the moving arc.
- `--astryx-spinner-color-accent`: arc color for the `default` shade.
- `--astryx-spinner-color-subtle`: arc color for the `subtle` shade.
- `--astryx-spinner-color-track`: track color for the `default` and `subtle` shades.
- `--astryx-spinner-color-on-dark`: arc and track color for the `onMedia` shade.
- `--astryx-spinner-group-gap`: space between the ring and the label.
- `--astryx-spinner-label-color`: label text color.
