primitives icon indicating copy to clipboard operation
primitives copied to clipboard

[ContextMenu] An open ContextMenu does not move after right clicking somewhere else

Open skratchdot opened this issue 2 years ago • 4 comments

Bug report

Current Behavior

I am wrapping a table row with a context menu trigger. On the first right-click, the context menu is opened and anchored next to my pointer. If I right-click a second time in a different location, the context menu re-opens anchored to where i first clicked (not where I right-clicked the 2nd time).

This differs from the native context menu behavior- and the behavior I would expect.

radix-context-menu

Expected behavior

Anytime I right-click, the context menu should be anchored next to the current right-click location.

Reproducible example

In this example: https://codesandbox.io/p/sandbox/xenodochial-minsky-fp4tmf

There is a table with 3 rows. The first row has a radix-ui context menu. The other rows use a native context menu.

Notice that each time you right-click on the 2nd and 3rd row, the native context menu updates to the latest position.

Suggested solution

I haven't looked at the code closely yet, but want to see how these refs are used: https://github.com/radix-ui/primitives/blob/c31c97274ff357aea99afe6c01c1c8c58b6356e0/packages/react/context-menu/src/ContextMenu.tsx#L97-L100

Additional context

I found someone already logged a bug here: https://github.com/radix-ui/themes/issues/147

(so I copied the title when logging this issue)

Your environment

Software Name(s) Version
Radix Package(s) @radix-ui/themes 2.0.3
React n/a 18
Browser firefox 121.0
Assistive tech
Node n/a 20.1.0
npm/yarn npm 9.6.4
Operating System macos 14.2.1

skratchdot avatar Dec 25 '23 04:12 skratchdot

I experience the exact same issue. I use next js and shadcn-ui context menu. Exactly same bug. I didn't use table. Just normal div can trigger this bug already. Is there anyone can kindly help have a look? Thanks!!

bxclib2 avatar Dec 27 '23 18:12 bxclib2

It seems the issue is due to having an exit animation, we have an example without exit animation in our storybook and it behaves fine: https://601857eb614bae0021c9b9dd-evgfcrkbbq.chromatic.com/?path=/story/components-contextmenu--multiple

So you could try that.

benoitgrelard avatar Mar 05 '24 14:03 benoitgrelard

I'm also not sure the native UX is great, as when doing another click, it has to wait for the exit fade, thus delaying opening at the new position, it feels worse as a user.

benoitgrelard avatar Mar 05 '24 14:03 benoitgrelard

I experience the exact same issue. I use next js and shadcn-ui context menu. Exactly same bug. I didn't use table. Just normal div can trigger this bug already. Is there anyone can kindly help have a look? Thanks!!

Just remove data-[state=closed]:animate-out in your ContextMenuContent component

Elvincth avatar May 17 '24 18:05 Elvincth