2016-08-11 11:41:06 +08:00
---
2022-11-09 12:28:04 +08:00
group: Feedback
2016-08-11 11:41:06 +08:00
category: Components
2016-09-21 11:28:38 +08:00
title: Modal
2024-03-22 14:22:42 +08:00
description: Display a modal dialog box, providing a title, content area, and action buttons.
2024-01-29 14:50:36 +08:00
cover: https://mdn.alipayobjects.com/huamei_7uahnr/afts/img/A*Z9vzQZAdJDQAAAAAAAAAAAAADrJ8AQ/original
coverDark: https://mdn.alipayobjects.com/huamei_7uahnr/afts/img/A*WtgsSLPa1Z4AAAAAAAAAAAAADrJ8AQ/original
2022-11-09 12:28:04 +08:00
demo:
cols: 2
2016-08-11 11:41:06 +08:00
---
2016-09-10 13:43:30 +08:00
## When To Use
2016-08-11 11:41:06 +08:00
2023-01-16 23:48:26 +08:00
When requiring users to interact with the application, but without jumping to a new page and interrupting the user's workflow, you can use `Modal` to create a new floating layer over the current page to get user feedback or display information.
2023-12-06 09:38:16 +08:00
Additionally, if you need to show a simple confirmation dialog, you can use [`App.useApp` ](/components/app/ ) hooks.
2016-08-11 11:41:06 +08:00
2022-11-09 12:28:04 +08:00
## Examples
2022-11-17 17:31:26 +08:00
<!-- prettier - ignore -->
2022-11-09 12:28:04 +08:00
< code src = "./demo/basic.tsx" > Basic< / code >
< code src = "./demo/async.tsx" > Asynchronously close< / code >
< code src = "./demo/footer.tsx" > Customized Footer< / code >
2024-05-15 11:21:40 +08:00
< code src = "./demo/loading.tsx" version = "5.18.0" > Loading< / code >
2024-06-06 16:03:47 +08:00
< code src = "./demo/footer-render.tsx" version = "5.9.0" > Customized Footer render function< / code >
2023-07-11 09:58:25 +08:00
< code src = "./demo/hooks.tsx" > Use hooks to get context< / code >
2022-11-09 12:28:04 +08:00
< code src = "./demo/locale.tsx" > Internationalization< / code >
< code src = "./demo/manual.tsx" > Manual to update destroy< / code >
< code src = "./demo/position.tsx" > To customize the position of modal< / code >
< code src = "./demo/dark.tsx" debug > Dark Bg< / code >
< code src = "./demo/button-props.tsx" > Customize footer buttons props< / code >
< code src = "./demo/modal-render.tsx" > Custom modal content render< / code >
< code src = "./demo/width.tsx" > To customize the width of modal< / code >
2023-01-16 23:48:26 +08:00
< code src = "./demo/static-info.tsx" > Static Method< / code >
2023-07-11 09:58:25 +08:00
< code src = "./demo/confirm.tsx" > Static confirmation< / code >
2023-09-25 14:27:41 +08:00
< code src = "./demo/classNames.tsx" > Customize className for build-in module< / code >
2023-01-16 23:48:26 +08:00
< code src = "./demo/confirm-router.tsx" > destroy confirmation modal dialog< / code >
2023-10-19 15:03:20 +08:00
< code src = "./demo/nested.tsx" debug > Nested Modal< / code >
2022-11-17 14:04:31 +08:00
< code src = "./demo/render-panel.tsx" debug > \_InternalPanelDoNotUseOrYouWillBeFired</ code >
2022-12-06 23:14:30 +08:00
< code src = "./demo/custom-mouse-position.tsx" debug > Control modal's animation origin position< / code >
< code src = "./demo/wireframe.tsx" debug > Wireframe< / code >
2023-05-09 19:24:50 +08:00
< code src = "./demo/component-token.tsx" debug > Component Token< / code >
2022-11-09 12:28:04 +08:00
2016-08-11 11:41:06 +08:00
## API
2023-08-08 18:27:48 +08:00
Common props ref: [Common props](/docs/react/common-props)
2022-11-17 17:31:26 +08:00
| Property | Description | Type | Default | Version |
| --- | --- | --- | --- | --- |
| afterClose | Specify a function that will be called when modal is closed completely | function | - | |
2023-09-25 14:27:41 +08:00
| classNames | Config Modal build-in module's className | `header?: string; body?: string; footer?: string; mask?: string; wrapper?: string;` | - | |
2023-10-23 11:19:46 +08:00
| styles | Config Modal build-in module's style | `header?: CSSProperties; body?: CSSProperties; footer?: CSSProperties; mask?: CSSProperties;` | - | 5.10.0 |
2022-12-22 14:12:26 +08:00
| cancelButtonProps | The cancel button props | [ButtonProps ](/components/button/#api ) | - | |
2022-11-17 17:31:26 +08:00
| cancelText | Text of the Cancel button | ReactNode | `Cancel` | |
| centered | Centered Modal | boolean | false | |
2024-06-03 11:30:31 +08:00
| closable | Whether a close (x) button is visible on top right or not | boolean \| { closeIcon?: React.ReactNode } | true | |
2024-02-01 10:36:21 +08:00
| closeIcon | Custom close icon. 5.7.0: close button will be hidden when setting to `null` or `false` | ReactNode | < CloseOutlined /> | |
2022-11-17 17:31:26 +08:00
| confirmLoading | Whether to apply loading visual effect for OK button or not | boolean | false | |
| destroyOnClose | Whether to unmount child components on onClose | boolean | false | |
| focusTriggerAfterClose | Whether need to focus trigger element after dialog is closed | boolean | true | 4.9.0 |
2024-06-06 16:03:47 +08:00
| footer | Footer content, set as `footer={null}` when you don't need default buttons | React.ReactNode \| ((params:[footerRenderParams](/components/modal-cn#footerrenderparams))=> React.ReactNode) | (OK and Cancel buttons) | renderFunction: 5.9.0 |
2022-11-17 17:31:26 +08:00
| forceRender | Force render Modal | boolean | false | |
| getContainer | The mounted node for Modal but still display at fullscreen | HTMLElement \| () => HTMLElement \| Selectors \| false | document.body | |
| keyboard | Whether support press esc to close | boolean | true | |
| mask | Whether show mask or not | boolean | true | |
| maskClosable | Whether to close the modal dialog when the mask (area outside the modal) is clicked | boolean | true | |
| modalRender | Custom modal content render | (node: ReactNode) => ReactNode | - | 4.7.0 |
2022-12-22 14:12:26 +08:00
| okButtonProps | The ok button props | [ButtonProps ](/components/button/#api ) | - | |
2022-11-17 17:31:26 +08:00
| okText | Text of the OK button | ReactNode | `OK` | |
| okType | Button `type` of the OK button | string | `primary` | |
| style | Style of floating layer, typically used at least for adjusting the position | CSSProperties | - | |
2024-05-15 11:21:40 +08:00
| loading | Show the skeleton | boolean | | 5.18.0 |
2022-11-17 17:31:26 +08:00
| title | The modal dialog's title | ReactNode | - | |
| open | Whether the modal dialog is visible or not | boolean | false | |
| width | Width of the modal dialog | string \| number | 520 | |
| wrapClassName | The class name of the container of the modal dialog | string | - | |
| zIndex | The `z-index` of the Modal | number | 1000 | |
| onCancel | Specify a function that will be called when a user clicks mask, close button on top right or Cancel button | function(e) | - | |
| onOk | Specify a function that will be called when a user clicks the OK button | function(e) | - | |
2023-03-15 21:32:20 +08:00
| afterOpenChange | Callback when the animation ends when Modal is turned on and off | (open: boolean) => void | - | 5.4.0 |
2017-12-28 16:26:03 +08:00
#### Note
2022-11-19 22:56:58 +08:00
- The state of Modal will be preserved at it's component lifecycle by default, if you wish to open it with a brand new state every time, set `destroyOnClose` on it.
2020-07-20 14:18:36 +08:00
- There is a situation that using `<Modal />` with Form, which won't clear fields value when closing Modal even you have set `destroyOnClose` . You need `<Form preserve={false} />` in this case.
- `Modal.method()` RTL mode only supports hooks.
2017-08-06 00:04:25 +08:00
2017-05-16 10:43:12 +08:00
### Modal.method()
2016-08-11 11:41:06 +08:00
There are five ways to display the information based on the content's nature:
- `Modal.info`
- `Modal.success`
- `Modal.error`
- `Modal.warning`
- `Modal.confirm`
2019-05-07 14:57:32 +08:00
The items listed above are all functions, expecting a settings object as parameter. The properties of the object are follows:
2016-08-11 11:41:06 +08:00
2022-11-17 17:31:26 +08:00
| Property | Description | Type | Default | Version |
| --- | --- | --- | --- | --- |
| afterClose | Specify a function that will be called when modal is closed completely | function | - | 4.9.0 |
| autoFocusButton | Specify which button to autofocus | null \| `ok` \| `cancel` | `ok` | |
2022-12-22 14:12:26 +08:00
| cancelButtonProps | The cancel button props | [ButtonProps ](/components/button/#api ) | - | |
2022-11-17 17:31:26 +08:00
| cancelText | Text of the Cancel button with Modal.confirm | string | `Cancel` | |
| centered | Centered Modal | boolean | false | |
| className | The className of container | string | - | |
2023-11-17 18:30:43 +08:00
| closable | Whether a close (x) button is visible on top right of the confirm dialog or not | boolean | false | 4.9.0 |
| closeIcon | Custom close icon | ReactNode | undefined | 4.9.0 |
2022-11-17 17:31:26 +08:00
| content | Content | ReactNode | - | |
2024-06-06 16:03:47 +08:00
| footer | Footer content, set as `footer: null` when you don't need default buttons | React.ReactNode \| ((params:[footerRenderParams](/components/modal-cn#footerrenderparams))=> React.ReactNode) | - | renderFunction: 5.9.0 |
2022-11-17 17:31:26 +08:00
| getContainer | Return the mount node for Modal | HTMLElement \| () => HTMLElement \| Selectors \| false | document.body | |
2023-02-28 16:12:05 +08:00
| icon | Custom icon | ReactNode | < ExclamationCircleFilled /> | |
2022-11-17 17:31:26 +08:00
| keyboard | Whether support press esc to close | boolean | true | |
| mask | Whether show mask or not. | boolean | true | |
| maskClosable | Whether to close the modal dialog when the mask (area outside the modal) is clicked | boolean | false | |
2022-12-22 14:12:26 +08:00
| okButtonProps | The ok button props | [ButtonProps ](/components/button/#api ) | - | |
2022-11-17 17:31:26 +08:00
| okText | Text of the OK button | string | `OK` | |
| okType | Button `type` of the OK button | string | `primary` | |
| style | Style of floating layer, typically used at least for adjusting the position | CSSProperties | - | |
| title | Title | ReactNode | - | |
| width | Width of the modal dialog | string \| number | 416 | |
| wrapClassName | The class name of the container of the modal dialog | string | - | 4.18.0 |
| zIndex | The `z-index` of the Modal | number | 1000 | |
| onCancel | Specify a function that will be called when the user clicks the Cancel button. The parameter of this function is a function whose execution should include closing the dialog. If the function does not take any parameter (`!onCancel.length`) then modal dialog will be closed unless returned value is `true` (`!!onCancel()`). You can also just return a promise and when the promise is resolved, the modal dialog will also be closed | function(close) | - | |
| onOk | Specify a function that will be called when the user clicks the OK button. The parameter of this function is a function whose execution should include closing the dialog. If the function does not take any parameter (`!onOk.length`) then modal dialog will be closed unless returned value is `true` (`!!onOk()`). You can also just return a promise and when the promise is resolved, the modal dialog will also be closed | function(close) | - | |
2016-08-11 11:41:06 +08:00
2018-08-25 21:51:19 +08:00
All the `Modal.method` s will return a reference, and then we can update and close the modal dialog by the reference.
2017-05-16 10:43:12 +08:00
```jsx
2018-08-25 21:51:19 +08:00
const modal = Modal.info();
modal.update({
title: 'Updated title',
2018-08-25 21:58:40 +08:00
content: 'Updated content',
2018-08-25 21:51:19 +08:00
});
2020-10-24 14:35:00 +08:00
// on 4.8.0 or above, you can pass a function to update modal
2022-11-30 20:14:41 +08:00
modal.update((prevConfig) => ({
2020-10-24 14:35:00 +08:00
...prevConfig,
title: `${prevConfig.title} (New)` ,
}));
2018-08-25 21:51:19 +08:00
modal.destroy();
2017-05-16 10:43:12 +08:00
```
2018-12-03 21:49:00 +08:00
- `Modal.destroyAll`
2018-12-03 17:08:48 +08:00
2020-11-26 10:00:48 +08:00
`Modal.destroyAll()` could destroy all confirmation modal dialogs(`Modal.confirm|success|info|error|warning`). Usually, you can use it in router change event to destroy confirm modal dialog automatically without use modal reference to close( it's too complex to use for all modal dialogs)
2018-12-03 17:08:48 +08:00
```jsx
import { browserHistory } from 'react-router';
// router change
browserHistory.listen(() => {
2018-12-03 21:49:00 +08:00
Modal.destroyAll();
2018-12-03 17:08:48 +08:00
});
```
2020-02-03 13:00:35 +08:00
### Modal.useModal()
2020-11-26 10:00:48 +08:00
When you need using Context, you can use `contextHolder` which created by `Modal.useModal` to insert into children. Modal created by hooks will get all the context where `contextHolder` are. Created `modal` has the same creating function with `Modal.method` .
2020-02-03 13:00:35 +08:00
```jsx
const [modal, contextHolder] = Modal.useModal();
React.useEffect(() => {
modal.confirm({
// ...
});
}, []);
return < div > {contextHolder}< / div > ;
```
2020-02-07 20:32:00 +08:00
2023-07-11 09:58:25 +08:00
`modal.confirm` return method:
- `destroy` : Destroy current modal
- `update` : Update current modal
- `then` : (Hooks only) Promise chain call, support `await` operation
```tsx
// Return `true` when click `onOk` and `false` when click `onCancel`
const confirmed = await modal.confirm({ ... });
```
2024-06-06 16:03:47 +08:00
#### footerRenderParams
2023-08-28 11:54:43 +08:00
<!-- prettier - ignore -->
| Property | Description | Type | Default |
| --- | --- | --- | --- |
2024-06-06 16:03:47 +08:00
| originNode | default node | React.ReactNode | - |
| extra | extended options | { OkBtn: FC; CancelBtn: FC } | - |
2023-08-28 11:54:43 +08:00
2024-02-05 11:45:42 +08:00
### `styles` and `classNames` attribute
< code src = "./demo/_semantic.tsx" simplify = "true" > < / code >
2023-04-11 10:25:24 +08:00
## Design Token
< ComponentTokenTable component = "Modal" > < / ComponentTokenTable >
2020-02-07 20:32:00 +08:00
## FAQ
2022-04-22 19:30:44 +08:00
### Why content not update when Modal closed?
Modal will use memo to avoid content jumping when closed. Also, if you use Form in Modal, you can reset `initialValues` by calling `resetFields` in effect.
2020-11-12 11:17:34 +08:00
### Why I can not access context, redux, ConfigProvider `locale/prefixCls` in Modal.xxx?
2020-02-07 20:32:00 +08:00
antd will dynamic create React instance by `ReactDOM.render` when call Modal methods. Whose context is different with origin code located context.
When you need context info (like ConfigProvider context), you can use `Modal.useModal` to get `modal` instance and `contextHolder` node. And put it in your children:
```tsx
const [modal, contextHolder] = Modal.useModal();
2020-03-21 11:45:24 +08:00
// then call modal.confirm instead of Modal.confirm
2020-02-07 20:32:00 +08:00
return (
< Context1.Provider value = "Ant" >
2021-10-09 12:03:30 +08:00
{/* contextHolder is in Context1, which means modal will get context of Context1 */}
2020-02-07 20:32:00 +08:00
{contextHolder}
< Context2.Provider value = "Design" >
2021-10-09 12:03:30 +08:00
{/* contextHolder is out of Context2, which means modal will not get context of Context2 */}
2020-02-07 20:32:00 +08:00
< / Context2.Provider >
< / Context1.Provider >
);
```
**Note:** You must insert `contextHolder` into your children with hooks. You can use origin method if you do not need context connection.
2020-10-15 18:32:48 +08:00
2022-12-26 11:55:18 +08:00
> [App Package Component](/components/app) can be used to simplify the problem of `useModal` and other methods that need to manually implant contextHolder.
2022-12-26 10:08:15 +08:00
2021-02-09 21:49:15 +08:00
### How to set static methods prefixCls ?
2022-12-22 14:12:26 +08:00
You can config with [`ConfigProvider.config` ](/components/config-provider#configproviderconfig-4130 )