# CircularProgress

## Introduction

The CircularProgress component displays a circular loading indicator. It is based on Joy UI's CircularProgress and supports both indeterminate (spinning) and determinate (progress percentage) modes. Use it to provide visual feedback during asynchronous operations like data fetching, file uploads, or form submissions.

```tsx
<CircularProgress />
```

| Field       | Description | Default |
| ----------- | ----------- | ------- |
| size        | —           | —       |
| color       | —           | —       |
| variant     | —           | —       |
| value       | —           | —       |
| determinate | —           | —       |
| thickness   | —           | —       |

## Usage

```tsx
import { CircularProgress } from '@ceed/cds';

function MyComponent() {
  return <CircularProgress />;
}
```

## Sizes

CircularProgress supports three sizes: `sm`, `md` (default), and `lg`.

```tsx
<>
    <CircularProgress size="sm" />
    <CircularProgress size="md" />
    <CircularProgress size="lg" />
  </>
```

```tsx
<CircularProgress size="sm" />
<CircularProgress size="md" />
<CircularProgress size="lg" />
```

## Colors

Five semantic colors are available via the `color` prop. The default is `primary`.

```tsx
<>
    <CircularProgress color="primary" />
    <CircularProgress color="neutral" />
    <CircularProgress color="danger" />
    <CircularProgress color="success" />
    <CircularProgress color="warning" />
  </>
```

```tsx
<CircularProgress color="primary" />
<CircularProgress color="neutral" />
<CircularProgress color="danger" />
<CircularProgress color="success" />
<CircularProgress color="warning" />
```

## Variants

Four visual variants are supported: `solid`, `soft` (default), `outlined`, and `plain`.

```tsx
<>
    <CircularProgress variant="solid" />
    <CircularProgress variant="soft" />
    <CircularProgress variant="outlined" />
    <CircularProgress variant="plain" />
  </>
```

```tsx
<CircularProgress variant="solid" />
<CircularProgress variant="soft" />
<CircularProgress variant="outlined" />
<CircularProgress variant="plain" />
```

## Determinate Mode

Set `determinate` to `true` and provide a `value` (0–100) to display specific progress.

```tsx
<>
    <CircularProgress determinate value={25} />
    <CircularProgress determinate value={50} />
    <CircularProgress determinate value={75} />
    <CircularProgress determinate value={100} />
  </>
```

```tsx
<CircularProgress determinate value={25} />
<CircularProgress determinate value={50} />
<CircularProgress determinate value={75} />
<CircularProgress determinate value={100} />
```

## With Label

Pass children to display a label inside the progress ring. This is useful for showing the current percentage.

```tsx
<CircularProgress determinate value={66}>
    <Typography level="body-xs">66%</Typography>
  </CircularProgress>
```

```tsx
<CircularProgress determinate value={66}>
  <Typography level="body-xs">66%</Typography>
</CircularProgress>
```

## In Button

Use CircularProgress inside a Button to indicate a loading state.

```tsx
<>
    <Button startDecorator={<CircularProgress variant="solid" size="sm" />}>Loading…</Button>
    <Button disabled>
      <CircularProgress variant="soft" size="sm" />
    </Button>
  </>
```

```tsx
<Button startDecorator={<CircularProgress variant="solid" size="sm" />}>
  Loading…
</Button>

<Button disabled>
  <CircularProgress variant="soft" size="sm" />
</Button>
```

## Thickness

Control the stroke thickness of the progress ring with the `thickness` prop.

```tsx
<>
    <CircularProgress thickness={2} />
    <CircularProgress thickness={4} />
    <CircularProgress thickness={8} />
  </>
```

```tsx
<CircularProgress thickness={2} />
<CircularProgress thickness={4} />
<CircularProgress thickness={8} />
```

## Common Use Cases

### Data Fetching Indicator

```tsx
function DataLoader() {
  const [loading, setLoading] = React.useState(true);
  const [data, setData] = React.useState(null);

  React.useEffect(() => {
    fetchData().then((result) => {
      setData(result);
      setLoading(false);
    });
  }, []);

  if (loading) {
    return (
      <Box sx={{ display: 'flex', justifyContent: 'center', py: 4 }}>
        <CircularProgress />
      </Box>
    );
  }

  return <DataView data={data} />;
}
```

### File Upload Progress

```tsx
function FileUpload({ progress }: { progress: number }) {
  return (
    <Stack direction="row" alignItems="center" spacing={2}>
      <CircularProgress determinate value={progress} size="lg">
        <Typography level="body-xs">{progress}%</Typography>
      </CircularProgress>
      <Typography level="body-md">Uploading file…</Typography>
    </Stack>
  );
}
```

### Inline Loading Button

```tsx
function SubmitButton({ isSubmitting }: { isSubmitting: boolean }) {
  return (
    <Button
      disabled={isSubmitting}
      startDecorator={isSubmitting ? <CircularProgress variant="solid" size="sm" /> : null}
    >
      {isSubmitting ? 'Submitting…' : 'Submit'}
    </Button>
  );
}
```

## Props and Customization

### Key Props

| Prop          | Type                                                           | Default     | Description                                  |
| ------------- | -------------------------------------------------------------- | ----------- | -------------------------------------------- |
| `determinate` | `boolean`                                                      | `false`     | Show determinate progress (requires `value`) |
| `value`       | `number`                                                       | -           | Progress value (0-100) for determinate mode  |
| `size`        | `'sm' \| 'md' \| 'lg'`                                         | `'md'`      | Progress indicator size                      |
| `variant`     | `'solid' \| 'soft' \| 'outlined' \| 'plain'`                   | `'soft'`    | Visual style                                 |
| `color`       | `'primary' \| 'neutral' \| 'danger' \| 'success' \| 'warning'` | `'primary'` | Color scheme                                 |
| `thickness`   | `number`                                                       | -           | Stroke width of the circle                   |
| `children`    | `ReactNode`                                                    | -           | Content rendered in the center               |
| `sx`          | `SxProps`                                                      | -           | Custom styles                                |

> **Note**: CircularProgress also accepts all Joy UI CircularProgress props.

## Best Practices

1. **Use indeterminate for unknown durations**: When you cannot estimate progress, use the default spinning mode. Switch to determinate only when you can track actual progress.

```tsx
// ✅ Unknown duration — indeterminate
<CircularProgress />

// ✅ Known progress — determinate
<CircularProgress determinate value={uploadProgress} />

// ❌ Don't use determinate with a static value for loading
<CircularProgress determinate value={50} />
```

2. **Size appropriately**: Use `sm` for inline contexts (buttons, table cells), `md` for general use, and `lg` for prominent page-level loading states.

3. **Match variant with context**: Use `solid` variant inside solid buttons, and `soft` (default) for standalone usage to maintain visual consistency.

4. **Add descriptive text**: Always pair the indicator with text or an `aria-label` so users understand what is loading.

5. **Avoid multiple spinners**: Show one loading indicator per distinct loading region. Multiple spinners on a page create visual clutter.

## Accessibility

- CircularProgress has `role="progressbar"` by default.
- In determinate mode, `aria-valuenow`, `aria-valuemin`, and `aria-valuemax` are set automatically.
- Add `aria-label` or nearby descriptive text to explain what is loading (e.g., `aria-label="Loading user data"`).
- Ensure sufficient color contrast between the progress ring and its background, especially with the `plain` variant.
