Context Menu
A right-click context menu with items, labels, shortcuts, separators, and nested sub-menus.
bun x bosia@latest add context-menuA right-click triggered menu that appears at the cursor position. Supports items, labels, keyboard shortcut hints, separators, and nested sub-menus.
Preview
Right click here
Sub-components
ContextMenu— root wrapper, manages open state and cursor coordinatesContextMenuTrigger— wraps the right-clickable areaContextMenuContent— fixed-positioned panel at cursor coordinatesContextMenuItem— action item (optionaldisabledprop)ContextMenuSeparator— divider between itemsContextMenuLabel— non-interactive group headingContextMenuShortcut— keyboard hint displayed on the rightContextMenuSub— sub-menu state managerContextMenuSubTrigger— item with arrow that opens a sub-menuContextMenuSubContent— fly-out panel for sub-menu
Usage
<script lang="ts">
import {
ContextMenu,
ContextMenuTrigger,
ContextMenuContent,
ContextMenuItem,
ContextMenuSeparator,
ContextMenuLabel,
ContextMenuShortcut,
} from "$lib/components/ui/context-menu";
</script>
<ContextMenu>
<ContextMenuTrigger>
<div class="h-36 w-72 border border-dashed rounded-md flex items-center justify-center">
Right click here
</div>
</ContextMenuTrigger>
<ContextMenuContent>
<ContextMenuLabel>Actions</ContextMenuLabel>
<ContextMenuSeparator />
<ContextMenuItem onclick={() => console.log("back")}>
Back
<ContextMenuShortcut>⌘[</ContextMenuShortcut>
</ContextMenuItem>
<ContextMenuItem onclick={() => console.log("reload")}>
Reload
<ContextMenuShortcut>⌘R</ContextMenuShortcut>
</ContextMenuItem>
</ContextMenuContent>
</ContextMenu>Sub-menus
<script lang="ts">
import {
ContextMenu,
ContextMenuTrigger,
ContextMenuContent,
ContextMenuItem,
ContextMenuSub,
ContextMenuSubTrigger,
ContextMenuSubContent,
} from "$lib/components/ui/context-menu";
</script>
<ContextMenu>
<ContextMenuTrigger>
<div class="h-36 w-72 border border-dashed rounded-md flex items-center justify-center">
Right click here
</div>
</ContextMenuTrigger>
<ContextMenuContent>
<ContextMenuItem>Back</ContextMenuItem>
<ContextMenuSub>
<ContextMenuSubTrigger>More Tools</ContextMenuSubTrigger>
<ContextMenuSubContent>
<ContextMenuItem>Save Page As...</ContextMenuItem>
<ContextMenuItem>Developer Tools</ContextMenuItem>
</ContextMenuSubContent>
</ContextMenuSub>
</ContextMenuContent>
</ContextMenu>Disabled Items
<ContextMenuItem disabled>Forward</ContextMenuItem>