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.

Pass triggering event on ContextMenu.Trigger to event handler

See original GitHub issue

Feature request

Overview

My application includes a map, which can be right-clicked in order to bring up more details. The map is rendered with WebGL, so is basically opaque to React. I’d like to be able to use the ContextMenu component and show more details about what was right-clicked. To make this possible, it would be nice to pass the event that triggered the context menu open to a method that looks up map details.

Who does this impact? Who is this for?

This will be nice if any of the things that trigger a context menu are, well, contextual to what was right-clicked. This map example is certainly one, but something like having one context menu at a top element and using, basically, delegation to trigger different right-click menus, would be an example.

Additional context

API might look like:

<Root onOpenChange={((open: boolean, event: React.MouseEvent | React.PointerEvent))}>

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:2
  • Comments:11 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
jjenzzcommented, Aug 25, 2021

So I’m thinking perhaps we raise a custom event/callback for when we generally detect an event (context menu or long press) that should open the menu.

We already have an onOpenChange that triggers when that happens so it sounds like the combination of the two is what is needed?

export default () => {
  const pointRef = React.useRef<number[] | null>(null);

  return (
    <ContextMenu.Root
      onOpenChange={(open) => {
        if (open) {
          queryRenderedFeatures(pointRef.current);
        } else {
          pointRef.current = null;
		}
      }}
    >
      <ContextMenu.Trigger
        as="canvas"
        onContextMenu={(event) => (pointRef.current = [event.clientX, event.clientY])}
      />
      <ContextMenu.Content>
        <ContextMenu.Item>One</ContextMenu.Item>
        <ContextMenu.Item>Two</ContextMenu.Item>
        <ContextMenu.Item>Three</ContextMenu.Item>
      </ContextMenu.Content>
    </ContextMenu.Root>
  );
}

https://codesandbox.io/s/weathered-bash-2ze4e

1reaction
tmcwcommented, Jul 5, 2021

I’m curious cause you say it’s basically a webGL thing, opaque to React. Would you be working out what to show based on the pointer position in the event?

Yep, exactly this. In particular, using Mapbox, which gives you a method to query features at a particular point within the WebGL canvas, based on the offset from the top-left corner.

If you’re wanting to render information about something then it’s probably not the appropriate use of the ContextMenu component which is meant for menus.

This will be a menu - basically, think of it as the same as the right-click menu on Figma. For example: right click on a stack of overlapping features and choose which feature to select.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Element: contextmenu event - Web APIs | MDN
The contextmenu event fires when the user attempts to open a context menu. This event is typically triggered by clicking the right mouse ......
Read more >
.contextmenu() | jQuery API Documentation
An object containing data that will be passed to the event handler. ... A function to execute each time the event is triggered....
Read more >
.contextmenu() - API Manual
Description: Bind an event handler to the "contextmenu" JavaScript event, or trigger that event on an element. version added: 1.0.contextmenu( handler ).
Read more >
Trigger right click using pure Javascript - Stack Overflow
Specifically, I've been able to stop a right click event from occurring on one element and in the same handler fire the context...
Read more >
Documentation - jQuery contextMenu (2.x)
The show and hide events are triggered before the menu is shown or hidden. The event handlers are executed in the context of...
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