thaw/src/components/wave/mod.rs

57 lines
1.6 KiB
Rust
Raw Normal View History

2023-11-13 16:17:45 +08:00
use crate::utils::{mount_style, ComponentRef};
2023-11-02 10:16:31 +08:00
use leptos::{leptos_dom::helpers::TimeoutHandle, *};
use std::time::Duration;
#[derive(Clone)]
pub struct WaveRef {
play: Callback<()>,
}
impl WaveRef {
pub fn play(&self) {
self.play.call(());
}
}
2023-04-27 13:22:34 +08:00
#[component]
2023-11-02 10:16:31 +08:00
pub fn Wave(#[prop(optional)] comp_ref: ComponentRef<WaveRef>) -> impl IntoView {
2023-10-07 21:41:03 +08:00
mount_style("wave", include_str!("./wave.css"));
2023-08-29 09:11:22 +08:00
let wave_ref = create_node_ref::<html::Div>();
2023-11-02 10:16:31 +08:00
let animation_timeout_handle = create_rw_signal(None::<TimeoutHandle>);
let play = Callback::new(move |_: ()| {
if let Some(handle) = animation_timeout_handle.get() {
handle.clear();
animation_timeout_handle.set(None);
}
if let Some(wave_ref) = wave_ref.get() {
_ = wave_ref.offset_height();
}
let handle = set_timeout_with_handle(
move || {
animation_timeout_handle.set(None);
},
Duration::from_secs(1),
);
if let Ok(handle) = handle {
animation_timeout_handle.set(Some(handle))
}
2023-04-27 13:22:34 +08:00
});
2023-11-02 10:16:31 +08:00
comp_ref.load(WaveRef { play });
on_cleanup(move || {
if let Some(handle) = animation_timeout_handle.get() {
handle.clear();
animation_timeout_handle.set(None);
}
});
2023-04-27 13:22:34 +08:00
view! {
2023-11-02 22:01:48 +08:00
<div
2023-11-05 16:03:58 +08:00
class="thaw-wave"
2023-11-02 22:01:48 +08:00
class=(
2023-11-05 16:03:58 +08:00
"thaw-wave--active",
2023-11-02 22:01:48 +08:00
move || animation_timeout_handle.with(|handle| handle.is_some()),
)
ref=wave_ref
></div>
2023-04-27 13:22:34 +08:00
}
}