Gauge
A value within a range, drawn as a bar or a ring in any of the SwiftUI gauge styles. Flat, as on iOS: no glass.
import { Gauge } from "@adamexu/glass";Bars and rings
automatic is linearCapacity, a thick bar with the label (children) over it. The accessory styles are the compact ones iOS draws in a widget: accessoryLinear is a line with a dot at the value, accessoryLinearCapacity a thin bar, and the circular ones are an open ring with a dot or a closed ring that fills.
<Gauge value={0.62} minimumValueLabel="0" maximumValueLabel="128 GB">Storage</Gauge>
<Gauge value={0.4} gaugeStyle="accessoryLinear" minimumValueLabel="0" maximumValueLabel="1" aria-label="Load" />
<Gauge value={0.75} gaugeStyle="accessoryLinearCapacity">Steps</Gauge>
<Gauge value={21} in={[10, 30]} gaugeStyle="accessoryCircular" currentValueLabel="21" minimumValueLabel="10" maximumValueLabel="30">
Temperature
</Gauge>
<Gauge value={0.8} gaugeStyle="accessoryCircularCapacity" tint="#0088FF" aria-label="Done">80</Gauge>Animated
With animated (a change under withAnimation) the default bar eases to a new value on a critically damped spring, on the compositor. Without it, and in the accessory styles, a change is a step. A tint array is a gradient whose stops span the whole bar, whatever the value.
const [level, setLevel] = useState(0.7);
<Gauge value={level} animated currentValueLabel={`${Math.round(level * 100)}%`}>
Battery
</Gauge>
<Gauge value={level} animated tint={["#FF2D55", "#34C759"]}>
Gradient
</Gauge>
<Button buttonStyle="plain" onClick={() => setLevel((g) => (g > 0.5 ? 0.2 : 0.9))}>
Change the gauge
</Button>In a List
<NavigationStack
root={
<NavigationPage title="Device">
<List>
<Section header="Gauge">
<Gauge value={level} animated={animated} currentValueLabel={`${Math.round(level * 100)}%`} minimumValueLabel="0" maximumValueLabel="1">
<Text>Battery</Text>
</Gauge>
<Gauge value={level} animated={animated} gaugeStyle="accessoryLinearCapacity">
<Text>Battery</Text>
</Gauge>
<Button
buttonStyle="plain"
onClick={() => {
setAnimated(true);
setLevel((g) => (g > 0.5 ? 0.2 : 0.9));
}}
>
Change the gauge (withAnimation)
</Button>
</Section>
</List>
</NavigationPage>
}
/>Props
| Prop | Type | Default | Description |
|---|---|---|---|
| value | number | - | Required. Clamped to the range. |
| in | readonly [number, number] | [0, 1] | The range. |
| gaugeStyle | "automatic" | "linearCapacity" | "accessoryLinear" | "accessoryLinearCapacity" | "accessoryCircular" | "accessoryCircularCapacity" | "automatic" | automatic is linearCapacity. accessoryLinearCapacity is drawn as a ProgressView bar. |
| children | ReactNode | - | The label: over the bar, in the open ring when there is no currentValueLabel, in the closed ring's center. |
| currentValueLabel | ReactNode | - | The value as text: under the bar, in the ring. |
| minimumValueLabel | ReactNode | - | The low end's label, beside the bar or under the ring. |
| maximumValueLabel | ReactNode | - | The high end's label. |
| tint | string | readonly string[] | - | A colour, or a gradient's colours across the whole default bar (the accessory styles take a single colour). |
| animated | boolean | false | The default bar eases to a new value on a spring. |
| disabled | boolean | false | Accepted and ignored: .disabled doesn't change a Gauge. |
| aria-label | string | - | On the role="meter" element; a string label names it otherwise. |
| className, style | string, CSSProperties | - | On the same element. |
SwiftUI
| SwiftUI | @adamexu/glass |
|---|---|
| Gauge(value:, in:) { label } currentValueLabel: { } | <Gauge value in currentValueLabel>label</Gauge> |
| minimumValueLabel: { } maximumValueLabel: { } | minimumValueLabel maximumValueLabel |
| .gaugeStyle(.accessoryCircular) | gaugeStyle="accessoryCircular" |
| .tint(Gradient(colors: [a, b])) | tint={[a, b]} |
| withAnimation { value = x } on a Gauge | animated |