English

スイッチ

オンとオフを切り替えるスイッチで、bool のシグナルに結び付きます。role="switch" を付けたネイティブのチェックボックスです。

実行結果

checkbox.rs
use hypersig::html::{div, p};
use hypersig::style::pattern::stack;
use hypersig::style::value::rem;
use hypersig::{Page, Signals, if_else};
use hypersig_ui::{checkbox, switch};
use serde::{Deserialize, Serialize};

#[derive(Signals, Serialize, Deserialize)]
pub(super) struct Settings {
    terms: bool,
    notifications: bool,
}

pub(super) fn page() -> Page<Settings> {
    let notifications = Settings::notifications();
    let initial = Settings {
        terms: false,
        notifications: true,
    };
    Page::new(
        "設定",
        initial,
    )
    .child(
        div()
            .css(stack(rem(1)))
            .child(checkbox(
                "利用規約に同意する",
                Settings::terms(),
            ))
            .child(switch(
                "通知",
                notifications,
            ))
            .child(p().bind_text(if_else(
                notifications,
                "リリースのたびにメールが届きます。",
                "メールは届きません。",
            ))),
    )
}

アクセシビリティ

チェックボックスと同じく <label> の中のネイティブのチェックボックスで、role="switch" を持ちます。そのため、スクリーンリーダーは「チェックボックス、チェックなし」ではなく「Airplane mode、スイッチ、オフ」のように読み上げ、切り替えると「オン」や「オフ」を読み上げます。トラックとつまみは描画したもので、支援技術からは隠れていて、入力要素の状態に従います。

キー動作
Tabスイッチにフォーカスを移します。
Spaceオンとオフを切り替えます。

`switch`

switch は checkbox と同じく、テキストと bool のシグナルを受け取ります。

pub fn switch<S, R: Writable>(text: impl Into<String>, on: Sig<S, bool, R>) -> El<S>

シグナルが true のとき、switch("Airplane mode", Settings::airplane()) は、class を除くと次のように描画されます。2 つの空の <span> がトラックとつまみです。

<label>
  <span>
    <input type="checkbox" role="switch" checked="">
    <span aria-hidden="true"></span>
    <span aria-hidden="true"></span>
  </span>
  <span>Airplane mode</span>
</label>

スイッチはオンかオフなので、Sig<S, bool, R> を受け取ります。"on" や "off" を持つ文字列のような、ほかの型のシグナルは E0308 で、計算シグナルは E0277 でコンパイルできません。

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

#[derive(Signals, Serialize, Deserialize)]
struct Settings {
    // "on" or "off"
    airplane: String,
}

// A text signal, where a switch needs on or off.
let airplane = switch("Airplane mode", Settings::airplane());

型では検査しないこと

  • スイッチは、変更がすぐに反映されることを表します。送信ボタンを待つフォームの中にスイッチを置いても止められませんが、そこではチェックボックスのほうが動作を正しく伝えます。
  • スクリーンリーダーが状態を読み上げるので、テキストはオンでもオフでも同じにします。

例: 設定

use hypersig::html::{div, p};
use hypersig::{Page, Signals, if_else};
use hypersig_ui::switch;
use serde::{Deserialize, Serialize};

#[derive(Signals, Serialize, Deserialize)]
struct Settings {
    airplane: bool,
    notifications: bool,
}

let notifications = Settings::notifications();
let page = Page::new("Settings", Settings { airplane: false, notifications: true }).child(
    div()
        .child(switch("Airplane mode", Settings::airplane()))
        .child(switch("Notifications", notifications))
        .child(p().bind_text(if_else(
            notifications,
            "You get a notice for each new message.",
            "Notices are paused.",
        ))),
);

次に読むページ

switch_in_form はフォームに結び付けた入力欄で説明しています。設定ではなくツールのオンとオフには、押された状態を保つボタンであるトグルを使います。