mirror of
https://github.com/leptos-rs/leptos
synced 2024-11-10 06:44:17 +00:00
examples: add autofocus in todomvc
This commit is contained in:
parent
9723cc466e
commit
a1144a5b6b
1 changed files with 13 additions and 0 deletions
|
@ -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">
|
||||
|
|
Loading…
Reference in a new issue