2023-06-14 18:19:59 +08:00
|
|
|
use crate::utils::mount_style::mount_style;
|
|
|
|
use leptos::*;
|
|
|
|
use std::time::Duration;
|
|
|
|
use stylers::style_sheet_str;
|
|
|
|
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-06-14 18:19:59 +08:00
|
|
|
let class_name = mount_style("toast", || style_sheet_str!("./src/mobile/toast/toast.css"));
|
|
|
|
|
|
|
|
let parent = Element::from(document().body().expect("body element not to exist"));
|
2023-08-29 09:11:22 +08:00
|
|
|
let children = view! { class=class_name,
|
2023-06-14 18:19:59 +08:00
|
|
|
<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"))]
|
|
|
|
{
|
|
|
|
use leptos_dom::Mountable;
|
|
|
|
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
|
|
|
}
|