---
title: "dialog"
description: "Overview"
permalink: /docs/build/ui-components/all-ui-components/dialog/
---
# dialog

## Overview

The `dialog` UI component appears as a blocking overlay that provides information or confirms destructive behavior. They often prompt users to make a decision.

<Info>

Version compatibility

`dialog` was introduced in version **4.58.0** of the JourneyApps Runtime.

</Info>

<Info>

This `dialog` component is generated from the view's XML. For dialogs triggered from JavaScript/TypeScript, see [the journey.dialog documentation](/docs/build/js-ts-apis/journey/journey.dialog/).

</Info>

<Warning title={"Known limitation"}>

A `dialog` can currently not be shown upon entering a view (e.g. by calling it from the `init()` or `resume()` function).

</Warning>

### `dialog` use cases

There are 3 main use cases for `dialog`. These are described in more detail below.

1. [Simple](#simple) dialogs
2. [Confirmation](#confirmation) dialogs
3. [Composition](#composition) dialogs

#### Simple

A simple `dialog` displays information to the user and does not require the user to make a decision, thus can be dismissed by selecting the primary button, hitting the `Esc` key or back button.

<Info title={"Tip"}>

Use simple dialogs sparingly as they are interruptive. Rather, less obtrusive [notifications](/docs/build/ui-components/all-ui-components/notification/) can be used.

</Info>

**Example**

**main.view\.xml**

```xml
<dialog id="success-dialog" title="Success" subtext="You have been successfully enrolled."/>
```

**main.js**

```javascript
function onEnrollment() {
    component.dialog({id: "success-dialog"}).show();
}
```

![](./media/spaces-2f9tchlr67elhbojpvhhud-2fuploads-2fgit-blob-4b2967ad9778bccb87fa3334c9c19372f5f7d61d-2fdialog-simple-example-0c4be93b.png)

#### Confirmation

A confirmation `dialog` gives the user the ability to provide final confirmation of a decision before committing to it. If the user confirms a choice, it’s carried out. Otherwise, the user can dismiss the dialog by selecting the secondary button.

**Example**

**main.view\.xml**

```xml
<dialog id="reset-settings" title="Reset settings?" submit-text="Reset"
        subtext="Would you like to reset your app to its default settings"
        on-submit="$:resetSettings(true)" on-cancel="$:resetSettings(false)"/>
```

**main.js**

```javascript
function showResetSettings() {
    component.dialog({id: "reset-settings"}).show();
}

function resetSettings(ok){
    if (ok) {
        // Logic here
    } else {
        // Other logic here
    }
}
```

![](./media/spaces-2f9tchlr67elhbojpvhhud-2fuploads-2fgit-blob-ed1800acf0b8d9614a8708074350c4e200e2bafa-2fdialog-confirmation-example-82e9fe07.png)

#### Composition

A composition dialogs allows developers to embed other UI components inside it, therefore offering a wide range of input options.

To embed a UI component to the `dialog`, add it inside a `body` tag. Refer to the syntax for more detail.

**Example**:

**main.view\.xml**

```xml
<!-- XML -->
<dialog id="dialog-input" title="Stock: {$:getStockTitle()}">
    <body>
        <text-input label="Capture stock Qty" bind="stock" required="true" />
    </body>
    <button-group>
        <button label="Cancel" color="negative" on-press="$:onCancel()" validate="false"/>
        <button label="Save" on-press="$:onSubmit()" validate="true"/>
    </button-group>
</dialog>
```

**main.js**

```javascript
// JS
function getStockTitle(){
    return view.cur_item.name;
}

function onCancel() {
    // Logic for when the user selects Cancel
}

function onSubmit() {
    // Logic for when the user selects Save
}
```

![](./media/spaces-2f9tchlr67elhbojpvhhud-2fuploads-2fgit-blob-8742c6ecf403cc72f4216fe89fb4cf6eea9ca45c-2fdialog-composition-example-b694fa38.png)

## Standard Attributes

### `title`

<SyntaxCard type={"string (static text, format string, or JS/TS function result)"} defaultValue={"unset"} />

```xml
<dialog id="login-dialog" title="Login successful"/>
```

A `dialog`'s `title` communicates its purpose to the user.

Titles should:

* Contain a brief, clear statement or question
* Summarize the dialog's content
* Avoid the following:
  * apologies (“Sorry for the interruption”)
  * alarm (“Warning!”)
  * ambiguity (“Are you sure?”)

```xml
<dialog id="task-complete" title="Task complete" />
```

```javascript
function onTaskComplete() {
    component.dialog({id: "task-complete"}).show();
}
```

## Advanced Attributes

### `auto-hide`

<SyntaxCard type={"boolean"} defaultValue={"true"} />

```xml
<dialog id="discard-dialog" title="Unsaved changes"
subtext="You have unsaved changes, are you sure you want to discard?" auto-hide="false" >
```

By default the dialog will automatically hide when the user selects any of its buttons. To override this behavior, specify `auto-hide="false"`.

<Info>

When specifying `auto-hide="false"`, you are responsible for hiding the dialog using `component.dialog({id:'myId'}).hide()`.

</Info>

```xml
<dialog id="tc-dialog" title="Terms and conditions" subtext="Do you agree to these Ts and Cs?"
        auto-hide="false"
        on-submit="$:agree(true)" on-cancel="$:agree(false)"
        submit-text="Agree" cancel-text="Disagree">
```

```javascript
function showTsAndCs() {
    component.dialog({id: "tc-dialog"}).show();
}

function agree(predicate){
    if (predicate){
        component.dialog({id: "tc-dialog"}).hide();
        // Other success logic here
    } else {
        var ok = confirmDialog('By not agreeing to these terms, you void your warranty.\n
                                Are you sure?');
        if (ok) {
            component.dialog({id: "tc-dialog"}).hide();
            // Other logic here
        }
        // The dialog will not hide and the user may change their selection
    }
}
```

### `on-cancel`

<SyntaxCard defaultValue={"unset"} />

**Triggered when**: The secondary button on a confirmation dialog is selected.

**Event parameter**: Empty by default. Can be a user-defined variable or field.

**Return value**: `undefined`, or the user-defined variable or field

`on-cancel` is an event that calls a JS/TS [`$:function`](/docs/app-features/calling-js-functions-from-xml/) or [navigation](/docs/get-started/journeyapps-fundamentals/view-navigation/). See more details:

- [js ts events](/docs/build/ui-components/js-ts-events/)

**main.view\.xml**

```xml
<dialog id="use-location" title="Use location Service?" subtext="The app requires access to your location"
        on-submit="$:useLocation(true)" on-cancel="$:useLocation(false)"/>
```

**main.js**

```javascript
function askLocation() {
    component.dialog({id: "use-location"}).show();
}

function useLocation(ok){
    if (ok) {
        // Logic for on-submit here
    } else {
        // Logic for on-cancel here
    }
}
```

<Info>

Specifying `on-cancel` will change the dialog to a confirmation dialog, with a secondary and primary button.

</Info>

### `on-submit`

<SyntaxCard defaultValue={"unset"} />

**Triggered when**: The primary button (for simple dialogs) or submit button (for multi-button dialogs) on a dialog is selected.

**Event parameter**: Empty by default. Can be a user-defined variable or field.

**Return value**: `undefined`, or the user-defined variable or field.

`on-submit` is an event that calls a JS/TS [`$:function`](/docs/app-features/calling-js-functions-from-xml/) or [navigation](/docs/get-started/journeyapps-fundamentals/view-navigation/). See more details:

- [js ts events](/docs/build/ui-components/js-ts-events/)

See example for [`on-cancel`](#on-cancel).

<Info>

`on-submit` will not trigger when a simple dialog is dismissable by selecting on the backdrop or the user pressing Esc key.

</Info>

### `submit-text` and `cancel-text`

<SyntaxCard type={"string"} defaultValue={"The default submit (primary) button text is OK and for confirmation dialogs the default cancel button text is Cancel. These will be translated by default."} />

```xml
<dialog id="tc-dialog" title="Terms and agreements" subtext="Do you agree to these Ts and Cs?" submit-text="Agree" cancel-text="Disagree">
```

The `submit-text` and `cancel-text` attributes allows for changing the default text on the dialog.

<Info>

Be as specific as possible with button copy to avoid ambiguity. Instead of "Yes", rather repeat the action that a button will trigger - for example "Discard", "Save" or "Reset"

</Info>

```xml
<dialog id="tc-dialog" title="Terms and agreements" subtext="Do you agree to these Ts and Cs?"
        on-submit="$:agree(true)" on-cancel="$:agree(false)"
        submit-text="Agree" cancel-text="Disagree">
```

```javascript
function showTsAndCs() {
    component.dialog({id: "tc-dialog"}).show();
}

function agree(ok){
    if (ok) {
        // Logic here
    } else {
        // Other logic here
    }
}
```

### `subtext`

<SyntaxCard type={"string"} defaultValue={"unset"} />

```xml
<dialog
    id="task-added"
    title="Job added"
    subtext="Your job with job-id:{$:getJobId()}\nwas added successfully"
/>
```

Subtext provides more detail to the user about the purpose of the dialog. The `subtext` attribute can be a [format string](/docs/app-features/xml-format-strings/) and it also supports newline characters.

```xml
<dialog id="task-added" title="Job added" subtext="Your job with job-id:{$:getJobId()}\nwas added successfully"/>
```

```javascript
function onTaskAdded() {
    component.dialog({id: "task-added"}).show();
}

function getJobId() {
    return view.currentJob.id_code;
}
```

## Common Attributes

### `border-radius`

<AttributeReference label="border-radius" href="/docs/build/ui-components/common-attributes/border-radius/" badge="new" sinceVersion="5.1.0" />

### `border-width`

<AttributeReference label="border-width" href="/docs/build/ui-components/common-attributes/border-width/" badge="new" sinceVersion="5.1.0" />

### `border-color`

<AttributeReference label="border-color" href="/docs/build/ui-components/common-attributes/border-color/" badge="new" sinceVersion="5.1.0" />

### `border-style`

<AttributeReference label="border-style" href="/docs/build/ui-components/common-attributes/border-style/" badge="new" sinceVersion="5.1.0" />

### `id`

<SyntaxCard required />

```xml
<dialog id="task-complete" title="Task complete" />
```

The `id` attribute is required to target a particular dialog to call `show()` or `hide()` on it.

```xml
<dialog id="task-complete" title="Task complete"/>
```

```javascript
function onTaskComplete() {
    component.dialog({id: "task-complete"}).show();
}

function onTaskUploadFail() {
    component.dialog({id: "task-complete"}).hide();
}
```

See also:

<AttributeReference label="id" href="/docs/build/ui-components/common-attributes/id/" />

## Component Methods

The following component methods are available when an [`id`](/docs/build/ui-components/common-attributes/id/) is assigned to the component and `component.dialog({id:'my-id'})` is called from JS/TS:

### `scrollIntoView`

Programmatically scroll until the component is visible in the view.
