Skip to content

DataViews: Keep focus in the view when an action removes the focused control - #87248

Open
HrithikDalal wants to merge 1 commit into
WordPress:trunkfrom
HrithikDalal:fix/dataviews-focus-after-trash
Open

HrithikDalal wants to merge 1 commit into
WordPress:trunkfrom
HrithikDalal:fix/dataviews-focus-after-trash

Conversation

@HrithikDalal

Copy link
Copy Markdown
Contributor

What?

Part of #85052

When an action removes the item whose control had focus, like moving a page to the trash, focus now stays in the view instead of falling to the document body.

Why?

Trashing a page removes its row, together with the Trash button and the confirm dialog rendered from it. Focus had nowhere to go back to, so it dropped to the body and keyboard and screen reader users had to start again from the top of the page. This happens in both site editors, from the row button, the actions menu and the bulk toolbar.

How?

  • Follows the approach of DataViews: Move bulk selection focus only on request from an interaction #86932: focus only moves on request. Running an action asks for it, through a new internal useFocusAfterAction hook in DataViews.
  • Once the data changes and the view is done loading (the layouts are inert while loading), if focus is on the body it goes to "Select all", or else the first control or item of the layout, or else the first control of the view (the search, when no items are left).
  • In the list layout, the list then moves focus on to the item that followed the removed one, as it already does when focus is inside the list.
  • If the items go while focus is still on the action's dialog or busy button (some actions close the dialog only after removing the items), it waits until that's gone, and until loading ends.
  • The request is dropped as soon as focus settles somewhere outside the view, other than the action's own dialog or menu, so it never pulls focus back.
  • A request from an action that removed nothing (e.g. a cancelled dialog) stays until focus leaves the view. If a later search or page change removes those items, it only acts when focus is already lost to the body.
  • It runs in a layout effect, before the router's effects. In the extensible site editor, a bulk trash clears the selection through the URL, and Boot: Maintain focus within page region when router changes page #86701 already moves focus to the page region at that point; this doesn't compete with that.

This replaces the broader focus fallback I had in #86150, which watched every removal.

Testing Instructions

  1. Site Editor > Pages, table layout.
  2. Trash a page with its row's Trash button and confirm. Focus lands on "Select all" (on trunk it's on the body).
  3. Trash a page from its actions menu. Same.
  4. Switch to the grid layout and trash a page from a card's menu. Focus lands on "Select all".
  5. Switch to the list layout and trash the first page. Focus moves to the next page.
  6. Select all pages in the table and trash them. With no pages left, focus lands on the search.
  7. Repeat steps 2 to 5 in the extensible site editor.

Testing Instructions for Keyboard

Tab to a row's Trash button, press Enter, then Enter on "Trash" in the dialog. Press Tab once: you're in the list, not at the top of the page.

Use of AI Tools

Used Claude Code to co-author the fix. Reviewed and tested by me.

@github-actions

github-actions Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

🤖 PR meta 🤖

Preview in WordPress Playground Preview in Storybook

🎉 Props

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: HrithikDalal <hrithikd@git.wordpress.org>

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


Props Bot commands

You can trigger Props Bot actions by commenting on this PR:

  • @propsbot add <username> to give props to a GitHub or WordPress.org user. Requires write access.
  • @propsbot remove <username> to leave a user out of the props. Requires write access.
  • @propsbot refresh to refresh this list. Adding the props-bot label works too.

Updated as activity occurs, without notifying anyone named here. Add the props-bot label to refresh.

🏷️ Labels

This pull request needs exactly one label indicating its type, and has 0.

  • Required: any label starting with [Type].
  • Found: none.

Read more about Type labels in Gutenberg. If you cannot add labels yourself, a reviewer can do it for you.

@HrithikDalal
HrithikDalal force-pushed the fix/dataviews-focus-after-trash branch from 9f337a2 to a5c2e51 Compare December 3, 2026 16:23

This branch has not been deployed

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

Labels

[Package] DataViews /packages/dataviews

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant