Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Block options menu: initial focus skips the first menu item #42064

Closed
afercia opened this issue Jun 30, 2022 · 2 comments · Fixed by #42187
Closed

Block options menu: initial focus skips the first menu item #42064

afercia opened this issue Jun 30, 2022 · 2 comments · Fixed by #42187
Assignees
Labels
[Feature] Block settings menu The block settings screen [Focus] Accessibility (a11y) Changes that impact accessibility and need corresponding review (e.g. markup changes). [Package] Compose /packages/compose [Type] Bug An existing feature does not function as intended

Comments

@afercia
Copy link
Contributor

afercia commented Jun 30, 2022

Description

When the 'Options' menu in the block contextual toolbar opens, initial focus is expected to be set to the first menu item. Instead, it's set on the second menu item.

Step-by-step reproduction instructions

  • Edit a post.
  • Select a paragraph to make the block contextual toolbar appear.
  • Click the last button within the toolbar to open the 'Options' menu.
  • Observe the initial focus within the menu is set to the 'Copy block' menu item, which is the second item.
  • Instead, it should be set to the first item 'Hide more settings'.

Screenshots, screen recording, code snippet

No response

Environment info

No response

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

@afercia afercia added [a11y] Keyboard & Focus [Feature] Block settings menu The block settings screen [Type] Bug An existing feature does not function as intended [Package] Compose /packages/compose labels Jun 30, 2022
@afercia
Copy link
Contributor Author

afercia commented Jun 30, 2022

Screenshot 2022-06-28 at 16 43 21

When opening the menu, useFocusOnMount is responsible for setting the initial focus. It finds all the 'tabbable' elements and sets focus on the first one:

if ( focusOnMountRef.current === 'firstElement' ) {
const firstTabbable = focus.tabbable.find( node )[ 0 ];
if ( firstTabbable ) {
target = /** @type {HTMLElement} */ ( firstTabbable );
}
}
target.focus( {

In the screenshot above:

  • I'm dumping all 'tabbable' elements focus.tabbable.find( node ) to the console.
  • useFocusOnMount finds 9 tabbable elements.
  • However, there are actually 11 tabbable menu items within the menu.
  • The 2 missing items are:
    • 'Hide more settings'
    • 'Add to Reusable blocks'

My guess: it's about timings. It appears useFocusOnMount runs before those two menu items are actually rendered.

Hide more settings
It renders via __unstableBlockSettingsMenuFirstItem which is a Slot/Fill implementation, which appears to render the menu item after useFocusOnMount ran.

Add to Reusable blocks
I'm not sure I can follow this implementation but it appears it renders later anyways.

Note:
I'm guessing the sam problem that occurs with __unstableBlockSettingsMenuFirstItem may happen also with __unstableBlockToolbarLastItem.

Overall, this seems a more general issue that's not limited to the block settings menu.

useFocusOnMount expects the DOM to be fully rendered. It appears that using Slot/Fill and similar implementations in places where useFocusOnMount isn't ideal, as it makes useFocusOnMount fail.

@afercia
Copy link
Contributor Author

afercia commented Jun 30, 2022

In my testing, using the setTimeout 0 workaround in useFocusOnMount would fix the issue.

Turns out useFocusOnMount used to have a SetTimeout that was removed in #27699. I do understand the reasons why it was removed, but it appears that change didn't take into account components that are mounted at a later time within the parent component where useFocusOnMount runs.

I don't know enough about some implementation details (e.g. whether the Slot/Fill implementation for the first item is really needed) so I'd defer to someone with greater knowledge than me. Cc @ellatrix @youknowriad

@afercia afercia self-assigned this Jul 6, 2022
@github-actions github-actions bot added the [Status] In Progress Tracking issues with work in progress label Jul 6, 2022
@priethor priethor removed the [Status] In Progress Tracking issues with work in progress label May 17, 2023
@priethor priethor added the [Focus] Accessibility (a11y) Changes that impact accessibility and need corresponding review (e.g. markup changes). label Jul 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Block settings menu The block settings screen [Focus] Accessibility (a11y) Changes that impact accessibility and need corresponding review (e.g. markup changes). [Package] Compose /packages/compose [Type] Bug An existing feature does not function as intended
Projects
None yet
2 participants