日本語

Checkbox and switch

A checkbox and an on and off switch, each bound to a bool signal.

Result

checkbox.rs
use hypersig::html::{div, p};
use hypersig::style::pattern::stack;
use hypersig::style::value::rem;
use hypersig::{Page, Signals, if_else};
use hypersig_ui::{checkbox, switch};
use serde::{Deserialize, Serialize};

#[derive(Signals, Serialize, Deserialize)]
pub struct Settings {
    terms: bool,
    notifications: bool,
}

pub fn page() -> Page<Settings> {
    let notifications = Settings::notifications();
    let initial = Settings {
        terms: false,
        notifications: true,
    };
    Page::new(
        "Settings",
        initial,
    )
    .child(
        div()
            .css(stack(rem(1)))
            .child(checkbox(
                "Accept the terms",
                Settings::terms(),
            ))
            .child(switch(
                "Notifications",
                notifications,
            ))
            .child(p().bind_text(if_else(
                notifications,
                "You will get an email for each release.",
                "You will not get emails.",
            ))),
    )
}

Accessibility

Both are a native <input type="checkbox"> inside the <label> that holds the text, so a click on the text toggles them and they need no id. Because they need no id, they can also be bound to a field of a list item.

The switch has role="switch", so a screen reader says "switch, on" rather than "checkbox, checked".

KeyWhat it does
SpaceToggles the checkbox or the switch.

API

ItemSummary
checkboxA checkbox labeled text, bound to checked.
checkbox_in_formA checkbox labeled text, bound to field of form.
switchAn on and off switch labeled text, bound to on.
switch_in_formAn on and off switch labeled text, bound to field of form.