Home / Blog / How to create a Dialog box

How to create a Dialog box

Ionic Design Kit, built on a design base of Ionic Framework components, simplifies app development by providing designers with a range of pre-made elements. Designers can effortlessly pick and place elements, eliminating the need to create components from scratch. For developers, this means no complex coding – all components are sourced from the Ionic Framework. The result is a speedy, efficient, and collaborative process, making the Ionic Design Kit essential for both designers and developers.

A dialog is like a pop-up window in an app that shows important information or asks the user to make a decision. When a dialog pops up, the user can't use the app until they do something about it – like confirming it, closing it, or taking a necessary action.

This article provides a detailed guide for designers and developers to easily add a user-friendly Dialog box to their applications. You can seamlessly integrate this feature by using design elements from the Ionic Kit, which is built on the strong foundation of the extensive Ionic framework library.

Let's add a dialog box on the chat screen to display alerts for users. The guide for creating the Chats screen is available here.

1. Import and add the useState to the functional component

Inside a functional component, add useState to create a state variable for managing the display of a dialog box, and a function that will modify this state. Please, make sure that you've imported the React Hook useState to Chats component.

const Chats: React.FC = () => {
  const [isOpen, setIsOpen] = useState(true)
  return (
    <IonPage>
      ...
    </IonPage>
  );
};

2. Add the Alert component

After the IonList add the IonAlert component to the IonContent section.

<IonPage>
  <IonHeader>
    ...
  </IonHeader>
  <IonContent>
    ...
    <IonAlert></IonAlert>
  </IonContent>
</IonPage>

3. Add the content to the Dialog box

Set the isOpen property to control the presentation state of the Alert. Define the properties header and message as 'Clear message history' and 'Are you sure you want to clear your message history? It will not be possible to undo this action.' to set the alert description. Set the buttons property as an array consisting of two items. For the first one, set the property text as 'Cancel', role as cancel and define the handler. For the second one, set the property text as 'Clear', role as confirm and define the handler.

<IonPage>
  <IonHeader>
    ...
  </IonHeader>
  <IonContent>
    ...
    <IonAlert
      isOpen={isOpen}
      header='Clear message history'
      message='Are you sure you want to clear your message history?
               It will not be possible to undo this action.'
      buttons={[
        {
          text: 'Cancel',
          role: 'cancel',
          handler: () => {}
        },
        {
          text: 'Clear',
          role: 'confirm',
          handler: () => {}
        }
      ]}
    ></IonAlert>
  </IonContent>
</IonPage>

Thank you for joining us on this exciting journey thus far. Stay tuned for more guides in our comprehensive series, where we'll not only delve into various aspects of mobile app development but also showcase how the Ionic Kit can be your game-changer.

Are you prepared to elevate your Ionic experience? Explore the advantages of incorporating the Ionic Design Kit for intuitive designs and seamless development. May your coding journey be marked by continual innovation and triumphant achievements!

Looking for Ionic kit for you platform?
Ionic Sketch
Ionic Figma
Ionic XD