Multiple-choice toggle group
A row of toggles, each bound to a bool signal of its own, that any number of can be on at once.
Result
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
The group is a <div> with role="group" and the name you give it, and each toggle is a toggle with aria-pressed. A screen reader announces the group's name as focus enters it, then each toggle as pressed or not pressed. The group is one Tab stop, and the arrow keys move between toggles without flipping them, as in the Text style group of the demo above.
| Key | What it does |
|---|---|
| Tab | Moves focus into the group, to its first toggle. |
| Left, Right | Moves to the previous or the next toggle, wrapping at either end, without flipping it. |
| Home, End | Moves to the first or the last toggle. |
| Enter, Space | Flips the toggle that has focus. |
`MultiToggleGroup`
MultiToggleGroup is a builder like ToggleGroup, but its new takes only the name, and each item or icon_item takes the bool signal its toggle flips:
impl<S> MultiToggleGroup<S> {
pub fn new(label: impl Into<String>) -> Self
pub fn item<R: Writable>(self, pressed: Sig<S, bool, R>, text: impl Into<String>) -> Self
pub fn icon_item<R: Writable>(self, pressed: Sig<S, bool, R>, label: impl Into<String>, icon: impl Into<Node<S>>) -> Self
pub fn render(self) -> El<S>
}Only the first toggle stays in the tab order, and the arrow keys reach the rest. With bold true and italic false, MultiToggleGroup::new("Style").item(Editor::bold(), "Bold").item(Editor::italic(), "Italic") renders as follows, without the class:
<div role="group" aria-label="Style">
<button type="button" tabindex="0" aria-pressed="true">Bold</button>
<button type="button" tabindex="-1" aria-pressed="false">Italic</button>
</div>Each toggle is pressed or not, so item takes a Sig<S, bool, R>, and a signal of another type fails to compile with E0308. A computed bool signal fails with E0277, as on Toggle:
use hypersig::Signals;
use hypersig_ui::MultiToggleGroup;
use serde::{Deserialize, Serialize};
#[derive(Signals, Serialize, Deserialize)]
struct Editor {
bold: bool,
size: i32,
}
// A number signal, which has no pressed and not pressed.
let style = MultiToggleGroup::new("Text style")
.item(Editor::bold(), "Bold")
.item(Editor::size(), "Size");What the types leave unchecked
- Tab always enters at the first toggle, wherever focus last was in the group.
- One signal given to two items gives two toggles that flip together, and a screen reader announces both.
- An empty name compiles, for the group and for an
icon_item, and leaves it without a name.
Example: bold, italic, and underline
use hypersig::html::{div, p};
use hypersig::style::Style;
use hypersig::style::value::{FontStyle, FontWeight, TextDecorationLine};
use hypersig::{Page, Signals};
use hypersig_ui::MultiToggleGroup;
use serde::{Deserialize, Serialize};
#[derive(Signals, Serialize, Deserialize)]
struct Note {
bold: bool,
italic: bool,
underline: bool,
}
let initial = Note { bold: true, italic: false, underline: false };
let page = Page::new("Note", initial).child(
div()
.child(
MultiToggleGroup::new("Text style")
.icon_item(Note::bold(), "Bold", "B")
.icon_item(Note::italic(), "Italic", "I")
.item(Note::underline(), "Underline"),
)
.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_when(
Style::new().text_decoration_line(TextDecorationLine::Underline),
Note::underline(),
)
.text("The quick brown fox jumps over the lazy dog."),
),
);Next
Toggle group keeps exactly one toggle on. Checkbox is the control for a set of choices inside a form.