2023-06-14 18:19:59 +08:00
|
|
|
use crate::utils::mount_style::mount_style;
|
|
|
|
use leptos::*;
|
|
|
|
use std::time::Duration;
|
|
|
|
use web_sys::Element;
|
|
|
|
|
|
|
|
pub struct ToastOptions {
|
|
|
|
pub message: String,
|
|
|
|
pub duration: Duration,
|
|
|
|
}
|
|
|
|
|
2023-08-29 09:11:22 +08:00
|
|
|
pub fn show_toast(options: ToastOptions) {
|
2023-10-07 21:41:03 +08:00
|
|
|
mount_style("toast", include_str!("./toast.css"));
|
2023-06-14 18:19:59 +08:00
|
|
|
|
|
|
|
let parent = Element::from(document().body().expect("body element not to exist"));
|
2023-10-08 09:28:13 +08:00
|
|
|
let children = view! { <div class="melt-toast">{options.message}</div> };
|
2023-08-29 09:11:22 +08:00
|
|
|
let node = children.into_view();
|
2023-06-14 18:19:59 +08:00
|
|
|
|
|
|
|
#[cfg(all(target_arch = "wasm32"))]
|
|
|
|
{
|
2023-10-10 15:48:51 +08:00
|
|
|
use leptos::leptos_dom::Mountable;
|
2023-06-14 18:19:59 +08:00
|
|
|
let node = node.get_mountable_node();
|
|
|
|
parent.append_child(&node).unwrap();
|
|
|
|
set_timeout(
|
|
|
|
move || {
|
|
|
|
_ = parent.remove_child(&node);
|
|
|
|
},
|
|
|
|
options.duration,
|
|
|
|
);
|
|
|
|
}
|
2023-08-29 09:11:22 +08:00
|
|
|
#[cfg(not(target_arch = "wasm32"))]
|
|
|
|
{
|
|
|
|
_ = parent;
|
|
|
|
_ = node;
|
|
|
|
}
|
2023-06-14 18:19:59 +08:00
|
|
|
}
|