-
I have an effect that depends on the same signal as the use std::time::Duration;
use leptos::{*, html::Input};
fn main() {
leptos::mount_to_body(|cx| view! { cx, <App/> })
}
#[component]
fn App(cx: Scope) -> impl IntoView {
let (is_animating, set_is_animating) = create_signal(cx, true);
let input: NodeRef<Input> = create_node_ref(cx);
create_effect(cx, move |was_animating| {
let is_animating = is_animating.get();
if !is_animating && was_animating.unwrap_or(false) {
log!("Will focus!");
if let Some(input) = input.get() {
// This doesn't work. It will print "Focused" but it runs while the element is still disabled.
match input.focus() {
Ok(()) => log!("Focused"),
Err(err) => log!("Error focusing input: {:?}", err),
};
}
}
is_animating
});
// Pretend this is triggered by some animation ending
set_timeout(move || set_is_animating.set(false), Duration::from_secs(1));
view! { cx,
<input
type="text"
node_ref=input
disabled=move || is_animating.get()
/>
}
} Is there any way? The workarounds I found are by using a mutation observer or by controlling the disabled property manually, but it's less declarative. |
Beta Was this translation helpful? Give feedback.
Answered by
gbj
Aug 26, 2023
Replies: 1 comment 1 reply
-
Does it work if you use request_animation_frame(move || {
match input.focus() {
Ok(()) => log!("Focused"),
Err(err) => log!("Error focusing input: {:?}", err),
};
}); |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
pheki
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Does it work if you use
request_animation_frame
to delay it a tick: