Meter
A measurement within its range, coloured by how near it sits to the best value.
Previews are drawn in the theme you made.
Edit itBasic #
import { Meter, MeterSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
import { stack } from '../../snippets/layout.js'
// A meter is a measurement within a known range, where Progress is work going
// on. `low`, `high` and `optimum` say which end is good: a battery is best
// full, a disk best nearly empty, so the same 91 reads well on one and badly
// on the other. The fill takes its colour from that judgment.
export default example({
title: 'Basic',
slots: MeterSlots,
recipe: Recipes.Meter,
view: ({ style, h }) =>
stack(h, [
Meter.view(
{ id: 'battery', label: 'Battery', value: 91, low: 20, high: 80, optimum: 100, style },
h,
),
Meter.view(
{
id: 'storage',
label: 'Storage used',
value: 91,
low: 20,
high: 80,
optimum: 0,
valueText: (value, max) => `${value} of ${max} GB`,
style,
},
h,
),
]),
})In your application: recipe goes to forSlots, and the body of view is the call. example is this site’s harness.
Levels #
import { Meter, MeterSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
import { stack } from '../../snippets/layout.js'
// The HTML meter's three regions: below `low`, between, and above `high`.
// A value in the optimum's region is `good`, one region away `fair`, two
// away `poor`; `Meter.levelOf` is that judgment, and the root carries it as
// `data-level` for a style of your own.
const signal = [12, 48, 86]
export default example({
title: 'Levels',
slots: MeterSlots,
recipe: Recipes.Meter,
view: ({ style, h }) =>
stack(
h,
signal.map(value =>
Meter.view(
{
id: `signal-${value}`,
label: `Signal ${value}% (${Meter.levelOf(value, { low: 30, high: 70, optimum: 100 })})`,
value,
low: 30,
high: 70,
optimum: 100,
style,
},
h,
),
),
),
})In your application: recipe goes to forSlots, and the body of view is the call. example is this site’s harness.
Vertical #
import { Meter, MeterSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
import { row } from '../../snippets/layout.js'
// `orientation: 'vertical'` stands the track upright, the fill growing from
// its foot: a tank, a level, an equalizer. The size is still the thickness.
const tanks = [
{ id: 'water', label: 'Water', value: 72 },
{ id: 'fuel', label: 'Fuel', value: 38 },
{ id: 'waste', label: 'Waste', value: 88 },
]
export default example({
title: 'Vertical',
slots: MeterSlots,
recipe: Recipes.Meter,
view: ({ style, h }) =>
row(
h,
tanks.map(({ id, label, value }) =>
Meter.view(
{
id,
label,
value,
low: 25,
high: 75,
optimum: id === 'waste' ? 0 : 100,
orientation: 'vertical',
style,
},
h,
),
),
),
})In your application: recipe goes to forSlots, and the body of view is the call. example is this site’s harness.
Segments #
import { Meter, MeterSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
import { row, stack } from '../../snippets/layout.js'
// `segments` cuts the track into equal steps, as a battery icon or a
// password's strength is drawn. It cuts across an upright track too.
export default example({
title: 'Segments',
slots: MeterSlots,
recipe: Recipes.Meter,
view: ({ style, h }) =>
row(h, [
stack(h, [
Meter.view(
{
id: 'strength',
label: 'Password strength',
value: 3,
max: 4,
low: 2,
high: 3,
optimum: 4,
valueText: 'Good',
segments: 4,
style,
},
h,
),
Meter.view(
{ id: 'cells', label: 'Charge', value: 40, optimum: 100, low: 20, segments: 5, style },
h,
),
]),
Meter.view(
{
id: 'volume',
label: 'Volume',
value: 60,
low: 80,
high: 90,
optimum: 0,
orientation: 'vertical',
segments: 10,
style,
},
h,
),
]),
})In your application: recipe goes to forSlots, and the body of view is the call. example is this site’s harness.
A size of your own #
import { Style } from 'foldkit-mixins'
import { Meter, MeterSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
// The shipped sizes stop at `lg`. `add` gives the recipe one more: the track
// reads its thickness from the custom property the sizes set, flat or upright.
const Meters = Recipes.Meter.add({
size: { xl: { root: Style.self({ '--_fk-meter-thick': '1.25rem' }) } },
})
export default example({
title: 'A size of your own',
slots: MeterSlots,
recipe: Meters,
pins: { size: 'xl' },
view: ({ style, h }) =>
Meter.view(
{ id: 'cpu', label: 'CPU load', value: 64, low: 50, high: 85, optimum: 0, style },
h,
),
})In your application: recipe goes to forSlots, and the body of view is the call. example is this site’s harness.
Usage
Each example above is a module of its own, shown whole and run as shown, over the theme module Create a theme writes. The page hands it the style picked above.