Skip to content
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

Open
wants to merge 1 commit into
base: v3
Choose a base branch
from

Conversation

kalicki2k
Copy link

@kalicki2k kalicki2k commented Mar 14, 2025

  • 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.

🔗 Linked issue

❓ Type of change

  • 📖 Documentation (updates to the documentation or readme)
  • 🐞 Bug fix (a non-breaking change that fixes an issue)
  • 👌 Enhancement (improving an existing functionality)
  • ✨ New feature (a non-breaking change that adds functionality)
  • 🧹 Chore (updates to the build process or auxiliary tools and libraries)
  • ⚠️ Breaking change (fix or feature that would cause existing functionality to change)

📚 Description

📝 Checklist

  • I have linked an issue or discussion.
  • I have updated the documentation accordingly.

- 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.
Copy link

pkg-pr-new bot commented Mar 14, 2025

npm i https://pkg.pr.new/@nuxt/ui@3571

commit: 77f63b6

@benjamincanac benjamincanac requested a review from romhml March 15, 2025 11:12
@@ -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(() => {
Copy link
Collaborator

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?

Copy link
Member

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 😕

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants