Spinner
A turning ring that marks work whose length is not known, named for a screen reader by its label.
Result
use hypersig::html::{div, p};
use hypersig::style::pattern::hstack;
use hypersig::style::value::rem;
use hypersig::{Page, Signals, if_else};
use hypersig_ui::{ButtonSize, ButtonVariant, button, spinner};
use serde::{Deserialize, Serialize};
#[derive(Signals, Serialize, Deserialize)]
pub(super) struct Sync {
busy: bool,
}
pub(super) fn page() -> Page<Sync> {
let busy = Sync::busy();
Page::new(
"Sync",
Sync { busy: true },
)
.child(
div()
.css(hstack(rem(0.75)))
.child(
spinner("Syncing")
.show(busy),
)
.child(p().show(!busy).text("Up to date."))
.child(
button(ButtonVariant::Outline, ButtonSize::Sm)
.on_click(busy.toggle())
.bind_text(if_else(
busy,
"Stop",
"Sync",
)),
),
)
}Accessibility
The spinner is a role="status" named by its label through aria-label, and the ring inside it is hidden from assistive technology. A screen reader reads the label where it reaches the spinner. Whether it also reads the label as the spinner appears differs between screen readers, so say in text when the work finishes, such as in a Toast.
The ring turns once a second in the current text color, and four times slower for a visitor who asks for reduced motion. The spinner takes no keys.
Building a spinner
spinner takes the label a screen reader reads, such as "Saving", and returns a <span> that sits in a line of text. Show it while the work runs with El::show on a signal, or with El::show on a call's busy state:
pub fn spinner<S>(label: impl Into<String>) -> El<S>;The ring is 1rem wide and takes the color of the text around it, so a spinner inside a button follows the button's text color. Busy and failed calls covers a call's busy state.
The HTML
While busy is true, the example at the end of this page renders this spinner, with the classes left out. The paragraph that says the sync is done is in the HTML too, hidden while busy is true, and is left out here:
<span role="status" aria-label="Syncing">
<svg aria-hidden="true" …></svg>
</span>What fails to compile
A spinner has no text of its own, so its label is a required argument, and a spinner without one is E0061, wrong number of arguments. A screen reader would otherwise find a status with nothing to read:
use hypersig::Signals;
use hypersig_ui::spinner;
use serde::{Deserialize, Serialize};
#[derive(Signals, Serialize, Deserialize)]
struct Sync {}
// A status without a name, which a screen reader has nothing to read for.
let busy = spinner::<Sync>();What is not checked
Nothing checks that the spinner goes away. A spinner shown with no condition turns forever, and its status says the work is running after it has finished. An empty label compiles and leaves the status with no name.
Example: syncing
The spinner shows while busy is true, and a line of text takes its place when the work is done:
use hypersig::html::{div, p};
use hypersig::{Page, Signals};
use hypersig_ui::spinner;
use serde::{Deserialize, Serialize};
#[derive(Signals, Serialize, Deserialize)]
struct Sync {
busy: bool,
}
let busy = Sync::busy();
let html = Page::new("Sync", Sync { busy: true })
.child(
div()
.child(spinner("Syncing").show(busy))
.child(p().show(!busy).text("Up to date.")),
)
.render();Work whose share done is known shows a Progress bar, and content still loading can show a Skeleton. The Keyframes page of the Style section builds a spinner of its own from keyframes.