アラートダイアログ
先に進む前に、訪問者に確認か取り消しを求めるモーダルダイアログです。訪問者が答えるまで閉じません。
実行結果
use hypersig::html::{Role, div, p};
use hypersig::style::pattern::stack;
use hypersig::style::value::rem;
use hypersig::{Page, Signals};
use hypersig_ui::{ButtonSize, ButtonVariant, Dialog, dialog_footer};
use serde::{Deserialize, Serialize};
#[derive(Signals, Serialize, Deserialize)]
pub(super) struct Project {
status: String,
}
hypersig::dialog_ref!(const DELETE_DIALOG: DialogRef<Project>);
const DELETE: Dialog<Project> = Dialog::new(DELETE_DIALOG);
pub(super) fn page() -> Page<Project> {
let status = Project::status();
Page::new(
"プロジェクト",
Project {
status: String::new(),
},
)
.child(
div()
.css(stack(rem(1)))
.child(
div().child(
DELETE
.trigger(ButtonVariant::Destructive, ButtonSize::Default)
.text("削除"),
),
)
.child(p().role(Role::Status).bind_text(status))
.child(
DELETE.alert_dialog(
"このプロジェクトを削除しますか?",
"プロジェクトとそのデプロイが削除されます。この操作は取り消せません。",
dialog_footer()
.child(
DELETE
.close(ButtonVariant::Outline, ButtonSize::Default)
.text("キャンセル"),
)
.child(
DELETE
.close(ButtonVariant::Destructive, ButtonSize::Default)
.on_click(status.set("プロジェクトを削除しました。"))
.text("削除"),
),
),
),
)
}アクセシビリティ
アラートダイアログは ダイアログ と同じモーダルの <dialog> で、role="alertdialog" を持ちます。スクリーンリーダーはアラートダイアログとして読み上げるので、訪問者は答えが必要なことが分かります。タイトルと説明文の読み上げ方はダイアログと同じです。
角の閉じるボタンはなく、背景をクリックしても閉じないので、訪問者は中身のボタンのどれかで答えます。WAI-ARIA の alert dialog パターンに従い、Escape では取り消せます。
| キー | 動作 |
|---|---|
| Tab、Shift+Tab | ダイアログのボタンの間を移り、後ろのページには移りません |
| Escape | ボタンの文を実行せずにダイアログを閉じ、ダイアログを開いた要素にフォーカスを戻します |
アラートダイアログを作る
Dialog::alert_dialog は同じ Dialog<S> のハンドルのメソッドで、trigger、close、open、dismiss は ダイアログ と同じように使えます。
impl<S> Dialog<S> {
pub fn alert_dialog(
self,
title: impl Into<String>,
description: impl Into<String>,
content: impl Into<Node<S>>,
) -> El<S>;
}title には「このプロジェクトを削除しますか?」のような問いを、description には何が起きるかを書きます。content には答えのボタンを入れます。ふつうは dialog_footer に、取り消す close のボタンと、on_click で操作を実行する close のボタンを並べます。操作のボタンには「削除」のように操作の名前を付けます。「OK」では、訪問者は問いを読み直すことになります。
出力される HTML
このページの最後の例は次のダイアログを出力します(クラスは省いています)。… はどれもダイアログ参照の名前から作った id です。ダイアログと違い、role="alertdialog" を持ち、closedby も中身のあとのボタンもありません。
<dialog id="…" aria-labelledby="…" aria-describedby="…" role="alertdialog">
<div>
<div>
<h2 id="…">Delete this project?</h2>
<p id="…">The project and its deployments are removed. This cannot be undone.</p>
</div>
<div>
<button type="button" commandfor="…" command="close">Cancel</button>
<button type="button" commandfor="…" command="close">Delete</button>
</div>
</div>
</dialog>コンパイルエラーになるもの
アラートダイアログには角のボタンがないので、alert_dialog は DialogText を受け取りません。Dialog::dialog と同じように渡すと、E0061(引数の数の誤り)になります。
use hypersig::{DialogRef, Signals};
use hypersig_ui::{Dialog, DialogText, dialog_footer};
use serde::{Deserialize, Serialize};
#[derive(Signals, Serialize, Deserialize)]
struct Project {}
hypersig::dialog_ref!(const DELETE_DIALOG: DialogRef<Project>);
const DELETE: Dialog<Project> = Dialog::new(DELETE_DIALOG);
// An alert dialog has no corner button to name.
let dialog = DELETE.alert_dialog(
"Delete this project?",
"This cannot be undone.",
dialog_footer(),
&DialogText::default(),
);ハンドルは同じ Dialog<S> なので、DialogRef の代わりに NodeRef を渡すと、ここでも E0308 になります。例は ダイアログ のページにあります。
検査されないこと
content にボタンがあるかどうかは検査されません。ボタンのないアラートダイアログは Escape でしか閉じられず、キーボードを使わない訪問者は閉じられません。ダイアログと同じく、1 つのハンドルの alert_dialog を 2 回置くと Page::render が panic します。
例: 削除を確認する
Cancel はダイアログを閉じ、Delete はシグナルを設定してから閉じます。
use hypersig::html::p;
use hypersig::{DialogRef, Page, Signals};
use hypersig_ui::{ButtonSize, ButtonVariant, Dialog, dialog_footer};
use serde::{Deserialize, Serialize};
#[derive(Signals, Serialize, Deserialize)]
struct Project {
deleted: bool,
}
hypersig::dialog_ref!(const DELETE_DIALOG: DialogRef<Project>);
const DELETE: Dialog<Project> = Dialog::new(DELETE_DIALOG);
let html = Page::new("Project", Project { deleted: false })
.child(DELETE.trigger(ButtonVariant::Destructive, ButtonSize::Default).text("Delete"))
.child(p().show(Project::deleted()).text("Deleted the project."))
.child(DELETE.alert_dialog(
"Delete this project?",
"The project and its deployments are removed. This cannot be undone.",
dialog_footer()
.child(DELETE.close(ButtonVariant::Outline, ButtonSize::Default).text("Cancel"))
.child(
DELETE
.close(ButtonVariant::Destructive, ButtonSize::Default)
.on_click(Project::deleted().set(true))
.text("Delete"),
),
))
.render();取り消せる操作なら、操作の前にダイアログで確認するより、操作のあとに トースト を出すほうが訪問者の手間が少なくなります。訪問者が自由に閉じられるダイアログには ダイアログ を使います。