| 1 |
--- |
| 2 |
id: Modal |
| 3 |
section: components |
| 4 |
cssPrefix: pf-v6-c-modal-box |
| 5 |
propComponents: ['Modal'] |
| 6 |
ouia: true |
| 7 |
deprecated: true |
| 8 |
--- |
| 9 |
|
| 10 |
import { Fragment, useRef, useState } from 'react'; |
| 11 |
|
| 12 |
import { Modal as ModalDeprecated, ModalVariant as ModalVariantDeprecated } from '@patternfly/react-core/deprecated'; |
| 13 |
import WarningTriangleIcon from '@patternfly/react-icons/dist/esm/icons/warning-triangle-icon'; |
| 14 |
import RhUiAttentionBellFillIcon from '@patternfly/react-icons/dist/esm/icons/rh-ui-attention-bell-fill-icon'; |
| 15 |
import HelpIcon from '@patternfly/react-icons/dist/esm/icons/help-icon'; |
| 16 |
import spacing from '@patternfly/react-styles/css/utilities/Spacing/spacing'; |
| 17 |
import formStyles from '@patternfly/react-styles/css/components/Form/form'; |
| 18 |
|
| 19 |
## Examples |
| 20 |
|
| 21 |
### Basic modals |
| 22 |
|
| 23 |
Basic modals give users the option to either confirm or cancel an action. To flag an open modal, use the `isOpen` property. To execute a callback when a modal is closed, use the `onClose` property. |
| 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 `<Modal>`. |
| 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 |
### Small modal |
| 54 |
|
| 55 |
To adjust the size of a modal, use the `variant` property. Modal variants include "small", "medium", "large", and "default". |
| 56 |
|
| 57 |
The following example displays a "small" modal by passing in `variant={ModalVariant.small}`. |
| 58 |
|
| 59 |
```ts file="./ModalSmall.tsx" |
| 60 |
|
| 61 |
``` |
| 62 |
|
| 63 |
### Medium modal |
| 64 |
|
| 65 |
The following example displays a "medium" modal by passing in `variant={ModalVariant.medium}`. |
| 66 |
|
| 67 |
```ts file="./ModalMedium.tsx" |
| 68 |
|
| 69 |
``` |
| 70 |
|
| 71 |
### Large modal |
| 72 |
|
| 73 |
The following example displays a "large" modal by passing in `variant={ModalVariant.large}`. |
| 74 |
|
| 75 |
```ts file="./ModalLarge.tsx" |
| 76 |
|
| 77 |
``` |
| 78 |
|
| 79 |
### Custom width |
| 80 |
|
| 81 |
To choose a specific width for a modal, use the `width` property. The following example has a `width` of "50%". |
| 82 |
|
| 83 |
```ts file="./ModalCustomWidth.tsx" |
| 84 |
|
| 85 |
``` |
| 86 |
|
| 87 |
### Custom header and footer |
| 88 |
|
| 89 |
To add a custom header and footer to a modal, set the `header` and `footer` properties to a custom implementation. The following example passes title components into both the header and the footer and also passes an icon to the footer. |
| 90 |
|
| 91 |
```ts file="./ModalCustomHeaderFooter.tsx" |
| 92 |
|
| 93 |
``` |
| 94 |
|
| 95 |
### No header or footer |
| 96 |
|
| 97 |
To exclusively present information in a modal, remove the `header` and/or `footer`. |
| 98 |
|
| 99 |
```ts file="./ModalNoHeaderFooter.tsx" |
| 100 |
|
| 101 |
``` |
| 102 |
|
| 103 |
### Title icon |
| 104 |
|
| 105 |
To add an icon before a modal’s title, use the `titleIconVariant`, which can be set to one of the predefined variants -- "success", "danger", "warning", "info", and "custom" -- or to an imported custom icon. The following example uses a "warning" variant. |
| 106 |
|
| 107 |
```ts file="./ModalTitleIcon.tsx" |
| 108 |
|
| 109 |
``` |
| 110 |
|
| 111 |
### Custom title icon |
| 112 |
|
| 113 |
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. |
| 114 |
|
| 115 |
```ts file="./ModalCustomTitleIcon.tsx" |
| 116 |
|
| 117 |
``` |
| 118 |
|
| 119 |
### With wizard |
| 120 |
|
| 121 |
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. |
| 122 |
|
| 123 |
```ts file="./ModalWithWizard.tsx" |
| 124 |
|
| 125 |
``` |
| 126 |
|
| 127 |
### With dropdown |
| 128 |
|
| 129 |
To present a menu of actions or links to a user, you can add a [dropdown](/components/menus/dropdown) to a modal. To allow the dropdown to visually break out of the modal container, set the `menuAppendTo` property to “parent”. Handle the modal’s closing behavior by listening to the `onEscapePress` callback on the `<Modal>` component. This allows the "escape" key to collapse the dropdown without closing the entire modal. |
| 130 |
|
| 131 |
```ts file="./ModalWithDropdown.tsx" |
| 132 |
|
| 133 |
``` |
| 134 |
|
| 135 |
### With help |
| 136 |
|
| 137 |
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. |
| 138 |
|
| 139 |
```ts file="./ModalWithHelp.tsx" |
| 140 |
|
| 141 |
``` |
| 142 |
|
| 143 |
### With form |
| 144 |
|
| 145 |
To collect user input within a modal, you can add a [form](/components/forms/form). |
| 146 |
|
| 147 |
To submit the form from a button in the modal's footer (outside of the `<Form>`), set the button's `form` property equal to the form's id. |
| 148 |
|
| 149 |
```ts file="ModalWithForm.tsx" |
| 150 |
|
| 151 |
``` |
| 152 |
|
| 153 |
### Custom focus |
| 154 |
|
| 155 |
Use the `elementToFocus` property to customize which element inside the Modal receives focus when initially opened. |
| 156 |
|
| 157 |
```ts file="./ModalCustomFocus.tsx" |
| 158 |
|
| 159 |
``` |