| 1 |
--- |
| 2 |
id: Modal |
| 3 |
section: components |
| 4 |
cssPrefix: pf-v6-c-modal-box |
| 5 |
propComponents: ['Modal', 'ModalBody', 'ModalHeader', 'ModalFooter'] |
| 6 |
ouia: true |
| 7 |
--- |
| 8 |
|
| 9 |
import { Fragment, useRef, useState } from 'react'; |
| 10 |
import WarningTriangleIcon from '@patternfly/react-icons/dist/esm/icons/warning-triangle-icon'; |
| 11 |
import RhUiAttentionBellFillIcon from '@patternfly/react-icons/dist/esm/icons/rh-ui-attention-bell-fill-icon'; |
| 12 |
import HelpIcon from '@patternfly/react-icons/dist/esm/icons/help-icon'; |
| 13 |
import formStyles from '@patternfly/react-styles/css/components/Form/form'; |
| 14 |
|
| 15 |
## Examples |
| 16 |
|
| 17 |
### Basic modals |
| 18 |
|
| 19 |
Basic modals give users the option to either confirm or cancel an action. |
| 20 |
|
| 21 |
To flag an open modal, use the `isOpen` property. To execute a callback when a modal is closed, use the `onClose` property. |
| 22 |
|
| 23 |
The `<ModalBody>`, `<ModalHeader>`, and `<ModalFooter>` components are not required, but are typically used to display the main content of the modal, modal title, and any button actions, respectively. |
| 24 |
|
| 25 |
```ts file="./ModalBasic.tsx" |
| 26 |
|
| 27 |
``` |
| 28 |
|
| 29 |
### Scrollable modals |
| 30 |
|
| 31 |
To enable keyboard-accessible scrolling of a modal’s content, pass `tabIndex={0}` to the `<ModalBody>`. |
| 32 |
|
| 33 |
```ts file="ModalWithOverflowingContent.tsx" |
| 34 |
|
| 35 |
``` |
| 36 |
|
| 37 |
### With a static description |
| 38 |
|
| 39 |
To provide additional information about a modal, use the `description` property. Descriptions are static and do not scroll with other modal content. |
| 40 |
|
| 41 |
```ts file="./ModalWithDescription.tsx" |
| 42 |
|
| 43 |
``` |
| 44 |
|
| 45 |
### Top aligned |
| 46 |
|
| 47 |
To override a modal's default center alignment, use the `position` property. In this example, `position` is set to "top", which moves the modal to the top of the screen. |
| 48 |
|
| 49 |
```ts file="./ModalTopAligned.tsx" |
| 50 |
|
| 51 |
``` |
| 52 |
|
| 53 |
### Modal sizes |
| 54 |
|
| 55 |
To adjust the size of a modal, use the `variant` property. Modal variants include "small", "medium", "large", and "default". |
| 56 |
|
| 57 |
In the following example, you can display each modal size option. To launch a modal with a specific size, first select the respective radio button, followed by the "Show modal" button. |
| 58 |
|
| 59 |
```ts file="./ModalSize.tsx" |
| 60 |
|
| 61 |
``` |
| 62 |
|
| 63 |
### Custom width |
| 64 |
|
| 65 |
To choose a specific width for a modal, use the `width` property. The following example has a `width` of "50%". |
| 66 |
|
| 67 |
```ts file="./ModalCustomWidth.tsx" |
| 68 |
|
| 69 |
``` |
| 70 |
|
| 71 |
### Custom header |
| 72 |
|
| 73 |
To add a custom header to a modal, your custom content must be passed as a child of the `<ModalHeader>` component. Do not pass the `title` property to `<ModalHeader>` when using a custom header. |
| 74 |
|
| 75 |
```ts file="./ModalCustomHeader.tsx" |
| 76 |
|
| 77 |
``` |
| 78 |
|
| 79 |
### No header or footer |
| 80 |
|
| 81 |
To exclusively present information in a modal, remove the header and/or footer. |
| 82 |
|
| 83 |
When a modal has no header or footer, make sure to add an `aria-label` explicitly stating this, so that those using assistive technologies can understand this context. |
| 84 |
|
| 85 |
```ts file="./ModalNoHeaderFooter.tsx" |
| 86 |
|
| 87 |
``` |
| 88 |
|
| 89 |
### Title icon |
| 90 |
|
| 91 |
To add an icon before a modal’s title, use the `titleIconVariant` property, which can be set to a "success", "danger", "warning", or "info" variant. The following example uses a "warning" variant. |
| 92 |
|
| 93 |
```ts file="./ModalTitleIcon.tsx" |
| 94 |
|
| 95 |
``` |
| 96 |
|
| 97 |
### Custom title icon |
| 98 |
|
| 99 |
To add a custom icon before a modal’s title, set `titleIconVariant` to an imported custom icon. The following example imports and uses an icon. |
| 100 |
|
| 101 |
```ts file="./ModalCustomTitleIcon.tsx" |
| 102 |
|
| 103 |
``` |
| 104 |
|
| 105 |
### With wizard |
| 106 |
|
| 107 |
To guide users through a series of steps in a modal, you can add a [wizard](/components/wizard) to a modal. To configure the `<Wizard>`, pass an array that contains a “name” and “component” value for each step into the `steps` property. |
| 108 |
|
| 109 |
```ts file="./ModalWithWizard.tsx" |
| 110 |
|
| 111 |
``` |
| 112 |
|
| 113 |
### With dropdown |
| 114 |
|
| 115 |
To present a menu of actions or links to a user, you can add a [dropdown](/components/menus/dropdown) to a modal. |
| 116 |
|
| 117 |
Using the Dropdown's default append location will interfere with keyboard accessibility due to the modal's |
| 118 |
built-in focus trap. To allow the dropdown to visually break out of the modal container, set the Dropdown's |
| 119 |
`popperProps appendTo` property to id of the focus trap for the modal. This can be done by adding prop |
| 120 |
`focusTrapId` to the modal, and then setting the append location to that as per this example. Otherwise you |
| 121 |
can use `inline` to allow it to scroll within the modal itself. Appending to the focus trap should allow the |
| 122 |
menu to expand visually outside the Modal (no scrollbar created in the Modal itself), and still allow |
| 123 |
focusing the content within that menu via keyboard. You should also handle the modal's closing behavior by |
| 124 |
listening to the |
| 125 |
`onEscapePress` callback on the `<Modal>` component. This allows the "escape" key to collapse the |
| 126 |
dropdown without closing the entire modal. |
| 127 |
|
| 128 |
```ts file="./ModalWithDropdown.tsx" |
| 129 |
|
| 130 |
``` |
| 131 |
|
| 132 |
### With help |
| 133 |
|
| 134 |
To help simplify and explain complex models, add a help [popover](/components/popover). Only place a help icon at the modal level if its information applies to all content in the modal. If the help popover is specific to a particular modal section, place the help icon beside that section instead. |
| 135 |
|
| 136 |
```ts file="./ModalWithHelp.tsx" |
| 137 |
|
| 138 |
``` |
| 139 |
|
| 140 |
### With form |
| 141 |
|
| 142 |
To collect user input within a modal, you can add a [form](/components/forms/form). |
| 143 |
|
| 144 |
To enable form submission from a button in the modal's footer (outside of the `<Form>`), set the button's `form` property equal to the form's id. |
| 145 |
|
| 146 |
```ts file="ModalWithForm.tsx" |
| 147 |
|
| 148 |
``` |
| 149 |
|
| 150 |
### Custom focus |
| 151 |
|
| 152 |
To customize which element inside the modal receives focus when initially opened, use the `elementToFocus` property`. |
| 153 |
|
| 154 |
```ts file="./ModalCustomFocus.tsx" |
| 155 |
|
| 156 |
``` |