alpha

Meter

A meter represents a quantity within a known range, or a fractional value.

installyarn add react-aria-components
version1.0.0-alpha.6
usageimport {Meter} from 'react-aria-components'

Example#


import {Meter, Label} from 'react-aria-components';

<Meter value={25}>
  {({percentage, valueText}) => <>
    <Label>Storage space</Label>
    <span className="value">{valueText}</span>
    <div className="bar">
      <div className="fill" style={{width: percentage + '%'}} />
    </div>
  </>}
</Meter>
import {Label, Meter} from 'react-aria-components';

<Meter value={25}>
  {({ percentage, valueText }) => (
    <>
      <Label>Storage space</Label>
      <span className="value">{valueText}</span>
      <div className="bar">
        <div
          className="fill"
          style={{ width: percentage + '%' }}
        />
      </div>
    </>
  )}
</Meter>
import {
  Label,
  Meter
} from 'react-aria-components';

<Meter value={25}>
  {(
    {
      percentage,
      valueText
    }
  ) => (
    <>
      <Label>
        Storage space
      </Label>
      <span className="value">
        {valueText}
      </span>
      <div className="bar">
        <div
          className="fill"
          style={{
            width:
              percentage +
              '%'
          }}
        />
      </div>
    </>
  )}
</Meter>
Show CSS
.react-aria-Meter {
  --bar-color: var(--spectrum-global-color-gray-500);
  --fill-color: forestgreen;
  --text-color: var(--spectrum-alias-text-color);

  display: grid;
  grid-template-areas: "label value"
                       "bar bar";
  grid-template-columns: 1fr auto;
  gap: 4px;
  width: 250px;
  color: var(--text-color);

  .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;
  }

  .fill {
    background: var(--fill-color);
    height: 100%;
  }
}

@media (forced-colors: active) {
  .react-aria-Meter {
    --bar-color: ButtonFace;
    --text-color: ButtonText;
    --fill-color: Highlight;

    .bar {
      border: 1px solid ButtonBorder;
      box-sizing: border-box;
    }
  }
}
.react-aria-Meter {
  --bar-color: var(--spectrum-global-color-gray-500);
  --fill-color: forestgreen;
  --text-color: var(--spectrum-alias-text-color);

  display: grid;
  grid-template-areas: "label value"
                       "bar bar";
  grid-template-columns: 1fr auto;
  gap: 4px;
  width: 250px;
  color: var(--text-color);

  .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;
  }

  .fill {
    background: var(--fill-color);
    height: 100%;
  }
}

@media (forced-colors: active) {
  .react-aria-Meter {
    --bar-color: ButtonFace;
    --text-color: ButtonText;
    --fill-color: Highlight;

    .bar {
      border: 1px solid ButtonBorder;
      box-sizing: border-box;
    }
  }
}
.react-aria-Meter {
  --bar-color: var(--spectrum-global-color-gray-500);
  --fill-color: forestgreen;
  --text-color: var(--spectrum-alias-text-color);

  display: grid;
  grid-template-areas: "label value"
                       "bar bar";
  grid-template-columns: 1fr auto;
  gap: 4px;
  width: 250px;
  color: var(--text-color);

  .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;
  }

  .fill {
    background: var(--fill-color);
    height: 100%;
  }
}

@media (forced-colors: active) {
  .react-aria-Meter {
    --bar-color: ButtonFace;
    --text-color: ButtonText;
    --fill-color: Highlight;

    .bar {
      border: 1px solid ButtonBorder;
      box-sizing: border-box;
    }
  }
}

Features#


The <meter> HTML element can be used to build a meter, however it is very difficult to style cross browser. Meter helps achieve accessible meters that can be styled as needed.

  • Accessible – Follows the ARIA meter pattern, with fallback to progressbar where unsupported. A nested label is automatically associated with the meter semantically.
  • International – The value is formatted as a percentage or custom format according to the user's locale.

Note: Meters are similar to progress bars, but represent a quantity as opposed to progress over time. See ProgressBar for more details about progress bars.

Anatomy#


ValueLabelTasks completed4 of 5TrackFill

Meters consist of a track element showing the full value in a range, a fill element showing the current value, a label, and an optional value label. The track and bar elements represent the value visually, while a wrapper element represents the meter to assistive technology using the meter 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 Meter uses the following components, which may also be used standalone or reused in other components.

Label
A label provides context for an element.

Props#


