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>- As Markdown
- meter.md
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
<script type="module">
import 'slab-elements/meter.js';
</script>
<slab-meter label="Storage used" value="0.62"></slab-meter>
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).
Style it through its parts and those states.
API
<slab-meter>
| Name | Type | Default | Description |
|---|---|---|---|
label
Required
|
string |
||
hide-label
|
boolean |
false |
|
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 |
|
version
|
string |
| Name | Description |
|---|---|
label
|
The label |
meter
|
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. Under the
hood, it's just the headless element, subclassed, with
styles set. That's it. 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.
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);
}
`;