> For the complete documentation index, see [llms.txt](https://docs.rierino.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.rierino.com/sdk-core-ui/components/menu.md). Agents can also search and read this documentation through its [MCP server](https://docs.rierino.com/~gitbook/mcp).

# Menu

Shows a list of actions or options when a button is pressed.

{% code title="menu/demo.tsx" %}

```tsx
import { useState } from "react";
import { Button, Menu } from "@rierino-open/core-ui";
import { Copy, Pencil, Trash2 } from "lucide-react";

export default function MenuDemo() {
  const [last, setLast] = useState("nothing yet");

  return (
    <div style={{ display: "grid", gap: 12, justifyItems: "center", fontSize: 12 }}>
      <Menu>
        <Button intent="g-button">Actions</Button>
        <Menu.Content aria-label="Product actions" onAction={(key) => setLast(String(key))}>
          <Menu.Item id="edit">
            <Pencil data-slot="icon" />
            Edit
          </Menu.Item>
          <Menu.Item id="duplicate">
            <Copy data-slot="icon" />
            Duplicate
          </Menu.Item>
          <Menu.Separator />
          <Menu.Item id="delete" isDanger>
            <Trash2 data-slot="icon" />
            Delete
          </Menu.Item>
        </Menu.Content>
      </Menu>
      <span>Last action: {last}</span>
    </div>
  );
}
```

{% endcode %}

## Usage

{% tabs %}
{% tab title="React app" %}

```tsx
import { Button, Menu } from '@rierino-open/core-ui';

<Menu>
  <Button intent="g-button">Actions</Button>
  <Menu.Content aria-label="Product actions" onAction={(key) => run(key)}>
    <Menu.Item id="edit">Edit</Menu.Item>
    <Menu.Item id="duplicate">Duplicate</Menu.Item>
  </Menu.Content>
</Menu>
```

{% endtab %}

{% tab title="Hosted editor" %}

```jsx
import CoreUI from '@rierino-open/core-ui';
const { Button, Menu } = CoreUI;

<Menu>
  <Button intent="g-button">Actions</Button>
  <Menu.Content aria-label="Product actions" onAction={(key) => run(key)}>
    <Menu.Item id="edit">Edit</Menu.Item>
    <Menu.Item id="duplicate">Duplicate</Menu.Item>
  </Menu.Content>
</Menu>
```

{% endtab %}
{% endtabs %}

`Menu` takes two children: the trigger, and `Menu.Content` with the items. The trigger can be any Core UI `Button`, so it looks like the rest of your screen. `Menu.Trigger` is also available, but it is an unstyled button.

When an item is chosen, `onAction` on `Menu.Content` is called with the item's `id` and the menu closes. You can also put `onAction` on a single `Menu.Item`.

### Parts

| Part               | Purpose                                                         |
| ------------------ | --------------------------------------------------------------- |
| `Menu`             | Connects the trigger to the menu and holds the open state       |
| `Menu.Trigger`     | An unstyled button to open the menu. Any `Button` works instead |
| `Menu.Content`     | The popover with the list of items                              |
| `Menu.Item`        | An action                                                       |
| `Menu.ItemDetails` | A label with a description, inside an item                      |
| `Menu.Header`      | A heading above a group of items                                |
| `Menu.Separator`   | A line between groups of items                                  |
| `Menu.Checkbox`    | An item that can be checked, with `selectionMode="multiple"`    |
| `Menu.Radio`       | An item in a pick-one list, with `selectionMode="single"`       |
| `Menu.Submenu`     | An item that opens a nested menu                                |
| `Menu.Primitive`   | React Aria's `Menu`, unstyled, for menus without a popover      |

## Examples

### Icon-only trigger

A plain, square `Button` with an icon is the usual trigger for "more" menus in table rows and cards. Give it an `aria-label`, since it has no text.

{% code title="menu/icon-trigger.tsx" %}

```tsx
import { Button, Menu } from "@rierino-open/core-ui";
import { MoreVertical } from "lucide-react";

export default function MenuIconTrigger() {
  return (
    <Menu>
      <Button appearance="plain" size="square-petite" aria-label="More options">
        <MoreVertical data-slot="icon" />
      </Button>
      <Menu.Content aria-label="More options" placement="bottom end">
        <Menu.Item>Rename</Menu.Item>
        <Menu.Item>Move to folder</Menu.Item>
        <Menu.Item>Export as CSV</Menu.Item>
      </Menu.Content>
    </Menu>
  );
}
```

{% endcode %}

Mark icons in items with `data-slot="icon"` so they are sized and spaced like the demo at the top.

### Labels with descriptions

`Menu.ItemDetails` shows a label with a description under it. Set `textValue` on the item to its plain label, so type-ahead and screen readers have text to use. `isDisabled` shows an item that can't be chosen.

{% code title="menu/item-details.tsx" %}

```tsx
import { Button, Menu } from "@rierino-open/core-ui";

export default function MenuItemDetails() {
  return (
    <Menu>
      <Button appearance="outline">Publish</Button>
      <Menu.Content aria-label="Publish options" className="min-w-64">
        <Menu.Item textValue="Publish now">
          <Menu.ItemDetails label="Publish now" description="Visible to customers immediately" />
        </Menu.Item>
        <Menu.Item textValue="Schedule">
          <Menu.ItemDetails label="Schedule" description="Pick a date and time" />
        </Menu.Item>
        <Menu.Item textValue="Save as draft" isDisabled>
          <Menu.ItemDetails label="Save as draft" description="Already a draft" />
        </Menu.Item>
      </Menu.Content>
    </Menu>
  );
}
```

{% endcode %}

### Headers and separators

`Menu.Header` labels a group of items, and `separator` draws a line under it. `Menu.Separator` draws a line between items.

{% code title="menu/sections.tsx" %}

```tsx
import { Button, Menu } from "@rierino-open/core-ui";

export default function MenuSections() {
  return (
    <Menu>
      <Button appearance="outline">Account</Button>
      <Menu.Content aria-label="Account">
        <Menu.Header separator>ada@example.com</Menu.Header>
        <Menu.Item>Profile</Menu.Item>
        <Menu.Item>Preferences</Menu.Item>
        <Menu.Separator />
        <Menu.Item>Sign out</Menu.Item>
      </Menu.Content>
    </Menu>
  );
}
```

{% endcode %}

### Checkbox and radio items

Set `selectionMode` on `Menu.Content` to let items stay selected. Use `Menu.Checkbox` items with `"multiple"` and `Menu.Radio` items with `"single"`. `selectedKeys` and `onSelectionChange` work with a `Set` of item `id`s.

{% code title="menu/selection.tsx" %}

```tsx
import { useState } from "react";
import { Button, Menu } from "@rierino-open/core-ui";
import type { Selection } from "react-aria-components";

export default function MenuSelection() {
  const [columns, setColumns] = useState<Selection>(new Set(["name", "price"]));
  const [sort, setSort] = useState<Selection>(new Set(["newest"]));

  return (
    <div style={{ display: "flex", gap: 12 }}>
      <Menu>
        <Button appearance="outline">Columns</Button>
        <Menu.Content
          aria-label="Visible columns"
          selectionMode="multiple"
          selectedKeys={columns}
          onSelectionChange={setColumns}
        >
          <Menu.Checkbox id="name">Name</Menu.Checkbox>
          <Menu.Checkbox id="price">Price</Menu.Checkbox>
          <Menu.Checkbox id="stock">Stock</Menu.Checkbox>
        </Menu.Content>
      </Menu>

      <Menu>
        <Button appearance="outline">Sort</Button>
        <Menu.Content
          aria-label="Sort by"
          selectionMode="single"
          selectedKeys={sort}
          onSelectionChange={setSort}
        >
          <Menu.Radio id="newest">Newest first</Menu.Radio>
          <Menu.Radio id="price-asc">Price, low to high</Menu.Radio>
          <Menu.Radio id="price-desc">Price, high to low</Menu.Radio>
        </Menu.Content>
      </Menu>
    </div>
  );
}
```

{% endcode %}

### Submenus

`Menu.Submenu` takes two children: the item that opens the submenu, and a `Menu.Content` with its items. The item shows an arrow, and the submenu opens on hover, click or `→`.

{% code title="menu/submenu.tsx" %}

```tsx
import { Button, Menu } from "@rierino-open/core-ui";

export default function MenuSubmenu() {
  return (
    <Menu>
      <Button appearance="outline">File</Button>
      <Menu.Content aria-label="File">
        <Menu.Item>New product</Menu.Item>
        <Menu.Submenu>
          <Menu.Item>Export</Menu.Item>
          <Menu.Content aria-label="Export formats">
            <Menu.Item>CSV</Menu.Item>
            <Menu.Item>Excel</Menu.Item>
            <Menu.Item>JSON</Menu.Item>
          </Menu.Content>
        </Menu.Submenu>
        <Menu.Item>Print</Menu.Item>
      </Menu.Content>
    </Menu>
  );
}
```

{% endcode %}

### Items from data

Pass an array as `items` and a function as the child to render each one. Each object needs an `id`, or a `key`.

{% code title="menu/dynamic.tsx" %}

```tsx
import { Button, Menu } from "@rierino-open/core-ui";

const locales = [
  { id: "en-US", name: "English (US)" },
  { id: "tr-TR", name: "Türkçe" },
  { id: "de-DE", name: "Deutsch" },
  { id: "ar-SA", name: "العربية" },
];

export default function MenuDynamic() {
  return (
    <Menu>
      <Button appearance="outline">Language</Button>
      <Menu.Content aria-label="Language" items={locales}>
        {(locale) => <Menu.Item id={locale.id}>{locale.name}</Menu.Item>}
      </Menu.Content>
    </Menu>
  );
}
```

{% endcode %}

### Links

An item with `href` is a link. `target` and the other link attributes work as on `<a>`.

{% code title="menu/links.tsx" %}

```tsx
import { Button, Menu } from "@rierino-open/core-ui";
import { ExternalLink } from "lucide-react";

export default function MenuLinks() {
  return (
    <Menu>
      <Button appearance="outline">Help</Button>
      <Menu.Content aria-label="Help">
        <Menu.Item href="https://docs.rierino.com" target="_blank">
          <ExternalLink data-slot="icon" />
          Documentation
        </Menu.Item>
        <Menu.Item href="https://github.com/rierino-open/core-ui" target="_blank">
          <ExternalLink data-slot="icon" />
          Source code
        </Menu.Item>
      </Menu.Content>
    </Menu>
  );
}
```

{% endcode %}

### Placement and arrow

`placement` sets where the menu opens relative to the trigger, for example `"bottom end"`, `"right top"` or `"top"`. The menu flips to the other side when there isn't room. `showArrow` adds an arrow pointing at the trigger.

{% code title="menu/placement.tsx" %}

```tsx
import { Button, Menu } from "@rierino-open/core-ui";

export default function MenuPlacement() {
  return (
    <div style={{ display: "flex", gap: 12 }}>
      <Menu>
        <Button appearance="outline">Right</Button>
        <Menu.Content aria-label="Placed right" placement="right top" showArrow>
          <Menu.Item>One</Menu.Item>
          <Menu.Item>Two</Menu.Item>
        </Menu.Content>
      </Menu>
      <Menu>
        <Button appearance="outline">Top</Button>
        <Menu.Content aria-label="Placed top" placement="top" showArrow>
          <Menu.Item>One</Menu.Item>
          <Menu.Item>Two</Menu.Item>
        </Menu.Content>
      </Menu>
    </div>
  );
}
```

{% endcode %}

### Controlled open state

Pass `isOpen` and `onOpenChange` to `Menu` to know when the menu opens, or to open it from code.

{% code title="menu/controlled.tsx" %}

```tsx
import { useState } from "react";
import { Button, Menu } from "@rierino-open/core-ui";

export default function MenuControlled() {
  const [open, setOpen] = useState(false);

  return (
    <div style={{ display: "grid", gap: 12, justifyItems: "center", fontSize: 12 }}>
      <Menu isOpen={open} onOpenChange={setOpen}>
        <Button appearance="outline">Filters</Button>
        <Menu.Content aria-label="Filters">
          <Menu.Item>In stock</Menu.Item>
          <Menu.Item>On sale</Menu.Item>
        </Menu.Content>
      </Menu>
      <span>The menu is {open ? "open" : "closed"}</span>
    </div>
  );
}
```

{% endcode %}

## On small screens

On screens up to 600px wide, the menu opens as a full-width sheet at the bottom of the screen instead of next to the trigger. Set `respectScreen={false}` on `Menu` to keep it next to the trigger at every size.

## Keyboard

| Key                   | Action                                                    |
| --------------------- | --------------------------------------------------------- |
| `Enter`, `Space`, `↓` | On the trigger: opens the menu and focuses the first item |
| `↑`                   | On the trigger: opens the menu and focuses the last item  |
| `↓` `↑`               | Moves between items                                       |
| `Home` `End`          | Moves to the first or last item                           |
| Letters               | Moves to the next item starting with the typed text       |
| `Enter`, `Space`      | Activates the focused item                                |
| `→` `←`               | Opens or closes a submenu                                 |
| `Esc`                 | Closes the menu and returns focus to the trigger          |

## Accessibility

* **Name every menu** with `aria-label` on `Menu.Content`, such as "Product actions".
* Items whose content isn't plain text need a `textValue`, used for type-ahead and announced by screen readers.
* The trigger is announced as a button that opens a menu, and gets `aria-expanded` while it is open.
* `isDanger` only changes the item's styling. Make the label itself say what happens, such as "Delete product".

## Styling

The menu takes its background from `--primary` and its text colour from `--primary-fg`. See [theme variables](https://docs.rierino.com/sdk-core-ui/foundations/colors.md#theme-variables).

| Target                | How                                                                |
| --------------------- | ------------------------------------------------------------------ |
| The list              | `className` on `Menu.Content`                                      |
| The popover around it | `popoverClassName` on `Menu.Content`                               |
| One item              | `className` on `Menu.Item`, as a string or a function of its state |

Items set these attributes for state styling:

| Attribute          | Present when                                         |
| ------------------ | ---------------------------------------------------- |
| `data-focused`     | The item has focus, from the keyboard or the pointer |
| `data-hovered`     | The pointer is over the item                         |
| `data-disabled`    | `isDisabled` is set                                  |
| `data-selected`    | A `Menu.Checkbox` or `Menu.Radio` is selected        |
| `data-danger`      | `isDanger` is set                                    |
| `data-has-submenu` | The item opens a submenu                             |
| `data-open`        | The item's submenu is open                           |

## API reference

### Menu

| Prop            | Type                          | Default   | Description                                                                                                        |
| --------------- | ----------------------------- | --------- | ------------------------------------------------------------------------------------------------------------------ |
| `isOpen`        | `boolean`                     |           | Whether the overlay is open by default (controlled).                                                               |
| `defaultOpen`   | `boolean`                     |           | Whether the overlay is open by default (uncontrolled).                                                             |
| `onOpenChange`  | `((isOpen: boolean) => void)` |           | Handler that is called when the overlay's open state changes.                                                      |
| `trigger`       | `MenuTriggerType`             | `'press'` | How the menu is triggered.                                                                                         |
| `respectScreen` | `boolean`                     | `true`    | On screens up to 600px wide, shows the menu as a sheet at the bottom of the screen instead of next to the trigger. |

### Menu.Content

| Prop                  | Type                          | Default    | Description                                                                                                                                        |
| --------------------- | ----------------------------- | ---------- | -------------------------------------------------------------------------------------------------------------------------------------------------- |
| `aria-label`          | `string`                      |            | Defines a string value that labels the current element.                                                                                            |
| `selectionMode`       | `SelectionMode`               |            | The type of selection that is allowed in the collection.                                                                                           |
| `selectedKeys`        | `"all" \| Iterable<Key>`      |            | The currently selected keys in the collection (controlled).                                                                                        |
| `defaultSelectedKeys` | `"all" \| Iterable<Key>`      |            | The initial selected keys in the collection (uncontrolled).                                                                                        |
| `onSelectionChange`   | `((keys: Selection) => void)` |            | Handler that is called when the selection changes.                                                                                                 |
| `items`               | `Iterable<object>`            |            | Item objects in the collection.                                                                                                                    |
| `disabledKeys`        | `Iterable<Key>`               |            | The item keys that are disabled. These items cannot be selected, focused, or otherwise interacted with.                                            |
| `placement`           | `Placement`                   | `'bottom'` | The placement of the element with respect to its anchor element.                                                                                   |
| `shouldFlip`          | `boolean`                     | `true`     | Whether the element should flip its orientation (e.g. top to bottom or left to right) when there is insufficient room for it to render completely. |
| `onAction`            | `((key: Key) => void)`        |            | Handler that is called when an item is selected.                                                                                                   |
| `offset`              | `number`                      | `8`        | The additional offset applied along the main axis between the element and its anchor element.                                                      |
| `className`           | `string`                      |            | Classes added to the menu list.                                                                                                                    |
| `popoverClassName`    | `string`                      |            | Classes added to the popover around the menu.                                                                                                      |
| `showArrow`           | `boolean`                     | `false`    | Shows an arrow pointing at the trigger.                                                                                                            |

### Menu.Item

`Menu.Checkbox` and `Menu.Radio` take the same props.

| Prop         | Type                                  | Default | Description                                                                                                                                                                        |
| ------------ | ------------------------------------- | ------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `href`       | `string`                              |         | A URL to link to. See [MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a#href).                                                                                     |
| `target`     | `React.HTMLAttributeAnchorTarget`     |         | The target window for the link. See [MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a#target).                                                                     |
| `className`  | `string \| ((state) => string)`       |         | The CSS [className](https://developer.mozilla.org/en-US/docs/Web/API/Element/className) for the element. A function may be provided to compute the class based on component state. |
| `children`   | `ReactNode \| ((state) => ReactNode)` |         | The children of the component. A function may be provided to alter the children based on component state.                                                                          |
| `id`         | `Key`                                 |         | The unique id of the item.                                                                                                                                                         |
| `textValue`  | `string`                              |         | A string representation of the item's contents, used for features like typeahead.                                                                                                  |
| `isDisabled` | `boolean`                             |         | Whether the item is disabled.                                                                                                                                                      |
| `onAction`   | `(() => void)`                        |         | Handler that is called when the item is selected.                                                                                                                                  |
| `isDanger`   | `boolean`                             | `false` | Marks a destructive action, such as Delete. Sets `data-danger` on the item.                                                                                                        |

### Menu.Header

| Prop        | Type              | Default | Description                    |
| ----------- | ----------------- | ------- | ------------------------------ |
| `children`  | `React.ReactNode` |         |                                |
| `className` | `string`          |         |                                |
| `separator` | `boolean`         | `false` | Draws a line under the header. |

These also accept the other props of React Aria's [Menu](https://react-spectrum.adobe.com/react-aria/Menu.html#props) components.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.rierino.com/sdk-core-ui/components/menu.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