NameTypeDefaultDescription
formatOptionsIntl.NumberFormatOptions{style: 'percent'}The display format of the value label.
valueLabelReactNodeThe content to display as the value's label (e.g. 1 of 4).
valuenumber0The current value (controlled).
minValuenumber0The smallest value allowed for the input.
maxValuenumber100The largest value allowed for the input.
childrenReactNode( (values: MeterRenderProps )) => ReactNodeThe children of the component. A function may be provided to alter the children based on component state.
classNamestring( (values: MeterRenderProps )) => stringThe CSS className for the element. A function may be provided to compute the class based on component state.
styleCSSProperties( (values: MeterRenderProps )) => CSSPropertiesThe inline style for the element. A function may be provided to compute the style based on component state.
Layout
NameTypeDefaultDescription
slotstringA slot name for the component. Slots allow the component to receive props from a parent component.
Accessibility
NameTypeDefaultDescription
idstringThe element's unique identifier. See MDN.
aria-labelstringDefines a string value that labels the current element.
aria-labelledbystringIdentifies the element (or elements) that labels the current element.
aria-describedbystringIdentifies the element (or elements) that describes the object.
aria-detailsstringIdentifies 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-Meter {
  /* ... */
}
.react-aria-Meter {
  /* ... */
}
.react-aria-Meter {
  /* ... */
}

A custom className can also be specified on any component. This overrides the default className provided by React Aria with your own.

<Meter className="my-meter">
  {/* ... */}
</Meter>
<Meter className="my-meter">
  {/* ... */}
</Meter>
<Meter className="my-meter">
  {/* ... */}
</Meter>

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.

<Meter
  className={({ percentage }) =>
    percentage > 50 ? 'bg-green-400' : 'bg-yellow-100'}
>
  Item
</Meter>
<Meter
  className={({ percentage }) =>
    percentage > 50 ? 'bg-green-400' : 'bg-yellow-100'}
>
  Item
</Meter>
<Meter
  className={(
    { percentage }
  ) =>
    percentage > 50
      ? 'bg-green-400'
      : 'bg-yellow-100'}
>
  Item
</Meter>

The selectors and render props for each component used in a Meter are documented below.

Meter#

A Meter can be targeted with the .react-aria-Meter CSS selector, or by overriding with a custom className. It supports the following states and render props:

NameCSS SelectorDescription
percentageThe value as a percentage between the minimum and maximum.
valueText[aria-valuetext]A formatted version of the value.

Label#

A Label can be targeted with the .react-aria-Label CSS selector, or by adding a custom className.

Reusable wrappers#


If you will use a Meter 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 Meter and all of its children together into a single component which accepts a label prop that is passed to the right place.

import type {MeterProps} from 'react-aria-components';

interface MyMeterProps extends MeterProps {
  label?: string
}

function MyMeter({label, ...props}: MyMeterProps) {
  return (
    <Meter {...props}>
      {({percentage, valueText}) => <>
        <Label>{label}</Label>
        <span className="value">{valueText}</span>
        <div className="bar">
          <div className="fill" style={{width: percentage + '%'}} />
        </div>
      </>}
    </Meter>
  );
}

<MyMeter label="Storage space" value={80} />
import type {MeterProps} from 'react-aria-components';

interface MyMeterProps extends MeterProps {
  label?: string;
}

function MyMeter({ label, ...props }: MyMeterProps) {
  return (
    <Meter {...props}>
      {({ percentage, valueText }) => (
        <>
          <Label>{label}</Label>
          <span className="value">{valueText}</span>
          <div className="bar">
            <div
              className="fill"
              style={{ width: percentage + '%' }}
            />
          </div>
        </>
      )}
    </Meter>
  );
}

<MyMeter label="Storage space" value={80} />
import type {MeterProps} from 'react-aria-components';

interface MyMeterProps
  extends MeterProps {
  label?: string;
}

function MyMeter(
  { label, ...props }:
    MyMeterProps
) {
  return (
    <Meter {...props}>
      {(
        {
          percentage,
          valueText
        }
      ) => (
        <>
          <Label>
            {label}
          </Label>
          <span className="value">
            {valueText}
          </span>
          <div className="bar">
            <div
              className="fill"
              style={{
                width:
                  percentage +
                  '%'
              }}
            />
          </div>
        </>
      )}
    </Meter>
  );
}

<MyMeter
  label="Storage space"
  value={80}
/>

Usage#


The following examples show how to use the MyMeter 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.

<MyMeter
  label="Widgets Used"
  minValue={50}
  maxValue={150}
  value={100} />
<MyMeter
  label="Widgets Used"
  minValue={50}
  maxValue={150}
  value={100} />
<MyMeter
  label="Widgets Used"
  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.

<MyMeter
  label="Currency"
  formatOptions={{style: 'currency', currency: 'JPY'}}
  value={60} />
<MyMeter
  label="Currency"
  formatOptions={{style: 'currency', currency: 'JPY'}}
  value={60} />
<MyMeter
  label="Currency"
  formatOptions={{
    style: 'currency',
    currency: 'JPY'
  }}
  value={60}
/>

Custom value label#

The valueLabel prop allows the formatted value to be replaced with a custom string.

<MyMeter
  label="Space used"
  valueLabel="54 of 60GB"
  value={90} />
<MyMeter
  label="Space used"
  valueLabel="54 of 60GB"
  value={90} />
<MyMeter
  label="Space used"
  valueLabel="54 of 60GB"
  value={90} />

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 useMeter for more details.