# Menu
URL: /react/components/menu
Source: https://github.com/daangn/seed-design/blob/dev/docs/content/react/components/menu.mdx
A component that provides a list of options or actions users can take.
사용 가능 버전: @seed-design/react@2.0.0, @seed-design/css@2.0.0
## Preview
```tsx
import {
IconPencilLine,
IconPlusLine,
IconTrashcanLine,
} from "@karrotmarket/react-monochrome-icon";
import { ActionButton } from "seed-design/ui/action-button";
import {
MenuContent,
MenuGroup,
MenuGroupLabel,
MenuItem,
MenuRoot,
MenuTrigger,
} from "seed-design/ui/menu";
export default function MenuPreview() {
return (
Open
Actions
} />
}
/>
}
/>
);
}
```
## Installation
- npm: npx @seed-design/cli@latest add ui:menu
- pnpm: pnpm dlx @seed-design/cli@latest add ui:menu
- yarn: yarn dlx @seed-design/cli@latest add ui:menu
- bun: bun x @seed-design/cli@latest add ui:menu
## Usage
Menu is structured as follows:
```tsx
import {
MenuRoot,
MenuTrigger,
MenuContent,
MenuGroup,
MenuGroupLabel,
MenuItem,
} from "seed-design/ui/menu";
```
```tsx
...
Group Title
```
- `MenuRoot`: Manages the menu's state (open/closed) and position.
- `MenuTrigger`: Trigger that opens and closes the menu when clicked.
- `MenuAnchor`: Provides only the positioning reference point for the menu and does not include open/close behavior. Use instead of `MenuTrigger` when you need to directly control the trigger behavior.
- `MenuContent`: Floating container that wraps menu items.
- `MenuGroup`: Groups related items together. All `MenuItem` must be inside a `MenuGroup`.
- `MenuGroupLabel`: Displays the group title.
- `MenuItem`: Individual menu item.
## Props
### `MenuRoot`
### `MenuTrigger`
### `MenuAnchor`
### `MenuContent`
### `MenuGroup`
### `MenuGroupLabel`
### `MenuItem`
## Examples
### Trigger
`` sets the `aria-haspopup="menu"` attribute and automatically sets the `aria-expanded` attribute based on the Menu's `open` state. This attribute is useful for assistive technologies like screen readers.
## Preview
```tsx
import {
IconPencilLine,
IconPlusLine,
IconTrashcanLine,
} from "@karrotmarket/react-monochrome-icon";
import { ActionButton } from "seed-design/ui/action-button";
import {
MenuContent,
MenuGroup,
MenuGroupLabel,
MenuItem,
MenuRoot,
MenuTrigger,
} from "seed-design/ui/menu";
export default function MenuPreview() {
return (
Open
Actions
} />
}
/>
}
/>
);
}
```
### Size
The size of MenuRoot is set by `size`. (default: `medium`)
`responsive` is a value where size automatically switches based on screen width. In products that support multiple screen widths, use `size=responsive` to adapt.
```tsx
import { IconPencilLine, IconPlusLine } from "@karrotmarket/react-monochrome-icon";
import { HStack } from "@seed-design/react";
import { ActionButton } from "seed-design/ui/action-button";
import {
MenuContent,
MenuGroup,
MenuGroupLabel,
MenuItem,
MenuRoot,
MenuTrigger,
} from "seed-design/ui/menu";
export default function MenuSize() {
return (
Medium
작업
} />
} />
Small
작업
} />
} />
Responsive
작업
} />
} />
);
}
```
### With Description
Add supplementary descriptions to items using the `description` prop of `MenuItem`.
```tsx
import {
IconArrowUpBracketDownLine,
IconPencilLine,
IconPlusLine,
} from "@karrotmarket/react-monochrome-icon";
import { ActionButton } from "seed-design/ui/action-button";
import { MenuContent, MenuGroup, MenuItem, MenuRoot, MenuTrigger } from "seed-design/ui/menu";
export default function MenuWithDescription() {
return (
열기
}
/>
}
/>
}
/>
);
}
```
### Tone
Set the tone of items using the `tone` prop of `MenuItem`.
```tsx
import {
IconPencilLine,
IconPlusLine,
IconTrashcanLine,
} from "@karrotmarket/react-monochrome-icon";
import { ActionButton } from "seed-design/ui/action-button";
import { MenuContent, MenuGroup, MenuItem, MenuRoot, MenuTrigger } from "seed-design/ui/menu";
export default function MenuTone() {
return (
열기
} />
} />
} />
);
}
```
### Disabled Items
Disable specific items using the `disabled` prop of `MenuItem`.
```tsx
import {
IconArrowUpBracketDownLine,
IconPencilLine,
IconPlusLine,
} from "@karrotmarket/react-monochrome-icon";
import { ActionButton } from "seed-design/ui/action-button";
import { MenuContent, MenuGroup, MenuItem, MenuRoot, MenuTrigger } from "seed-design/ui/menu";
export default function MenuDisabled() {
return (
열기
} />
}
disabled
/>
} />
);
}
```
### Placement
Set the position of the menu using the `placement` prop of `MenuRoot`. The default is `"bottom"`.
The maximum height of the menu is determined by the smaller of the specified maximum height and the remaining screen space around the trigger (or `MenuAnchor`). If there are many items, the menu scrolls within itself. The menu is positioned to avoid the notch and home indicator areas.
```tsx
import { IconPencilLine, IconPlusLine } from "@karrotmarket/react-monochrome-icon";
import { Box } from "@seed-design/react";
import { ActionButton } from "seed-design/ui/action-button";
import {
MenuContent,
MenuGroup,
MenuItem,
MenuRoot,
MenuTrigger,
type MenuRootProps,
} from "seed-design/ui/menu";
function PlacementMenu({ placement }: { placement: NonNullable }) {
return (
{placement}
} />
} />
);
}
export default function MenuPlacement() {
return (
);
}
```
### Anchor
The `children` of `MenuAnchor` are used only for positioning the Menu, and unlike `MenuTrigger`, it does not include click to open/close behavior.
You must use the `open` prop in a controlled manner to directly manage the open/closed state.
In the example below, conditions are added to the `onOpenChange` callback so the menu does not close even when clicking outside the area.
```tsx
import { Avatar } from "seed-design/ui/avatar";
import { IdentityPlaceholder } from "seed-design/ui/identity-placeholder";
import { IconPencilLine, IconPlusLine } from "@karrotmarket/react-monochrome-icon";
import { useState } from "react";
import { Switch } from "seed-design/ui/switch";
import { MenuAnchor, MenuContent, MenuGroup, MenuItem, MenuRoot } from "seed-design/ui/menu";
import { HStack } from "@seed-design/react";
export default function MenuAnchorExample() {
const [open, setOpen] = useState(false);
return (
{
if (!nextOpen && details?.reason === "interactOutside") return;
setOpen(nextOpen);
}}
>
}
/>
} />
} />
);
}
```
### Match Width
Using the `matchReferenceWidth` prop of `MenuRoot`, the menu width matches the width of the reference element (Trigger or Anchor).
```tsx
import { ActionButton } from "seed-design/ui/action-button";
import { MenuContent, MenuGroup, MenuItem, MenuRoot, MenuTrigger } from "seed-design/ui/menu";
export default function MenuMatchReferenceWidth() {
return (
열기
);
}
```
### `onOpenChange` Details
`details` is provided as the second argument to `onOpenChange`.
#### `reason`
**When opening** (`open: true`)
- `"trigger"`: Opened by `MenuTrigger`
**When closing** (`open: false`)
- `"trigger"`: Closed by `MenuTrigger`
- `"escapeKeyDown"`: ESC key pressed
- `"interactOutside"`: Click outside area
- `"itemClick"`: Menu item clicked
- `"cascadeDismiss"`: Cascade close due to parent layer closing
```tsx
import { IconPencilLine, IconPlusLine } from "@karrotmarket/react-monochrome-icon";
import { HStack, Text, VStack } from "@seed-design/react";
import { useState } from "react";
import { ActionButton } from "seed-design/ui/action-button";
import { MenuContent, MenuGroup, MenuItem, MenuRoot, MenuTrigger } from "seed-design/ui/menu";
export default function MenuOpenChangeReason() {
const [open, setOpen] = useState(false);
const [openReason, setOpenReason] = useState(null);
const [closeReason, setCloseReason] = useState(null);
return (
{
setOpen(open);
(open ? setOpenReason : setCloseReason)(details?.reason ?? null);
}}
>
열기
} />
} />
마지막 열림 이유: {openReason ?? "-"}
마지막 닫힘 이유: {closeReason ?? "-"}
);
}
```