Keyboard patterns
Focus that moves between items with the arrow keys, the active option of a listbox while focus stays in a text field, and shortcuts that move focus through node refs.
Arrow keys between items
A tab list, a menu, or a toolbar is one stop for Tab, and the arrow keys move between its items. El::roving_focus, fn roving_focus(self, orientation: Orientation) -> Self, gives an element that behavior. Orientation picks the keys: Horizontal uses left and right, swapped where the element's direction is rtl, and Vertical uses up and down.
- An item is an element inside this one that has a
tabindexattribute and is neither disabled noraria-disabled="true". Where a nearer element inside this one also hasroving_focus, the item belongs to that one. - The two arrow keys move to the next and the previous item, wrapping at either end. Home and End move to the first and the last.
- The browser calls
preventDefaultfor a key it acts on. It leaves a key alone when a handler fromEl::on_keyalready took it, or while Alt, Control, or Meta is held.
Only focus moves, so each item keeps the tabindex it is given. A tab list gives the selected tab tabindex="0" and the others -1 with bind_attr, so that Tab enters the list at the selected tab, and selects a tab when it gains focus with Event::Focus:
use hypersig::html::{Event, Orientation, Role, attr, button, div};
use hypersig::{Signals, if_else};
use serde::{Deserialize, Serialize};
#[derive(Signals, Serialize, Deserialize)]
struct Settings {
tab: i32,
}
let tab = Settings::tab();
let names = ["General", "Members", "Billing"];
let tabs = div::<Settings>()
.role(Role::TabList)
.roving_focus(Orientation::Horizontal)
.children((0..3).map(|i| {
button()
.role(Role::Tab)
// Tab enters the list at the selected tab.
.bind_attr(attr::TABINDEX, if_else(tab.equals(i), "0", "-1"))
.bind_attr(attr::ARIA_SELECTED, if_else(tab.equals(i), "true", "false"))
// An arrow key moves focus, and focus selects the tab.
.on(Event::Focus, tab.set(i))
.text(names[i as usize])
}));The types leave the items unchecked. An element without tabindex is not an item, so the arrow keys skip it, and giving every item tabindex="0" makes Tab stop at each of them.
The active option of a listbox
In a combobox, focus stays in the text field while the up and down arrows move through the options of a listbox. El::active_descendant, fn active_descendant(self, listbox: NodeRef<S>, active: Sig<S, String>) -> Self, goes on the field. listbox names the listbox, and active holds the id of the active option.
- An option is an element inside
listboxwithrole="option"and anid, which is notaria-disabled="true"and is not hidden byEl::show. - The down arrow sets
activeto the id of the next shown option, and the up arrow to the previous one, wrapping at either end. Whereactivenames no shown option, they start from the first or the last. - Enter clicks the option
activenames. The browser callspreventDefaultfor a key it acts on, scrolls the new option into view, and leaves a key alone while Alt, Control, or Meta is held. - It runs before the handlers of
El::on_key, which then still see the key.
use hypersig::html::{Id, Role, div, input};
use hypersig::{NodeRef, Signals};
use serde::{Deserialize, Serialize};
#[derive(Signals, Serialize, Deserialize)]
struct Search {
query: String,
// The id of the option the arrow keys reached.
active: String,
}
hypersig::node_ref!(const RESULTS: NodeRef<Search>);
const RUST: Id = Id::new("option-rust");
const ZIG: Id = Id::new("option-zig");
let field = input::<Search>()
.role(Role::Combobox)
.bind(Search::query())
.active_descendant(RESULTS, Search::active());
let results = div::<Search>()
.role(Role::ListBox)
.node_ref(RESULTS)
.child(div().role(Role::Option).id(RUST).text("Rust"))
.child(div().role(Role::Option).id(ZIG).text("Zig"));The method also binds aria-activedescendant to active, so a screen reader reads the option active names. Nothing else changes, so style that option yourself, for example with css_when.
active holds an id, so a signal of another type is E0308, mismatched types:
use hypersig::html::{Role, input};
use hypersig::{NodeRef, Signals};
use serde::{Deserialize, Serialize};
#[derive(Signals, Serialize, Deserialize)]
struct Search {
query: String,
// The position of the option, where the method takes its id.
active: i32,
}
hypersig::node_ref!(const RESULTS: NodeRef<Search>);
let field = input::<Search>()
.role(Role::Combobox)
.bind(Search::query())
.active_descendant(RESULTS, Search::active());The ids in active are not checked against the options. Where active holds an id that no option carries, aria-activedescendant names no element, and the next arrow key starts from the first or the last option.
Shortcuts that move focus
A node ref's statements and a key handler together make the patterns a search box needs. Events covers on_key, Key, and Shortcut. Below, / anywhere on the page outside a text field moves focus to the search field, and Escape in the field clears it and removes focus from it:
use hypersig::html::{Key, input};
use hypersig::{NodeRef, Page, Signals};
use serde::{Deserialize, Serialize};
#[derive(Signals, Serialize, Deserialize)]
struct Docs {
query: String,
}
hypersig::node_ref!(const SEARCH: NodeRef<Docs>);
let query = Docs::query();
let page = Page::new("Docs", Docs { query: String::new() })
// `/` anywhere on the page, outside a text field, moves focus to the search field.
.on_key(Key::Char('/'), SEARCH.focus())
.child(
input()
.node_ref(SEARCH)
.bind(query)
// Escape in the field clears it and gives focus back to the page.
.on_key(Key::Escape, query.set("").then(SEARCH.blur())),
);The components built on them
hypersig_ui builds its tabs, menu, listbox, and combobox with roving_focus and active_descendant, together with the roles and the aria-* attributes each pattern needs. Reach for those first, and for the two methods when a pattern of your own needs the keys.
Next, JavaScript libraries runs a map or an editor inside a page.