Radix UI Context Menu Not Working — Fix Right-Click
In this fix, you'll learn how to implement right-click context menus. This matters for power user interactions, and appears in file managers and code editors.
The Problem
The following code demonstrates the typical mistake:
<ContextMenu>
<div className="target-area">
Right-click here
</div>
<ContextMenuContent>
<ContextMenuItem>Edit</ContextMenuItem>
<ContextMenuItem>Delete</ContextMenuItem>
</ContextMenuContent>
</ContextMenu>
// Right-click shows browser context menu, not Radix menu
The Fix
Here is the corrected version:
<ContextMenu>
<ContextMenuTrigger className="block p-8 border-2 border-dashed rounded-lg">
<p>Right-click anywhere in this area</p>
</ContextMenuTrigger>
<ContextMenuContent className="w-48">
<ContextMenuItem inset onClick={() => edit()}>
<Pencil className="mr-2 h-4 w-4" />
Edit
</ContextMenuItem>
<ContextMenuItem inset onClick={() => delete()}>
<Trash className="mr-2 h-4 w-4" />
Delete
</ContextMenuItem>
<ContextMenuSeparator />
<ContextMenuItem inset disabled>
Properties
</ContextMenuItem>
</ContextMenuContent>
</ContextMenu>
// Right-click shows Radix context menu with custom items
Prevention
- Wrap the trigger target element with ContextMenuTrigger, not just any div.
- ContextMenuTrigger prevents the browser's default context menu on the wrapped element.
- Use inset on ContextMenuItem for proper icon spacing.
- Use ContextMenuSeparator to group related actions.
- ContextMenuCheckboxItem and ContextMenuRadioItem work inside context menus.
Common Mistakes with ui context menu
- Overlapping type class instances that cause GHC to reject the program with ambiguous dispatch errors
- Non-exhaustive pattern matches that compile with warnings then crash at runtime
- Misunderstanding that
Stringis[Char]with poor performance for large text operations
These mistakes appear frequently in real-world RADIX code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro