-
Notifications
You must be signed in to change notification settings - Fork 694
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: add setTimeout to ensure element receives focus #3571
base: v3
Are you sure you want to change the base?
Conversation
- Wrap focus and scrollIntoView calls in a setTimeout with 0ms delay. - This allows the browser to complete its render cycle before applying focus. - Fixes issue where the input field was found but not focused.
commit: |
@@ -22,8 +22,10 @@ async function onSubmit(event: FormSubmitEvent<any>) { | |||
async function onError(event: FormErrorEvent) { | |||
if (event?.errors?.[0]?.id) { | |||
const element = document.getElementById(event.errors[0].id) | |||
element?.focus() | |||
element?.scrollIntoView({ behavior: 'smooth', block: 'center' }) | |||
setTimeout(() => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This feels a bit hacky, can we use nextTick
instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This feels a bit hacky, can we use
nextTick
instead?
indeed, as the main thread could be put on hold by the browser/OS while setTimeout run just fine in another thread taking us nowhere 😕
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have tried nextTrick, but it does not work. That's why I used setTimeout. @romhml
🔗 Linked issue
❓ Type of change
📚 Description
📝 Checklist