Context Menu
Opens a set of link actions from a right-click, a keyboard shortcut, or a visible button.
pnpm dlx shadcn@latest add @haze/context-menu---
import { cn } from "../../../lib/haze/utils";
import DropdownMenu from "./dropdown-menu.astro";
interface MenuItem {
label: string;
href?: string;
hint?: string;
destructive?: boolean;
separatorBefore?: boolean;
heading?: string;
icon?: (props: Record<string, unknown>) => unknown;
/** Makes the item a submenu trigger. One level. */
items?: { label: string; href: string; hint?: string; destructive?: boolean; separatorBefore?: boolean; icon?: (props: Record<string, unknown>) => unknown }[];
}
interface Props {
id: string;
items?: MenuItem[];
label?: string;
menuLabel?: string;
class?: string;
}
const {
id, items = [{ label: "View overview", href: "#overview" }], label = "Item actions",
menuLabel = "Actions", class: className,
} = Astro.props;
---
<div data-haze-context-menu class={cn("relative grid min-w-0 gap-2", className)}>
<div role="group" aria-label={label} aria-keyshortcuts="Shift+F10" data-context-target
class="min-w-0 rounded-xl border border-border bg-card p-4.5 text-card-foreground focus-visible:outline-2 focus-visible:outline-offset-1 focus-visible:outline-ring">
<slot><p class="text-[0.8125rem]/5 font-medium">Untitled item</p></slot>
</div>
<div class="justify-self-start">
<DropdownMenu id={id} label={menuLabel} items={items} size="sm"
class="data-[context-point]:m-0! data-[context-point]:[position-area:none]! data-[context-point]:inset-[var(--haze-menu-y)_auto_auto_var(--haze-menu-x)]! data-[context-point]:max-w-[calc(100vw-1rem)]" />
</div>
</div>
<script>
function enhance(root: HTMLElement) {
if (root.hasAttribute("data-enhanced")) return;
const target = root.querySelector<HTMLElement>("[data-context-target]");
const menu = root.querySelector<HTMLElement>("[data-haze-menu]");
const trigger = root.querySelector<HTMLButtonElement>("[popovertarget]");
if (!target || !menu || !trigger || typeof menu.showPopover !== "function") return;
const links = Array.from(menu.querySelectorAll<HTMLElement>("a[href], button[data-submenu-trigger]")).filter((item) => !item.closest("[data-submenu-panel]"));
if (!links.length) return;
const openAt = (x: number, y: number) => {
target.focus({ preventScroll: true });
menu.setAttribute("data-context-point", "");
menu.style.setProperty("--haze-menu-x", "0px");
menu.style.setProperty("--haze-menu-y", "0px");
if (!menu.matches(":popover-open")) menu.showPopover();
const left = Math.max(8, Math.min(x, document.documentElement.clientWidth - menu.offsetWidth - 8));
const top = Math.max(8, Math.min(y, window.innerHeight - menu.offsetHeight - 8));
menu.style.setProperty("--haze-menu-x", `${left}px`);
menu.style.setProperty("--haze-menu-y", `${top}px`);
links[0].focus({ preventScroll: true });
};
target.addEventListener("contextmenu", (event) => {
event.preventDefault();
openAt(event.clientX, event.clientY);
});
target.addEventListener("keydown", (event) => {
if (event.target !== target || (event.key !== "ContextMenu" && !(event.shiftKey && event.key === "F10"))) return;
event.preventDefault();
const rect = target.getBoundingClientRect();
openAt(rect.left, rect.bottom + 4);
});
trigger.addEventListener("click", () => menu.removeAttribute("data-context-point"));
menu.addEventListener("keydown", (event) => {
if (event.key === "Escape" && menu.hasAttribute("data-context-point")) {
event.preventDefault();
event.stopPropagation();
menu.hidePopover();
target.focus({ preventScroll: true });
return;
}
// Arrow, Home, End and typeahead navigation, and submenu keys, live in dropdown-menu.astro.
});
target.tabIndex = 0;
root.setAttribute("data-enhanced", "");
}
const init = () => document.querySelectorAll<HTMLElement>("[data-haze-context-menu]").forEach(enhance);
init();
if (!document.documentElement.hasAttribute("data-haze-context-menu-listener")) {
document.documentElement.setAttribute("data-haze-context-menu-listener", "");
document.addEventListener("astro:page-load", init);
}
</script>When to use
- A file, card, or list item needs contextual navigation shortcuts.
- The same actions should be discoverable by touch and keyboard.
When not to use
- A primary action needs to stay prominent on the item itself.
Installation
Add the Haze registry to components.json once, then install Context Menu with the shadcn CLI. Works in any Astro project with Tailwind CSS v4.
pnpm dlx shadcn@latest add @haze/context-menunpx shadcn@latest add @haze/context-menuyarn dlx shadcn@latest add @haze/context-menubunx --bun shadcn@latest add @haze/context-menuFirst time? Registry and theme setup
1. Add to components.json:
{
"registries": {
"@haze": "http://localhost:4329/r/{name}.json"
}
}2. Install the theme once: npx shadcn@latest add @haze/theme, then import it after Tailwind in your global CSS:@import "./haze.css";
API reference
| Prop | Type | Default | Description |
|---|---|---|---|
| id* | string | – | Unique popover ID. |
| items | MenuItem[] | – | Links with labels and optional icons, hints, and grouping. An item with items (and no href) becomes a one-level submenu. |
| label | string | Item actions | Accessible name of the target region. |
| menuLabel | string | Actions | Label of the visible menu button. |
| class | string | – | Classes for the target and menu wrapper. |
Keyboard interactions
- ShiftF10
- Open the menu while the target is focused.
- ↑↓
- Move between links in the menu.
- HomeEnd
- Focus the first or last link.
- →EnterSpace
- Open a submenu and focus its first link.
- ←Escape
- Close a submenu and return to its item.
- Escape
- Dismiss the menu.
Accessibility
- The visible menu button opens the same links even when scripting is disabled.
- Escape from a context-opened popup returns focus to the target.
- Submenus work as in the dropdown menu: the trigger has aria-haspopup="menu" and aria-expanded, and without scripting the submenu links list inline.
Notes for AI
Copy into Claude Code, Cursor, Codex or any coding agent. Agents can also read the Markdown version of this page.
Install and use
Install the Haze UI component "Context Menu" with `npx shadcn@latest add @haze/context-menu`. If components.json has no "@haze" registry yet, add "registries": { "@haze": "http://localhost:4329/r/{name}.json" }. If src/styles/haze.css does not exist yet, run `npx shadcn@latest add @haze/theme` once and import it from the global stylesheet after Tailwind. The CLI stops at an overwrite question when a Haze file already exists and differs, even with --yes, and then installs nothing after it: read its output. If the user has not edited their Haze files, re-run with --overwrite to take the registry versions; if they have, ask them before overwriting. Then import ContextMenu from "@/components/haze/ui/context-menu.astro" (or the relative path) and render it where it fits the page. Keep Haze token classes (bg-background, text-muted-foreground, border-border...) and never hardcode colors. Docs: http://localhost:4329/ui/context-menu.md
Customize with real content
Adapt the Haze context-menu ui for our product. Replace the demo values of these props with our real content: id, items, label, menuLabel, class. Keep the layout, the heading level rules and these accessibility details: The visible menu button opens the same links even when scripting is disabled. Escape from a context-opened popup returns focus to the target. Submenus work as in the dropdown menu: the trigger has aria-haspopup="menu" and aria-expanded, and without scripting the submenu links list inline. Do not add raw color classes; use the Haze tokens.
Choose the right block
Before using context-menu, check it fits: use it when a file, card, or list item needs contextual navigation shortcuts.; the same actions should be discoverable by touch and keyboard.. Avoid it when a primary action needs to stay prominent on the item itself.. Alternatives: @haze/dropdown-menu, @haze/popover, @haze/command-menu.
Dependencies
- @haze/dropdown-menu
- @haze/utils
- @haze/theme (one-time setup)
Source · Original Haze UI work.