Skip to content

Conversation

alpadev
Copy link
Contributor

@alpadev alpadev commented Aug 31, 2025

Description

Enforce focus on the dropdown trigger after an item has been selected (and the dropdown is hidden).

Motivation & Context

See #35793

Regarding the implementation - I've seen some proposed implementation by @julien-deramond in another issue, but he mentioned it might be quite complex. This way the focus is only moved to the trigger, if the active focus resides within the dropdown menu. As such it shouldn't conflict if the focus was moved prior/in the event of being hidden.

I set the focus before the hidden event is dispatched, in case some implementor wanted to change the focus to something else, via the event handler (or in case the dropdown is being disposed in the handler - to prevent an error in case the element doesn't exist anymore). Although in that case, I could imagine that screen readers would announce multiple focus jumps (not sure tho), that may require additional tweaks.

Changed the implementation to focus after the hidden event dispatched. If a user manually moves the focus in the event outside of the dropdown, it shouldn't interfere with it anyway. Added a test to the spec to confirm that. As well as another test to make sure no error is thrown if the menu is disposed in the hidden event.

Type of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Refactoring (non-breaking change)
  • Breaking change (fix or feature that would change existing functionality)

Checklist

  • I have read the contributing guidelines
  • My code follows the code style of the project (using npm run lint)
  • My change introduces changes to the documentation
  • I have updated the documentation accordingly
  • I have added tests to cover my changes
  • All new and existing tests passed

Live previews

Related issues

Closes #35793
Closes #41588 (tbf this can be closed already, just so we don't forget about it)

@alpadev alpadev requested a review from a team as a code owner August 31, 2025 08:49
@alpadev alpadev changed the title Enforce focus on the dropdown trigger Enforce focus on dropdown trigger Aug 31, 2025
Allow users to move the focus in the hidden event, without interfering.
Make sure if dispose is called in the hidden event, no error is thrown.
Allow users to move the focus manually in the hidden event without interfering.
Because of transpiling using optional chaining is suboptimal
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Needs review
Development

Successfully merging this pull request may close these issues.

dropdown.js: _completeHide results in uncaught TypeError A11y: Closing a dropdown should return focus to the dropdown control
3 participants