# ProgressBar

Horizontal progress bar that fills up to `value / total`, with an optional `label` above it and the percentage on the right (`showValue`). `indeterminate` shows a bar that slides back and forth, for work of unknown length. Four colors (`variant`). `valueTooltip` shows the value in a tooltip when the pointer is over the bar. The root is a `role="progressbar"` named by the label; without a label pass `aria-label`. Its state is exposed as `data-variant` and `data-indeterminate` for app CSS.

```ts
import { ProgressBar } from '@likable-hair/svelte';
```

Category: simple. Source: `src/lib/components/simple/progress/ProgressBar.svelte`.

## Examples

### Label and value

`label` names the bar (also for screen readers) and `showValue` adds the percentage. Changes of `value` are animated.

```svelte
<script lang="ts">
	import { Button, ProgressBar } from '@likable-hair/svelte';

	let booked = $state(72);
</script>

<div class="stack">
	<ProgressBar value={booked} label="Monthly goal" showValue />
	<div class="actions">
		<Button variant="secondary" size="sm" onclick={() => (booked = Math.max(0, booked - 12))}>−12</Button>
		<Button variant="secondary" size="sm" onclick={() => (booked = Math.min(100, booked + 12))}>+12</Button>
	</div>
</div>

<style>
	.stack {
		display: grid;
		gap: 16px;
		width: min(100%, 420px);
	}

	.actions {
		display: flex;
		gap: 8px;
	}
</style>
```

### Variants and custom value

Status colors for quotas and limits. `total` sets the value that fills the bar and `valueSnippet` replaces the percentage, here with "used of total".

```svelte
<script lang="ts">
	import { ProgressBar } from '@likable-hair/svelte';

	const quotas = [
		{ label: 'SMS reminders', used: 380, total: 1000, variant: 'success' },
		{ label: 'Storage', used: 8.2, total: 10, variant: 'warning' },
		{ label: 'Staff seats', used: 12, total: 12, variant: 'error' }
	] as const;
</script>

<div class="stack">
	{#each quotas as quota (quota.label)}
		<ProgressBar
			value={quota.used}
			total={quota.total}
			label={quota.label}
			variant={quota.variant}
			showValue
			--progress-bar-height="6px"
		>
			{#snippet valueSnippet({ value, total })}{value} / {total}{/snippet}
		</ProgressBar>
	{/each}
</div>

<style>
	.stack {
		display: grid;
		gap: 20px;
		width: min(100%, 420px);
	}
</style>
```

### Indeterminate and tooltip

`indeterminate` slides a bar back and forth while the length of the work is unknown. `valueTooltip` shows the value when the pointer is over the bar; `valueTooltipLabel` replaces it with a text.

```svelte
<script lang="ts">
	import { ProgressBar } from '@likable-hair/svelte';
</script>

<div class="stack">
	<ProgressBar indeterminate label="Syncing calendar…" --progress-bar-height="4px" />
	<ProgressBar
		value={36}
		total={50}
		aria-label="Seats booked"
		valueTooltip
		valueTooltipLabel="36 of 50 seats booked"
	/>
</div>

<style>
	.stack {
		display: grid;
		gap: 24px;
		width: min(100%, 420px);
	}
</style>
```

## Props

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `value` | `number` | `0` | Progress, from 0 to `total`. Values outside the range are clamped. |
| `total` | `number` | `100` | Value that fills the bar. With `0` the bar is full. |
| `label` | `string` |  | Text above the bar, on the left. It also names the progress bar for screen readers. |
| `showValue` | `boolean` | `false` | Shows the percentage above the bar, on the right. |
| `indeterminate` | `boolean` | `false` | A bar that slides back and forth, for work of unknown length. `value` is ignored. |
| `variant` | `'primary' \| 'success' \| 'warning' \| 'error'` | `'primary'` | Fill color: the theme gradient or a status color. |
| `valueTooltip` | `boolean` | `false` | Shows the value in a tooltip when the pointer is over the bar. |
| `valueTooltipLabel` | `string \| number` |  | Text of the tooltip, instead of the value. Screen readers read it as the value text. |
| `class` | `string` | `''` | Extra classes on the root element. |

It also accepts the native attributes and events of `<div>` (for example `id`, `title`, `aria-label`, `onkeydown`), which are forwarded to the element.

## Snippets

| Name | Parameters | Description |
| --- | --- | --- |
| `labelSnippet` | `{ label: string \| undefined }` | Replaces the text of the label. |
| `valueSnippet` | `{ value: number; total: number; percent: number }` | Replaces the percentage shown with `showValue`. Receives the value, the total and the percentage (0–100). |

## CSS variables

For a single instance, pass them as style props: `<ProgressBar --progress-bar-background="tomato">`. The `--progress-bar-default-*` defaults apply to the whole app: override them in `:root` or in a theme.

| Per-instance variable | Default |
| --- | --- |
| `--progress-bar-gap` | `--progress-bar-default-gap`: `8px` |
| `--progress-bar-width` | `--progress-bar-default-width`: `100%` |
| `--progress-bar-label-color` | `--progress-bar-default-label-color`: `var(--global-color-text)` |
| `--progress-bar-label-font-size` | `--progress-bar-default-label-font-size`: `13px` |
| `--progress-bar-label-font-weight` | `--progress-bar-default-label-font-weight`: `600` |
| `--progress-bar-value-color` | `--progress-bar-default-value-color`: `var(--global-color-text-3)` |
| `--progress-bar-value-font-family` | `--progress-bar-default-value-font-family`: `var(--global-font-family-mono)` |
| `--progress-bar-value-font-size` | `--progress-bar-default-value-font-size`: `13px` |
| `--progress-bar-height` | `--progress-bar-default-height`: `8px` |
| `--progress-bar-border-radius` | `--progress-bar-default-border-radius`: `var(--global-radius-full)` |
| `--progress-bar-background` | `--progress-bar-default-background`: `var(--global-color-surface-3)` |
| `--progress-bar-fill-background` | `--progress-bar-default-fill-background`: `var(--global-gradient)`<br>`--progress-bar-default-success-fill-background`: `var(--global-color-success)`<br>`--progress-bar-default-warning-fill-background`: `var(--global-color-warning)`<br>`--progress-bar-default-error-fill-background`: `var(--global-color-error)` |
| `--progress-bar-fill-box-shadow` | `--progress-bar-default-fill-box-shadow`: `0 0 16px var(--global-color-primary-glow)`<br>`--progress-bar-default-success-fill-box-shadow`: `0 0 #0000`<br>`--progress-bar-default-warning-fill-box-shadow`: `0 0 #0000`<br>`--progress-bar-default-error-fill-box-shadow`: `0 0 16px var(--global-color-error-glow)` |
| `--progress-bar-duration` | `--progress-bar-default-duration`: `var(--global-duration-slow)` |
| `--progress-bar-shimmer-color` | `--progress-bar-default-shimmer-color`: `color-mix(in oklab, var(--global-color-on-primary) 35%, transparent)` |
| `--progress-bar-shimmer-duration` | `--progress-bar-default-shimmer-duration`: `1.8s` |
| `--progress-bar-indeterminate-width` | `--progress-bar-default-indeterminate-width`: `35%` |
| `--progress-bar-indeterminate-duration` | `--progress-bar-default-indeterminate-duration`: `1.4s` |
