useMeterGroup
Hook that manages meter value aggregation and percentage calculation within a defined range.
- Apps (16%)
- Messages (8%)
- Media (24%)
- System (10%)
basic-demo
Usage#
import { useMeterGroup } from '@primereact/headless/metergroup';
const { rootProps, percent, percentAsString } = useMeterGroup();
return <div {...rootProps}></div>;useMeterGroup tracks cumulative meter values and converts them to percentages within min/max bounds — see Primitive for a component-based API.
Features#
- Returns spread-ready
rootPropswithrole="meter", ARIA value attributes, anddata-orientation - Calculates percentage of individual meter values relative to the
min/maxrange - Tracks aggregated
totalPercentacross all meters viaupdateTotalPercent - Provides
percentandpercentAsStringmethods for rendering meter widths - Supports vertical and horizontal orientation
Behavior#
Default Range#
The default range is 0 to 100. A meter with value: 25 renders at 25% width.
const { percent, percentAsString } = useMeterGroup();
percent(25); // 25
percentAsString(25); // '25%'Custom Range#
Set min and max to change the value boundaries. Percentages are recalculated against the new range.
const { percent } = useMeterGroup({ max: 200 });
percent(50); // 25Orientation#
Set orientation to switch between horizontal and vertical layouts.
const { rootProps } = useMeterGroup({ orientation: 'vertical' });
rootProps['data-orientation']; // 'vertical'Tracking Total Percent#
Call updateTotalPercent to accumulate values across meters. The state.totalPercent reflects the running total.
const { state, updateTotalPercent, resetTotalPercent } = useMeterGroup();
updateTotalPercent(15);
updateTotalPercent(25);
state.totalPercent; // 40
resetTotalPercent();
state.totalPercent; // 0Custom Styling with Data Attributes#
The prop objects include data-scope="metergroup" and data-part for styling.
[data-scope='metergroup'][data-part='root'] {
display: flex;
flex-direction: column;
gap: 0.75rem;
}
[data-scope='metergroup'][data-orientation='vertical'] {
flex-direction: row;
}API#
useMeterGroup#
| Name | Type | Default |
|---|---|---|
min | number | 0 |
| Minimum boundary value. | ||
max | number | 100 |
| Maximum boundary value. | ||
orientation | "horizontal" | "vertical" | horizontal |
| Specifies the layout of the component. | ||
Accessibility#
See MeterGroup Primitive for WAI-ARIA compliance details and keyboard support.