---
title: "Context Menu · Ark UI — StyleX Blocks"
description: "A menu that opens at the pointer on right-click or long-press, with items, shortcuts, checkable items, and separators."
url: "https://stylexblocks.com/components/ark-ui/context-menu/"
agent:
  instructions: "Use this search API to find relevant components and blocks across this site with keywords or natural-language queries, instead of crawling individual pages. Results are returned in Markdown."
  endpoint: "https://stylexblocks.com/.well-known/search?q={query}"
  method: "GET"
  documentation: "https://developers.cloudflare.com/ai-search/how-to/search-for-agents/"
---

# Context Menu

A menu that opens at the pointer on right-click or long-press, with items, shortcuts, checkable items, and separators.

- Behavior engine: Ark UI
- Also available for: [Base UI](https://stylexblocks.com/components/base-ui/context-menu.md), [UI SDK](https://stylexblocks.com/components/ui-sdk/context-menu.md)
- Category: Display
- Variants: subtle, solid
- Sizes: sm, md
- HTML page: https://stylexblocks.com/components/ark-ui/context-menu/

## Install

Configure the project once (sets the behavior engine and import aliases):

```sh
npx stylexblocks init
```

Then add the item as source you own:

```sh
npx stylexblocks add context-menu
```

Equivalent: `npx shadcn add @stylexblocks/context-menu`. Registry item JSON: https://stylexblocks.com/r/ark-ui/context-menu.json

## Usage

```tsx
import { ContextMenu } from "#components/ui/ark-ui/context-menu.tsx";

export function Example() {
  return (
    // Right-click (or long-press) the trigger to open the menu at the
    // pointer. size/variant resolve once on Root; parts inherit them.
    <ContextMenu.Root size="md">
      <ContextMenu.Trigger asChild>
        <div>Right-click here</div>
      </ContextMenu.Trigger>
      <ContextMenu.Portal>
        <ContextMenu.Positioner>
          <ContextMenu.Content>
            <ContextMenu.Item value="reload">
              <ContextMenu.ItemText>Reload</ContextMenu.ItemText>
              <ContextMenu.ItemCommand>⌘R</ContextMenu.ItemCommand>
            </ContextMenu.Item>
            <ContextMenu.Separator />
            <ContextMenu.CheckboxItem value="bookmarks" checked onCheckedChange={() => {}}>
              <ContextMenu.ItemIndicator />
              <ContextMenu.ItemText>Show bookmarks</ContextMenu.ItemText>
            </ContextMenu.CheckboxItem>
          </ContextMenu.Content>
        </ContextMenu.Positioner>
      </ContextMenu.Portal>
    </ContextMenu.Root>
  );
}
```

## Source

`components/ui/ark-ui/context-menu.tsx`

```tsx
import * as stylex from "@stylexjs/stylex";
import { Menu as ArkMenu } from "@ark-ui/react/menu";
import { Portal } from "@ark-ui/react/portal";
import { createSlotRecipeContext } from "#lib/ark-ui/create-slot-recipe-context.tsx";
import {
  defineSlotRecipe,
  resolveSlotStyles,
  type StyleOverrideProps,
} from "#lib/recipe-props.ts";
import { semanticShadows } from "@stylexblocks/ui/semantic-tokens/shadows.stylex.ts";
import { cursor } from '@stylexblocks/ui/tokens/cursor.stylex.ts'
import { durations } from '@stylexblocks/ui/tokens/durations.stylex.ts'
import { fontSizes } from '@stylexblocks/ui/tokens/font-sizes.stylex.ts'
import { letterSpacings } from '@stylexblocks/ui/tokens/letter-spacings.stylex.ts'
import { lineHeights } from '@stylexblocks/ui/tokens/line-heights.stylex.ts'
import { semanticColors } from '@stylexblocks/ui/semantic-tokens/colors.stylex.ts'
import { semanticRadii } from '@stylexblocks/ui/semantic-tokens/radii.stylex.ts'
import { sizes } from '@stylexblocks/ui/tokens/sizes.stylex.ts'
import { spacing } from '@stylexblocks/ui/tokens/spacing.stylex.ts'
import { zIndices } from '@stylexblocks/ui/tokens/z-indices.stylex.ts'

const menuContent = stylex.create({
  base: {
    outline: 'none',
    color: semanticColors.fg,
    maxHeight: 'var(--available-height)',
    '--menu-z-index': zIndices.layer3,
    zIndex: 'calc(var(--menu-z-index) + var(--layer-index, 0))',
    overflowY: 'auto',
    scrollbarWidth: 'thin',
    transformOrigin: 'var(--transform-origin)',
    animationName: {
      default: null,
      ':is([data-open], [data-state="open"])': 'fade-in, slide-from-top',
      ':is([data-closed], [data-state="closed"])': 'fade-out',
    },
    animationDuration: {
      default: null,
      ':is([data-open], [data-state="open"])': durations.fast,
      ':is([data-closed], [data-state="closed"])': durations.faster,
    },
  },
})

const menuContentSizes = stylex.create({
  sm: {
    minWidth: '8rem',
    padding: spacing._1,
    borderRadius: semanticRadii.panel,
  },
  md: {
    minWidth: '8rem',
    padding: spacing._1,
  },
})

const menuItem = stylex.create({
  base: {
    textDecoration: 'none',
    color: semanticColors.fg,
    userSelect: 'none',
    borderRadius: semanticRadii.control,
    width: '100%',
    display: 'flex',
    cursor: {
      default: cursor.menuitem,
      ':is(:disabled, [disabled], [data-disabled])': 'not-allowed',
    },
    alignItems: 'center',
    textAlign: 'start',
    position: 'relative',
    flex: '0 0 auto',
    outline: 'none',
    opacity: {
      default: null,
      ':is(:disabled, [disabled], [data-disabled])': 0.5,
    },
  },
})

const menuItemSizes = stylex.create({
  sm: {
    gap: spacing._1,
    fontSize: fontSizes.xs,
    lineHeight: lineHeights.xs,
    minHeight: sizes._6,
    paddingInline: spacing._1_5,
    paddingInlineStart: `var(--menu-item-inset, ${spacing._1_5})`,
  },
  md: {
    gap: spacing._2,
    fontSize: fontSizes.sm,
    lineHeight: lineHeights.sm,
    minHeight: sizes._7,
    paddingInline: spacing._2,
    paddingInlineStart: `var(--menu-item-inset, ${spacing._2})`,
  },
})

const menuItemVariants = stylex.create({
  subtle: {},
  solid: {},
})

const menuItemText = stylex.create({
  base: {
    flex: 1,
  },
})

const menuItemGroupLabel = stylex.create({
  base: {
    display: 'flex',
    alignItems: 'center',
    fontSize: fontSizes.sm,
    lineHeight: lineHeights.sm,
    color: semanticColors.fgSubtle,
  },
})

const menuItemGroupLabelSizes = stylex.create({
  sm: {
    fontSize: fontSizes.xs,
    lineHeight: lineHeights.xs,
    minHeight: sizes._6,
    paddingInline: spacing._1_5,
  },
  md: {
    fontSize: fontSizes.sm,
    lineHeight: lineHeights.sm,
    minHeight: sizes._7,
    paddingInline: spacing._2,
  },
})

const menuIndicator = stylex.create({
  base: {
    display: 'inline-flex',
    alignItems: 'center',
    justifyContent: 'center',
    flexShrink: 0,
  },
})

const menuItemCommand = stylex.create({
  base: {
    opacity: 0.6,
    fontSize: fontSizes.xs,
    lineHeight: lineHeights.xs,
    marginInlineStart: 'auto',
    paddingInlineStart: spacing._4,
    letterSpacing: letterSpacings.widest,
  },
})

const menuSeparator = stylex.create({
  base: {
    height: '1px',
    backgroundColor: semanticColors.bgMuted,
    marginBlock: spacing._1,
    marginInline: spacing._1,
  },
})

export type MenuVariant = keyof typeof menuItemVariants

export type MenuSize = keyof typeof menuContentSizes

// defineSlotRecipe validates the shape at compile time: default values must
// exist in the axis maps, and slot keys must be `styles` or a declared axis
const menuRecipe = defineSlotRecipe({
  slots: {
    content: { styles: menuContent, sizes: menuContentSizes },
    item: { styles: menuItem, sizes: menuItemSizes, variants: menuItemVariants },
    itemText: { styles: menuItemText },
    itemGroupLabel: {
      styles: menuItemGroupLabel,
      sizes: menuItemGroupLabelSizes,
    },
    indicator: { styles: menuIndicator },
    itemCommand: { styles: menuItemCommand },
    separator: { styles: menuSeparator },
  },
  defaultVariants: { size: "md", variant: "subtle" },
});

// Local chrome the recipe leaves to the composition layer: the dropdown
// surface, keyboard/pointer highlight, and logical-property fixes for
// generated Chakra shorthands (`ms`/`ps`).
const chrome = stylex.create({
  content: {
    "--menu-z-index": zIndices.dropdown,
    backgroundColor: semanticColors.bgPanel,
    borderRadius: semanticRadii.panel,
    boxShadow: semanticShadows.overlay,
  },
  item: {
    paddingBlock: spacing._1,
    backgroundColor: {
      default: null,
      ":is([data-highlighted])": semanticColors.bgSubtle,
    },
  },
  itemCommand: {
    marginInlineStart: "auto",
    paddingInlineStart: spacing._4,
  },
  indicator: {
    width: "1rem",
    height: "1rem",
    display: "inline-flex",
    visibility: {
      default: "visible",
      ":is([hidden])": "hidden",
    },
  },
});

function CheckIcon(props: React.ComponentProps<"svg">) {
  return (
    <svg width="16" height="16" viewBox="0 0 16 16" fill="none" aria-hidden="true" {...props}>
      <path d="m3.5 8.5 3 3 6-7" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round" />
    </svg>
  );
}

const { withRootProvider, withContext } = createSlotRecipeContext(menuRecipe);

const StyledItemIndicator = withContext(
  ArkMenu.ItemIndicator,
  (v) => [...resolveSlotStyles(menuRecipe.slots.indicator, v), chrome.indicator],
  "ContextMenuItemIndicator",
);

function ItemIndicator(
  allProps: Omit<React.ComponentProps<typeof ArkMenu.ItemIndicator>, "style"> & StyleOverrideProps,
) {
  const { children, ...rest } = allProps;
  return (
    <StyledItemIndicator {...rest}>{children ?? <CheckIcon />}</StyledItemIndicator>
  );
}
ItemIndicator.displayName = "ContextMenuItemIndicator";

/**
 * ContextMenu — Ark's menu machine opened from a context trigger
 * (`Menu.ContextTrigger`): right-click, or long-press on touch, opens the
 * menu at the pointer. Everything else is the menu slot recipe as in Menu:
 * real portal to `document.body`, typeahead, keyboard navigation, and
 * highlight styles keyed off the machine's `data-highlighted`.
 * `<ContextMenu.Root size variant>` resolves the recipe once; every part
 * inherits it through context and composes via `asChild`. Portal and
 * Positioner stay unstyled engine parts.
 */
export const ContextMenu = {
  Root: withRootProvider(ArkMenu.Root, "ContextMenuRoot"),
  Trigger: ArkMenu.ContextTrigger,
  Context: ArkMenu.Context,
  Portal,
  Positioner: ArkMenu.Positioner,
  Content: withContext(
    ArkMenu.Content,
    (v) => [...resolveSlotStyles(menuRecipe.slots.content, v), chrome.content],
    "ContextMenuContent",
  ),
  Item: withContext(
    ArkMenu.Item,
    (v) => [...resolveSlotStyles(menuRecipe.slots.item, v), chrome.item],
    "ContextMenuItem",
  ),
  TriggerItem: withContext(
    ArkMenu.TriggerItem,
    (v) => [...resolveSlotStyles(menuRecipe.slots.item, v), chrome.item],
    "ContextMenuTriggerItem",
  ),
  ItemText: withContext(ArkMenu.ItemText, "itemText", "ContextMenuItemText"),
  ItemGroup: ArkMenu.ItemGroup,
  ItemGroupLabel: withContext(ArkMenu.ItemGroupLabel, "itemGroupLabel", "ContextMenuItemGroupLabel"),
  RadioItemGroup: ArkMenu.RadioItemGroup,
  RadioItem: withContext(
    ArkMenu.RadioItem,
    (v) => [...resolveSlotStyles(menuRecipe.slots.item, v), chrome.item],
    "ContextMenuRadioItem",
  ),
  CheckboxItem: withContext(
    ArkMenu.CheckboxItem,
    (v) => [...resolveSlotStyles(menuRecipe.slots.item, v), chrome.item],
    "ContextMenuCheckboxItem",
  ),
  ItemIndicator,
  ItemCommand: withContext(
    "span",
    (v) => [...resolveSlotStyles(menuRecipe.slots.itemCommand, v), chrome.itemCommand],
    "ContextMenuItemCommand",
  ),
  Separator: withContext(ArkMenu.Separator, "separator", "ContextMenuSeparator"),
};
```
