Block Comments: Select block when comment is selected#72082
Conversation
|
Size Change: -13 B (0%) Total Size: 1.96 MB
ℹ️ View Unchanged
|
| // The related block should be selected, but the focus should remain on the comment. | ||
| await expect( | ||
| editor.canvas.getByText( 'Testing block comments' ) | ||
| ).toHaveClass( /is-selected/ ); | ||
| await expect( thread ).toBeFocused(); |
There was a problem hiding this comment.
This is the most important thing we want to test in this PR.
|
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. |
|
This worked well in my testing, nice work! |
| } ); | ||
| } | ||
| setSelectedThread( thread.id ); | ||
| selectBlock( thread.blockClientId, null ); |
There was a problem hiding this comment.
Let's document what the second arg null does; otherwise, it's easy to forget.
| // Auto-select the related comment thread when a block is selected. | ||
| useEffect( () => { | ||
| setSelectedThread( blockCommentId ?? undefined ); | ||
| }, [ blockCommentId ] ); |
There was a problem hiding this comment.
I think we should be able to drop this effect after #71969.
|
Flaky tests detected in d563112. 🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/18271747827
|
What?
This PR actually "selects" the block when a comment is selected, but the focus remains on the comment.
This allows comment selection to work properly in Spotlight mode.
Why?
When a comment is selected,
scrollIntoView + toggleHighlightis performed to visually indicate which block the comment relates to. The reason we didn't use theselectBlockis that we didn't want the focus to move to the block when a comment was clicked.However, this behaviour is problematic when the spotlight mode is enabled. When a comment is selected, the related block is visually highlighted, but the block remains semi-transparent because it is not actually selected.
How?
I realized that we can prevent the focus from being moved to the block by passing
nullas the second parameter of theselectBlock. This allows us to completely replacescrollIntoView + toggleBlockHighlightwith justselectBlock.However, this introduced an unintended issue. In the current implementation, the Comments component has a key prop to force a re-rendering of associated comments when a block is selected. This means that:
getSelectedBlockClientId()is updatedInstead, I've added a new effect here. This prevents the focus from being lost when a comment is selected and a block is selected.
Testing Instructions
Testing Instructions for Keyboard
Screenshots or screencast
8406faaf5c3a9d914560d7e8c4fa30d8.1.mp4