#[non_exhaustive]pub struct Toast {
pub title: String,
pub body: String,
pub tone: ToastVariant,
/* private fields */
}Expand description
One notice in a Toaster: a title, an optional description, and an id.
The id tells toasts apart while the list changes, and nothing outside
this type sets it. On the server, Toast::new numbers each toast from
a counter of the process, so toasts made in one process have different
ids until 2³² of them have been made. In the browser, Toast::push and
Toast::push_expr give a toast one more than the highest id in the
list. A clone keeps its id, so a list that holds a toast and its clone
repeats one; the runtime then tells the two apart by position and writes
a warning to the console.
use hypersig::{Expr, Signals};
use hypersig_ui::{Toast, ToastVariant};
use serde::{Deserialize, Serialize};
#[derive(Signals, Serialize, Deserialize)]
struct App {
toasts: Vec<Toast>,
name: String,
}
// On the server, in the list a patch sends.
let failed = App::toasts().patch(vec![
Toast::new("Upload failed").variant(ToastVariant::Destructive),
]);
// In the browser, with fixed text.
let saved = Toast::new("Saved").description("Your changes are live.").push(App::toasts());
// In the browser, with text from a signal.
let renamed = Toast::push_expr(
App::toasts(),
Expr::lit(&Toast::new("Renamed")).with(Toast::body(), App::name().concat(" is the new name.")),
);Fields (Non-exhaustive)§
This struct is marked as non-exhaustive
Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.title: StringThe title, in bold.
body: StringThe line under the title, hidden while it is empty.
tone: ToastVariantHow the toast is drawn.
Implementations§
Source§impl Toast
impl Toast
Sourcepub fn new(title: impl Into<String>) -> Self
pub fn new(title: impl Into<String>) -> Self
A toast titled title, with an id no other toast of the process has.
Sourcepub fn description(self, text: impl Into<String>) -> Self
pub fn description(self, text: impl Into<String>) -> Self
Adds a line of text under the title.
Sourcepub fn variant(self, variant: ToastVariant) -> Self
pub fn variant(self, variant: ToastVariant) -> Self
Sets the look of the toast.
Sourcepub fn push<S>(self, toasts: Sig<S, Vec<Self>>) -> Stmt<S>
pub fn push<S>(self, toasts: Sig<S, Vec<Self>>) -> Stmt<S>
A statement that adds this toast to toasts in the browser.
Sourcepub fn push_expr<S>(
toasts: Sig<S, Vec<Self>>,
toast: impl IntoExpr<S, Self>,
) -> Stmt<S>
pub fn push_expr<S>( toasts: Sig<S, Vec<Self>>, toast: impl IntoExpr<S, Self>, ) -> Stmt<S>
A statement that adds toast, which the browser evaluates, to toasts.
Build toast from a literal toast with Expr::with on
Toast::title, Toast::body, or Toast::tone, so that its
text can come from a signal. Its id is replaced by one more than the
highest id in toasts.