hypersig_ui/spinner.rs
1//! Spinners: a turning ring that marks work in progress.
2
3use hypersig::html::{El, Role, attr, span};
4use hypersig::style::value::{
5 AlignItems, AnimationIterationCount, AnimationTimingFunction, Display, ms, raw, rem,
6};
7use hypersig::style::{Keyframes, Media, Style};
8
9use crate::look::{self, icon};
10
11/// A turning ring named `label`, which marks work that has no known length.
12///
13/// It is a `role="status"` with `aria-label` set to `label`, and the ring
14/// inside it is hidden from assistive technology. A screen reader reads
15/// `label` where it reaches the spinner, and whether it also reads it as the
16/// spinner appears differs between screen readers, so say in text when the
17/// work finishes, such as in a [`Toast`](crate::Toast). The ring turns in
18/// the current text color, and four times slower for a visitor who asks for
19/// reduced motion. Use [`progress`](crate::progress) where the share done is
20/// known.
21///
22/// ```
23/// use hypersig::Signals;
24/// use hypersig_ui::spinner;
25/// use serde::{Deserialize, Serialize};
26///
27/// #[derive(Signals, Serialize, Deserialize)]
28/// struct Upload {
29/// busy: bool,
30/// }
31///
32/// let busy = spinner::<Upload>("Uploading").show(Upload::busy());
33/// assert!(busy.render().contains(r#"role="status""#));
34/// ```
35#[must_use]
36pub fn spinner<S>(label: impl Into<String>) -> El<S> {
37 let spin = Keyframes::new("hs-ui-spin").at(100, Style::new().rotate(raw("360deg")));
38 span()
39 .role(Role::Status)
40 .attr(attr::ARIA_LABEL, label)
41 .css(
42 Style::new()
43 .display(Display::InlineFlex)
44 .align_items(AlignItems::Center),
45 )
46 .child(
47 icon(look::icon::LOADER_CIRCLE, rem(1)).css(
48 Style::new()
49 .keyframes(&spin)
50 .animation_duration(ms(1000))
51 .animation_timing_function(AnimationTimingFunction::Linear)
52 .animation_iteration_count(AnimationIterationCount::Infinite)
53 .on(
54 Media::ReducedMotion,
55 Style::new().animation_duration(ms(4000)),
56 ),
57 ),
58 )
59}