{"slug":"context-menu","title":"Context Menu","description":"Using the menu machine for context menus in your project.","contentType":"component","framework":"react","content":"An accessible dropdown and context menu that is used to display a list of\nactions or options that a user can choose when a trigger element is\nright-clicked or long pressed.\n\n## Resources\n\n\n[Latest version: v1.39.1](https://www.npmjs.com/package/@zag-js/menu)\n[Logic Visualizer](https://zag-visualizer.vercel.app/menu)\n[Source Code](https://github.com/chakra-ui/zag/tree/main/packages/machines/menu)\n\n\n\n**Features**\n\n- Supports items, labels, groups of items\n- Focus is fully managed using `aria-activedescendant` pattern\n- Typeahead to allow focusing items by typing text\n- Keyboard navigation support including arrow keys, home/end, page up/down\n\n## Installation\n\nInstall the menu package:\n\n```bash\nnpm install @zag-js/menu @zag-js/react\n# or\nyarn add @zag-js/menu @zag-js/react\n```\n\n## Anatomy\n\nCheck the menu anatomy and part names.\n\n> Each part includes a `data-part` attribute to help identify them in the DOM.\n\n\n\n## Usage\n\nImport the menu package:\n\n```tsx\nimport * as menu from \"@zag-js/menu\"\n```\n\nThe menu package exports two key functions:\n\n- `machine` - Behavior logic for the menu.\n- `connect` - Maps behavior to JSX props and event handlers.\n\n> Pass a unique `id` to `useMachine` so generated element ids stay predictable.\n\nThen use the framework integration helpers:\n\nTo show the menu when a trigger element is right-clicked, use\n`api.getContextTriggerProps()`.\n\nContext menus also open during a long-press of roughly `700ms` when the pointer\nis pen or touch.\n\n```tsx\nimport * as menu from \"@zag-js/menu\"\nimport { useMachine, normalizeProps } from \"@zag-js/react\"\nimport { useId } from \"react\"\n\nexport function ContextMenu() {\n  const service = useMachine(menu.machine, { id: useId() })\n  const api = menu.connect(service, normalizeProps)\n\n  return (\n    <div>\n      <div {...api.getContextTriggerProps()}>\n        <div>Open context menu</div>\n      </div>\n      <div {...api.getPositionerProps()}>\n        <ul {...api.getContentProps()}>\n          <li {...api.getItemProps({ value: \"edit\" })}>Edit</li>\n          <li {...api.getItemProps({ value: \"duplicate\" })}>Duplicate</li>\n          <li {...api.getItemProps({ value: \"delete\" })}>Delete</li>\n          <li {...api.getItemProps({ value: \"export\" })}>Export...</li>\n        </ul>\n      </div>\n    </div>\n  )\n}\n```\n\n### Default open state\n\nUse `defaultOpen` for an uncontrolled initial state.\n\n```tsx\nconst service = useMachine(menu.machine, {\n  defaultOpen: true,\n})\n```\n\n### Controlling open state\n\nUse `open` and `onOpenChange` to control the open state.\n\n```tsx\nconst service = useMachine(menu.machine, {\n  open,\n  onOpenChange(details) {\n    // details => { open: boolean }\n    setOpen(details.open)\n  },\n})\n```\n\n### Listening for highlighted items\n\nUse `onHighlightChange` to react when keyboard or pointer highlight changes.\n\n```tsx\nconst service = useMachine(menu.machine, {\n  onHighlightChange(details) {\n    // details => { highlightedValue: string | null }\n    console.log(details.highlightedValue)\n  },\n})\n```\n\n### Listening for item selection\n\nUse `onSelect` to react when an item is selected.\n\n```tsx\nconst service = useMachine(menu.machine, {\n  onSelect(details) {\n    // details => { value: string }\n    console.log(details.value)\n  },\n})\n```\n\n### Positioning the menu\n\nUse `positioning` to configure menu placement.\n\n```tsx\nconst service = useMachine(menu.machine, {\n  positioning: { placement: \"right-start\" },\n})\n```\n\n### Keeping the menu open after selection\n\nSet `closeOnSelect` to `false` to keep the menu open after selecting an item.\n\n```tsx\nconst service = useMachine(menu.machine, {\n  closeOnSelect: false,\n})\n```\n\n## Styling guide\n\nEach menu part includes a `data-part` attribute you can target in CSS.\n\n### Highlighted item state\n\nWhen an item is highlighted, via keyboard navigation or pointer, it is given a\n`data-highlighted` attribute.\n\n```css\n[data-part=\"item\"][data-highlighted] {\n  /* styles for highlighted state */\n}\n\n[data-part=\"item\"][data-type=\"radio|checkbox\"][data-highlighted] {\n  /* styles for highlighted state */\n}\n```\n\n### Disabled item state\n\nWhen an item or an option item is disabled, it is given a `data-disabled`\nattribute.\n\n```css\n[data-part=\"item\"][data-disabled] {\n  /* styles for disabled state */\n}\n\n[data-part=\"item\"][data-type=\"radio|checkbox\"][data-disabled] {\n  /* styles for disabled state */\n}\n```\n\n### Using arrows\n\nWhen using arrows within the menu, you can style it using CSS variables.\n\n```css\n[data-part=\"arrow\"] {\n  --arrow-size: 20px;\n  --arrow-background: red;\n}\n```\n\n### Checked option item state\n\nWhen an option item is checked, it is given a `data-state` attribute.\n\n```css\n[data-part=\"item\"][data-type=\"radio|checkbox\"][data-state=\"checked\"] {\n  /* styles for checked state */\n}\n```\n\n## Methods and Properties\n\n### Machine Context\n\nThe menu machine exposes the following context properties:\n\n<ContextTable name=\"menu\" />\n\n### Machine API\n\nThe menu `api` exposes the following methods:\n\n<ApiTable name=\"menu\" />\n\n### Data Attributes\n\n<DataAttrTable name=\"menu\" />\n\n### CSS Variables\n\n<CssVarTable name=\"menu\" />\n\n## Accessibility\n\nUses\n[aria-activedescendant](https://www.w3.org/WAI/ARIA/apg/patterns/menu-button/examples/menu-button-actions-active-descendant/)\npattern to manage focus movement among menu items.\n\n### Keyboard Interactions\n\n**`Space`**\nDescription: Activates/Selects the highlighted item\n\n**`Enter`**\nDescription: Activates/Selects the highlighted item\n\n**`ArrowDown`**\nDescription: Highlights the next item in the menu\n\n**`ArrowUp`**\nDescription: Highlights the previous item in the menu\n\n**`ArrowRight + ArrowLeft`**\nDescription: When focus is on trigger, opens or closes the submenu depending on reading direction.\n\n**`Esc`**\nDescription: Closes the context menu","package":"@zag-js/menu","editUrl":"https://github.com/chakra-ui/zag/edit/main/website/data/components/context-menu.mdx"}