Build an accessible dropdown menu
Create a semantic menu button with complete keyboard, focus-return, and outside-dismiss behavior.Description
Requirements
Semantic trigger menu works deterministically for the documented normal, boundary, and repeated-use scenarios.
Aria expanded controls works deterministically for the documented normal, boundary, and repeated-use scenarios.
Open close works deterministically for the documented normal, boundary, and repeated-use scenarios.
Arrow navigation works deterministically for the documented normal, boundary, and repeated-use scenarios.
Home end works deterministically for the documented normal, boundary, and repeated-use scenarios.
Escape restores focus works deterministically for the documented normal, boundary, and repeated-use scenarios.
Outside dismiss works deterministically for the documented normal, boundary, and repeated-use scenarios.
Escape closes
Open the menu, move to Profile, then press Escape
The menu closes and focus returns to the trigger
Focus recovery lets keyboard users continue from the control that opened the popup.
Constraints
Start the menu closedUse native buttons or links for operable controlsDo not trap Tab focus inside the menu
Hints
Hint 1
Treat the trigger, popup, and active menu item as three coordinated pieces of state.
Hint 2
Set aria-expanded from the same state that controls visibility.
Hint 3
Install outside listeners only while the menu is open and remove them when it closes.