useProgressBar
Hook that normalizes progress values to a percentage range and provides ARIA attributes.
Usage#
import { useProgressBar } from '@primereact/headless/progressbar';const { rootProps, trackProps, state } = useProgressBar({ value: 50 });
<div {...rootProps}>
<div {...trackProps}>
<div style={{ width: state.computedValue + '%' }}></div>
</div>
</div>;useProgressBar normalizes a value within min/max bounds and returns spread-ready prop objects with ARIA attributes — see Primitive for a component-based API.
Features#
- Value normalization —
state.computedValueconverts any raw value in themin/maxrange into a 0-100 percentage - Formatted display text —
state.formattedValuerenders via a defaultpercentformatter or your custom one - ARIA attributes —
rootPropscarriesaria-valuenow,aria-valuemin, andaria-valuemax - Custom range — configurable
minandmaxwith a console error guard when bounds are invalid - Reactive state —
stateis safe to read from custom labels or indicators that sit outside the track
Working with callbacks#
Working with a custom range#
Set min and max to match your data model. The returned computedValue is always expressed as a percentage so your styles stay range-agnostic.
const { state } = useProgressBar({ value: 50, min: 0, max: 200 });
state.computedValue; // 25Custom formatted labels#
Pass a formatter to control the text rendered in state.formattedValue. Handy for fractional, ratio, or localized displays.
const { state } = useProgressBar({
value: 50,
formatter: (value) => `${value}/100`
});
state.formattedValue; // '50/100'Rendering a label alongside the track#
Read state.computedValue and state.formattedValue to render progress text wherever you need it — no extra calls required.
const { state } = useProgressBar({ value: 75 });
<span>{Math.round(state.computedValue ?? 0)}%</span>;
<span>{state.formattedValue}</span>;Styling with data attributes#
The prop objects include data-scope="progressbar" and data-part for styling.
| Scope | Part |
|---|---|
progressbar | root |
progressbar | track |
[data-scope='progressbar'][data-part='root'] {
display: flex;
flex-direction: column;
gap: 0.5rem;
}
[data-scope='progressbar'][data-part='track'] {
height: 1.25rem;
border-radius: 9999px;
overflow: hidden;
}API#
useProgressBar#
| Name | Type | Default |
|---|---|---|
value | number | — |
| Current value of the progress. | ||
max | number | — |
| Defines the mode of the progress | ||
min | number | — |
| Defines the mode of the progress | ||
formatter | (value: number) => string | — |
| Custom formatter function to format the display value | ||
Accessibility#
Value is exposed via aria-valuenow, aria-valuemin, and aria-valuemax for screen readers. See Primitive for full WAI-ARIA compliance details.