question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Dialog] Add imperative methods to display them directly

See original GitHub issue

Refer to the call and implementation of the ant-design dialog box. This solution brings more flexibility and concise code.

Will there be such a solution?

import { Dialog } from "@material-ui/core";

Dialog.show({
  type: "info",
  title:  "Message",
  content: "Hello",
  onOk: () => { ... }
});

Benchmark

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:4
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
eps1loncommented, Feb 3, 2021

Could you expand a bit on how you would use it and why the existing API cannot be used for that use case?

Offering an imperative API for state updates can be quite problematic since we or React is no longer in control of the semantics of such an update. So these APIs impose quite the maintenance burden and it’s not clear why it is needed considering this is a React library.

0reactions
koistyacommented, Sep 18, 2022

@CarbonPool most likely you want imperative handler(s) to be set on the instance of the dialog component rather than relying on static methods, for example:

import * as React from "react";
import { Button, Container } from "@mui/material";
import { ConfirmDialog, DialogElement } from "./ConfirmDialog.js";

export function Example(): JSX.Element {
  const dialogRef = React.useRef<DialogElement>(null);

  return (
    <Container>
      <Button onClick={() => dialogRef.current?.open()}>Show Dialog</Button>
      
      <ConfirmDialog ref={dialogRef} onConfirm={...} />
    </Container>
  );
}

@oliviertassinari BTW, it might be a good idea to add .open() method to the Dialog’s ref out of the box which should be a small tweak without any breaking changes.

https://github.com/kriasoft/react-starter-kit/discussions/2004


Alternatively, with <DialogProvider> and useDialog() hook, it may look like this:

import * as React from "react";
import { Button, Container } from "@mui/material";
import { useDialog } from "./core/dialog.js";
import { ConfirmDialog } from "./dialogs/ConfirmDialog.js"; // Custom dialog based on Material UI <Dialog />

export function Example(): JSX.Element {
  const dialog = useDialog(
    // factory method returning an instance of Material UI dialog
    () => <ConfirmDialog onConfirm={...} />, // `open`, and `onClose` props  will be added by the hook
    [] // React hook dependencies
  );

  return (
    <Container>
      <Button onClick={dialog.show}>Show Dialog</Button>
    </Container>
  );
}

By default, the DialogProvider can mount a single instance of the target dialog when used on multiple screens, and if a new instance of the dialog is needed, it can be done by passing key={...} with a unique value to the dialog props.


Alternatively, the useDialog() hook can operate without a need to add <DialogProvider> upstream like this:

import * as React from "react";
import { Button, Container } from "@mui/material";
import { useDialog } from "./core/dialog.js";
import { ConfirmDialog } from "./dialogs/ConfirmDialog.js"; // Custom dialog based on Material UI <Dialog />

export function Example(): JSX.Element {
  const dialog = useDialog(
    // factory method returning an instance of Material UI dialog
    () => <ConfirmDialog onConfirm={...} />, // `open`, and `onClose` props  will be added by the hook
    [] // React hook dependencies
  );

  return (
    <Container>
      <Button onClick={dialog.show}>Show Dialog</Button>
      {dialog.component}
    </Container>
  );
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

[Dialog] Add imperative methods to display them directly #24759
Refer to the call and implementation of the ant-design dialog box. This solution brings more flexibility and concise code.
Read more >
MUI - How to open Dialog imperatively/programmatically
First create a custom Provider component in this case I'll call DialogProvider . This component will manage a list of Dialog s in...
Read more >
How to Implement and Style the Dialog Element | CSS-Tricks
A look from Christian Kozalla on the HTML element and using it to create a nice-looking and accessible modal.
Read more >
Windows 7 Dialog Boxes (Design basics) - Win32 apps
A dialog box is a secondary window that allows users to perform a command, asks users a question, or provides users with information...
Read more >
Adding AlertDialog with Jetpack Compose to Android apps
This article presents a simple example using Jetpack Compose in a project and how to create an alert dialog that can come in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found