question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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 of usePress under the hood, but add a timer to track how long the user has been pressing and only fire the onLongPress event after a threshold has been met. The keyboard events emitted by usePress should be ignored though, as individual components may wish to have different keyboard shortcuts for activating this behavior.
  • Call the useLongPress hook inside useMenuTrigger to handle opening the menu on long press. Also add support for the Alt + arrow key behavior to open the menu instead of the normal Enter/Space/Arrow keys. The trigger="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:closed
  • Created 3 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
devongovettcommented, Jul 18, 2020

Hey @Sunny-Nishan, thanks for taking a look at this!

  1. 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.

  2. 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 from usePress into that handler as well?

0reactions
devongovettcommented, Oct 6, 2021

I did some work on this recently in #2363

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found