日本語

Alert dialog

A modal dialog that asks the visitor to confirm or cancel before going on, and stays open until they answer.

Result

alert_dialog.rs
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",
        Project {
            status: String::new(),
        },
    )
    .child(
        div()
            .css(stack(rem(1)))
            .child(
                div().child(
                    DELETE
                        .trigger(ButtonVariant::Destructive, ButtonSize::Default)
                        .text("Delete"),
                ),
            )
            .child(p().role(Role::Status).bind_text(status))
            .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(status.set("Deleted the project."))
                                .text("Delete"),
                        ),
                ),
            ),
    )
}

Accessibility

An alert dialog is the same modal <dialog> as a Dialog, with role="alertdialog". A screen reader announces it as an alert dialog, which tells the visitor that it needs an answer, and reads its title and its description as a dialog's.

It has no close button in its corner, and a click on the backdrop leaves it open, so the visitor answers with one of the buttons in its content. Escape still cancels it, as the WAI-ARIA alert dialog pattern asks.

KeyWhat it does
Tab, Shift+TabMoves between the dialog's buttons and never reaches the page behind
EscapeCloses the dialog without running a button's statement, and returns focus to the element that opened it

Building an alert dialog

Dialog::alert_dialog is a method of the same Dialog<S> handle, and trigger, close, open, and dismiss work on it as on a Dialog:

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 asks the question, such as "Delete this project?", and description says what happens. content holds the answers, usually a dialog_footer with a close button that cancels and a close button whose on_click runs the action. The action's button carries the action's name, such as "Delete", where "OK" would make the visitor read the question again.

The HTML

The example at the end of this page renders this dialog, with the classes left out. Each … is an id made from the dialog ref's name. Unlike a dialog's, it has role="alertdialog", no closedby, and no button after the content:

<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>

What fails to compile

An alert dialog has no corner button, so alert_dialog takes no DialogText. Passing one, as to Dialog::dialog, is E0061, wrong number of arguments:

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(),
);

The handle is the same Dialog<S>, so a NodeRef in place of the DialogRef is E0308 here too, as the Dialog page shows.

What is not checked

Nothing checks that content holds a button. An alert dialog without one can be left only with Escape, and a visitor without a keyboard cannot leave it at all. As with a dialog, one handle's alert_dialog placed twice makes Page::render panic.

Example: confirming a delete

Cancel closes the dialog, and Delete sets a signal before closing it:

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();

Where the action can be undone, a Toast after the action asks less of the visitor than a dialog before it. A dialog the visitor can dismiss is a Dialog.