Compose: Fix useFocusOnMount falling back too early when input controls load asynchronously#76019
Conversation
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
|
Warning: Type of PR label mismatch To merge this PR, it requires exactly 1 label indicating the type of PR. Other labels are optional and not being checked here.
Read more about Type labels in Gutenberg. Don't worry if you don't have the required permissions to add labels; the PR reviewer should be able to help with the task. |
|
@Mustafabharmal Good approach, but I’d recommend using the state to reinitialize the element focus instead of relying on setTimeout and a four-iteration checking loop. |
What?
Fixes
useFocusOnMount(firstInputElementmode) incorrectly focusing the close button on first open of the DataViews quick edit Author field.Closes #75540.
Why?
useFocusOnMounttried to find an input element in a singlesetTimeout(..., 0). Async DataViews controls (e.g. Author select) render a<Spinner />while fetching, replacing it with<select>only after the API response arrives. The hook fired before that happened and immediately fell back to the first tabbable element, the close button.How?
When in
firstInputElementmode and no input is found, the hook now retries every 50ms up to 4 times (~200ms total). If the input appears within that window it gets focused; otherwise it falls back to the existing behaviour (first tabbable element).Testing Instructions
Screenshots or screencast
Before:
Before.Fix.mov
After:
After.Fix.mov