日本語

Switch

An on and off switch bound to a bool signal. It is a native checkbox with role="switch".

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(super) struct Settings {
    terms: bool,
    notifications: bool,
}

pub(super) 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

It is the native checkbox of Checkbox, inside its <label>, with role="switch". A screen reader therefore says "Airplane mode, switch, off" rather than "checkbox, not checked", and announces "on" or "off" as it flips. The track and the thumb are drawn and hidden from assistive technology, and follow the input's state.

KeyWhat it does
TabMoves focus to the switch.
SpaceTurns it on or off.

`switch`

switch takes the text and the bool signal, as checkbox does:

pub fn switch<S, R: Writable>(text: impl Into<String>, on: Sig<S, bool, R>) -> El<S>

With the signal true, switch("Airplane mode", Settings::airplane()) renders as follows, without the class. The two empty <span> elements are the track and the thumb:

<label>
  <span>
    <input type="checkbox" role="switch" checked="">
    <span aria-hidden="true"></span>
    <span aria-hidden="true"></span>
  </span>
  <span>Airplane mode</span>
</label>

A switch is on or off, so it takes a Sig<S, bool, R>. A signal of another type, such as text holding "on" or "off", fails to compile with E0308, and a computed signal with E0277:

use hypersig::Signals;
use hypersig_ui::switch;
use serde::{Deserialize, Serialize};

#[derive(Signals, Serialize, Deserialize)]
struct Settings {
    // "on" or "off"
    airplane: String,
}

// A text signal, where a switch needs on or off.
let airplane = switch("Airplane mode", Settings::airplane());

What the types leave unchecked

  • A switch says that the change takes effect at once. Nothing stops a switch inside a form that waits for a submit button, where a checkbox says what happens.
  • Keep the text the same whether the switch is on or off, since the screen reader announces the state.

Example: settings

use hypersig::html::{div, p};
use hypersig::{Page, Signals, if_else};
use hypersig_ui::switch;
use serde::{Deserialize, Serialize};

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

let notifications = Settings::notifications();
let page = Page::new("Settings", Settings { airplane: false, notifications: true }).child(
    div()
        .child(switch("Airplane mode", Settings::airplane()))
        .child(switch("Notifications", notifications))
        .child(p().bind_text(if_else(
            notifications,
            "You get a notice for each new message.",
            "Notices are paused.",
        ))),
);

Next

Fields bound to a form has switch_in_form. Toggle is a button that stays pressed, for a tool rather than a setting.