react-spectrum icon indicating copy to clipboard operation
react-spectrum copied to clipboard

Positioning popovers relative to a virtual element (support right-click context menus)

Open tryggvigy opened this issue 1 year ago โ€ข 1 comments

Provide a general summary of the feature here

Hey folks, I wanted to gauge interest in having first class support in react-aria / react-aria-components for patterns like right click context menus.

Perhaps this should only be supported for menus, in that case it could perhaps be supported via a new trigger-type

<MenuTrigger trigger="contextmenu">

๐Ÿค” Expected Behavior?

The first child of MenuTrigger could be any element that receives a onContextMenu prop to trigger it's menu on 'contextmenu' events.

There is probably a way to describe this functionality more accurately in the lower layers of react-aria and react-stately but I'm fairly new to this library and not familiar enough with the architecture :smi

๐Ÿ˜ฏ Current Behavior

Currently right click menus can be implemented by replacing the trigger ref with a {getBoundingClientRect: () => DOMRect } type and continuously updating the position of that DOMRect based on the mouse click coordinates. See code sandbox below

๐Ÿ’ Possible Solution

I don't want to speculate too much on the details of how the API should be structured since I've not spent enough time thinking about it. But one way would be

<MenuTrigger trigger="contextmenu">
  {/* The component that gets onContextMenu handler.
        Does not need to be interactive. */}
  <Box />
  {/* The menu to pop up */}
  <MyMenu />
</MenuTrigger>

๐Ÿ”ฆ Context

The usecase is to implement UIs similar to the native MacOS context menus image

or figmas context menus image

See floating-ui docs about virtual elements https://floating-ui.com/docs/virtual-elements

๐Ÿ’ป Examples

They can be implemented with some effort since react-aria is so incredibly flexible (love that).

See code sandbox

๐Ÿงข Your Company/Team

No response

๐Ÿ•ท Tracking Issue

No response

tryggvigy avatar May 21 '24 09:05 tryggvigy

I don't think we have a big interest in supporting context menus. Mostly because they are only for mouse users and are frequently (not on purpose though) used without other input methods being considered. It's a bit of a slippery slope argument though, so I won't say never. Only that it's not a priority. Linking to related Issue https://github.com/adobe/react-spectrum/issues/5020

snowystinger avatar May 21 '24 22:05 snowystinger

That's fair. It's possible albeit a bit awkward to do context menus today. Feel free to close this issue ๐Ÿ‘

tryggvigy avatar May 22 '24 08:05 tryggvigy

FWIW, this could be made easier to do, but it is possible to implement today using the offset and crossOffset props props on Popover. Basically force the placement to always bottom left, and then adjust the offset relative to that.

https://stackblitz.com/edit/rac-context-menu?file=src%2FApp.tsx

devongovett avatar May 25 '24 15:05 devongovett