日本語

Toggle group

A row of toggles of which exactly one is on, bound to a signal that holds the chosen value.

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

Exactly one toggle is on at a time, so the group follows the WAI-ARIA radio group pattern. The group has role="radiogroup" and the name you give it, and each toggle has role="radio" and aria-checked. A screen reader announces the Alignment group in the demo above as a radio group and each toggle as "Center, radio button, 2 of 3". The group is one Tab stop: Tab enters it at the toggle that is on, and the arrow keys move between toggles and turn on the one they reach.

KeyWhat it does
TabMoves focus into the group, to the toggle that is on.
Left, RightMoves to the previous or the next toggle, wrapping at either end, and turns it on.
Home, EndMoves to the first or the last toggle and turns it on.
Enter, SpaceTurns on the toggle that has focus.

`ToggleGroup`

ToggleGroup is a builder: new takes the group's name and the signal, each item or icon_item adds a toggle with the value it picks, and render returns the <div>. El::child takes the builder itself, so the render call is optional:

impl<S, T: Serialize + PartialEq> ToggleGroup<S, T> {
    pub fn new(label: impl Into<String>, selected: Sig<S, T>) -> Self
    pub fn item(self, value: T, text: impl Into<String>) -> Self
    pub fn icon_item(self, value: T, label: impl Into<String>, icon: impl Into<Node<S>>) -> Self
    pub fn render(self) -> El<S>
}

A toggle from icon_item is square and named by label through aria-label. With the signal holding Plan::Hobby, ToggleGroup::new("Plan", Billing::plan()).item(Plan::Hobby, "Hobby").item(Plan::Pro, "Pro") renders as follows, without the class:

<div role="radiogroup" aria-label="Plan">
  <button type="button" role="radio" aria-checked="true" tabindex="0">Hobby</button>
  <button type="button" role="radio" aria-checked="false" tabindex="-1">Pro</button>
</div>

Each item takes a value of the signal's type T, so a value of another type, such as text where the signal holds an enum, fails to compile with E0308:

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

#[derive(Serialize, Deserialize, Clone, Copy, PartialEq)]
enum Align {
    Left,
    Right,
}

#[derive(Signals, Serialize, Deserialize)]
struct Text {
    align: Align,
}

// A value written as text, where the signal holds an `Align`.
let align = ToggleGroup::new("Alignment", Text::align())
    .item(Align::Left, "Left")
    .item("right", "Right");

What the types leave unchecked

  • Nothing checks that every value of T has a toggle. While the signal holds a value no toggle has, no toggle is on, every toggle has tabindex="-1", and Tab skips the group, so the keyboard cannot reach it. Give the signal an initial value that has a toggle. Listbox takes its options from the variants instead.
  • Two items with one value are both on at once, and both are Tab stops.
  • An empty group name compiles, and a screen reader then announces the group without saying what it chooses.

Example: text alignment

use hypersig::html::{div, p};
use hypersig::style::Style;
use hypersig::style::value::TextAlign;
use hypersig::{Page, Signals};
use hypersig_ui::ToggleGroup;
use serde::{Deserialize, Serialize};

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

#[derive(Signals, Serialize, Deserialize)]
struct Note {
    align: Align,
}

let align = Note::align();
let page = Page::new("Note", Note { align: Align::Start }).child(
    div()
        .child(
            ToggleGroup::new("Alignment", align)
                .item(Align::Start, "Start")
                .item(Align::Center, "Center")
                // A toggle that shows an icon is named by its label.
                .icon_item(Align::End, "End", "⇥"),
        )
        .child(
            p().css_match(
                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."),
        ),
);

Next

Multiple-choice toggle group lets any number of toggles be on. Radio group picks one value in a form with native radio buttons. Keyboard patterns covers El::roving_focus, which moves focus within the group.