| 1 |
--- |
| 2 |
id: Menu |
| 3 |
section: components |
| 4 |
subsection: menus |
| 5 |
cssPrefix: pf-v6-c-menu |
| 6 |
propComponents: |
| 7 |
[ |
| 8 |
'Menu', |
| 9 |
'MenuList', |
| 10 |
'MenuItem', |
| 11 |
'MenuItemAction', |
| 12 |
'MenuContent', |
| 13 |
'MenuSearch', |
| 14 |
'MenuSearchInput', |
| 15 |
'MenuGroup', |
| 16 |
'MenuContainer', |
| 17 |
'TooltipProps', |
| 18 |
'PopperOptions' |
| 19 |
] |
| 20 |
ouia: true |
| 21 |
--- |
| 22 |
|
| 23 |
import { Fragment, createRef, useEffect, useRef, useState } from 'react'; |
| 24 |
import RhUiMenuBarsIcon from '@patternfly/react-icons/dist/esm/icons/rh-ui-menu-bars-icon'; |
| 25 |
import RhUiClipboardFillIcon from '@patternfly/react-icons/dist/esm/icons/rh-ui-clipboard-fill-icon'; |
| 26 |
import RhUiBranchFillIcon from '@patternfly/react-icons/dist/esm/icons/rh-ui-branch-fill-icon'; |
| 27 |
import RhUiServerStackFillIcon from '@patternfly/react-icons/dist/esm/icons/rh-ui-server-stack-fill-icon'; |
| 28 |
import CubeIcon from '@patternfly/react-icons/dist/esm/icons/cube-icon'; |
| 29 |
import RhUiTableIcon from '@patternfly/react-icons/dist/esm/icons/rh-ui-table-icon'; |
| 30 |
import RhUiNotificationFillIcon from '@patternfly/react-icons/dist/esm/icons/rh-ui-notification-fill-icon'; |
| 31 |
import RhUiStorageDomainFillIcon from '@patternfly/react-icons/dist/esm/icons/rh-ui-storage-domain-fill-icon'; |
| 32 |
import RhMicronsCaretLeftIcon from '@patternfly/react-icons/dist/esm/icons/rh-microns-caret-left-icon'; |
| 33 |
import RhMicronsCaretDownFillIcon from '@patternfly/react-icons/dist/esm/icons/rh-microns-caret-down-fill-icon'; |
| 34 |
|
| 35 |
## Examples |
| 36 |
|
| 37 |
### Basic menus |
| 38 |
|
| 39 |
A menu may contain multiple variations of `<MenuItem>` components. The following example shows a few different states of menu items, where they may: |
| 40 |
|
| 41 |
- Use the `itemId` property to link to callbacks. In this example, the `onSelect` property logs information to the console when a menu item is selected. In practice, specific actions can be linked to `onSelect` callbacks. |
| 42 |
- Use the `to` property to direct users to other resources or webpages after selecting a menu item, and the `onClick` property to pass in a callback for specific menu items. |
| 43 |
- Use the `isDisabled` property to disable a menu item. |
| 44 |
- Use the `isPlain` property to remove the outer box shadow and style the menu plainly instead. |
| 45 |
|
| 46 |
```ts file="MenuBasic.tsx" |
| 47 |
|
| 48 |
``` |
| 49 |
|
| 50 |
### Danger menu item |
| 51 |
|
| 52 |
To indicate that a `<MenuItem>` is connected to a potentially destructive action, use the `isDanger` property. The "Delete" item in the following example demonstrates how danger items look. |
| 53 |
|
| 54 |
```ts file="MenuDangerMenuItem.tsx" |
| 55 |
|
| 56 |
``` |
| 57 |
|
| 58 |
### With icons |
| 59 |
|
| 60 |
Use the `icon` property to add a familiar icon before a `<MenuItem>` to accelerate text label recognition. |
| 61 |
|
| 62 |
```ts file="MenuWithIcons.tsx" |
| 63 |
|
| 64 |
``` |
| 65 |
|
| 66 |
### With actions |
| 67 |
|
| 68 |
To connect a menu item to an action icon, add a `<MenuItemAction>` to a `<MenuItem>`, and use the `icon` property to load an easily recognizable icon. |
| 69 |
|
| 70 |
To trigger an action when any menu action icon is selected, pass a callback to the `onActionClick` property of the `<Menu>`. The following example logs to the console any time any action icon is selected. |
| 71 |
|
| 72 |
To trigger an action when a specific item's action icon is selected, pass in the `onClick` property to that `<MenuItemAction>`. The following example logs "clicked on code icon" to the console when the "code" icon is selected. |
| 73 |
|
| 74 |
```ts file="MenuWithActions.tsx" |
| 75 |
|
| 76 |
``` |
| 77 |
|
| 78 |
### With links |
| 79 |
|
| 80 |
Use the `to` property to add a link to a `<MenuItem>` that directs users to a new page when the item is selected. Use the `isExternalLink` property when linking to external resources. This will annotate a menu item link with an external link icon when they navigate to the link or hover over it, as well as add `target="_blank"` so that the link opens in a new tab or window. |
| 81 |
|
| 82 |
```ts file="MenuWithLinks.tsx" |
| 83 |
|
| 84 |
``` |
| 85 |
|
| 86 |
### With descriptions |
| 87 |
|
| 88 |
Use the `description` property to add short descriptive text below any menu item that needs additional context. |
| 89 |
|
| 90 |
```ts file="MenuWithDescription.tsx" |
| 91 |
|
| 92 |
``` |
| 93 |
|
| 94 |
### Item checkbox |
| 95 |
|
| 96 |
Use the `hasCheck` property to add a checkbox to a `<MenuItem>`. Use the `isSelected` property to indicate when a `<MenuItem>` is selected. |
| 97 |
|
| 98 |
```ts file="./MenuWithCheckbox.tsx" |
| 99 |
|
| 100 |
``` |
| 101 |
|
| 102 |
### Menu footer |
| 103 |
|
| 104 |
Add a `<MenuFooter>` that contains separate, but related actions at the bottom of a menu. |
| 105 |
|
| 106 |
```ts file="MenuWithFooter.tsx" |
| 107 |
|
| 108 |
``` |
| 109 |
|
| 110 |
### Separated items |
| 111 |
|
| 112 |
Use a [divider](/components/divider) to visually separate `<MenuContent>`. Use the `component` property to specify the type of divider component to use. |
| 113 |
|
| 114 |
```ts file="MenuWithSeparators.tsx" |
| 115 |
|
| 116 |
``` |
| 117 |
|
| 118 |
### Titled groups of items |
| 119 |
|
| 120 |
Add a `<MenuGroup>` to organize `<MenuContent>` and use the `label` property to title a group of menu items. Use the `labelHeadingLevel` property to assign a heading level to the menu group label. |
| 121 |
|
| 122 |
```ts file="MenuWithTitledGroups.tsx" |
| 123 |
|
| 124 |
``` |
| 125 |
|
| 126 |
### With favorites |
| 127 |
|
| 128 |
The following menu example allows users to favorite menu items, an action that duplicates a menu item and places it in a separate group at the top of the menu. The `isFavorited` property identifies items that a user has favorited. |
| 129 |
|
| 130 |
```ts file="MenuWithFavorites.tsx" |
| 131 |
|
| 132 |
``` |
| 133 |
|
| 134 |
### Filtering with search input |
| 135 |
|
| 136 |
A [search input](/components/search-input) component can be placed within `<MenuSearch>` and `<MenuSearchInput>` to render a search input at the top of the menu. In the following example, the `onChange` property of the text input is passed a callback that filters menu items as a user types. |
| 137 |
|
| 138 |
```ts file="MenuFilteringWithSearchInput.tsx" |
| 139 |
|
| 140 |
``` |
| 141 |
|
| 142 |
### Option single select menu |
| 143 |
|
| 144 |
The following example demonstrates a single option select menu that persists a selected menu item. Use the `selected` property on the `<Menu>` to label a selected item with a checkmark. You can also use the `isSelected` property on a `<MenuItem>` to indicate that it is selected. |
| 145 |
|
| 146 |
You must also use the `role` property on the `<Menu>` with a value of `"listbox"` when using a non-checkbox select menu. |
| 147 |
|
| 148 |
```ts file="MenuOptionSingleSelect.tsx" |
| 149 |
|
| 150 |
``` |
| 151 |
|
| 152 |
### Option multi select menu |
| 153 |
|
| 154 |
To persist multiple selections that a user makes, use a multiple option select menu. To enable multi select, pass an array containing each selected `itemId` to the `selected` property on the `<Menu>`, and pass the `isAriaMultiselectable` property on the `<MenuList>`. |
| 155 |
|
| 156 |
Similar to a single select menu, you must also pass `role="listbox"` to the `<Menu>`. |
| 157 |
|
| 158 |
```ts file="MenuOptionMultiSelect.tsx" |
| 159 |
|
| 160 |
``` |
| 161 |
|
| 162 |
### Scrollable menus |
| 163 |
|
| 164 |
Use the `isScrollable` property to make a long `<Menu>` scrollable and visually condensed. |
| 165 |
|
| 166 |
```ts file="MenuScrollable.tsx" |
| 167 |
|
| 168 |
``` |
| 169 |
|
| 170 |
### Scrollable menu with custom height |
| 171 |
|
| 172 |
Adjust the visual size of a scrollable menu by using the `menuHeight` property within `<MenuContent>`. This example adjusts the height to 200px. |
| 173 |
|
| 174 |
```ts file="MenuScrollableCustomMenuHeight.tsx" |
| 175 |
|
| 176 |
``` |
| 177 |
|
| 178 |
### With view more |
| 179 |
|
| 180 |
If you want to initially render only a certain number of menu items within a large menu, you can add a "view more" menu item with a callback passed into its `onClick` property that will render additional menu items. |
| 181 |
|
| 182 |
In this example, 3 additional menu items are revealed each time the "view more" option is selected, with a loading icon simulating a network call to fetch more items. After all items are visible, the "view more" link disappears. |
| 183 |
|
| 184 |
```ts file="MenuWithViewMore.tsx" |
| 185 |
|
| 186 |
``` |
| 187 |
|
| 188 |
### With drilldown |
| 189 |
|
| 190 |
Use a drilldown menu to contain different levels of menu items. When a parent menu item (an item that has a submenu of children) is selected, the menu is replaced with the children items. |
| 191 |
|
| 192 |
- To indicate that a menu contains a drilldown, use the `containsDrilldown` property. |
| 193 |
- To indicate the path of drilled-in menu item ids, use the `drilldownItemPath` property. |
| 194 |
- Pass in an array of drilled-in menus with the `drilledInMenus` property. |
| 195 |
- Use the `onDrillIn` and `onDrillOut` properties to contain callbacks for drilling into and drilling out of a submenu, respectively. |
| 196 |
- To account for updated heights as menus drill in and out of use, use the `onGetMenuHeight` property. When starting from a drilled-in state, the `onGetMenuHeight` property must define the height of the root menu. |
| 197 |
|
| 198 |
```ts file="./MenuWithDrilldown.tsx" |
| 199 |
|
| 200 |
``` |
| 201 |
|
| 202 |
### Initially drilled-in menu |
| 203 |
|
| 204 |
To render an initially drilled-in menu, the `drilldownItemPath`, `drilledInMenus`, and `activeMenu` properties must be set to default values. |
| 205 |
|
| 206 |
```ts file="./MenuWithDrilldownInitialState.tsx" |
| 207 |
|
| 208 |
``` |
| 209 |
|
| 210 |
### With drilldown - submenu functions |
| 211 |
|
| 212 |
For added flexibility with large menus, you may create a menu by passing a function to `drilldownMenu`. This approach allows you to create menu items dynamically, rather than creating everything up front. |
| 213 |
|
| 214 |
```ts file="./MenuWithDrilldownSubmenuFunctions.tsx" |
| 215 |
|
| 216 |
``` |
| 217 |
|
| 218 |
### With drilldown breadcrumbs |
| 219 |
|
| 220 |
Use [breadcrumbs](/components/breadcrumb) when a drilldown menu has more than 2 levels to offer users better navigation. |
| 221 |
|
| 222 |
To control the height of a menu, use the `maxMenuHeight` property. Selecting the "Set max menu height" checkbox in the following example sets the menu height to "100px" and makes the menu scrollable. |
| 223 |
|
| 224 |
```ts file="MenuWithDrilldownBreadcrumbs.tsx" |
| 225 |
|
| 226 |
``` |
| 227 |
|
| 228 |
### With drilldown and inline filter |
| 229 |
|
| 230 |
```ts file="MenuFilterDrilldown.tsx" |
| 231 |
|
| 232 |
``` |
| 233 |
|
| 234 |
## Using router links |
| 235 |
|
| 236 |
Router links can be used for in-app linking in React environments to prevent page reloading. To use a `Link` component from a router package, you can pass a callback to the `component` property of the `MenuItem`: |
| 237 |
|
| 238 |
```nolive |
| 239 |
<MenuItem |
| 240 |
component={(props) => <Link {...props} to="#" />} |
| 241 |
> |
| 242 |
{...Link Content} |
| 243 |
</MenuItem> |
| 244 |
``` |