thaw/thaw/src/mobile/toast/mod.rs

29 lines
872 B
Rust
Raw Normal View History

2023-11-13 16:17:45 +08:00
use crate::utils::mount_style;
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;
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, *};
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
}