Data
Meter
A measurement in a known range, like disk usage. It tells you which region the value's in, so your design can show it.
- Element
<slab-meter>- Built on
<meter>
Example
Here's a styled version, <slab-styled-meter>. It's not
in the package — it's an example of what you can build with the
headless element. Copy it and make it yours at the
bottom of this page. Switch to "As it ships" to see the same markup
with the headless element and no CSS at all. That's your starting
point.
<div class="stack">
<slab-styled-meter label="Lot 12: on track" high="0.85" low="0.6" optimum="0" value="0.4"></slab-styled-meter>
<slab-styled-meter label="Lot 13: mist it" high="0.85" low="0.6" optimum="0" value="0.72"></slab-styled-meter>
<slab-styled-meter label="Lot 14: cover it now" high="0.85" low="0.6" optimum="0" value="0.94"></slab-styled-meter>
</div>
<div class="stack">
<slab-meter label="Lot 12: on track" high="0.85" low="0.6" optimum="0" value="0.4"></slab-meter>
<slab-meter label="Lot 13: mist it" high="0.85" low="0.6" optimum="0" value="0.72"></slab-meter>
<slab-meter label="Lot 14: cover it now" high="0.85" low="0.6" optimum="0" value="0.94"></slab-meter>
</div>
Usage
pnpm add slab-elements lit
import 'slab-elements/meter.js';
export function MyMeter() {
return (
<slab-meter label="Storage used" value={0.62}></slab-meter>
);
}
MyMeter.jsx React 19 or later, which sets props on custom elements as properties.
import 'slab-elements/meter.js';
import { html, LitElement } from 'lit';
import { customElement } from 'lit/decorators.js';
@customElement('my-meter')
export class MyMeter extends LitElement {
render() {
return html`
<slab-meter label="Storage used" value="0.62"></slab-meter>
`;
}
}
my-meter.ts Slab is built on Lit, so its elements are at home in Lit's templates.
<script setup>
import 'slab-elements/meter.js';
</script>
<template>
<slab-meter label="Storage used" :value="0.62"></slab-meter>
</template>
MyMeter.vue Let Vue's compiler know slab- tags are custom elements with isCustomElement.
<script>
import 'slab-elements/meter.js';
</script>
<slab-meter label="Storage used" value={0.62}></slab-meter>
MyMeter.svelte Svelte binds properties and events to custom elements like any other element.
import 'slab-elements/meter.js';
export const MyMeter = <template>
<slab-meter label="Storage used" value="0.62"></slab-meter>
</template>;
MyMeter.gjs Ember sets attributes, and a dynamic value like {{this.max}} sets the property when the element has one. {{on}} listens for any event by its name.
<script type="module">
import 'slab-elements/meter.js';
</script>
<slab-meter label="Storage used" value="0.62"></slab-meter>
index.html Any bundler (or an import map) takes care of the imports.
Screenreaders read the value as a bare number. When it means more than that, set formatValue to a function of value, such as (value) => `${value * 5} of 5 GB`, and they’ll read what it returns instead.
Behavior
A meter with no visual styles, around a native <meter>: a measurement in a known range, such as disk usage, rather than progress through a task, which is Progress.
min, max, low, high and optimum are passed through to it, and default the way a native <meter>'s do. The label and the <meter> are both in the shadow root, so a plain for reference names it.
A native <meter> colors itself by which region its value is in, and only Firefox lets a design select that. So it's worked out here the way browsers do it, and set as :state(optimum), :state(suboptimum) or :state(even-less-good).
Screenreaders read its value as a number. Set formatValue to a function of value that says what it means, such as "3 of 5 GB", which becomes the <meter>'s aria-valuetext.
Style it through its parts and those states.
API
<slab-meter>
| Name | Type | Default | Description |
|---|---|---|---|
label
Required
|
string |
Names it. It's the accessible name, and the visible label where there is one. | |
hide-label
|
boolean |
false |
Hides the label visually and leaves it as the accessible name, for a design that shows the label some other way. |
high
|
number |
Where the high region starts. max when unset. |
|
low
|
number |
Where the low region ends. min when unset. |
|
max
|
number |
1 when unset, as on a native <meter>. |
|
min
|
number |
0 when unset, as on a native <meter>. |
|
optimum
|
number |
The best value, which decides which region is the optimum one. Halfway between min and max when unset. |
|
value
|
number |
0 |
The measurement, between min and max. |
| Name | Type | Description |
|---|---|---|
formatValue
|
(value: number) => string | undefined |
What screenreaders read for the value, such as "3 of 5 GB" rather than "3". The value itself when unset. |
| Name | Description |
|---|---|
label
|
The label |
meter
|
The meter |
| Name | Description |
|---|---|
:state(even-less-good)
|
While its value is in the region furthest from the optimum one. |
:state(optimum)
|
While its value is in the optimum region, as a native <meter> works it out. |
:state(suboptimum)
|
While its value is in a region next to the optimum one. |
Copy and own it
<slab-styled-meter> isn't in the package. It's an example —
copy it into your project and change whatever you want. Keyboard,
focus, ARIA, forms, and events all come from the headless element,
and the styles only reach in through ::part() and
:state(), same as yours would.
This one styles its native control's own pieces, like a
range input's thumb or a meter's bar. Browsers only let
those be styled from inside the element's shadow root, not
through ::part(), so it can't be written as
plain CSS. Copy the Lit files instead. They're a custom
element like any other, so they work in React as they are.
Under the hood, it's just the headless element, subclassed,
with styles set. That's it. To use it, copy
these files, swap the tag in @customElement()
for one of your own, and import it.
tokens.styles.ts holds the colors and fonts
every styled example shares, so you only need it once.
meter.ts
import { customElement } from 'lit/decorators.js';
import Meter from 'slab-elements/meter.js';
import styles from './meter.styles.js';
import tokens from './tokens.styles.js';
declare global {
interface HTMLElementTagNameMap {
'slab-styled-meter': StyledMeter;
}
}
/**
* Meter, styled: Progress's bar, solid rather than in bricks. Ink while the
* value is in the optimum region, ember outside it, and hatched deep ember
* when it's furthest from it.
*/
@customElement('slab-styled-meter')
export default class StyledMeter extends Meter {
static override styles = [tokens, styles];
}
meter.styles.ts
import { css } from 'lit';
/*
Chromium and WebKit draw each region's value with a pseudo-element of its
own. Firefox has one, which takes its color from Meter's states.
*/
export default css`
:host {
color: var(--ink);
display: grid;
font-family: var(--font-body);
gap: 0.5rem;
}
[part~='label'] {
font-family: var(--font-display);
font-size: 1.0625rem;
font-weight: 600;
}
[part~='meter'] {
appearance: none;
background: var(--paper);
block-size: 1.375rem;
border: var(--border);
box-sizing: border-box;
inline-size: 100%;
padding: 2px;
/*
The bar keeps its own height otherwise, a line or so, rather than
filling the meter's.
*/
&::-webkit-meter-inner-element {
block-size: 100%;
display: flex;
}
&::-webkit-meter-bar {
background: transparent;
border: 0;
border-radius: 0;
flex-grow: 1;
}
&::-webkit-meter-optimum-value,
&::-webkit-meter-suboptimum-value,
&::-webkit-meter-even-less-good-value {
block-size: 100%;
}
&::-webkit-meter-optimum-value {
background: var(--ink);
}
&::-webkit-meter-suboptimum-value {
background: var(--ember);
}
&::-webkit-meter-even-less-good-value {
background: repeating-linear-gradient(
-45deg,
var(--ember-deep) 0 0.375rem,
var(--ember) 0.375rem 0.75rem
);
}
&::-moz-meter-bar {
background: var(--ink);
}
}
:host(:state(suboptimum)) [part~='meter']::-moz-meter-bar {
background: var(--ember);
}
:host(:state(even-less-good)) [part~='meter']::-moz-meter-bar {
background: repeating-linear-gradient(
-45deg,
var(--ember-deep) 0 0.375rem,
var(--ember) 0.375rem 0.75rem
);
}
`;
tokens.styles.ts
import { css } from 'lit';
/*
The styled components' palette and type, taken from the logo: ink for the
courses, ember for the base, paper behind them.
Each is a short alias for a "--slab-styled-*" property, with the default as
its fallback. Retheming is "--slab-styled-ember" and the rest on ":root",
which every shadow root inherits. Were the defaults declared under those
names here instead, each host's own declaration would shadow the page's.
Each default is "light-dark()", so the components follow the page's
"color-scheme": "dark" for dark, "light" for light, and "light dark" to
follow the operating system. Ink and paper trade places in the dark, so
ink is always the text and paper always what's behind it. Contrast is given
as light, then dark.
*/
export default css`
:host {
/* Secondary text. 6.8:1 and 6.9:1 on paper. */
--ash: var(--slab-styled-ash, light-dark(#5e5a55, #a8a19a));
--border: 2px solid var(--ink);
/* Behind disabled and read-only controls, and hovered rows. */
--chalk: var(--slab-styled-chalk, light-dark(#f4f1ec, #2b2926));
/*
The logo's orange, brighter in the dark. 4.25:1 and 5.2:1 on paper is
enough for borders, fills and large text, but not always for small text,
which takes "--ember-deep".
*/
--ember: var(--slab-styled-ember, light-dark(#d9492a, #ec5f3b));
/*
Orange for small text on paper, and for paper text on it. 5.7:1 and
7.4:1.
*/
--ember-deep: var(--slab-styled-ember-deep, light-dark(#b3381a, #ff8566));
--font-body: var(
--slab-styled-font-body,
'IBM Plex Sans',
system-ui,
sans-serif
);
--font-display: var(
--slab-styled-font-display,
'Zilla Slab',
rockwell,
'Roboto Slab',
serif
);
--font-mono: var(
--slab-styled-font-mono,
'IBM Plex Mono',
ui-monospace,
monospace
);
--ink: var(--slab-styled-ink, light-dark(#1c1c1c, #f4f1ec));
--paper: var(--slab-styled-paper, light-dark(#fff, #1a1918));
/* The hard, offset shadow a raised surface casts. */
--shadow: 4px 4px 0 var(--ink);
}
/*
Forced colors, such as Windows' contrast themes, replace every color that
isn't a system color, so ember fills and paper marks all become the same
background, and a checked Checkbox looks like an unchecked one. The palette
becomes the user's system colors instead, which are kept. Ember is the
highlight, the color of what's selected, and ember-deep, being for text and
for paper text on it, is the text color.
*/
@media (forced-colors: active) {
:host {
--ash: GrayText;
--chalk: Canvas;
--ember: Highlight;
--ember-deep: CanvasText;
--ink: CanvasText;
--paper: Canvas;
}
}
`;
This one styles its native control's own pieces, like a
range input's thumb or a meter's bar. Browsers only let
those be styled from inside the element's shadow root, not
through ::part(), so it can't be written as
plain CSS. Copy the Lit files instead. They're a custom
element like any other, so they work in Vue as they are.
This one styles its native control's own pieces, like a
range input's thumb or a meter's bar. Browsers only let
those be styled from inside the element's shadow root, not
through ::part(), so it can't be written as
plain CSS. Copy the Lit files instead. They're a custom
element like any other, so they work in Svelte as they are.
This one styles its native control's own pieces, like a
range input's thumb or a meter's bar. Browsers only let
those be styled from inside the element's shadow root, not
through ::part(), so it can't be written as
plain CSS. Copy the Lit files instead. They're a custom
element like any other, so they work in Ember as they are.