2023-11-09 16:46:14 +08:00
|
|
|
use cfg_if::cfg_if;
|
2023-06-14 18:19:59 +08:00
|
|
|
use std::time::Duration;
|
2024-03-19 22:02:43 +08:00
|
|
|
use thaw_utils::mount_style;
|
2023-06-14 18:19:59 +08:00
|
|
|
|
|
|
|
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"));
|
2024-03-18 22:00:09 +08:00
|
|
|
cfg_if! { if #[cfg(all(target_arch = "wasm32", any(feature = "csr", feature = "hydrate")))] {
|
2023-11-09 16:46:14 +08:00
|
|
|
use leptos::{leptos_dom::Mountable, *};
|
2024-01-31 20:13:26 +08:00
|
|
|
let mount = document().body().expect("body element to exist");
|
2023-11-09 16:46:14 +08:00
|
|
|
let children = view! { <div class="thaw-toast">{options.message}</div> };
|
|
|
|
let node = children.into_view();
|
2023-06-14 18:19:59 +08:00
|
|
|
let node = node.get_mountable_node();
|
2023-11-09 16:46:14 +08:00
|
|
|
_ = mount.append_child(&node);
|
2023-06-14 18:19:59 +08:00
|
|
|
set_timeout(
|
|
|
|
move || {
|
2023-11-09 16:46:14 +08:00
|
|
|
_ = mount.remove_child(&node);
|
2023-06-14 18:19:59 +08:00
|
|
|
},
|
|
|
|
options.duration,
|
|
|
|
);
|
2023-11-09 16:46:14 +08:00
|
|
|
} else {
|
|
|
|
_ = options;
|
|
|
|
}}
|
2023-06-14 18:19:59 +08:00
|
|
|
}
|