Add support for long press to MenuTrigger
See original GitHub issue🙋 Feature Request
MenuTrigger
currently supports opening a menu when a user presses or presses the enter or space key on a trigger element (e.g. button). It would be useful to also support opening it on long press instead of immediately. For example, in a toolbar UI, you could switch between multiple tools under the same button by opening a menu on long press. Long press to open a menu is also fairly common on mobile.
💁 Possible Solution
A few steps are required to implement this:
- Add a
useLongPress
hook to the@react-aria/interactions
package. This should make use ofusePress
under the hood, but add a timer to track how long the user has been pressing and only fire theonLongPress
event after a threshold has been met. The keyboard events emitted byusePress
should be ignored though, as individual components may wish to have different keyboard shortcuts for activating this behavior. - Call the
useLongPress
hook insideuseMenuTrigger
to handle opening the menu on long press. Also add support for theAlt
+ arrow key behavior to open the menu instead of the normal Enter/Space/Arrow keys. Thetrigger="longPress"
prop should be added to MenuTrigger to enable this behavior.
🔦 Context
Eventually we will support a hold affordance in Spectrum buttons to show that a menu is available on long press. See https://spectrum.adobe.com/page/action-button/#Hold-icon.
🧢 Your Company/Team
RSP
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (4 by maintainers)
Top Results From Across the Web
mat menu should trigger only on long press not on click event
I am using both click and long press event on the same element. Long press triggers the mat menu that is working fine...
Read more >MenuTrigger - React Spectrum Libraries - Adobe
Documentation for MenuTrigger in the React Spectrum package. ... This behavior can be changed by providing "longPress" to the trigger prop.
Read more >تويتر \ Devon Govett على تويتر: "New React Spectrum and React ...
Sometimes a button has a default action when it is pressed, but more options are also available under a long press menu. We've...
Read more >How to show a context menu - a free SwiftUI by Example tutorial
To try that out on iOS, long press on the “Options” text to bring up the menu, or right-click on macOS. It's worth...
Read more >Use Switch Control on Apple TV
To see the control menu, trigger your Select Item button before the Auto Press interval expires. The control menu skips the Press button...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Hey @Sunny-Nishan, thanks for taking a look at this!
Normally, buttons get triggered when you press the Enter or Space keys. In this case, clicking or activating the button with the keyboard may have a default operation, and long pressing or using Alt + ArrowDown/ArrowUp may activate the alternate operation of opening the menu. Using the Alt key modifier is a way to trigger this alternate behavior.
Yes possibly we could use
onPressStart
. I think the reason it’s a separate keyboard handler is because the behavior is different between the Enter and Space keys. Space doesn’t close the menu when selecting an item whereas Enter always closes it. I don’t think the press events expose which key was pressed. If there’s no way to replicate that behavior, maybe add the repeating check fromusePress
into that handler as well?I did some work on this recently in #2363