--- title: Menu description: "`Menu`は、一般的なドロップダウンメニューを表示するコンポーネントです。" links: - style: https://github.com/yamada-ui/yamada-ui/tree/main/packages/react/src/components/menu/menu.style.ts - source: https://github.com/yamada-ui/yamada-ui/tree/main/packages/react/src/components/menu - storybook: https://yamada-ui.github.io/yamada-ui?path=/story/components-menu--basic --- ```tsx うずまきナルト うちはサスケ 春野サクラ ``` ## 使い方 ```tsx import { Menu } from "@yamada-ui/react" ``` ```tsx import { Menu } from "@/components/ui" ``` ```tsx import { Menu } from "@workspaces/ui" ``` ```tsx ``` ### itemsを使う ```tsx const items = useMemo( () => [ { label: "うずまきナルト", value: "naruto" }, { label: "うちはサスケ", value: "sasuke" }, { label: "春野サクラ", value: "sakura" }, ], [], ) return ( ) ``` ### サイズを変更する ```tsx const items = useMemo( () => [ { label: "うずまきナルト", value: "naruto" }, { label: "うちはサスケ", value: "sasuke" }, { label: "春野サクラ", value: "sakura" }, ], [], ) return ( {(size) => ( )} ) ``` ### 選択時のイベントをハンドルする 選択時のイベントをハンドルする場合は、`onSelect`を使用します。 ```tsx const items = useMemo( () => [ { label: "うずまきナルト", value: "naruto" }, { label: "うちはサスケ", value: "sasuke" }, { label: "春野サクラ", value: "sakura" }, ], [], ) return ( console.log("selected", value)}> ) ``` ### 区切り線を追加する ```tsx const items = useMemo( () => [ { label: "うずまきナルト", value: "naruto" }, { label: "うちはサスケ", value: "sasuke" }, { label: "春野サクラ", value: "sakura" }, { type: "separator" }, { label: "大蛇丸", value: "orochimaru" }, { label: "自來也", value: "pervy-sage" }, { label: "綱手", value: "tsunade" }, ], [], ) return ( うずまきナルト うちはサスケ 春野サクラ 大蛇丸 自來也 綱手 ) ``` ### グループ化する ```tsx const items = useMemo( () => [ { items: [ { label: "うずまきナルト", value: "naruto" }, { label: "うちはサスケ", value: "sasuke" }, { label: "春野サクラ", value: "sakura" }, ], label: "第七班", }, { items: [ { label: "大蛇丸", value: "orochimaru" }, { label: "自來也", value: "pervy-sage" }, { label: "綱手", value: "tsunade" }, ], label: "伝説の三忍", }, ], [], ) return ( うずまきナルト うちはサスケ 春野サクラ 大蛇丸 自來也 綱手 ) ``` ### アイコンを表示する ```tsx const items = useMemo( () => [ { label: ( <> New Tab ), value: "tab", }, { label: ( <> New Window ), value: "window", }, { label: ( <> New File ), value: "file", }, ], [], ) return ( New Tab New Window New File ) ``` ### コマンドを表示する ```tsx const items = useMemo( () => [ { label: ( <> New Tab ⌘T ), value: "tab", }, { label: ( <> New Window ⌘N ), value: "window", }, { label: ( <> New File ⌘O ), value: "file", }, ], [], ) return ( New Tab ⌘T New Window ⌘N New File ⌘O ) ``` ### 選択可能な項目を設定する ```tsx const [value, setValue] = useState("naruto") const items = useMemo( () => [ { type: "checkbox", items: [ { label: "うずまきナルト", value: "naruto" }, { label: "うちはサスケ", value: "sasuke" }, { label: "春野サクラ", value: "sakura" }, { label: "はたけカカシ", value: "kakashi", disabled: true }, ], value, onChange: setValue, }, ], [value], ) return ( うずまきナルト うちはサスケ 春野サクラ はたけカカシ ) ``` ### ネストさせる ```tsx うずまきナルト うちはサスケ 春野サクラ Settings Extensions Theme User Tasks ``` ### コンテキストメニューを使用する ```tsx
Right Click Here
Copy Paste Delete
``` ### 別の要素を参照して表示する ```tsx const items = useMemo( () => [ { label: "うずまきナルト", value: "naruto" }, { label: "うちはサスケ", value: "sasuke" }, { label: "春野サクラ", value: "sakura" }, ], [], ) return (
Here display Popover
) ``` ### 選択時の閉じる動作を無効にする 選択時の閉じる動作を無効にする場合は、`closeOnSelect`を`false`に設定します。 ```tsx const items = useMemo( () => [ { label: "うずまきナルト", value: "naruto" }, { label: "うちはサスケ", value: "sasuke" }, { label: "春野サクラ", value: "sakura" }, ], [], ) return ( ) ``` ### 内部状態にアクセスする 内部状態にアクセスする場合は、`Menu.Root`の`children`に関数を渡します。 ```tsx const items = useMemo( () => [ { label: "うずまきナルト", value: "naruto" }, { label: "うちはサスケ", value: "sasuke" }, { label: "春野サクラ", value: "sakura" }, ], [], ) return ( {({ open }) => ( <> )} ) ``` ### 初回のフォーカスを設定する 初回のフォーカスを設定する場合は、`initialFocusRef`に`Ref`を渡します。 ```tsx const ref = useRef(null) return ( Set Status Edit Profile Preferences ) ``` ### 配置を変更する 表示位置を変更する場合は、`placement`に`"start"`や`"end-end"`などを設定します。デフォルトでは、`"end-start"`が設定されています。 ```tsx const items = useMemo( () => [ { label: "うずまきナルト", value: "naruto" }, { label: "うちはサスケ", value: "sasuke" }, { label: "春野サクラ", value: "sakura" }, ], [], ) return ( {(placement) => ( )} ) ``` ### アニメーションを変更する アニメーションを変更する場合は、`animationScheme`に`"inline-start"`や`"block-end"`などを設定します。デフォルトでは、`"scale"`が設定されています。 ```tsx const items = useMemo( () => [ { label: "うずまきナルト", value: "naruto" }, { label: "うちはサスケ", value: "sasuke" }, { label: "春野サクラ", value: "sakura" }, ], [], ) return ( {(animationScheme) => ( )} ) ``` ### オフセットを変更する オフセットを変更する場合は、`gutter`または`offset`に値を設定します。 ```tsx const items = useMemo( () => [ { label: "うずまきナルト", value: "naruto" }, { label: "うちはサスケ", value: "sasuke" }, { label: "春野サクラ", value: "sakura" }, ], [], ) return ( ) ``` ### 継続時間を変更する 所要時間を変更する場合は、`duration`に数値(秒)を設定します。 ```tsx const items = useMemo( () => [ { label: "うずまきナルト", value: "naruto" }, { label: "うちはサスケ", value: "sasuke" }, { label: "春野サクラ", value: "sakura" }, ], [], ) return ( ) ``` ### 外側のクリック時に閉じない 外側のクリック時に閉じない場合は、`closeOnBlur`を`false`に設定します。 ```tsx const items = useMemo( () => [ { label: "うずまきナルト", value: "naruto" }, { label: "うちはサスケ", value: "sasuke" }, { label: "春野サクラ", value: "sakura" }, ], [], ) return ( ) ``` ### 項目を無効にする 項目を無効にする場合は、`disabled`を`true`に設定します。 ```tsx const items = useMemo( () => [ { label: "うずまきナルト", value: "naruto" }, { label: "うちはサスケ", value: "sasuke", disabled: true }, { label: "春野サクラ", value: "sakura" }, ], [], ) return ( ) ``` ### ヘッダーやフッターを追加する ヘッダーやフッターを追加する場合は、`Menu.Content`の`header`または`footer`に`ReactNode`を設定します。 ```tsx const items = useMemo( () => [ { label: "うずまきナルト", value: "naruto" }, { label: "うちはサスケ", value: "sasuke" }, { label: "春野サクラ", value: "sakura" }, { label: "大蛇丸", value: "orochimaru" }, { label: "自來也", value: "pervy-sage" }, { label: "綱手", value: "tsunade" }, ], [], ) return ( ) ``` ### 制御する ```tsx const { open, onOpen, onClose } = useDisclosure() const items = useMemo( () => [ { label: "うずまきナルト", value: "naruto" }, { label: "うちはサスケ", value: "sasuke" }, { label: "春野サクラ", value: "sakura" }, ], [], ) return ( ) ``` ## Props ### Menu.Root | Prop | Default | Type | Description | | ---------------------- | ------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | `size` | `"md"` | `"lg" \| "md" \| "sm"` | The size of the component. | | `animationScheme` | `"scale"` | `"scale" \| "none" \| SimplePlacement` | The animation of the element. | | `autoUpdate` | `true` | `boolean` | If `true`, automatically updates the position of the floating element when necessary. | | `blockScrollOnMount` | `false` | `boolean` | If `true`, scrolling will be disabled on the `body` when the modal opens. | | `children` | - | `string \| number \| bigint \| boolean \| ReactElement> \| Iterable \| ReactPortal \| Promise<...> \| ((props: { ...; }) => ReactNode)` | The children of the popover. | | `closeOnBlur` | `true` | `boolean` | If `true`, the popover will close when you blur out it by clicking outside or tabbing out. | | `closeOnEsc` | `true` | `boolean` | If `true`, the popover will hide on pressing Esc key. | | `closeOnScroll` | `false` | `boolean` | If `true`, the popover will hide on scroll. | | `closeOnSelect` | `true` | `boolean` | If `true`, the menu item will be closed when selected. | | `defaultOpen` | - | `boolean` | If `true`, the element will be initially opened. | | `disabled` | `false` | `boolean` | If `true`, the menu will be disabled. | | `duration` | `0.2` | `number \| MotionLifecycleProps` | The animation duration. | | `elements` | - | `{ floating?: HTMLElement \| null \| undefined; reference?: HTMLButtonElement \| null \| undefined }` | Object containing the reference and floating elements. | | `flip` | `true` | `boolean` | If `true`, the popper will change its placement and flip when it's about to overflow its boundary area. | | `gutter` | `8` | `number` | The distance or margin between the reference and popper. It is used internally to create an `offset` modifier. | | `initialFocusRef` | - | `RefObject` | The `ref` of the element that should receive focus when the popover opens. | | `matchWidth` | `false` | `boolean` | If `true`, the popper will match the width of the reference at all times. It's useful for `autocomplete`, `date-picker` and `select` patterns. | | `middleware` | - | `(false \| { name: string; options?: any; fn: (state: { x: number; y: number; placement: Placement; platform: Platform; strategy: Strategy; initialPlacement: Placement; middlewareData: MiddlewareData; rects: ElementRects; elements: Elements; }) => Promisable<...>; } \| null \| undefined)[]` | Array of middleware objects to modify the positioning or provide data for rendering. | | `offset` | - | `[number, number]` | The main and cross-axis offset to displace popper element from its reference element. | | `onClose` | - | `() => void \| Promise` | Callback invoked to close the element. | | `onOpen` | - | `() => void \| Promise` | Callback invoked to open the element. | | `onSelect` | - | `(value?: string) => void` | Callback invoked when a menu item is selected. | | `open` | - | `boolean` | If `true`, the element will be opened. | | `openOnClick` | `true` | `boolean` | If `true`, the popover will be opened when click on the field. | | `placement` | `"end-start"` | `Direction` | The placement of the popper relative to its reference. | | `platform` | - | `Platform` | Custom or extended platform object. | | `preventOverflow` | `true` | `boolean` | If `true`, will prevent the popper from being cut off and ensure it's visible within the boundary area. | | `strategy` | `"absolute"` | `Strategy` | The CSS positioning strategy to use. | | `subMenuDirection` | `"end"` | `SubMenuDirection` | The direction of the sub menu. | | `transferFocus` | `true` | `boolean` | If `true`, the focus will be transferred to the popover content when the tab key is pressed. | | `transform` | `true` | `boolean` | Whether to use `transform` for positioning instead of `top` and `left` (layout) in the `floatingStyles` object. | | `whileElementsMounted` | - | `(reference: HTMLButtonElement, floating: HTMLElement, update: () => void) => () => void` | A callback invoked when both the reference and floating elements are mounted, and cleaned up when either is unmounted. This is useful for setting up event listeners (e.g. pass `autoUpdate`). | ### Menu.Anchor | Prop | Default | Type | Description | | ------------- | ------- | ------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------ | | `as` | - | `As` | The HTML element to render. | | `asChild` | - | `boolean` | Merges its props onto its immediate child. | | `css` | - | `CSSObject \| CSSObject[]` | The CSS object. | | `colorScheme` | - | `"amber" \| "black" \| "blackAlpha" \| "blue" \| "cyan" \| "danger" \| "emerald" \| "error" \| "flashy" \| "fuchsia" ...` | Set color scheme variables. | ### Menu.Command | Prop | Default | Type | Description | | ------------- | ------- | ------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------ | | `as` | - | `As` | The HTML element to render. | | `asChild` | - | `boolean` | Merges its props onto its immediate child. | | `css` | - | `CSSObject \| CSSObject[]` | The CSS object. | | `colorScheme` | - | `"amber" \| "black" \| "blackAlpha" \| "blue" \| "cyan" \| "danger" \| "emerald" \| "error" \| "flashy" \| "fuchsia" ...` | Set color scheme variables. | ### Menu.Content | Prop | Default | Type | Description | | ------------- | ------- | ---------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------- | | `as` | - | `keyof IntrinsicElements` | The HTML element to render. | | `asChild` | - | `boolean` | Merges its props onto its immediate child. | | `css` | - | `CSSObject \| CSSObject[]` | The CSS object. | | `colorScheme` | - | `"amber" \| "black" \| "blackAlpha" \| "blue" \| "cyan" \| "danger" \| "emerald" \| "error" \| "flashy" \| "fuchsia" ...` | Set color scheme variables. | | `footer` | - | `ReactNode` | The footer of the menu. | | `footerProps` | - | `MenuFooterProps` | Props for the footer component. | | `header` | - | `ReactNode` | The header of the menu. | | `headerProps` | - | `MenuHeaderProps` | Props for the header component. | | `items` | - | `(MenuItemWithCheckboxGroup \| MenuItemWithItems \| MenuItemWithRadioGroup \| MenuItemWithSeparator \| MenuItemWithValue)[]` | If provided, generate elements based on items. | | `portalProps` | - | `Omit` | Props for portal component. | ### Menu.Footer | Prop | Default | Type | Description | | ------------- | ------- | ------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------ | | `as` | - | `As` | The HTML element to render. | | `asChild` | - | `boolean` | Merges its props onto its immediate child. | | `css` | - | `CSSObject \| CSSObject[]` | The CSS object. | | `colorScheme` | - | `"amber" \| "black" \| "blackAlpha" \| "blue" \| "cyan" \| "danger" \| "emerald" \| "error" \| "flashy" \| "fuchsia" ...` | Set color scheme variables. | ### Menu.Group | Prop | Default | Type | Description | | ------------- | ------- | ------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------ | | `as` | - | `As` | The HTML element to render. | | `asChild` | - | `boolean` | Merges its props onto its immediate child. | | `css` | - | `CSSObject \| CSSObject[]` | The CSS object. | | `colorScheme` | - | `"amber" \| "black" \| "blackAlpha" \| "blue" \| "cyan" \| "danger" \| "emerald" \| "error" \| "flashy" \| "fuchsia" ...` | Set color scheme variables. | | `label` | - | `ReactNode` | The label of the group. | | `labelProps` | - | `MenuLabelProps` | Props for the label component. | ### Menu.Header | Prop | Default | Type | Description | | ------------- | ------- | ------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------ | | `as` | - | `As` | The HTML element to render. | | `asChild` | - | `boolean` | Merges its props onto its immediate child. | | `css` | - | `CSSObject \| CSSObject[]` | The CSS object. | | `colorScheme` | - | `"amber" \| "black" \| "blackAlpha" \| "blue" \| "cyan" \| "danger" \| "emerald" \| "error" \| "flashy" \| "fuchsia" ...` | Set color scheme variables. | ### Menu.Indicator | Prop | Default | Type | Description | | ------------- | ------- | ------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------ | | `as` | - | `As` | The HTML element to render. | | `asChild` | - | `boolean` | Merges its props onto its immediate child. | | `css` | - | `CSSObject \| CSSObject[]` | The CSS object. | | `colorScheme` | - | `"amber" \| "black" \| "blackAlpha" \| "blue" \| "cyan" \| "danger" \| "emerald" \| "error" \| "flashy" \| "fuchsia" ...` | Set color scheme variables. | ### Menu.Item | Prop | Default | Type | Description | | --------------- | ------- | ------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------ | | `as` | - | `As` | The HTML element to render. | | `asChild` | - | `boolean` | Merges its props onto its immediate child. | | `css` | - | `CSSObject \| CSSObject[]` | The CSS object. | | `colorScheme` | - | `"amber" \| "black" \| "blackAlpha" \| "blue" \| "cyan" \| "danger" \| "emerald" \| "error" \| "flashy" \| "fuchsia" ...` | Set color scheme variables. | | `closeOnSelect` | - | `boolean` | If `true`, the menu item will be closed when selected. | | `disabled` | `false` | `boolean` | If `true`, the menu item will be disabled. | | `value` | - | `string` | The value of the menu item. | ### Menu.Label | Prop | Default | Type | Description | | ------------- | ------- | ------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------ | | `as` | - | `As` | The HTML element to render. | | `asChild` | - | `boolean` | Merges its props onto its immediate child. | | `css` | - | `CSSObject \| CSSObject[]` | The CSS object. | | `colorScheme` | - | `"amber" \| "black" \| "blackAlpha" \| "blue" \| "cyan" \| "danger" \| "emerald" \| "error" \| "flashy" \| "fuchsia" ...` | Set color scheme variables. | ### Menu.OptionGroup | Prop | Default | Type | Description | | -------------- | ------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------ | ---------------------------------------------------------------------- | | `as` | - | `As` | The HTML element to render. | | `asChild` | - | `boolean` | Merges its props onto its immediate child. | | `css` | - | `CSSObject \| CSSObject[]` | The CSS object. | | `colorScheme` | - | `"amber" \| "black" \| "blackAlpha" \| "blue" \| "cyan" \| "danger" \| "emerald" \| "error" \| "flashy" \| "fuchsia" ...` | Set color scheme variables. | | `defaultValue` | - | `M` | The initial value of the menu item group. | | `label` | - | `string \| number \| bigint \| boolean \| ReactElement> \| Iterable \| ReactPortal \| Promise<...>` | The label of the group. | | `labelProps` | - | `MenuLabelProps` | Props for the label component. | | `onChange` | - | `(value: M) => void` | The callback fired when any children checkbox is checked or unchecked. | | `type` | - | `Y` | The type of the menu option group. | | `value` | - | `M` | The value of the menu item group. | ### Menu.OptionItem | Prop | Default | Type | Description | | --------------- | ------- | ------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------ | | `as` | - | `As` | The HTML element to render. | | `asChild` | - | `boolean` | Merges its props onto its immediate child. | | `css` | - | `CSSObject \| CSSObject[]` | The CSS object. | | `colorScheme` | - | `"amber" \| "black" \| "blackAlpha" \| "blue" \| "cyan" \| "danger" \| "emerald" \| "error" \| "flashy" \| "fuchsia" ...` | Set color scheme variables. | | `value` | - | `string` | The value of the menu option item. | | `closeOnSelect` | - | `boolean` | If `true`, the menu item will be closed when selected. | | `disabled` | `false` | `boolean` | If `true`, the menu item will be disabled. | | `icon` | - | `ReactNode` | The icon to be used in the menu option item. | ### Menu.Separator | Prop | Default | Type | Description | | ------------- | ------- | ------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------ | | `as` | - | `As` | The HTML element to render. | | `asChild` | - | `boolean` | Merges its props onto its immediate child. | | `css` | - | `CSSObject \| CSSObject[]` | The CSS object. | | `colorScheme` | - | `"amber" \| "black" \| "blackAlpha" \| "blue" \| "cyan" \| "danger" \| "emerald" \| "error" \| "flashy" \| "fuchsia" ...` | Set color scheme variables. | ### Menu.Trigger | Prop | Default | Type | Description | | ------------- | ------- | ------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------ | | `as` | - | `As` | The HTML element to render. | | `asChild` | - | `boolean` | Merges its props onto its immediate child. | | `css` | - | `CSSObject \| CSSObject[]` | The CSS object. | | `colorScheme` | - | `"amber" \| "black" \| "blackAlpha" \| "blue" \| "cyan" \| "danger" \| "emerald" \| "error" \| "flashy" \| "fuchsia" ...` | Set color scheme variables. | ## アクセシビリティ 現在、v2の移行に伴い、このセクションは更新中です。