JGuang

Modal

Modal · Ant Design Mobile

Preview
npm install antd-mobile@5.43.0

Modal

It is used for notification of important information or feedback of operation, with a small number of options for users to operate.

Install

npm install antd-mobile@5.43.0

antd-mobile is for mobile web, not React Native or Mini Programs. React 19 projects must register createRoot with unstableSetRender at the entry, or imperative APIs such as Toast and Dialog cannot render. Switch dark mode with <html data-prefers-color-scheme="dark">.

Examples come from the official Ant Design Mobile repository (MIT) at the commit published as npm 5.43.0. Presentation helpers such as DemoBlock are JGuang rewrites, not antd-mobile APIs.

It is used for notification of important information or feedback of operation, with a small number of options for users to operate.

When to Use

When users need to process transactions, but do not want to jump to pages to interrupt the workflow, you can use Modal to open a floating layer in the middle of the current page to carry the corresponding operations.

Demos

Props

NameDescriptionTypeDefault
actionsThe list of the operation buttonAction[][]
afterCloseCallback after Modal is completely closed() => void-
afterShowTriggered after fully displayed() => void-
bodyClassNameModal content class namestring-
bodyStyleModal content styleReact.CSSProperties-
closeOnActionWhether to close after clicking the operation buttonbooleanfalse
closeOnMaskClickWhether to support clicking the mask to close the modal boxbooleanfalse
contentThe content of the ModalReact.ReactNode-
destroyOnCloseDestroy dom when not visiblebooleanfalse
disableBodyScrollMask Whether to disable body scrollingbooleantrue
forceRenderWhether to render the DOM structure when hiddenbooleanfalse
getContainerThe parent container of the custom modalHTMLElement | (() => HTMLElement) | nullnull
headerThe top areaReact.ReactNode-
imageThe url of the picturestring-
maskClassNameModal mask class namestring-
maskStyleModal mask styleReact.CSSProperties-
onActionTriggered when the action button is clicked(action: Action, index: number) => void | Promise<void>-
onCloseTriggered when closed() => void-
showCloseButtonWhether to show a close button on the top right cornerbooleanfalse
stopPropagationStop the propagation of some events.PropagationEvent[]['click']
titleThe title of the ModalReact.ReactNode-
visibleTo show or hidebooleanfalse

Action

NameDescriptionTypeDefault
classNameClass name of the action buttonstring-
dangerWhether in dangerous state or notbooleanfalse
disabledWhether disabled state or notbooleanfalse
keyThe unique markstring | number-
onClickTriggered on click() => void | Promise<void>-
primaryWhether in primary state or notbooleanfalse
styleStyle of the action buttonReact.CSSProperties-
textThe titleReact.ReactNode-

Imperative

You can use Modal in an imperative way:

Modal.show

const handler = Modal.show(props)

You can directly open the modal box by calling the show method on the Modal. The type of the props parameter is the same as the above table, but the visible prop is not supported.

When the modal box is closed, the component instance would be automatically destroyed.

The return value of the show method is a component controller, which contains the following properties:

NameDescriptionTypeDefault
closeTo close the modal() => void-

show is just a very basic method. In actual business, the following alert and confirm methods are more commonly used:

Modal.alert

alert accepts the same parameters as show, but does not support the closeOnAction actions prop. Its return value is not a controller object, but Promise<void>.

In addition, it supports the following props:

NameDescriptionTypeDefault
confirmTextThe content of the confirm buttonReactNode'Ok'
onConfirmTriggered when the confirm button is clicked() => void | Promise<void>-

Modal.confirm

confirm accepts the same parameters as show, but does not support the closeOnAction actions prop. Its return value is not a controller object, but Promise<boolean>.

In addition, it supports the following props:

NameDescriptionTypeDefault
cancelTextThe content of the cancel buttonReactNode'Cancel'
confirmTextThe content of the confirm buttonReactNode'Ok'
onCancelTriggered when the cancel button is clicked() => void | Promise<void>-
onConfirmTriggered when the confirm button is clicked() => void | Promise<void>-

Modal.clear

You can directly close all modals by calling the clear method on Modal. Usually, you can use it in router change event to close all modals automatically without using modal reference to close.

FAQ

Are there any pop-up windows with no content, no buttons, and no margins?

Yes, you can directly use the lower-level CenterPopup component.