Skip to main content

melt_ui/mobile/toast/
mod.rs

1use crate::utils::mount_style::mount_style;
2use leptos::*;
3use std::time::Duration;
4use web_sys::Element;
5
6pub struct ToastOptions {
7    pub message: String,
8    pub duration: Duration,
9}
10
11pub fn show_toast(options: ToastOptions) {
12    mount_style("toast", include_str!("./toast.css"));
13
14    let parent = Element::from(document().body().expect("body element not to exist"));
15    let children = view! { <div class="melt-toast">{options.message}</div> };
16    let node = children.into_view();
17
18    #[cfg(target_arch = "wasm32")]
19    {
20        use leptos::leptos_dom::Mountable;
21        let node = node.get_mountable_node();
22        parent.append_child(&node).unwrap();
23        set_timeout(
24            move || {
25                _ = parent.remove_child(&node);
26            },
27            options.duration,
28        );
29    }
30    #[cfg(not(target_arch = "wasm32"))]
31    {
32        _ = parent;
33        _ = node;
34    }
35}