日本語

Skeleton

A pulsing block that stands in for content still loading, sized to the content it replaces.

Result

skeleton.rs
use hypersig::html::{attr, div, p};
use hypersig::style::Style;
use hypersig::style::pattern::stack;
use hypersig::style::value::rem;
use hypersig::{Page, Signals};
use hypersig_ui::{ButtonSize, ButtonVariant, button, skeleton};
use serde::{Deserialize, Serialize};

#[derive(Signals, Serialize, Deserialize)]
pub(super) struct Report {
    loading: bool,
}

pub(super) fn page() -> Page<Report> {
    let loading = Report::loading();
    Page::new(
        "Report",
        Report { loading: true },
    )
    .child(
        div()
            .css(stack(rem(1)).max_width(rem(24)))
            .child(
                div()
                    .bind_attr(attr::ARIA_BUSY, loading.to_text())
                    .css(stack(rem(0.5)))
                    .child(
                        div()
                            .css(stack(rem(0.5)))
                            .show(loading)
                            .child(skeleton().css(Style::new().height(rem(1)).width(rem(16))))
                            .child(skeleton().css(Style::new().height(rem(1)).width(rem(12)))),
                    )
                    .child(p().show(!loading).text("The report is ready.")),
            )
            .child(
                div().child(
                    button(ButtonVariant::Outline, ButtonSize::Sm)
                        .on_click(loading.toggle())
                        .text("Toggle loading"),
                ),
            ),
    )
}

Accessibility

A skeleton has aria-hidden="true", so a screen reader does not read it. Tell a screen reader that the content is loading somewhere else, such as with aria-busy="true" on the region the skeleton fills, as the demo does, or with a Spinner named "Loading".

The block pulses every two seconds. For a visitor who asks for reduced motion, it stays still. The skeleton takes no keys.

Building a skeleton

skeleton returns an empty block in token::accent with rounded corners. It has no size of its own, so give it one with El::css, close to the size of the content it stands in for:

pub fn skeleton<S>() -> El<S>;

Show the skeletons and the content with El::show on one signal, so that a patch on that signal swaps them.

The HTML

While loading is true, the example at the end of this page renders this HTML, with the classes left out. The paragraph that says the report is ready is in the HTML too, hidden until loading turns false, and is left out here:

<div aria-busy="true">
  <div>
    <div aria-hidden="true"></div>
    <div aria-hidden="true"></div>
  </div>
</div>

What fails to compile

skeleton takes no arguments, and its types stop no mistake. Its size goes through El::css like any element's, where a length without a unit is a compile error, as Lengths and units shows.

What is not checked

Nothing checks that a skeleton has a size. Without a height it is 0 pixels tall and does not show. Nothing checks either that something tells a screen reader the content is loading, so a page whose only sign of loading is skeletons says nothing to a visitor who cannot see them.

Example: a report that is loading

Two lines of skeleton show while loading is true, and the region's aria-busy follows the same signal:

use hypersig::html::{attr, div, p};
use hypersig::style::Style;
use hypersig::style::value::rem;
use hypersig::{Page, Signals};
use hypersig_ui::skeleton;
use serde::{Deserialize, Serialize};

#[derive(Signals, Serialize, Deserialize)]
struct Report {
    loading: bool,
}

let loading = Report::loading();
let html = Page::new("Report", Report { loading: true })
    .child(
        div()
            // The region says it is loading, since the skeleton is hidden.
            .bind_attr(attr::ARIA_BUSY, loading.to_text())
            .child(
                div()
                    .show(loading)
                    .child(skeleton().css(Style::new().height(rem(1)).width(rem(16))))
                    .child(skeleton().css(Style::new().height(rem(1)).width(rem(12)))),
            )
            .child(p().show(!loading).text("The report is ready.")),
    )
    .render();

Work whose progress is known shows a Progress bar, and work whose length is not known a Spinner.