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

{% hint style="info" %}
Version compatibility

`dialog` was introduced in version **4.58.0** of the JourneyApps Runtime.
{% endhint %}

{% hint style="success" %}
This `dialog` component is generated from the view's XML. For dialogs triggered from JavaScript/TypeScript, see [the journey.dialog documentation](https://docs.journeyapps.com/reference/build/js-ts-apis/journey/journey.dialog).
{% endhint %}

{% hint style="warning" %}
**Known limitation**

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

### `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.

{% hint style="info" %}
**Tip**

Use simple dialogs sparingly as they are interruptive. Rather, less obtrusive [notifications](https://docs.journeyapps.com/reference/build/ui-components/all-ui-components/notification) can be used.
{% endhint %}

**Example**

{% code title="main.view\.xml" %}

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

{% endcode %}

{% code title="main.js" %}

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

{% endcode %}

![](https://2865107717-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F9TCHLR67eLHBOjPvHhud%2Fuploads%2Fgit-blob-4b2967ad9778bccb87fa3334c9c19372f5f7d61d%2Fdialog-simple-example.png?alt=media)

#### 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**

{% code title="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)"/>
```

{% endcode %}

{% code title="main.js" %}

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

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

{% endcode %}

![](https://2865107717-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F9TCHLR67eLHBOjPvHhud%2Fuploads%2Fgit-blob-ed1800acf0b8d9614a8708074350c4e200e2bafa%2Fdialog-confirmation-example.png?alt=media)

#### 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**:

{% code title="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>
```

{% endcode %}

{% code title="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
}
```

{% endcode %}

![](https://2865107717-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F9TCHLR67eLHBOjPvHhud%2Fuploads%2Fgit-blob-8742c6ecf403cc72f4216fe89fb4cf6eea9ca45c%2Fdialog-composition-example.png?alt=media)

## Standard Attributes

### `id`

**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:

{% content-ref url="../xml-fields/id" %}
[id](https://docs.journeyapps.com/reference/build/ui-components/xml-fields/id)
{% endcontent-ref %}

### `title`

**Optional**

**Type**: `string` (static text, a [format string](https://docs.journeyapps.com/reference/app-features/xml-format-strings) or the return value of a [JS/TS function](https://docs.journeyapps.com/reference/app-features/calling-js-functions-from-xml))

**Default**: 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`

**Optional**

**Type**: `boolean`

**Default**: `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"`.

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

```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`

**Optional**

**Default**: 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`](https://docs.journeyapps.com/reference/app-features/calling-js-functions-from-xml) or [navigation](https://docs.journeyapps.com/reference/get-started/journeyapps-fundamentals/view-navigation). See more details:

{% content-ref url="../js-ts-events" %}
[js-ts-events](https://docs.journeyapps.com/reference/build/ui-components/js-ts-events)
{% endcontent-ref %}

{% code title="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)"/>
```

{% endcode %}

{% code title="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
    }
}
```

{% endcode %}

{% hint style="info" %}
Specifying `on-cancel` will change the dialog to a confirmation dialog, with a secondary and primary button.
{% endhint %}

### `on-submit`

**Optional**

**Default**: 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`](https://docs.journeyapps.com/reference/app-features/calling-js-functions-from-xml) or [navigation](https://docs.journeyapps.com/reference/get-started/journeyapps-fundamentals/view-navigation). See more details:

{% content-ref url="../js-ts-events" %}
[js-ts-events](https://docs.journeyapps.com/reference/build/ui-components/js-ts-events)
{% endcontent-ref %}

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

{% hint style="info" %}
`on-submit` will not trigger when a simple dialog is dismissable by selecting on the backdrop or the user pressing Esc key.
{% endhint %}

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

**Optional**

**Type**: `string`

**Default**: 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.

{% hint style="success" %}
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"
{% endhint %}

```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`

**Optional**

**Type**: `string`

**Default**: 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](https://docs.journeyapps.com/reference/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;
}
```

## Component Methods

The following component methods are available when an [`id`](https://docs.journeyapps.com/reference/build/ui-components/xml-fields/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.
