Skip to content

DataViews: focus is lost when the focused row is removed from the list/table layout #82240

Description

@hbhalodia

Description

Following up from the PR comment here - #80306 (comment),

🤖 Claude Code Generate it for me

In the DataViews list layout, removing the row that currently has focus drops focus to <body>. Keyboard and screen-reader users lose their place in the list and have to tab back from the top of the page after every deletion.

The layout already tries to handle this. packages/dataviews/src/components/dataviews-layouts/list/index.tsx has an effect whose comment states the intent:

Select a new active composite item when the current active item is removed from the list.
By picking previousActiveItemIndex […] we are basically picking the item that would have been after the deleted one. If the previously active (and removed) item was the last of the list, we will select the item before it — which is the new last item.

That effect calls selectCompositeItem(), which moves DOM focus only when focus is still inside the list:

// The active composite item is controlled state that can update without needing a
// focus move (e.g., searching can trigger an active ID update). Only move DOM focus
// when it's already within the list.
if ( compositeRef.current?.contains(
        compositeRef.current.ownerDocument.activeElement ) ) {
    document.getElementById( targetCompositeItemId )?.focus();
}

By the time the effect runs, the removed row is unmounted, so activeElement is already <body> — the guard is false and the focus move is skipped. The active composite id is updated (so the next Tab into the list lands in the right place), but focus itself is lost in the moment that matters.

The guard is correct for the case it was written for (search re-ordering the list must not steal focus). Removal is a different case that needs to be distinguished from it.

What is your proposed solution?

Let the removal path move DOM focus when focus was inside the list immediately before the row disappeared:

  1. Track whether the composite held focus just before the data change — e.g. a ref updated on the composite's focusin/focusout, rather than reading activeElement after the fact.
  2. Give selectCompositeItem() an opt-in to force the focus move, and use it only from the item-removed effect. Every other caller keeps today's behavior.
  3. Only force it when focus is actually orphaned — activeElement is null, <body>, or inside the unmounted row. If the user has since moved focus somewhere real, or a dialog is open and owns focus return, do nothing.

Point 3 matters because removal is often confirmed in a modal: the dialog's own focus return should win, and DataViews should not fight it.

Worth deciding as part of this: whether the other layouts (grid, table) need the same treatment, and what should happen when the removed row was the only one on the last page.

Step-by-step reproduction instructions

  1. Open site-editor/pages
  2. Delete/Trash any page from the list
  3. Check the focus is now moved to body, but it should fallback to next/previous element.

Screenshots, screen recording, code snippet

Screen.Recording.2026-08-31.at.3.25.46.PM.mov

Environment info

  • WP Latest.
  • GB Trunk.

Please confirm that you have searched existing issues in the repo.

  • Yes

Please confirm that you have tested with all plugins deactivated except Gutenberg.

  • Yes

Please confirm which theme type you used for testing.

  • Block
  • Classic
  • Hybrid (e.g. classic with theme.json)
  • Not sure

Activity

  1. self-assigned this
    on Aug 31, 2026
  2. added
    [Type] Bug
    [Focus] Accessibility (a11y)
    [Feature] DataViews
    [Package] DataViews
    on Aug 31, 2026
  3. added theissue type on Aug 31, 2026
  4. removed their assignment
    on Aug 31, 2026
  5. changed the title [-]DataViews: focus is lost when the focused row is removed from the list layout[/-] [+]DataViews: focus is lost when the focused row is removed from the list/table layout[/+] on Aug 31, 2026
  6. raoazwar commented on Sep 2, 2026

    @raoazwar

    Hey, I just tried to reproduce the issue on my local server, but I wasn’t able to reproduce the issue shown in your video.

    Recording.2026-09-03.003406.1.mp4
  7. added
    [Status] In Progress
    on Sep 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    [Feature] DataViews[Focus] Accessibility (a11y)[Package] DataViews[Status] In Progress[Type] Bug

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions