use leptos::prelude::*; use crate::components::toasts::toast::Toast; #[derive(Clone, Debug)] pub struct Toast { id: String, header: String, message: String, } impl Toast { pub fn new() -> Self { Toast { id: "".to_owned(), header: "".to_owned(), message: "".to_owned(), } } pub fn header(mut self, header: impl Into) -> Self { self.header = header.into(); self } pub fn msg(mut self, msg: impl Into) -> Self { self.message = msg.into(); self } pub fn id(mut self, id: impl Into) -> Self { self.id = id.into(); self } } #[derive(Clone, Copy)] pub struct ToastContext { messages: RwSignal>, } impl ToastContext { pub fn new() -> Self { Self { messages: RwSignal::new(vec![]), } } pub fn toast(&mut self, toast: Toast) { self.messages.update(|list| list.push(toast)); } } /// # Toast List Component /// Displays a list of toasts. They must be queued using the [`ToastContext`]. #[component] pub fn ToastList( children: Children, ) -> impl IntoView { // This context should be used wherever the application might need to use a toast. // That specific component should also be wrapped in a ToastList component, otherwise // this context won't exist. let context = use_context::() .unwrap_or_else(provide_toast_context); view! { {children()}
} } fn provide_toast_context() -> ToastContext { let context = ToastContext::new(); provide_context(context); context }