> Markdown version of [Dialog](https://vaadin.com/docs/v24/components/dialog). Section index: [llms.txt](https://vaadin.com/docs/v24/components/llms.txt)

# Dialog

Dialog is a small window that can be used to present information and user interface elements in an overlay.

**Lit** — `dialog-basic.ts`

```html
<vaadin-dialog
  header-title="New employee"
  .opened="${this.dialogOpened}"
  @closed="${() => {
    this.dialogOpened = false;
  }}"
  ${dialogRenderer(this.renderDialog, [])}
  ${dialogFooterRenderer(this.renderFooter, [])}
></vaadin-dialog>

<vaadin-button
  @click="${() => {
    this.dialogOpened = true;
  }}"
>
  Show dialog
</vaadin-button>
```

**Flow** — `DialogBasic.java`

```java
Dialog dialog = new Dialog();

dialog.setHeaderTitle("New employee");

VerticalLayout dialogLayout = createDialogLayout();
dialog.add(dialogLayout);

Button saveButton = createSaveButton(dialog);
Button cancelButton = new Button("Cancel", e -> dialog.close());
dialog.getFooter().add(cancelButton);
dialog.getFooter().add(saveButton);

Button button = new Button("Show dialog", e -> dialog.open());

add(dialog, button);
```

**React** — `dialog-basic.tsx`

```tsx
<Dialog
  headerTitle="New employee"
  opened={dialogOpened.value}
  onClosed={() => {
    dialogOpened.value = false;
  }}
  footer={
    <>
      <Button
        onClick={() => {
          dialogOpened.value = false;
        }}
      >
        Cancel
      </Button>
      <Button
        theme="primary"
        onClick={() => {
          dialogOpened.value = false;
        }}
      >
        Add
      </Button>
    </>
  }
>
  <VerticalLayout style={{ alignItems: 'stretch', width: '18rem', maxWidth: '100%' }}>
    <TextField label="First name" />
    <TextField label="Last name" />
  </VerticalLayout>
</Dialog>

<Button
  onClick={() => {
    dialogOpened.value = true;
  }}
>
  Show dialog
</Button>
```

## <a id="structure"></a>Structure

The Dialog component has static header and footer areas, and a scrolling content area between them. The header and footer are optional, and are hidden if empty and not explicitly enabled.

### <a id="header"></a>Header (since V23.1)

The header contains an optional title element, and a slot next to it for custom header content, such as a close button.

If the dialog has a title (set via text or custom element), the header content slot is right-aligned. If no title is present, the header content slot is left-aligned.

**Lit** — `dialog-header.ts`

```html
<vaadin-dialog
  header-title="User details"
  .opened="${this.dialogOpened}"
  @closed="${() => {
    this.dialogOpened = false;
  }}"
  ${dialogHeaderRenderer(
    () => html`
      <vaadin-button theme="tertiary" @click="${this.close}">
        <vaadin-icon icon="lumo:cross"></vaadin-icon>
      </vaadin-button>
    `,
    []
  )}
  ${dialogRenderer(this.renderDialog, this.user)}
></vaadin-dialog>
```

**Flow** — `DialogHeader.java`

```java
dialog.setHeaderTitle("User details");

Button closeButton = new Button(new Icon("lumo", "cross"),
        (e) -> dialog.close());
closeButton.addThemeVariants(ButtonVariant.LUMO_TERTIARY);
dialog.getHeader().add(closeButton);
```

**React** — `dialog-header.tsx`

```tsx
<Dialog
  header-title="User details"
  opened={dialogOpened.value}
  onClosed={() => {
    dialogOpened.value = false;
  }}
  header={
    <Button theme="tertiary" onClick={close}>
      <Icon icon="lumo:cross" />
    </Button>
  }
>
  <VerticalLayout
    theme="spacing"
    style={{ width: '300px', maxWidth: '100%', alignItems: 'stretch' }}
  >
    <VerticalLayout style={{ alignItems: 'stretch' }}>
      <TextField
        label="Name"
        value={`${user.value?.firstName} ${user.value?.lastName}`}
        readonly
        style={{ paddingTop: 0 }}
      />
      <EmailField label="Email" value={user.value?.email} readonly />
      <TextField label="Address" value={addressDescription.value} readonly />
    </VerticalLayout>
  </VerticalLayout>
</Dialog>
<Button onClick={open}>Show dialog</Button>
```

### <a id="footer"></a>Footer (since V23.1)

Buttons for closure actions (e.g., *Save*, *Cancel*, *Delete*) should be placed in the footer. See the [Button](https://vaadin.com/docs/v24/components/button.md#buttons-in-dialogs) component for guidelines for the placement of buttons in Dialogs. Footer content is right-aligned by default. Components can be left-aligned by applying a margin like so:

**Lit** — `dialog-footer.ts`

```html
<vaadin-dialog
  header-title="${`Delete user "${this.user?.firstName} ${this.user?.lastName}"?`}"
  .opened="${this.dialogOpened}"
  @closed="${() => {
    this.dialogOpened = false;
  }}"
  ${dialogRenderer(() => html`Are you sure you want to delete this user permanently?`, [])}
  ${dialogFooterRenderer(
    () => html`
      <vaadin-button theme="primary error" @click="${this.close}" style="margin-right: auto;">
        Delete
      </vaadin-button>
      <vaadin-button theme="tertiary" @click="${this.close}">Cancel</vaadin-button>
    `,
    []
  )}
></vaadin-dialog>
```

**Flow** — `DialogFooter.java`

```java
Button deleteButton = new Button("Delete", (e) -> dialog.close());
deleteButton.addThemeVariants(ButtonVariant.LUMO_PRIMARY,
        ButtonVariant.LUMO_ERROR);
deleteButton.getStyle().set("margin-right", "auto");
dialog.getFooter().add(deleteButton);

Button cancelButton = new Button("Cancel", (e) -> dialog.close());
cancelButton.addThemeVariants(ButtonVariant.LUMO_TERTIARY);
dialog.getFooter().add(cancelButton);
```

**React** — `dialog-footer.tsx`

```tsx
<Dialog
  headerTitle={`Delete user "${user.value?.firstName} ${user.value?.lastName}"?`}
  opened={dialogOpened.value}
  onClosed={() => {
    dialogOpened.value = false;
  }}
  footer={
    <>
      <Button theme="primary error" onClick={close} style={{ marginRight: 'auto' }}>
        Delete
      </Button>
      <Button theme="tertiary" onClick={close}>
        Cancel
      </Button>
    </>
  }
>
  Are you sure you want to delete this user permanently?
</Dialog>
```

### <a id="padding"></a>Padding

The content area’s built-in padding can be removed by applying the `no-padding` theme variant.

**Lit** — `dialog-no-padding.ts`

```html
<vaadin-dialog
  theme="no-padding"
  header-title="Filter reports by users:"
  .opened="${this.dialogOpened}"
  @closed="${() => {
    this.dialogOpened = false;
  }}"
  ${dialogRenderer(
    () => html`
      <vaadin-grid .items="${this.people}" style="width: 500px; max-width: 100%;">
        <vaadin-grid-selection-column></vaadin-grid-selection-column>
        <vaadin-grid-column
          header="Name"
          ${columnBodyRenderer<Person>(
            (item) => html`${item.firstName} ${item.lastName}`,
            []
          )}
        ></vaadin-grid-column>
      </vaadin-grid>
    `,
    this.people
  )}
  ${dialogFooterRenderer(
    () => html`
      <vaadin-button theme="primary" @click="${this.close}">Filter</vaadin-button>
    `,
    []
  )}
></vaadin-dialog>
```

**Flow** — `DialogNoPadding.java`

```java
dialog.addThemeVariants(DialogVariant.LUMO_NO_PADDING);
```

**React** — `dialog-no-padding.tsx`

```tsx
<Dialog
  theme="no-padding"
  header-title="Filter reports by users:"
  opened={dialogOpened.value}
  onClosed={() => {
    dialogOpened.value = false;
  }}
  footer={
    <Button
      theme="primary"
      onClick={() => {
        dialogOpened.value = false;
      }}
    >
      Filter
    </Button>
  }
>
  <Grid items={people.value} style={{ width: '500px', maxWidth: '100%' }}>
    <GridSelectionColumn />
    <GridColumn header="Name">
      {({ item }) => (
        <>
          {item.firstName} {item.lastName}
        </>
      )}
    </GridColumn>
  </Grid>
</Dialog>
```

## <a id="modality"></a>Modality

A modal dialog blocks the user from interacting with the rest of the user interface while the dialog is open. A non-modal dialog, however, doesn’t block interaction. Dialogs are modal by default.

Use modal dialogs for:

- Displaying important information, like system errors;

- Requesting user input as part of a workflow, for example an edit dialog;

- Confirmation of irreversible actions, such as deleting data — Confirm Dialog is a convenient alternative for these use cases; and

- Breaking out sub-tasks into a separate user interface.

> **Important: Modal Parent Dialogs in Flow**
>
> See the [Technical](#technical) section for details on the behavior of modal dialogs in Vaadin Flow.

Use non-modal dialogs when the user needs access to the content below the dialog, and for less critical, optional, or support tasks.

**Lit**

```html
<vaadin-dialog modeless>...</vaadin-dialog>
```

**Flow**

```java
Dialog dialog = new Dialog();
dialog.setModal(false);
```

**React**

```html
<Dialog modeless>...</Dialog>
```

Usually, non-modal dialogs should be draggable, so that the user can move them to access the user interface beneath.

> **Note:** In some use cases you may want to disable the server side modality. This can be done after Dialog has been opened.

```java
        Dialog dialog = new Dialog();
        dialog.addOpenedChangeListener(e -> {
            if (e.isOpened()) {
                UI.getCurrent().setChildComponentModal(dialog, false);
            }
        });
        // Dialog needs to be added to layout
        add(dialog);
```

## <a id="position"></a>Position

By default, dialogs open in the center of the viewport. A different positioning can be set programmatically (since V24.6). Dialogs can also be made draggable, allowing the end user to move them around.

The position of the dialog can be set using the `top` and `left` properties:

**Lit**

```html
<vaadin-dialog top="50px" left="50px">...</vaadin-dialog>
```

**Flow**

```java
Dialog dialog = new Dialog();
dialog.setTop("50px");
dialog.setLeft("50px");
```

**React**

```html
<Dialog top="50px" left="50px">...</Dialog>
```

### <a id="draggable"></a>Draggable

Dialogs can be made draggable, enabling the user to move them around using a pointing device.

By default, the outer edges of a dialog, as well as the space between its components, can be used to move the dialog around.

The default areas from which a dialog can be dragged depend on whether the built-in header is used if the built-in header or footer is used: they function as the default drag handles of the dialog. Without the built-in header, any empty space within the dialog functions can be used as a drag handle.

Any component contained within a dialog can be marked and used as a drag handle by applying the `draggable` class name to it. You can choose whether to make the component’s content draggable as well, or only the component itself.

**Lit** — `dialog-draggable.ts`

```html
<vaadin-dialog
  aria-label="Add note"
  draggable
  modeless
  .opened="${this.dialogOpened}"
  @closed="${() => {
    this.dialogOpened = false;
  }}"
  ${dialogHeaderRenderer(
    () => html`
      <h2
        class="draggable"
        style="flex: 1; cursor: move; margin: 0; font-size: 1.5em; font-weight: bold; padding: var(--lumo-space-m) 0;"
      >
        Add note
      </h2>
    `,
    []
  )}
  ${dialogRenderer(
    () => html`
      <vaadin-vertical-layout
        theme="spacing"
        style="width: 300px; max-width: 100%; align-items: stretch;"
      >
        <vaadin-vertical-layout style="align-items: stretch;">
          <vaadin-text-field label="Title"></vaadin-text-field>
          <vaadin-text-area label="Description"></vaadin-text-area>
        </vaadin-vertical-layout>
      </vaadin-vertical-layout>
    `,
    []
  )}
  ${dialogFooterRenderer(
    () => html`
      <vaadin-button @click="${this.close}">Cancel</vaadin-button>
      <vaadin-button theme="primary" @click="${this.close}">Add note</vaadin-button>
    `,
    []
  )}
></vaadin-dialog>
```

**Flow** — `DialogDraggable.java`

```java
dialog.setModal(false);
dialog.setDraggable(true);

...

H2 headline = new H2("Add note");
headline.addClassName("draggable");
```

**React** — `dialog-draggable.tsx`

```tsx
<Dialog
  aria-label="Add note"
  draggable
  modeless
  opened={dialogOpened.value}
  onClosed={() => {
    dialogOpened.value = false;
  }}
  header={
    <h2
      className="draggable"
      style={{
        flex: 1,
        cursor: 'move',
        margin: 0,
        fontSize: '1.5em',
        fontWeight: 'bold',
        padding: 'var(--lumo-space-m) 0',
      }}
    >
      Add note
    </h2>
  }
  footerRenderer={() => (
    <>
      <Button onClick={close}>Cancel</Button>
      <Button theme="primary" onClick={close}>
        Add note
      </Button>
    </>
  )}
>
  <VerticalLayout
    theme="spacing"
    style={{ width: '300px', maxWidth: '100%', alignItems: 'stretch' }}
  >
    <VerticalLayout style={{ alignItems: 'stretch' }}>
      <TextField label="Title" />
      <TextArea label="Description" />
    </VerticalLayout>
  </VerticalLayout>
</Dialog>
```

Make non-modal dialogs draggable so the user can interact with content that might otherwise be obscured by the Dialog. For example, a Dialog for taking notes or for adding widgets to a dashboard by dragging can offer a better experience by allowing the user to move the Dialog around.

Modal dialogs don’t benefit from being draggable, as their modality curtain (the dark overlay behind the dialog) obscures the underlying user interface.

### <a id="constraining-a-dialog-to-the-viewport"></a>Constraining a Dialog to the Viewport (new in V24.10)

By default, dialogs are not constrained to the viewport, which means they can become partially invisible when dragging the dialog or when resizing the viewport. To prevent this, set the `keepInViewport` property to `true` to always keep the dialog fully visible within the viewport. Note that enabling this option may also adjust the dialog’s size and position if it can not fit the viewport otherwise.

**Lit**

```html
<vaadin-dialog keep-in-viewport>...</vaadin-dialog>
```

**Flow**

```java
Dialog dialog = new Dialog();
dialog.setKeepInViewport(true);
```

**React**

```html
<Dialog keepInViewport>...</Dialog>
```

## <a id="size"></a>Size

The Dialog size can be set with the width and height on the Dialog itself (*since V24.6 for React and Lit*). You can also set the size of the content of Dialog, whereby the Dialog scales to accommodate it. In both cases, the Dialog can also be made resizable.

**Lit**

```html
<vaadin-dialog width="400px" height="200px">...</vaadin-dialog>
```

**Flow**

```java
Dialog dialog = new Dialog();
dialog.setWidth("400px");
dialog.setHeight("200px");
```

**React**

```html
<Dialog width="400px" height="200px">...</Dialog>
```

### <a id="resizable"></a>Resizable

The Dialog can be configured to allow the end user to resize it by dragging from its edges.

Dialogs containing dynamic content or plenty of information, such as complex forms or Grids, may benefit from being resizable. This offers the user some flexibility with how much data is visible at once. It also gives the user control over which part of the underlying user interface is obscured.

Dialogs that contain very little or compact information don’t need to be resizable.

**Lit** — `dialog-resizable.ts`

```html
<vaadin-dialog
  header-title="Employee list"
  resizable
  draggable
  .opened="${this.dialogOpened}"
  @closed="${() => {
    this.dialogOpened = false;
  }}"
  ${dialogRenderer(
    () => html`
      <vaadin-vertical-layout
        theme="spacing"
        style="max-width: 100%; min-width: 300px; height: 100%; align-items: stretch;"
      >
        <vaadin-grid .items="${this.people}">
          <vaadin-grid-column path="firstName" title="First name"></vaadin-grid-column>
          <vaadin-grid-column path="lastName" title="Last name"></vaadin-grid-column>
          <vaadin-grid-column path="email" title="Email"></vaadin-grid-column>
          <vaadin-grid-column path="profession" title="Profession"></vaadin-grid-column>
          <vaadin-grid-column path="membership" title="Membership"></vaadin-grid-column>
        </vaadin-grid>
      </vaadin-vertical-layout>
    `,
    this.people
  )}
></vaadin-dialog>
```

**Flow** — `DialogResizable.java`

```java
dialog.setDraggable(true);
dialog.setResizable(true);
```

**React** — `dialog-resizable.tsx`

```tsx
<Dialog
  headerTitle="Employee list"
  resizable
  draggable
  opened={dialogOpened.value}
  onClosed={() => {
    dialogOpened.value = false;
  }}
>
  <VerticalLayout
    theme="spacing"
    style={{ maxWidth: '100%', minWidth: '300px', height: '100%', alignItems: 'stretch' }}
  >
    <Grid items={people.value}>
      <GridColumn path="firstName" title="First name" />
      <GridColumn path="lastName" title="Last name" />
      <GridColumn path="email" title="Email" />
      <GridColumn path="profession" title="Profession" />
      <GridColumn path="membership" title="Membership" />
    </Grid>
  </VerticalLayout>
</Dialog>
```

## <a id="closing"></a>Closing

Modal dialogs are closable in three ways: by pressing the `Esc` key; clicking outside the Dialog; or programmatically, for example through the click of a Button.

Providing an explicit button for closing a Dialog is recommended.

**Lit** — `dialog-closing.ts`

```html
<vaadin-dialog
  aria-label="System maintenance notice"
  .opened="${this.dialogOpened}"
  @closed="${() => {
    this.dialogOpened = false;
  }}"
  ${dialogRenderer(
    () => html`
      <vaadin-vertical-layout
        theme="spacing"
        style="width: 300px; max-width: 100%; align-items: stretch;"
      >
        <h2 style="margin: var(--lumo-space-m) 0; font-size: 1.5em; font-weight: bold;">
          System maintenance
        </h2>
        <p>
          System maintenance will begin at 3 PM. It is schedule to conclude at 5PM. We
          apologise for any inconvenience.
        </p>
        <vaadin-button @click="${this.close}" style="align-self: flex-end;">
          Close
        </vaadin-button>
      </vaadin-vertical-layout>
    `,
    []
  )}
></vaadin-dialog>
```

**Flow** — `DialogClosing.java`

```java
Button closeButton = new Button("Close");
closeButton.addClickListener(e -> dialog.close());
```

**React** — `dialog-closing.tsx`

```tsx
<Dialog
  aria-label="System maintenance notice"
  opened={dialogOpened.value}
  onClosed={() => {
    dialogOpened.value = false;
  }}
>
  <VerticalLayout
    theme="spacing"
    style={{ width: '300px', maxWidth: '100%', alignItems: 'stretch' }}
  >
    <h2 style={{ margin: 'var(--lumo-space-m) 0', fontSize: '1.5em', fontWeight: 'bold' }}>
      System maintenance
    </h2>
    <p>
      System maintenance will begin at 3 PM. It is schedule to conclude at 5PM. We apologise
      for any inconvenience.
    </p>
    <Button onClick={close} style={{ alignSelf: 'flex-end' }}>
      Close
    </Button>
  </VerticalLayout>
</Dialog>
```

## <a id="best-practices"></a>Best Practices

### <a id="use-sparingly"></a>Use Sparingly

Dialogs are disruptive by nature and should be used sparingly. Don’t use them to communicate non-essential information, such as success messages like "Logged in", "Copied", and so on. Instead, use [Notifications](https://vaadin.com/docs/v24/components/notification.md) when appropriate.

### <a id="button-placement"></a>Button Placement

See [Buttons in Dialogs](https://vaadin.com/docs/v24/components/button.md#buttons-in-dialogs).

## <a id="related-components"></a>Related Components

| Component                                                                  | Usage recommendations                                                 |
| -------------------------------------------------------------------------- | --------------------------------------------------------------------- |
| [Confirm Dialog](https://vaadin.com/docs/v24/components/confirm-dialog.md) | Dialog for confirming user actions and decisions                      |
| [Popover](https://vaadin.com/docs/v24/components/popover.md)               | A generic overlay whose position is anchored to an element in the UI. |

## <a id="technical"></a>Technical

### <a id="multiple-dialogs-and-server-side-modality-in-flow"></a>Multiple Dialogs and Server-side Modality in Flow

By default, opening multiple Dialog components simultaneously in Flow adds each subsequent Dialog as a child of the previous one.

Due to the [server-side modality mechanism in Flow](https://vaadin.com/docs/v24/flow/advanced/server-side-modality.md#server-side-modality), this means that closing a modal Dialog automatically also closes all its children, that is, any subsequent Dialogs opened after the modal.

This can be avoided by explicitly adding each Dialog to the UI before opening it:

```java
Dialog d1 = new Dialog();
Dialog d2 = new Dialog();
add(d1, d2);  // Add dialogs to the UI before opening
d1.open();
d2.open();
```

`EBCF8110-6074-4DAB-BE6B-662C813EDDC9`
