Repository navigation
DataViews: Move bulk selection focus only on request from an interaction - #86932
Conversation
The table and grid layouts moved focus to a selection checkbox whenever the selection was cleared while focus was on the document body. That guess also claimed focus that something else lost, such as an app router that moves focus after a navigation, and ran after the router did. Record the focus target when the user clears the selection, selects all items from the table header, or finishes a bulk action, and move focus once the toolbar reaches the expected state. The move runs in a layout effect, so it happens before the effects of ancestors. Co-authored-by: t-hamano <54422211+t-hamano@users.noreply.github.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011g9URyzU5uLZFCXsBBvXn4
Co-authored-by: t-hamano <54422211+t-hamano@users.noreply.github.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011g9URyzU5uLZFCXsBBvXn4
🤖 PR meta 🤖🎉 PropsIf 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. Props Bot commandsYou can trigger Props Bot actions by commenting on this PR:
Updated as activity occurs, without notifying anyone named here. Add the 📦 Bundle sizeSize Change: -142 B (0%) Total Size: 8.68 MB 📦 View Changed
⚡ PerformanceShow the resultsClient side metrics exclude the server response time. front-end-block-theme
front-end-classic-theme
media-processing
media-upload
post-editor
site-editor
🏁 Flaky testsSome tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information. does not double up an event React already delivers in
|
Co-authored-by: t-hamano <54422211+t-hamano@users.noreply.github.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011g9URyzU5uLZFCXsBBvXn4
Co-authored-by: t-hamano <54422211+t-hamano@users.noreply.github.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011g9URyzU5uLZFCXsBBvXn4
Co-authored-by: t-hamano <54422211+t-hamano@users.noreply.github.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011g9URyzU5uLZFCXsBBvXn4
Co-authored-by: t-hamano <54422211+t-hamano@users.noreply.github.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011g9URyzU5uLZFCXsBBvXn4
Co-authored-by: t-hamano <54422211+t-hamano@users.noreply.github.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011g9URyzU5uLZFCXsBBvXn4
Co-authored-by: t-hamano <54422211+t-hamano@users.noreply.github.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011g9URyzU5uLZFCXsBBvXn4
Co-authored-by: t-hamano <54422211+t-hamano@users.noreply.github.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011g9URyzU5uLZFCXsBBvXn4
| // Layout effects run before the effects of ancestors, such as a router | ||
| // that moves focus when a navigation removes the focused element. | ||
| useLayoutEffect( () => { | ||
| targetRef.current?.current?.focus(); | ||
| targetRef.current = null; | ||
| }, [ isShown ] ); |
There was a problem hiding this comment.
This is simple and matches the typical behavior we'd expect, but it's also somewhat a loose dependency between "trigger focus from close request" and "is bulk actions shown", and that the former necessarily triggers the latter. I don't know if there's actual scenarios we have to worry about here where toolbar visibility changes independently, or if e.g. selection changes asynchronously from the click on the button (e.g. if trashing triggers a network request and only changes selection after the network request).
It looks like your initial commit handled this a bit more exhaustively at the cost of extra complexity.
There was a problem hiding this comment.
That’s right. I initially tried to handle every case, but I wasn’t sure it was worth it, and the code got quite complex, so I simplified it in 0c2a4c5. That commit also removed the focus request after a bulk action, so the only remaining triggers are Cancel and the select all checkbox. Both should toggle the toolbar immediately, so I don’t expect the async case to happen.
|
@aduth Thanks for the review!
We already have browser tests in |
Co-authored-by: t-hamano <54422211+t-hamano@users.noreply.github.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011g9URz5qnxZyAFDMiJeNWv
Co-authored-by: t-hamano <54422211+t-hamano@users.noreply.github.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011g9URz5qnxZyAFDMiJeNWv
…ion (WordPress#86932) * DataViews: Move bulk selection focus only on request from an interaction The table and grid layouts moved focus to a selection checkbox whenever the selection was cleared while focus was on the document body. That guess also claimed focus that something else lost, such as an app router that moves focus after a navigation, and ran after the router did. Record the focus target when the user clears the selection, selects all items from the table header, or finishes a bulk action, and move focus once the toolbar reaches the expected state. The move runs in a layout effect, so it happens before the effects of ancestors. Co-authored-by: t-hamano <54422211+t-hamano@users.noreply.github.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011g9URyzU5uLZFCXsBBvXn4 * Add changelog entry Co-authored-by: t-hamano <54422211+t-hamano@users.noreply.github.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011g9URyzU5uLZFCXsBBvXn4 * Remove redundant comments Co-authored-by: t-hamano <54422211+t-hamano@users.noreply.github.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011g9URyzU5uLZFCXsBBvXn4 * Remove redundant comments Co-authored-by: t-hamano <54422211+t-hamano@users.noreply.github.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011g9URyzU5uLZFCXsBBvXn4 * Remove the added browser tests Co-authored-by: t-hamano <54422211+t-hamano@users.noreply.github.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011g9URyzU5uLZFCXsBBvXn4 * Inline the table focus request Co-authored-by: t-hamano <54422211+t-hamano@users.noreply.github.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011g9URyzU5uLZFCXsBBvXn4 * Pass an object to useBulkActionsFocus Co-authored-by: t-hamano <54422211+t-hamano@users.noreply.github.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011g9URyzU5uLZFCXsBBvXn4 * Remove the focus request after a bulk action Co-authored-by: t-hamano <54422211+t-hamano@users.noreply.github.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011g9URyzU5uLZFCXsBBvXn4 * Rename requestFocus to focusAfterToggle Co-authored-by: t-hamano <54422211+t-hamano@users.noreply.github.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011g9URyzU5uLZFCXsBBvXn4 * Add a test for clearing the selection without an interaction Co-authored-by: t-hamano <54422211+t-hamano@users.noreply.github.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011g9URz5qnxZyAFDMiJeNWv * Move the changelog entry to Unreleased Co-authored-by: t-hamano <54422211+t-hamano@users.noreply.github.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011g9URz5qnxZyAFDMiJeNWv --------- Co-authored-by: t-hamano <wildworks@git.wordpress.org> Co-authored-by: aduth <aduth@git.wordpress.org>
What?
See #86701, #86700
Replaces the "focus is on the document body" check in the table and grid layouts with focus requests made by the interactions that change the selection.
site-editor-v1-table-select-all-cancel.mp4
Why?
Since #82730, the table and grid layouts move focus to a selection checkbox when the selection is cleared and focus is on the document body. This is meant for the case where the Cancel button removes the bulk actions toolbar along with the focused element. But the
useEffectonly depends on whether there is a selection, so it can't tell why focus was lost. It also moves focus when focus was lost for another reason, such as a navigation that re-renders the page.This conflicts with #86701, which moves focus to the page region after a navigation loses focus. In the page list, clearing the selection is also a navigation, so both try to recover focus, and the page region wins.
How?
Instead of checking for focus on the document body, the interactions that change the selection (Cancel and the select all checkbox) ask a new internal hook,
useBulkActionsFocus, to move focus. The hook moves focus once the bulk actions toolbar is shown or hidden.Testing Instructions
Use of AI Tools
Implemented with Claude Code, reviewed by the author.
Generated by Claude Code