JGuang

Dialog

Dialog · Ant Design Mobile

Preview
npm install antd-mobile@5.43.0

Dialog

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

Dialog

Props

NameDescriptionTypeDefault
actionsThe list of the operation button, a secondary array can be passed in to realize multiple buttons side by side in the same row(Action | Action[])[][]
afterCloseCallback after Dialog is completely closed() => void-
afterShowTriggered after fully displayed() => void-
bodyClassNameDialog content class namestring-
bodyStyleDialog content styleReact.CSSProperties-
closeOnActionWhether to close after clicking the operation buttonbooleanfalse
closeOnMaskClickWhether to support clicking the mask to close the dialog boxbooleanfalse
contentThe content of the DialogReact.ReactNode-
destroyOnCloseDestroy dom when not visiblebooleanfalse
disableBodyScrollWhether to disable body scrollingbooleantrue
forceRenderWhether to render the DOM structure when hiddenbooleanfalse
getContainerThe parent container of the custom dialogHTMLElement | (() => HTMLElement) | nullnull
headerThe top areaReact.ReactNode-
imageThe url of the picturestring-
maskClassNameDialog mask class namestring-
maskStyleDialog mask styleReact.CSSProperties-
onActionTriggered when the action button is clicked(action: Action, index: number) => void | Promise<void>-
onCloseTriggered when closed() => void-
stopPropagationStop the propagation of some events.PropagationEvent[]['click']
titleThe title of the DialogReact.ReactNode-
visibleTo show or hidebooleanfalse

Action

NameDescriptionTypeDefault
boldWhether the text is boldbooleanfalse
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>-
styleStyle of the action buttonReact.CSSProperties-
textThe titleReact.ReactNode-

Imperative

You can use Dialog in an imperative way:

Dialog.show

const handler = Dialog.show(props)

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

When the dialog 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 dialog() => void-

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

Dialog.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>-

Dialog.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>-

It should be noted that for the Dialog created by instructive, ** will not perceive the re-rendering of the parent component and the update of the state in it**, so the following writing is completely wrong:

export default function App() {
  const [captcha, setCaptcha] = useState<string>("");
  const showCaptcha = () => {
    return Dialog.confirm({
      title: "SMS verification",
      content: (
        <div>
          <Input
            placeholder="Please enter verification code"
            value={captcha} // Updates to the captcha state in App will not be passed to the Dialog
            onChange={(v) => {
              setCaptcha(v)
            }}
          />
        </div>
      )
    });
  };
  return (
    <div>
      <Button onClick={showCaptcha}>Show</Button>
    </div>
  );
}

If you need to include a lot of complex states and logic in Dialog, you can use declarative syntax, or consider encapsulating the internal state and logic as a separate component, see #4762.

Dialog.clear

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