キーボード操作
矢印キーで項目間を移るフォーカス、フォーカスをテキスト欄に置いたまま動かすリストボックスのアクティブな選択肢、ノード参照を通してフォーカスを動かすショートカットを説明します。
矢印キーで項目間を移動する
タブの一覧、メニュー、ツールバーは、Tab キーでは 1 か所として扱い、その中の項目間は矢印キーで移動します。El::roving_focus(fn roving_focus(self, orientation: Orientation) -> Self)は要素にこの動作を与えます。Orientation が使うキーを決めます。Horizontal は左右の矢印キーを使い、要素の direction が rtl なら左右を入れ替えます。Vertical は上下の矢印キーを使います。
- 項目とはこの要素の中にあり、
tabindex属性を持ち、無効でもaria-disabled="true"でもない要素です。この要素の中に、より近い位置でroving_focusを持つ要素があれば、項目はそちらに属します。 - 2 つの矢印キーで次と前の項目に移動し、端まで行くと反対の端に戻ります。Home と End で最初と最後の項目に移動します。
- ブラウザは処理したキーに対して
preventDefaultを呼びます。El::on_keyのハンドラがすでにそのキーを処理した場合と、Alt、Control、Meta が押されている間は、キーに何もしません。
動くのはフォーカスだけなので、各項目の tabindex は与えたまま変わりません。タブの一覧では、bind_attr で選択中のタブに tabindex="0"、ほかのタブに -1 を与えます。こうすると Tab キーで一覧に入ったときに選択中のタブにフォーカスが移ります。また、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])
}));項目の内容は型では検査されません。tabindex のない要素は項目にならないので、矢印キーで飛ばされます。すべての項目に tabindex="0" を与えると、Tab キーが項目ごとに止まります。
リストボックスのアクティブな選択肢
コンボボックスでは、フォーカスをテキスト欄に置いたまま、上下の矢印キーでリストボックスの選択肢を移動します。El::active_descendant(fn active_descendant(self, listbox: NodeRef<S>, active: Sig<S, String>) -> Self)はテキスト欄に付けます。listbox はリストボックスを指し、active はアクティブな選択肢の id を持ちます。
- 選択肢とは
listboxの中にあってrole="option"とidを持ち、aria-disabled="true"でもEl::showで隠れてもいない要素です。 - 下矢印キーで
activeを次に表示されている選択肢の id に、上矢印キーで前の選択肢の id にし、端まで行くと反対の端に戻ります。activeが表示中のどの選択肢も指していなければ、最初か最後の選択肢から始めます。 - Enter で
activeが指す選択肢をクリックします。ブラウザは処理したキーに対してpreventDefaultを呼び、新しい選択肢が見える位置までスクロールします。Alt、Control、Meta が押されている間は、キーに何もしません。 El::on_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"));このメソッドは aria-activedescendant も active に束縛するので、スクリーンリーダーは active が指す選択肢を読み上げます。ほかには何も変わらないので、その選択肢の見た目は css_when などで自分で指定してください。
active には id が入るので、ほかの型のシグナルを渡すと E0308(型の不一致)になります。
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());active の id が選択肢の id と合っているかは検査されません。どの選択肢も持たない id が active に入っていると、aria-activedescendant はどの要素も指さず、次に矢印キーを押したときに最初か最後の選択肢から始まります。
フォーカスを動かすショートカット
ノード参照の文とキーのハンドラを組み合わせると、検索欄に必要な操作を作れます。on_key、Key、Shortcut は イベント で説明します。下の例では、テキスト欄の外でページのどこかにフォーカスがあるときに / を押すと検索欄にフォーカスが移り、検索欄で Escape を押すと内容を消してフォーカスを外します。
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())),
);これらを使ったコンポーネント
hypersig_ui の タブ、メニュー、リストボックス、コンボボックス は、roving_focus と active_descendant に、それぞれのパターンに必要なロールと aria-* 属性を組み合わせて作っています。まずはこれらを使い、独自のパターンでキー操作が必要なときにこの 2 つのメソッドを使ってください。
次の JavaScript のライブラリ では、地図やエディタをページの中で動かす方法を説明します。