Render a dialog using utility functions in React Dialog component

18 Jan 202320 minutes to read

The dialog component provides built-in utility functions to render the alert and confirm dialogs with the minimal code.
The following options are used as an argument on calling the utility functions:

Options Description
title Specifies the title of dialog like the header property.
content Specifies the value that can be displayed in dialog’s content area like the content property.
isModal Specifies the Boolean value whether the dialog can be displayed as modal or non-modal. For more details, refer to the isModal property.
position Specifies the value where the alert or confirm dialog is positioned within the document. For more details, refer to the position property { X: ‘center’, Y: ‘center’}
okButton Configures the OK button that contains button properties with the click events. okButton:{ icon:'prefix icon to the button', cssClass:'custom class to the button', click: 'action for OK button click', text: 'Yes' // <-- Default value is 'OK' }
cancelButton Configures the Cancel button that contains button properties with the click events. cancelButton:{ icon:'prefix icon to the button', cssClass:'custom class to the button', click: 'action for ‘Cancel’ button click', text: 'No' // <-- Default value is 'Cancel'}
isDraggable Specifies the value whether the alert or confirm dialog can be dragged by the user.
showCloseIcon When set to true, the close icon is shown in the dialog component.
closeOnEscape When set to true, you can close the dialog by pressing ESC key.
animationSettings Specifies the animation settings of the dialog component.
cssClass Specifies the CSS class name that can be appended to the dialog.
zIndex Specifies the order of the dialog, that is displayed in front or behind of another component.
open Event which is triggered after the dialog is opened.
Close Event which is triggered after the dialog is closed.

Alert dialog

An alert dialog box is used to display warning like messages to the users. Use the following code to render a simple alert dialog in an application.

[Class-component]

import { DialogUtility } from '@syncfusion/ej2-popups';
import * as React from "react";
class App extends React.Component {
    buttonClick() {
        DialogUtility.alert('This is an Alert Dialog!');
    }
    render() {
        return (<button className="e-control e-btn" onClick={this.buttonClick} id="targetButton" role="button">Open Alert Dialog</button>);
    }
}
export default App;
import { DialogUtility } from '@syncfusion/ej2-popups';
import * as React from "react";

class App extends React.Component<{}, {}> {
  public buttonClick(): void {
    DialogUtility.alert('This is an Alert Dialog!');
}
public render() {
return (
  <button className="e-control e-btn" onClick={this.buttonClick} id="targetButton" role="button">Open Alert Dialog</button>);
}
}
export default App;

[Functional-component]

import { DialogUtility } from '@syncfusion/ej2-popups';
import * as React from "react";
function App() {
    function buttonClick() {
        DialogUtility.alert('This is an Alert Dialog!');
    }
    return (<button className="e-control e-btn" onClick={buttonClick} id="targetButton" role="button">Open Alert Dialog</button>);
}
export default App;
import { DialogUtility } from '@syncfusion/ej2-popups';
import * as React from "react";

function App() {
  function buttonClick(): void {
      DialogUtility.alert('This is an Alert Dialog!');
  }
  return (
    <button className="e-control e-btn" onClick={buttonClick} id="targetButton" role="button">Open Alert Dialog</button>);
}

export default App;

Render an alert dialog with options

[Class-component]

import * as React from 'react';
import * as ReactDOM from 'react-dom';
import App from './App';
ReactDOM.render(<App />, document.getElementById('container'));
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import App  from './App';

ReactDOM.render(<App />, document.getElementById('container'));
<!DOCTYPE html>
<html lang="en">

<head>
    <title>Syncfusion React Dialog Utility</title>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta name="description" content="Essential JS 2 for React Components" />
    <meta name="author" content="Syncfusion" />
    <link href="styles.css" rel="stylesheet" />
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
    <link href="https://cdn.syncfusion.com/ej2/27.2.2/ej2-base/styles/material.css" rel="stylesheet" />
    <link href="https://cdn.syncfusion.com/ej2/27.2.2/ej2-react-buttons/styles/material.css" rel="stylesheet" />
    <link href="https://cdn.syncfusion.com/ej2/27.2.2/ej2-react-popups/styles/material.css" rel="stylesheet" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
    <script src="systemjs.config.js"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>

<body>
    <div id='container'>
        <div id="dialog">
            <div id='loader'>Loading....</div>
        </div>
    </div>
</body>

</html>

[Functional-component]

import * as React from 'react';
import * as ReactDOM from 'react-dom';
import App from './App';
ReactDOM.render(<App />, document.getElementById('container'));
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import App  from './App';

ReactDOM.render(<App />, document.getElementById('container'));
<!DOCTYPE html>
<html lang="en">

<head>
    <title>Syncfusion React Dialog Utility</title>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta name="description" content="Essential JS 2 for React Components" />
    <meta name="author" content="Syncfusion" />
    <link href="styles.css" rel="stylesheet" />
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
    <link href="https://cdn.syncfusion.com/ej2/27.2.2/ej2-base/styles/material.css" rel="stylesheet" />
    <link href="https://cdn.syncfusion.com/ej2/27.2.2/ej2-react-buttons/styles/material.css" rel="stylesheet" />
    <link href="https://cdn.syncfusion.com/ej2/27.2.2/ej2-react-popups/styles/material.css" rel="stylesheet" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
    <script src="systemjs.config.js"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>

<body>
    <div id='container'>
        <div id="dialog">
            <div id='loader'>Loading....</div>
        </div>
    </div>
</body>

</html>

Confirm dialog

A confirm dialog displays a specified message along with ‘OK’ and ‘Cancel’ button.

[Class-component]

import { DialogUtility } from '@syncfusion/ej2-popups';
import * as React from "react";
class App extends React.Component {
    buttonClick() {
        DialogUtility.confirm('This is a Confirmation Dialog!');
    }
    render() {
        return (<button className="e-control e-btn" onClick={this.buttonClick = this.buttonClick.bind(this)} id="targetButton" role="button">Open Confirm Dialog</button>);
    }
}
export default App;
import { DialogUtility } from '@syncfusion/ej2-popups';
import * as React from "react";

class App extends React.Component<{}, {}> {
  public buttonClick(): void {
    DialogUtility.confirm('This is a Confirmation Dialog!');
}
  public render() {
    return (
    <button className="e-control e-btn" onClick={this.buttonClick = this.buttonClick.bind(this)} id="targetButton" role="button">Open Confirm Dialog</button>
    );
  }
}
export default App;

[Functional-component]

import { DialogUtility } from '@syncfusion/ej2-popups';
import * as React from "react";
function App() {
    function buttonClick() {
        DialogUtility.confirm('This is a Confirmation Dialog!');
    }
    return (<button className="e-control e-btn" onClick={buttonClick.bind(this)} id="targetButton" role="button">Open Confirm Dialog</button>);
}
export default App;
import { DialogUtility } from '@syncfusion/ej2-popups';
import * as React from "react";

function App() {
    function buttonClick(): void {
      DialogUtility.confirm('This is a Confirmation Dialog!');
    }

    return (
      <button className="e-control e-btn" onClick={buttonClick.bind(this)} id="targetButton" role="button">Open Confirm Dialog</button>
    );
}

export default App;

Render a confirmation dialog with options

[Class-component]

import { DialogUtility } from '@syncfusion/ej2-popups';
import * as React from "react";
class App extends React.Component {
    buttonClick() {
        DialogUtility.confirm({
            animationSettings: { effect: 'Zoom' },
            cancelButton: { text: 'Cancel', click: this.cancelClick.bind(this) },
            closeOnEscape: true,
            content: "This is a Confirmation Dialog!",
            okButton: { text: 'OK', click: this.okClick.bind(this) },
            showCloseIcon: true,
            title: ' Confirmation Dialog',
        });
    }
    okClick() {
        alert('you clicked OK button');
    }
    cancelClick() {
        alert('you clicked Cancel button');
    }
    render() {
        return (<button className="e-control e-btn" onClick={this.buttonClick = this.buttonClick.bind(this)} id="targetButton" role="button">Open Confirm Dialog</button>);
    }
}
export default App;
import { DialogUtility } from '@syncfusion/ej2-popups';
import * as React from "react";

class App extends React.Component<{}, {}> {
  public buttonClick(): void {
    DialogUtility.confirm({
      animationSettings: { effect: 'Zoom' },
      cancelButton: {  text: 'Cancel', click: this.cancelClick.bind(this) },
      closeOnEscape: true,
      content: "This is a Confirmation Dialog!",
      okButton: {  text: 'OK', click: this.okClick.bind(this) },
      showCloseIcon: true,
      title: ' Confirmation Dialog',
    });
  }

  public okClick(): void {
    alert('you clicked OK button');
  }

  public cancelClick(): void {
    alert('you clicked Cancel button');
  }
  public render() {
  return (
    <button className="e-control e-btn" onClick={this.buttonClick = this.buttonClick.bind(this)} id="targetButton" role="button">Open Confirm Dialog</button>
  );
  }
  }
export default App;

[Functional-component]

import { DialogUtility } from '@syncfusion/ej2-popups';
import * as React from "react";
function App() {
    function buttonClick() {
        DialogUtility.confirm({
            animationSettings: { effect: 'Zoom' },
            cancelButton: { text: 'Cancel', click: cancelClick.bind(this) },
            closeOnEscape: true,
            content: "This is a Confirmation Dialog!",
            okButton: { text: 'OK', click: okClick.bind(this) },
            showCloseIcon: true,
            title: ' Confirmation Dialog',
        });
    }
    function okClick() {
        alert('you clicked OK button');
    }
    function cancelClick() {
        alert('you clicked Cancel button');
    }
    return (<button className="e-control e-btn" onClick={buttonClick.bind(this)} id="targetButton" role="button">Open Confirm Dialog</button>);
}
export default App;
import { DialogUtility } from '@syncfusion/ej2-popups';
import * as React from "react";

function App() {
    function buttonClick(): void {
      DialogUtility.confirm({
        animationSettings: { effect: 'Zoom' },
        cancelButton: {  text: 'Cancel', click: cancelClick.bind(this) },
        closeOnEscape: true,
        content: "This is a Confirmation Dialog!",
        okButton: {  text: 'OK', click: okClick.bind(this) },
        showCloseIcon: true,
        title: ' Confirmation Dialog',
      });
    }
  
    function okClick(): void {
      alert('you clicked OK button');
    }
  
    function cancelClick(): void {
      alert('you clicked Cancel button');
    }

    return (
      <button className="e-control e-btn" onClick={buttonClick.bind(this)} id="targetButton" role="button">Open Confirm Dialog</button>
    );
}

export default App;