Wire a loader to real app state, from a wait to done or failed, a progress value, a pause, and an exit that fades the dots out.
Engine · 3 min read
A loader follows your app's state through its props. You never call a method on it. Change a prop, and the dots morph to the new state.
| App state | Props |
|---|---|
| Working, length unknown | A loop, such as pattern="spinner" |
| Working, with progress | pattern="progress" and value from 0 to 1 |
| Done | pattern="check" |
| Failed | pattern="cross" |
| On hold | state="paused" |
| Gone | active={false}, then unmount |
From a wait to a result
Switch the pattern when the work ends. The dots morph from the loop to the result. check and cross play one time and hold the last frame.
type Status = "pending" | "done" | "failed";
const PATTERN = { pending: "spinner", done: "check", failed: "cross" } as const;
const COLOR = {
pending: undefined,
done: "#16a34a",
failed: "var(--color-destructive)",
} as const;
export function SaveStatus({ status }: { status: Status }) {
return (
<DotMatrix
pattern={PATTERN[status]}
color={COLOR[status]}
lattice="circle"
size={20}
look={{ morph: "fly", grid: 7 }}
/>
);
}- A screen reader hears "Done" or "Something went wrong" once, when the pattern changes.
- Set
look.gridwhen one loader switches patterns. Patterns can pick different grids, and a fixed grid keeps each dot in its place during the morph. - Use your own theme tokens for the colors. shadcn themes have
--color-destructivebut no success color, so this example uses a plain green.undefinedkeeps the text color.
Show progress
progress, progressRing and voice read value. Pass a number from 0 to 1. Change it as often as you want: the dots blend to the new value.
<DotMatrix
pattern="progressRing"
lattice="circle"
value={total === 0 ? 0 : loaded / total}
aria-label={`Uploading, ${Math.round((loaded / total) * 100)}%`}
/>When the upload ends, switch to check. The ring morphs into the mark.
The canvas and WebGL renderers blend the two nearest steps of the value. The DOM and SVG renderers show the nearest step. So they suit a progress better than a sound level.
Pause
state="paused" holds the current frame. state="running" goes on. Use it when work stops for a while, such as a queue that waits for the network.
<DotMatrix pattern="equalizer" state={online ? "running" : "paused"} />The loop also stops by itself when the loader is off screen or the tab is hidden. You do not need to pause it for that.
Exit and enter
active={false} morphs every dot dark, in 560ms. Keep the loader mounted during that time, then remove it.
export function Job({ busy }: { busy: boolean }) {
const [shown, setShown] = useState(true);
useEffect(() => {
if (busy) {
return;
}
// The exit morph takes 560ms: unmount after it.
const timer = setTimeout(() => setShown(false), 560);
return () => clearTimeout(timer);
}, [busy]);
return shown ? <DotMatrix pattern="orbit" active={busy} /> : null;
}enter does the opposite. The loader starts dark and morphs in the first time it comes on screen.
<DotMatrix pattern="radar" lattice="circle" enter />When a change does not morph
A morph needs a running loader on screen. In these cases the change shows at once:
look.morphis"none".- The loader is paused, off screen, or in a hidden tab.
- The change is to
size,look.dot,look.glow,look.floororrenderer. These mount a new layer.
Motion lists the morph kinds and the speed.