Toast
Short notices shown together in a corner of the page, added from the browser or by the server.
Result
toast.rs
use hypersig::html::div;
use hypersig::style::pattern::wrap;
use hypersig::style::value::rem;
use hypersig::{Page, Signals};
use hypersig_ui::{ButtonSize, ButtonVariant, Toast, ToastVariant, Toaster, button};
use serde::{Deserialize, Serialize};
#[derive(Signals, Serialize, Deserialize)]
pub struct App {
toasts: Vec<Toast>,
}
pub fn page() -> Page<App> {
let toasts = App::toasts();
let initial = App { toasts: Vec::new() };
Page::new(
"App",
initial,
)
.child(
div()
.css(wrap(rem(0.5)))
.child(
button(ButtonVariant::Outline, ButtonSize::Default)
.on_click(
Toast::new("Event created")
.description("Sunday, December 3 at 9:00")
.push(toasts),
)
.text("Show a toast"),
)
.child(
button(ButtonVariant::Outline, ButtonSize::Default)
.on_click(
Toast::new("Upload failed")
.description("The file is larger than 10 MB.")
.variant(ToastVariant::Destructive)
.push(toasts),
)
.text("Show an error"),
)
.child(Toaster::new(toasts)),
)
}Accessibility
The toaster is a landmark region named "Notifications" holding an aria-live="polite" list, so a screen reader reads a toast when it is added, after what it is reading. Each toast has a button that removes it.
A toast stays until it is dismissed, unless Toaster::dismiss_after sets a time, because a visitor who reads slowly or uses a magnifier can miss a notice that is removed automatically (WCAG 2.2.1). The region is in the page rather than in the top layer, so an open modal dialog makes it inert until the dialog closes.
| Key | What it does |
|---|---|
| Tab, Enter | Reach a toast's Dismiss button and press it. |
API
| Item | Summary |
|---|---|
Toaster::new | A region that shows toasts, newest last. |
label | Names the region for assistive technology, in place of "Notifications". |
dismiss_after | Removes each toast delay after it appears. |
Toast::new | A toast titled title, with an id no other toast of the process has. |
description | Adds a line of text under the title. |
variant | Sets the look of the toast. |
push | A statement that adds this toast to toasts in the browser. |
Toast::push_expr | A statement that adds toast, which the browser evaluates, to toasts. |