dioxus_spring/
use_spring.rs

1use crate::use_spring_signal;
2use dioxus::{
3    hooks::{use_memo, use_reactive},
4    signals::Signal,
5};
6use interpolation::Lerp;
7use std::time::Duration;
8
9/// Hook to create an animated signal from a reactive value and [`Duration`].
10///
11/// When `value` is changed, this signal will linearly interpolate from the current value to `value`.
12pub fn use_spring<V>(value: V, duration: Duration) -> Signal<V>
13where
14    V: PartialEq + Lerp<Scalar = f32> + Clone + 'static,
15{
16    let (signal, spring_ref) = use_spring_signal(value.clone());
17
18    use_memo(use_reactive((&value,), move |(to,)| {
19        spring_ref.animate(to, duration);
20    }));
21
22    signal
23}