Ways to define a ContextMenu trigger element? #866
Replies: 2 comments 3 replies
-
You can use the <ContextMenu.Trigger>
{#snippet child({ props })}
<thead {...props}>
<!-- ... -->
</thead>
{/snippet}
</ContextMenu.Trigger> |
Beta Was this translation helpful? Give feedback.
-
I have found a working solution for now, but I am not sure how well it will pan out in the future for maintainability. {
width: 0,
height: 0,
x: event.clientX,
y: event.clientY,
top: event.clientY,
right: event.clientX,
bottom: event.clientY,
left: event.clientX
} So now I pass this virtual element to the Because my code is a mess I'll just outline the workflow.
The workflow is rough and probably needs polishing. |
Beta Was this translation helpful? Give feedback.
-
I am a bit confused. It is entirely possible that I don't fully understand Svelte5 and the bits-ui API.
I am using bits-ui@next for compatibility with Svelte5.
Here is what I want to achieve:
I have a
table
element in my component. I want to display aContextMenu
when the user right-clicks in thethead
section.If I understand the API examples correctly, I'll need to wrap my
thead
element in aContextMenu.Trigger
and then continue wrapping with the rest of the menu components (ContextMenu.Root
,ContextMenu.Content
,ContextMenu.Item
).This will result in a total mess.
Is that how
ContextMenu
is supposed to work? Is there a way to cut down on the wrapping to not interfere with the rest of the table markup code?On a semi-related note: This is a part of a hobby project of mine that now I started to transition to Svelte5 and bits-ui. Before this it was Svelte4 and skeleton. This part of the project (aka the context-menu on the
thead
) was implemented the following way. After thediv
that contained thetable
markup, anotherdiv
followed which contained the "context menu" markup. Thisdiv
was normally hidden. The right-click handler showed the hiddendiv
and repositioned it under the cursor with the help of thefloating-ui
library.Even though this approach is "cleaner" in the way of markup mess, it is full of tedious work and traps (
Esc
handling, reacting on lost focus, keyboard handling etc).Having described the above, I guess what I am looking for is for a way to define the contextmenu markup in its own space and somehow tell it to trigger on X element.
I am looking forward to your suggestions.
Beta Was this translation helpful? Give feedback.
All reactions