# Progress Component

Show how far along a task is, like an upload, a storage quota, or a set of onboarding steps. A track and an indicator whose width is the current value, built with Tailwind CSS only.

## Features

- Accessible `role="progressbar"` with `aria-valuenow`, `aria-valuemin`, and `aria-valuemax`
- Optional visible label linked with `aria-labelledby`, and the value next to it
- Custom value text ("7.2 GB of 10 GB") announced through `aria-valuetext`
- Three sizes and four colors (default, success, warning, danger)
- Animated width changes, turned off for reduced motion
- Works in right-to-left layouts
- Dark mode support
- Zero JavaScript required

## Implementation Options

| Format | Location | Best For |
| ------ | -------- | -------- |
| **Plain ERB** | `app/views/components/progress/` | Full control, copy-paste |
| **Shared Partial** | `app/views/shared/components/progress/` | Reusable partial with locals |
| **ViewComponent** | `app/components/progress/` | Ruby-based, testable, object-oriented |

### Plain ERB

```erb
<div role="progressbar" aria-label="Upload progress" aria-valuenow="60" aria-valuemin="0" aria-valuemax="100" class="h-2 w-full max-w-sm overflow-hidden rounded-full bg-neutral-900/10 dark:bg-white/15">
  <div class="h-full bg-neutral-900 transition-[width] duration-500 ease-out motion-reduce:transition-none dark:bg-white" style="width: 60%"></div>
</div>
```

### Shared Partial

```erb
<%= render "shared/components/progress/progress", value: 60, aria_label: "Upload progress", classes: "max-w-sm" %>
<%= render "shared/components/progress/progress", value: 66, label: "Uploading files" %>
<%= render "shared/components/progress/progress", value: 7.2, max: 10, label: "Storage", value_text: "7.2 GB of 10 GB" %>
```

### ViewComponent

```erb
<%= render Progress::Component.new(value: 60, aria_label: "Upload progress", classes: "max-w-sm") %>
<%= render Progress::Component.new(value: 66, label: "Uploading files") %>
<%= render Progress::Component.new(value: 7.2, max: 10, label: "Storage", value_text: "7.2 GB of 10 GB") %>
```

---

## Examples

### Basic Progress

```erb
<%= render Progress::Component.new(value: 60, aria_label: "Upload progress", classes: "max-w-sm") %>
```

### Progress with Label

```erb
<div class="w-full max-w-sm space-y-6">
  <%= render Progress::Component.new(value: 66, label: "Uploading files", id: "upload-progress") %>
  <%= render Progress::Component.new(value: 7.2, max: 10, label: "Storage", value_text: "7.2 GB of 10 GB") %>
</div>
```

### Progress Variants

```erb
<%= render Progress::Component.new(value: 40, label: "Small", size: :sm) %>
<%= render Progress::Component.new(value: 55, label: "Default") %>
<%= render Progress::Component.new(value: 70, label: "Large", size: :lg) %>

<%= render Progress::Component.new(value: 90, label: "Tasks completed", variant: :success) %>
<%= render Progress::Component.new(value: 75, label: "Monthly quota", variant: :warning) %>
<%= render Progress::Component.new(value: 95, label: "Disk space", variant: :danger) %>
```

---

## Options Reference

The shared partial takes the same options as locals.

| Option | Type | Default | Description |
| ------ | ---- | ------- | ----------- |
| `value` | Numeric | required | Current value, between `0` and `max` |
| `max` | Numeric | `100` | Maximum value |
| `label` | String | `nil` | Visible label above the bar, linked with `aria-labelledby` |
| `aria_label` | String | `nil` | Accessible name when there is no visible label |
| `value_text` | String | `nil` | Value shown next to the label and announced as `aria-valuetext`. Defaults to the percentage |
| `show_value` | Boolean | `true` | Show the value next to the label |
| `size` | Symbol | `:md` | `:sm` (4px), `:md` (8px), or `:lg` (12px) |
| `variant` | Symbol | `:default` | `:default`, `:success`, `:warning`, or `:danger` |
| `id` | String | random | ID of the progress bar. The label gets `"#{id}-label"` |
| `classes` | String | `nil` | Extra classes for the outer element (the wrapper with a label, the track without one) |

Unknown sizes and variants fall back to `:md` and `:default`.

## Updating the Value

The indicator animates its width. Update the `width` of the indicator and `aria-valuenow` on the bar, or replace the whole bar with a Turbo Stream:

```javascript
const bar = document.querySelector("#upload-progress")
bar.setAttribute("aria-valuenow", 80)
bar.firstElementChild.style.width = "80%"
```

```erb
<%= turbo_stream.replace "upload-progress-wrapper" do %>
  <div id="upload-progress-wrapper">
    <%= render Progress::Component.new(value: @upload.percent, label: "Uploading files", id: "upload-progress") %>
  </div>
<% end %>
```

## Accessibility

- Every progress bar needs an accessible name: a `label:` (visible) or an `aria_label:`.
- Use `value_text:` when the raw number is not meaningful on its own, like "Step 2 of 5" or "7.2 GB of 10 GB".
- The visible value is `aria-hidden`, since the progress bar already announces it.
- Don't rely on color alone to tell a warning from a normal state. Pair the color with a label or a value.
- For work with no known end, use a Loading Indicator instead.

---

## AI Instructions

### Choose An Implementation

- **ViewComponent**: Default when the app has `view_component`. Use `Progress::Component`.
- **Shared partial**: Default when the app has no `view_component`: `shared/components/progress/progress` takes the same options as locals.
- **Plain ERB**: Use when you need markup the component doesn't cover; copy the closest file from `app/views/components/progress/`.

### Quick Reference

- **Vanilla examples**: `app/views/components/progress/`
- **ViewComponent**: `render Progress::Component.new(value: 66, label: "Uploading files")`
- **ViewComponent files**: `app/components/progress/`
- **Shared partial**: `render "shared/components/progress/progress", value: 66, label: "Uploading files"` (`app/views/shared/components/progress/`)
- **JavaScript**: none

### Implementation Checklist

- Pass either `label:` or `aria_label:` so the bar has an accessible name.
- Pass `max:` when the value is not a percentage, and `value_text:` to show and announce a readable value.
- Give the bar an `id:` when you update it from JavaScript or a Turbo Stream.
- When updating from JavaScript, change both the indicator `width` and `aria-valuenow`.
- Keep `role="progressbar"`, the `aria-value*` attributes, and `overflow-hidden` on the track when copying plain ERB.

### Common Patterns

- File uploads: `label: "Uploading files"`, updated from a Turbo Stream or upload events.
- Storage or plan limits: `max:` with `value_text: "7.2 GB of 10 GB"`, and `variant: :warning` or `:danger` near the limit.
- Onboarding: `value: 2, max: 5, value_text: "Step 2 of 5"`.

### Common Mistakes

- Do not use a progress bar for loading with no known end; use a Loading Indicator.
- Do not set only the width when updating; screen readers read `aria-valuenow`.
- Do not omit both `label:` and `aria_label:`.
- Do not invent options (`color:`, `striped:`, `size: :xl`, `variant: :primary`).
- Do not mix ERB partial locals with ViewComponent initializer arguments in the same example.