examples: add autofocus in todomvc

This commit is contained in:
Greg Johnston 2023-04-24 08:07:49 -04:00
parent 9723cc466e
commit a1144a5b6b

View file

@ -202,6 +202,19 @@ pub fn TodoMVC(cx: Scope) -> impl IntoView {
}
});
// focus the main input on load
create_effect(cx, move |_| {
if let Some(input) = input_ref.get() {
// We use request_animation_frame here because the NodeRef
// is filled when the element is created, but before it's mounted
// to the DOM. Calling .focus() before it's mounted does nothing.
// So inside, we wait a tick for the browser to mount it, then .focus()
request_animation_frame(move || {
input.focus();
});
}
});
view! { cx,
<main>
<section class="todoapp">