Skip to content

Commit

Permalink
Merge pull request #5 from matthunz/fix-signals
Browse files Browse the repository at this point in the history
Fix reactivity after signals migration
  • Loading branch information
matthunz authored Jun 5, 2024
2 parents 438e9e6 + 62855a6 commit 79fcc84
Show file tree
Hide file tree
Showing 11 changed files with 109 additions and 148 deletions.
85 changes: 32 additions & 53 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@ license = "MIT OR Apache-2.0"

[dependencies]
async-channel = "1.9.0"
dioxus = "0.5.0"
dioxus-web = "0.5.0"
dioxus-signals = "0.5.0"
dioxus = { git = "https://github.com/DioxusLabs/dioxus" }
dioxus-web = { git = "https://github.com/DioxusLabs/dioxus" }
dioxus-signals = { git = "https://github.com/DioxusLabs/dioxus" }
futures = "0.3.28"
interpolation = "0.3.0"
js-sys = "0.3.64"
log = "0.4.20"
dioxus-logger = "0.4.1"
wasm-bindgen = "0.2.87"
web-sys = "0.3.64"
dioxus-use-mounted = "0.2.0"
dioxus-use-mounted = { git = "https://github.com/matthunz/dioxus-use-mounted" }
slotmap = "1.0.6"

[dev-dependencies]
dioxus-resize-observer = "0.2.0-alpha.1"
dioxus-resize-observer = { git = "https://github.com/dioxus-community/dioxus-resize-observer", rev = "1a16141a63dbb17230a3dacc97d949861aa2125f" }
35 changes: 0 additions & 35 deletions examples/use_spring.rs.todo

This file was deleted.

27 changes: 27 additions & 0 deletions examples/use_spring_ref.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use dioxus::prelude::*;
use dioxus_spring::use_spring_ref;
use log::LevelFilter;
use std::time::Duration;

fn app() -> Element {
let mut signal = use_signal(|| 0.);
let spring_ref = use_spring_ref(0f32, move |x| {
signal.set(x)
});

use_hook(move || {
spring_ref.animate(1., Duration::from_secs(1));
});

use_effect(move || {
log::info!("{}", signal());
});

rsx!()
}

fn main() {
dioxus_logger::init(LevelFilter::Info).expect("failed to init logger");

dioxus_web::launch::launch_cfg(app, Default::default())
}
24 changes: 24 additions & 0 deletions examples/use_spring_signal.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use dioxus::prelude::*;
use dioxus_spring::use_spring_signal;
use log::LevelFilter;
use std::time::Duration;

fn app() -> Element {
let (value, value_spring) = use_spring_signal(0f32);

use_hook(move || {
value_spring.animate(1., Duration::from_secs(1));
});

use_effect(move || {
log::info!("{}", value());
});

rsx!()
}

fn main() {
dioxus_logger::init(LevelFilter::Info).expect("failed to init logger");

dioxus_web::launch::launch_cfg(app, Default::default())
}
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ mod use_spring;
pub use use_spring::use_spring;

mod use_spring_signal;
pub use use_spring_signal::{use_spring_signal, UseSpringSignal};
pub use use_spring_signal::use_spring_signal;

mod use_spring_ref;
pub use use_spring_ref::{use_spring_ref, UseSpringRef};
Expand Down
1 change: 1 addition & 0 deletions src/spring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use interpolation::Lerp;
use js_sys::Date;
use std::time::Duration;

/// Create a stream of interpolated values from one value to another over a [`Duration`].
pub fn spring<V>(from: V, to: V, duration: Duration) -> impl Stream<Item = V>
where
V: Lerp<Scalar = f32> + Clone + 'static,
Expand Down
1 change: 1 addition & 0 deletions src/use_animated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use dioxus::hooks::use_effect;
use dioxus_signals::{Readable, Signal};
use dioxus_use_mounted::UseMounted;

/// Hook to use an animated value and apply it to a mounted element.
pub fn use_animated<V>(
mounted: UseMounted,
value_ref: Signal<V>,
Expand Down
5 changes: 4 additions & 1 deletion src/use_spring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ use dioxus_signals::Signal;
use interpolation::Lerp;
use std::time::Duration;

/// Hook to create an animated signal from a reactive value and [`Duration`].
///
/// When `value` is changed, this signal will linearly interpolate from the current value to `value`.
pub fn use_spring<V>(value: V, duration: Duration) -> Signal<V>
where
V: PartialEq + Lerp<Scalar = f32> + Clone + 'static,
{
let (mut spring_ref, signal) = use_spring_signal(value.clone());
let (signal, spring_ref) = use_spring_signal(value.clone());

use_effect(use_reactive((&value,), move |(to,)| {
spring_ref.animate(to, duration);
Expand Down
Loading

0 comments on commit 79fcc84

Please sign in to comment.