Skip to content

Compose: Fix useFocusOnMount falling back too early when input controls load asynchronously#76019

Open
Mustafabharmal wants to merge 1 commit intoWordPress:trunkfrom
Mustafabharmal:fix/75540-focus-on-mount-async-input
Open

Compose: Fix useFocusOnMount falling back too early when input controls load asynchronously#76019
Mustafabharmal wants to merge 1 commit intoWordPress:trunkfrom
Mustafabharmal:fix/75540-focus-on-mount-async-input

Conversation

@Mustafabharmal
Copy link
Contributor

What?

Fixes useFocusOnMount (firstInputElement mode) incorrectly focusing the close button on first open of the DataViews quick edit Author field.

Closes #75540.

Why?

useFocusOnMount tried to find an input element in a single setTimeout(..., 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 firstInputElement mode 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

  1. Enable "Data Views: add Quick Edit" from Gutenberg Experiments
  2. Visit Site Editor → Pages (table or grid layout)
  3. Open the quick edit dialog
  4. Click the "Author" field
  5. The select element should receive focus (not the close button)
  6. Subsequent openings should also focus the select correctly

Screenshots or screencast

Before:

Before.Fix.mov

After:

After.Fix.mov

@github-actions
Copy link

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 props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: Mustafabharmal <mustafabharmal@git.wordpress.org>
Co-authored-by: nirav7707 <niravsherasiya7707@git.wordpress.org>
Co-authored-by: Mayank-Tripathi32 <mayanktripathi32@git.wordpress.org>
Co-authored-by: stokesman <presstoke@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@github-actions
Copy link

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.

  • Required label: Any label starting with [Type].
  • Labels found: .

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.

@github-actions github-actions bot added the [Package] Compose /packages/compose label Feb 27, 2026
@nirav7707
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Package] Compose /packages/compose

Projects

None yet

Development

Successfully merging this pull request may close these issues.

DataViews: async loading controls foil first focus on mount

2 participants