mirror of
https://github.com/adoyle0/thaw.git
synced 2025-03-13 05:59:49 -04:00
50 lines
1.4 KiB
Rust
50 lines
1.4 KiB
Rust
use crate::utils::{mount_style::mount_style, ComponentRef};
|
|
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(());
|
|
}
|
|
}
|
|
|
|
#[component]
|
|
pub fn Wave(#[prop(optional)] comp_ref: ComponentRef<WaveRef>) -> impl IntoView {
|
|
mount_style("wave", include_str!("./wave.css"));
|
|
let wave_ref = create_node_ref::<html::Div>();
|
|
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))
|
|
}
|
|
});
|
|
comp_ref.load(WaveRef { play });
|
|
view! {
|
|
<div
|
|
class="melt-wave"
|
|
class=(
|
|
"melt-wave--active",
|
|
move || animation_timeout_handle.with(|handle| handle.is_some()),
|
|
)
|
|
ref=wave_ref
|
|
></div>
|
|
}
|
|
}
|