ProgressBar
Progress bars show either determinate or indeterminate progress of an operation over time.
install | yarn add react-aria-components |
---|---|
version | 1.0.0-alpha.0 |
usage | import {ProgressBar} from 'react-aria-components' |
Example#
import {ProgressBar, Label} from 'react-aria-components';
<ProgressBar value={80}>
{({percentage, valueText}) => <>
<Label>Loading…</Label>
<span className="value">{valueText}</span>
<div className="bar">
<div className="fill" style={{width: percentage + '%'}} />
</div>
</>}
</ProgressBar>
import {Label, ProgressBar} from 'react-aria-components';
<ProgressBar value={80}>
{({ percentage, valueText }) => (
<>
<Label>Loading…</Label>
<span className="value">{valueText}</span>
<div className="bar">
<div
className="fill"
style={{ width: percentage + '%' }}
/>
</div>
</>
)}
</ProgressBar>
import {
Label,
ProgressBar
} from 'react-aria-components';
<ProgressBar
value={80}
>
{(
{
percentage,
valueText
}
) => (
<>
<Label>
Loading…
</Label>
<span className="value">
{valueText}
</span>
<div className="bar">
<div
className="fill"
style={{
width:
percentage +
'%'
}}
/>
</div>
</>
)}
</ProgressBar>
Show CSS
.react-aria-ProgressBar {
--bar-color: var(--spectrum-global-color-gray-500);
--circle-color: var(--bar-color);
--fill-color: slateblue;
display: grid;
grid-template-areas: "label value"
"bar bar";
grid-template-columns: 1fr auto;
gap: 4px;
width: 250px;
.value {
grid-area: value;
}
.bar {
grid-area: bar;
box-shadow: inset 0px 0px 0px 1px var(--bar-color);
height: 10px;
border-radius: 5px;
overflow: hidden;
will-change: transform;
}
.fill {
background: var(--fill-color);
height: 100%;
}
&:not([aria-valuenow]) {
.fill {
width: 120px;
border-radius: inherit;
animation: indeterminate 1.5s infinite ease-in-out;
will-change: transform;
}
}
}
@keyframes indeterminate {
from {
transform: translateX(-100%);
}
to {
transform: translateX(250px);
}
}
@media (forced-colors: active) {
.react-aria-ProgressBar {
--bar-color: ButtonFace;
--circle-color: ButtonBorder;
--fill-color: Highlight;
.bar {
border: 1px solid ButtonBorder;
box-sizing: border-box;
}
}
}
.react-aria-ProgressBar {
--bar-color: var(--spectrum-global-color-gray-500);
--circle-color: var(--bar-color);
--fill-color: slateblue;
display: grid;
grid-template-areas: "label value"
"bar bar";
grid-template-columns: 1fr auto;
gap: 4px;
width: 250px;
.value {
grid-area: value;
}
.bar {
grid-area: bar;
box-shadow: inset 0px 0px 0px 1px var(--bar-color);
height: 10px;
border-radius: 5px;
overflow: hidden;
will-change: transform;
}
.fill {
background: var(--fill-color);
height: 100%;
}
&:not([aria-valuenow]) {
.fill {
width: 120px;
border-radius: inherit;
animation: indeterminate 1.5s infinite ease-in-out;
will-change: transform;
}
}
}
@keyframes indeterminate {
from {
transform: translateX(-100%);
}
to {
transform: translateX(250px);
}
}
@media (forced-colors: active) {
.react-aria-ProgressBar {
--bar-color: ButtonFace;
--circle-color: ButtonBorder;
--fill-color: Highlight;
.bar {
border: 1px solid ButtonBorder;
box-sizing: border-box;
}
}
}
.react-aria-ProgressBar {
--bar-color: var(--spectrum-global-color-gray-500);
--circle-color: var(--bar-color);
--fill-color: slateblue;
display: grid;
grid-template-areas: "label value"
"bar bar";
grid-template-columns: 1fr auto;
gap: 4px;
width: 250px;
.value {
grid-area: value;
}
.bar {
grid-area: bar;
box-shadow: inset 0px 0px 0px 1px var(--bar-color);
height: 10px;
border-radius: 5px;
overflow: hidden;
will-change: transform;
}
.fill {
background: var(--fill-color);
height: 100%;
}
&:not([aria-valuenow]) {
.fill {
width: 120px;
border-radius: inherit;
animation: indeterminate 1.5s infinite ease-in-out;
will-change: transform;
}
}
}
@keyframes indeterminate {
from {
transform: translateX(-100%);
}
to {
transform: translateX(250px);
}
}
@media (forced-colors: active) {
.react-aria-ProgressBar {
--bar-color: ButtonFace;
--circle-color: ButtonBorder;
--fill-color: Highlight;
.bar {
border: 1px solid ButtonBorder;
box-sizing: border-box;
}
}
}
Features#
The <progress>
HTML element can be used to build a progress bar, however it is
very difficult to style cross browser. ProgressBar
helps achieve accessible
progress bars and spinners that can be styled as needed.
- Accessible – Follows the ARIA progressbar pattern, supporting both determinate and indeterminate progress bars. A nested label is automatically associated with the progress bar semantically.
- International – The value is formatted as a percentage or custom format according to the user's locale.
Anatomy#
Progress bars consist of a track element showing the full progress of an operation, a fill element showing the current progress, a label, and an optional value label. The track and bar elements represent the progress visually, while a wrapper element represents the progress to assistive technology using the progressbar ARIA role.
If there is no visual label, an aria-label
or aria-labelledby
prop must be passed instead
to identify the element to screen readers.
Composed components#
A ProgressBar
uses the following components, which may also be used standalone or reused in other components.
Props#
Name | Type | Default | Description |
isIndeterminate | boolean | — | Whether presentation is indeterminate when progress isn't known. |
formatOptions | Intl.NumberFormatOptions | {style: 'percent'} | The display format of the value label. |
valueLabel | ReactNode | — | The content to display as the value's label (e.g. 1 of 4). |
value | number | 0 | The current value (controlled). |
minValue | number | 0 | The smallest value allowed for the input. |
maxValue | number | 100 | The largest value allowed for the input. |
children | ReactNode | (
(values: ProgressBarRenderProps
)) => ReactNode | — | The children of the component. A function may be provided to alter the children based on component state. |
className | string | (
(values: ProgressBarRenderProps
)) => string | — | The CSS className for the element. A function may be provided to compute the class based on component state. |
style | CSSProperties | (
(values: ProgressBarRenderProps
)) => CSSProperties | — | The inline style for the element. A function may be provided to compute the style based on component state. |
Layout
Name | Type | Default | Description |
slot | string | — | A slot name for the component. Slots allow the component to receive props from a parent component. |
Accessibility
Name | Type | Default | Description |
id | string | — | The element's unique identifier. See MDN. |
aria-label | string | — | Defines a string value that labels the current element. |
aria-labelledby | string | — | Identifies the element (or elements) that labels the current element. |
aria-describedby | string | — | Identifies the element (or elements) that describes the object. |
aria-details | string | — | Identifies the element (or elements) that provide a detailed, extended description for the object. |
Styling#
React Aria components can be styled in many ways, including using CSS classes, inline styles, utility classes (e.g. Tailwind), CSS-in-JS (e.g. Styled Components), etc. By default, all components include a builtin className
attribute which can be targeted using CSS selectors. These follow the react-aria-ComponentName
naming convention.
.react-aria-ProgressBar {
/* ... */
}
.react-aria-ProgressBar {
/* ... */
}
.react-aria-ProgressBar {
/* ... */
}
A custom className
can also be specified on any component. This overrides the default className
provided by React Aria with your own.
<ProgressBar className="my-progressbar">
{/* ... */}
</ProgressBar>
<ProgressBar className="my-progressbar">
{/* ... */}
</ProgressBar>
<ProgressBar className="my-progressbar">
{/* ... */}
</ProgressBar>
The className
and style
props also accept functions which receive states for styling. This lets you dynamically determine the classes or styles to apply, which is useful when using utility CSS libraries like Tailwind.
<ProgressBar
className={({ percentage }) =>
percentage > 50 ? 'bg-green-400' : 'bg-yellow-100'}
>
Item
</ProgressBar>
<ProgressBar
className={({ percentage }) =>
percentage > 50 ? 'bg-green-400' : 'bg-yellow-100'}
>
Item
</ProgressBar>
<ProgressBar
className={(
{ percentage }
) =>
percentage > 50
? 'bg-green-400'
: 'bg-yellow-100'}
>
Item
</ProgressBar>
The selectors and render props for each component used in a ProgressBar
are documented below.
ProgressBar#
A ProgressBar
can be targeted with the .react-aria-ProgressBar
CSS selector, or by overriding with a custom className
. It supports the following states and render props:
Name | CSS Selector | Description |
isIndeterminate | :not([aria-valuenow]) | Whether the progress bar is indeterminate. |
Label#
A Label
can be targeted with the .react-aria-Label
CSS selector, or by overriding with a custom className
.
Circular#
Progress bars may also be represented using a circular visualization rather than a line. This is often used to represent indeterminate operations, but may also be used for determinate progress indicators when space is limited. The following example shows a progress bar visualized as a circular spinner using SVG.
let center = 16;
let strokeWidth = 4;
let r = 16 - strokeWidth;
let c = 2 * r * Math.PI;
<ProgressBar aria-label="Loading…" value={60}>
{({ percentage }) => (
<>
<svg
width={64}
height={64}
viewBox="0 0 32 32"
fill="none"
strokeWidth={strokeWidth}
>
<circle
cx={center}
cy={center}
r={r - (strokeWidth / 2 - 0.25)}
stroke="var(--circle-color)"
strokeWidth={0.5}
/>
<circle
cx={center}
cy={center}
r={r + (strokeWidth / 2 - 0.25)}
stroke="var(--circle-color)"
strokeWidth={0.5}
/>
<circle
cx={center}
cy={center}
r={r}
stroke="var(--fill-color)"
strokeDasharray={` `}
strokeDashoffset={c - percentage / 100 * c}
strokeLinecap="round"
transform="rotate(-90 16 16)"
/>
</svg>
</>
)}
</ProgressBar>
let center = 16;
let strokeWidth = 4;
let r = 16 - strokeWidth;
let c = 2 * r * Math.PI;
<ProgressBar aria-label="Loading…" value={60}>
{({ percentage }) => (
<>
<svg
width={64}
height={64}
viewBox="0 0 32 32"
fill="none"
strokeWidth={strokeWidth}
>
<circle
cx={center}
cy={center}
r={r - (strokeWidth / 2 - 0.25)}
stroke="var(--circle-color)"
strokeWidth={0.5}
/>
<circle
cx={center}
cy={center}
r={r + (strokeWidth / 2 - 0.25)}
stroke="var(--circle-color)"
strokeWidth={0.5}
/>
<circle
cx={center}
cy={center}
r={r}
stroke="var(--fill-color)"
strokeDasharray={` `}
strokeDashoffset={c - percentage / 100 * c}
strokeLinecap="round"
transform="rotate(-90 16 16)"
/>
</svg>
</>
)}
</ProgressBar>
let center = 16;
let strokeWidth = 4;
let r = 16 - strokeWidth;
let c = 2 * r * Math.PI;
<ProgressBar
aria-label="Loading…"
value={60}
>
{(
{ percentage }
) => (
<>
<svg
width={64}
height={64}
viewBox="0 0 32 32"
fill="none"
strokeWidth={strokeWidth}
>
<circle
cx={center}
cy={center}
r={r -
(strokeWidth /
2 -
0.25)}
stroke="var(--circle-color)"
strokeWidth={0.5}
/>
<circle
cx={center}
cy={center}
r={r +
(strokeWidth /
2 -
0.25)}
stroke="var(--circle-color)"
strokeWidth={0.5}
/>
<circle
cx={center}
cy={center}
r={r}
stroke="var(--fill-color)"
strokeDasharray={` `}
strokeDashoffset={c -
percentage /
100 * c}
strokeLinecap="round"
transform="rotate(-90 16 16)"
/>
</svg>
</>
)}
</ProgressBar>
Reusable wrappers#
If you will use a ProgressBar in multiple places in your app, you can wrap all of the pieces into a reusable component. This way, the DOM structure, styling code, and other logic are defined in a single place and reused everywhere to ensure consistency.
This example wraps ProgressBar
and all of its children together into a single component which accepts a label
prop that is passed to the right place.
import type {ProgressBarProps} from 'react-aria-components';
interface MyProgressBarProps extends ProgressBarProps {
label?: string
}
function MyProgressBar({label, ...props}: MyProgressBarProps) {
return (
<ProgressBar {...props}>
{({percentage, valueText}) => <>
<Label>{label}</Label>
<span className="value">{valueText}</span>
<div className="bar">
<div className="fill" style={{width: percentage + '%'}} />
</div>
</>}
</ProgressBar>
);
}
<MyProgressBar label="Loading…" value={80} />
import type {ProgressBarProps} from 'react-aria-components';
interface MyProgressBarProps extends ProgressBarProps {
label?: string;
}
function MyProgressBar(
{ label, ...props }: MyProgressBarProps
) {
return (
<ProgressBar {...props}>
{({ percentage, valueText }) => (
<>
<Label>{label}</Label>
<span className="value">{valueText}</span>
<div className="bar">
<div
className="fill"
style={{ width: percentage + '%' }}
/>
</div>
</>
)}
</ProgressBar>
);
}
<MyProgressBar label="Loading…" value={80} />
import type {ProgressBarProps} from 'react-aria-components';
interface MyProgressBarProps
extends
ProgressBarProps {
label?: string;
}
function MyProgressBar(
{ label, ...props }:
MyProgressBarProps
) {
return (
<ProgressBar
{...props}
>
{(
{
percentage,
valueText
}
) => (
<>
<Label>
{label}
</Label>
<span className="value">
{valueText}
</span>
<div className="bar">
<div
className="fill"
style={{
width:
percentage +
'%'
}}
/>
</div>
</>
)}
</ProgressBar>
);
}
<MyProgressBar
label="Loading…"
value={80}
/>
Usage#
The following examples show how to use the MyProgressBar
component created in the above example.
Custom value scale#
By default, the value
prop represents the current percentage of progress, as the minimum and maximum values default to 0 and 100, respectively. Alternatively, a different scale can be used by setting the minValue
and maxValue
props.
<MyProgressBar
label="Loading…"
minValue={50}
maxValue={150}
value={100} />
<MyProgressBar
label="Loading…"
minValue={50}
maxValue={150}
value={100} />
<MyProgressBar
label="Loading…"
minValue={50}
maxValue={150}
value={100} />
Value formatting#
Values are formatted as a percentage by default, but this can be modified by using the formatOptions
prop to specify a different format.
formatOptions
is compatible with the option parameter of Intl.NumberFormat and is applied based on the current locale.
<MyProgressBar
label="Sending…"
formatOptions={{style: 'currency', currency: 'JPY'}}
value={60} />
<MyProgressBar
label="Sending…"
formatOptions={{style: 'currency', currency: 'JPY'}}
value={60} />
<MyProgressBar
label="Sending…"
formatOptions={{
style: 'currency',
currency: 'JPY'
}}
value={60}
/>
Custom value label#
The valueLabel
prop allows the formatted value to be replaced with a custom string.
<MyProgressBar
label="Feeding…"
valueLabel="30 of 100 dogs"
value={30} />
<MyProgressBar
label="Feeding…"
valueLabel="30 of 100 dogs"
value={30} />
<MyProgressBar
label="Feeding…"
valueLabel="30 of 100 dogs"
value={30}
/>
Indeterminate#
The isIndeterminate
prop can be used to represent an interdeterminate operation.
<MyProgressBar
aria-label="Loading…"
isIndeterminate />
<MyProgressBar
aria-label="Loading…"
isIndeterminate />
<MyProgressBar
aria-label="Loading…"
isIndeterminate />
Advanced customization#
Hooks#
If you need to customize things further, such as customizing the DOM structure, you can drop down to the lower level Hook-based API. See useProgressBar for more details.