| 1 |
--- |
| 2 |
id: Menu toggle |
| 3 |
section: components |
| 4 |
subsection: menus |
| 5 |
cssPrefix: pf-v6-c-menu-toggle |
| 6 |
propComponents: ['MenuToggle', 'MenuToggleAction', 'MenuToggleCheckbox'] |
| 7 |
--- |
| 8 |
|
| 9 |
import { Fragment, useState } from 'react'; |
| 10 |
import './MenuToggle.css' |
| 11 |
import PlusIcon from '@patternfly/react-icons/dist/esm/icons/plus-icon'; |
| 12 |
import RhUiEllipsisVerticalFillIcon from '@patternfly/react-icons/dist/esm/icons/rh-ui-ellipsis-vertical-fill-icon'; |
| 13 |
import imgAvatar from '@patternfly/react-core/src/components/assets/avatarImg.svg'; |
| 14 |
import RhMicronsCloseIcon from '@patternfly/react-icons/dist/esm/icons/rh-microns-close-icon'; |
| 15 |
|
| 16 |
## Examples |
| 17 |
|
| 18 |
### Collapsed toggle |
| 19 |
|
| 20 |
A toggle is collapsed until it is selected by a user. |
| 21 |
|
| 22 |
```ts file="MenuToggleCollapsed.tsx" |
| 23 |
|
| 24 |
``` |
| 25 |
|
| 26 |
### Expanded toggle |
| 27 |
|
| 28 |
When a user selects a toggle, it becomes expanded and is styled with a blue underline. To flag expanded toggles, and apply expanded styling, use the `isExpanded` property . |
| 29 |
|
| 30 |
```ts file ="MenuToggleExpanded.tsx" |
| 31 |
|
| 32 |
``` |
| 33 |
|
| 34 |
### Small toggle |
| 35 |
|
| 36 |
You can pass `size="sm"` to a MenuToggle to style it as a small toggle, such as within a [breadcrumb](/components/breadcrumb). |
| 37 |
|
| 38 |
```ts file="MenuToggleSmall.tsx" |
| 39 |
|
| 40 |
``` |
| 41 |
|
| 42 |
### Disabled toggle |
| 43 |
|
| 44 |
To disable the selection and expansion of a toggle, use the `isDisabled` property. |
| 45 |
|
| 46 |
```ts file="MenuToggleDisabled.tsx" |
| 47 |
|
| 48 |
``` |
| 49 |
|
| 50 |
### With a badge |
| 51 |
|
| 52 |
To display a count of selected items in a toggle, use the `badge` property. You can also pass in `variant="plainText"` for a badge only toggle. |
| 53 |
|
| 54 |
```ts file="MenuToggleBadge.tsx" |
| 55 |
|
| 56 |
``` |
| 57 |
|
| 58 |
### Settings toggle |
| 59 |
|
| 60 |
To create a "settings" menu toggle that will animate on hover and focus, you can pass the `isSettings` property in. Doing so will override the `icon` property, as a specific icon is used internally for the animations. |
| 61 |
|
| 62 |
```ts file="MenuToggleSettings.tsx" |
| 63 |
|
| 64 |
``` |
| 65 |
|
| 66 |
### Custom icons |
| 67 |
|
| 68 |
To add a recognizable icon to a menu toggle, use the `icon` property. The following example adds a `PlusIcon` to the toggle. |
| 69 |
|
| 70 |
For most basic icons, it is recommended to wrap it inside our [icon component](/components/icon). |
| 71 |
|
| 72 |
```ts file="MenuToggleCustomIcon.tsx" |
| 73 |
|
| 74 |
``` |
| 75 |
|
| 76 |
### With avatar and text |
| 77 |
|
| 78 |
You can also pass images into the `icon` property. The following example passes in an `<Avatar>` component with an `imgAvatar`. |
| 79 |
|
| 80 |
This can be used alongside a text label that provides more context for the image. |
| 81 |
|
| 82 |
```ts file="MenuToggleAvatarText.tsx" |
| 83 |
|
| 84 |
``` |
| 85 |
|
| 86 |
### Variant styles |
| 87 |
|
| 88 |
Variant styling can be applied to menu toggles. In the following example, the toggle uses primary styling by passing `variant="primary"` into the `<MenuToggle>` component. Additional variant options include “default”, “plain”, “plainText”, “secondary”, and “typeahead”. |
| 89 |
|
| 90 |
```ts file="MenuToggleVariantStyles.tsx" |
| 91 |
|
| 92 |
``` |
| 93 |
|
| 94 |
### Plain toggle with icon |
| 95 |
|
| 96 |
To apply plain styling to a menu toggle with an icon, pass in `variant="plain"`. This will remove the default bottom border and caret. You may pass in an `icon` to serve as the menu toggle. The following example passes in an `RhUiEllipsisVerticalFillIcon`. |
| 97 |
|
| 98 |
If the toggle does not have any visible text content, use the `aria-label` property to provide an accessible name. |
| 99 |
|
| 100 |
```ts file="MenuTogglePlainIcon.tsx" |
| 101 |
|
| 102 |
``` |
| 103 |
|
| 104 |
### Plain circle toggle |
| 105 |
|
| 106 |
You can also pass the `isCircle` property to a plain, icon-only toggle to adjust the styling to a complete circular shape, rather than a rounded square. |
| 107 |
|
| 108 |
```ts file="MenuTogglePlainCircle.tsx" |
| 109 |
|
| 110 |
``` |
| 111 |
|
| 112 |
### Plain toggle with text label |
| 113 |
|
| 114 |
To apply plain styling to a menu toggle with a text label, pass in `variant="plainText"`. Unlike the “plain” variant, “plainText” adds a caret pointing down in the toggle. |
| 115 |
|
| 116 |
```ts file="MenuTogglePlainTextLabel.tsx" |
| 117 |
|
| 118 |
``` |
| 119 |
|
| 120 |
### Split toggle with checkbox |
| 121 |
|
| 122 |
To add a checkbox (or other action/control) to a menu toggle, use a split button. |
| 123 |
|
| 124 |
A `<MenuToggle>` can be rendered as a split button by adding a `splitButtonItems` property. Elements to be displayed before the toggle button must be included in the `splitButtonItems`. |
| 125 |
|
| 126 |
The following example shows a split button with a `<MenuToggleCheckbox>`. |
| 127 |
|
| 128 |
Variant styling can be applied to split button toggles to adjust their appearance for different scenarios. Both "primary" and "secondary" variants can be used with split button toggles. |
| 129 |
|
| 130 |
```ts file='MenuToggleSplitButtonCheckbox.tsx' |
| 131 |
|
| 132 |
``` |
| 133 |
|
| 134 |
### Split toggle with labeled checkbox |
| 135 |
|
| 136 |
You can allow users to select a toggle checkbox by clicking either the checkbox or the text label. |
| 137 |
|
| 138 |
To link a split toggle label to the toggle's checkbox, pass both the label and the `<MenuToggleCheckbox>` component to `splitButtonItems`. |
| 139 |
|
| 140 |
```ts file='MenuToggleSplitButtonCheckboxWithText.tsx' |
| 141 |
|
| 142 |
``` |
| 143 |
|
| 144 |
### Split toggle with checkbox and toggle text |
| 145 |
|
| 146 |
To link a split toggle label to the toggle button itself, pass the label to the `<MenuToggle>` component, instead of `splitButtonItems`. |
| 147 |
|
| 148 |
```ts file='MenuToggleSplitButtonCheckboxWithToggleText.tsx' |
| 149 |
|
| 150 |
``` |
| 151 |
|
| 152 |
### Split toggle with action |
| 153 |
|
| 154 |
To add an action to a split button, add a `<MenuToggleAction>` to the `splitButtonItems` property. |
| 155 |
|
| 156 |
Actions may be used with primary and secondary toggle variants. |
| 157 |
|
| 158 |
```ts file='MenuToggleSplitButtonAction.tsx' |
| 159 |
|
| 160 |
``` |
| 161 |
|
| 162 |
### Full height toggle |
| 163 |
|
| 164 |
A full height toggle fills the height of its parent. To flag a full height toggle, use the `isFullHeight` property. |
| 165 |
|
| 166 |
In the following example, the toggle fills the size of the "80px" `<div>` element that it is within. |
| 167 |
|
| 168 |
```ts file="MenuToggleFullHeight.tsx" |
| 169 |
|
| 170 |
``` |
| 171 |
|
| 172 |
### Full width toggle |
| 173 |
|
| 174 |
A full width toggle fills the width of its parent. To flag a full width toggle, use the `isFullWidth` property. |
| 175 |
|
| 176 |
In the following example, the toggle fills the width of its parent as the window size changes. |
| 177 |
|
| 178 |
```ts file="MenuToggleFullWidth.tsx" |
| 179 |
|
| 180 |
``` |
| 181 |
|
| 182 |
### Toggle in a form |
| 183 |
|
| 184 |
When a menu toggle is used inside a form, pass the `isInForm` property so the toggle receives form-appropriate styling. |
| 185 |
|
| 186 |
```ts file="MenuToggleInForm.tsx" |
| 187 |
|
| 188 |
``` |
| 189 |
|
| 190 |
### Typeahead toggle |
| 191 |
|
| 192 |
To create a typeahead toggle, pass in `variant="typeahead"` to the `<MenuToggle>`. Then, pass a `<TextInputGroup>` component as a child of the `<MenuToggle>`. |
| 193 |
|
| 194 |
To create a multiple typeahead toggle, pass a `<TextInputGroup>` component implemented like the [text input group's filter example](/components/text-input-group#filters) as a child of `<MenuToggle>`. |
| 195 |
|
| 196 |
```ts file='MenuToggleTypeahead.tsx' |
| 197 |
|
| 198 |
``` |
| 199 |
|
| 200 |
### Status toggle |
| 201 |
|
| 202 |
To create a toggle with a status, pass in the `status` property to the `MenuToggle`. The default icon associated with each status may be overridden by using the `statusIcon` property. |
| 203 |
|
| 204 |
When the status value is "warning" or "danger", you must include helper text that conveys what is causing the warning/error. |
| 205 |
|
| 206 |
```ts file='MenuToggleStatus.tsx' |
| 207 |
|
| 208 |
``` |
| 209 |
|
| 210 |
### Placeholder text in toggle |
| 211 |
|
| 212 |
To indicate the toggle contains placeholder text, pass the `isPlaceholder` property to the `MenuToggle`. |
| 213 |
|
| 214 |
```ts file='MenuTogglePlaceholder.tsx' |
| 215 |
|
| 216 |
``` |