日本語

Toggle

A button that is pressed or not, bound to a bool signal. A click flips the signal.

Result

toggle.rs
use hypersig::html::{div, p};
use hypersig::style::Style;
use hypersig::style::pattern::{stack, wrap};
use hypersig::style::value::{FontStyle, FontWeight, TextAlign, rem};
use hypersig::{Page, Signals};
use hypersig_ui::{MultiToggleGroup, ToggleGroup, toggle};
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Clone, Copy, PartialEq)]
pub(super) enum Align {
    Start,
    Center,
    End,
}

#[derive(Signals, Serialize, Deserialize)]
pub(super) struct Note {
    bold: bool,
    italic: bool,
    align: Align,
    pinned: bool,
}

pub(super) fn page() -> Page<Note> {
    let initial = Note {
        bold: false,
        italic: false,
        align: Align::Start,
        pinned: false,
    };
    Page::new(
        "Note",
        initial,
    )
    .child(
        div()
            .css(stack(rem(1)))
            .child(
                div()
                    .css(wrap(rem(0.75)))
                    .child(
                        MultiToggleGroup::new("Text style")
                        .icon_item(
                            Note::bold(),
                            "Bold",
                            "B",
                        )
                        .icon_item(
                            Note::italic(),
                            "Italic",
                            "I",
                        ),
                    )
                    .child(
                        ToggleGroup::new(
                            "Alignment",
                            Note::align(),
                        )
                        .item(
                            Align::Start,
                            "Start",
                        )
                        .item(
                            Align::Center,
                            "Center",
                        )
                        .item(
                            Align::End,
                            "End",
                        ),
                    )
                    .child(toggle(Note::pinned()).text("Pin")),
            )
            .child(
                p().css_when(
                    Style::new().font_weight(FontWeight::Number(700.0)),
                    Note::bold(),
                )
                .css_when(Style::new().font_style(FontStyle::Italic), Note::italic())
                .css_match(
                    Note::align(),
                    [
                        (Align::Start, Style::new().text_align(TextAlign::Start)),
                        (Align::Center, Style::new().text_align(TextAlign::Center)),
                        (Align::End, Style::new().text_align(TextAlign::End)),
                    ],
                )
                .text("The quick brown fox jumps over the lazy dog."),
            ),
    )
}

Accessibility

A toggle is a native <button> with aria-pressed, which follows the signal. A screen reader announces it as a toggle button and says whether it is pressed, such as "Pin, toggle button, not pressed" for the Pin toggle in the demo above, and announces the new state when it flips.

KeyWhat it does
TabMoves focus to the toggle.
Enter, SpaceFlips the toggle.

`toggle` and `icon_toggle`

toggle takes the bool signal it shows and flips. icon_toggle is the same toggle, square, with the name that aria-label gives it as its first argument, for a toggle that shows only an icon:

pub fn toggle<S, R: Writable>(pressed: Sig<S, bool, R>) -> El<S>
pub fn icon_toggle<S, R: Writable>(label: impl Into<String>, pressed: Sig<S, bool, R>) -> El<S>

A click sets the signal to its opposite, and aria-pressed and the toggle's colors follow. With bold true and italic false, toggle(Note::bold()).text("Bold") and icon_toggle("Italic", Note::italic()).text("I") render as follows, without the class:

<button type="button" aria-pressed="true">Bold</button>
<button type="button" aria-label="Italic" aria-pressed="false">I</button>

The toggle writes to its signal, so the signal's third type parameter has to be Writable. A computed signal is Computed, which the page derives from other signals, and a toggle bound to one fails to compile with E0277, a signal marked Computed cannot be written:

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

#[derive(Signals, Serialize, Deserialize)]
struct Inbox {
    unread: i32,
    #[signals(computed)]
    has_unread: bool,
}

// A computed signal, which the page derives and a click cannot set.
let filter = toggle(Inbox::has_unread()).text("Unread");

What the types leave unchecked

  • Keep the text the same in both states. A screen reader announces the state on its own, so a text that changes with it, such as "Mute" and "Unmute", reads as "Unmute, pressed" and leaves the meaning unclear.
  • An empty label for icon_toggle compiles and leaves the toggle with no name.
  • A toggle needs no id, so it works on a field of a list item, such as item.at(Task::done()) inside each.

Example: text style

use hypersig::html::{div, p};
use hypersig::style::Style;
use hypersig::style::value::{FontStyle, FontWeight};
use hypersig::{Page, Signals};
use hypersig_ui::{icon_toggle, toggle};
use serde::{Deserialize, Serialize};

#[derive(Signals, Serialize, Deserialize)]
struct Note {
    bold: bool,
    italic: bool,
}

let page = Page::new("Note", Note { bold: true, italic: false }).child(
    div()
        // The text stays "Bold" in both states; `aria-pressed` says which.
        .child(toggle(Note::bold()).text("Bold"))
        .child(icon_toggle("Italic", Note::italic()).text("I"))
        .child(
            p().css_when(Style::new().font_weight(FontWeight::Number(700.0)), Note::bold())
                .css_when(Style::new().font_style(FontStyle::Italic), Note::italic())
                .text("The quick brown fox jumps over the lazy dog."),
        ),
);

Next

Toggle group picks one value from a row of toggles, and Multiple-choice toggle group groups toggles that each flip their own signal. Switch is the on and off control for a setting.