Fix ActionMenu item activation #2885
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Authors: Please fill out this form carefully and completely.
Reviewers: By approving this Pull Request you are approving the code change, as well as its deployment and mitigation plans.
Please read this description carefully. If you feel there is anything unclear or missing, please ask for updates.
What are you trying to accomplish?
In working on the experimental
SelectPanelin dotcom, we have noticed a number of issues using the keyboard to activate items in multiple- and single-select modes. SinceSelectPanelis based onActionMenu, I decided to take another look atActionMenuto inform my work onSelectPanel. In so doing, I discovered a number of deficiencies inActionMenu's custom element code that I believe are causing the issues we're seeing inSelectPanel.Integration
No changes necessary in production.
Risk Assessment
What approach did you choose and why?
One of the big issues my work uncovered is that browsers appear to fire both a
PointerEventand aKeyboardEventwhen you press 'Enter' on anActionMenuitem. This is causing the item to be activated twice, and is why we're seeing the item fail to toggle when we press 'Enter' on it (i.e. the first event checks and the second one unchecks so quickly it appears nothing happens). I don't remember two events firing before, so it's possible some browser-level thing changed recently 🤷It turns out we can use
PointerEvents almost exclusively, as they fire when clicking and pressing 'Enter.' I say "almost" because activating menu items is also possible by pressing 'Space' on anchor tag items (eg. items that use the<a>tag), which does not cause aPointerEventbut instead aKeyboardEvent(confused yet?) By listening forkeydownspecifically on anchor items andclickfor everything else, all interactions via the mouse and keyboard work as expected.I also simplified how activating items works by removing the
#activateItemmethod. Previously,#handleEventwould call#activateItemand then#handleItemActivated. The#activateItemmethod contained some logic to handle issues to override the default button click behavior, which included callingclick()on the button to simulate a mouse-driven click (as opposed to a keyboard-driven one). This was causing another form of the double activation issue I mentioned earlier, ashandleEventwould call#activateItemwhich would callclick()on the item, which would triggerhandleEventagain. This would eventually result in two calls to#handleItemActivated, which would check and then uncheck the item in rapid succession, appearing to have no effect.Accessibility
Merge checklist
- [ ] Added/updated documentation- [ ] Added/updated previews (Lookbook)Take a look at the What we look for in reviews section of the contributing guidelines for more information on how we review PRs.