Dialog
- Usage
- Styling
Dialog is a small window that can be used to present information and user interface elements in an overlay.
new tab
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);
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.
Header
The header contains an optional title element, and a slot next to it for custom header content, such as a close button.
new tab
dialog.setHeaderTitle("User details");
Button closeButton = new Button(new Icon("lumo", "cross"),
(e) -> dialog.close());
closeButton.addThemeVariants(ButtonVariant.LUMO_TERTIARY);
dialog.getHeader().add(closeButton);
Footer
Buttons for closure actions (e.g., Save, Cancel, Delete) should be placed in the footer. See the Button 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:
new tab
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);
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 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.
Dialog dialog = new Dialog();
dialog.setModal(false);
Usually, non-modal dialogs should be draggable, so that the user can move them to access the user interface beneath.
Position
By default, dialogs open in the center of the viewport. A different positioning can be set programmatically. 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:
Dialog dialog = new Dialog();
dialog.setTop("50px");
dialog.setLeft("50px");
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.
new tab
dialog.setModal(false);
dialog.setDraggable(true);
...
H2 headline = new H2("Add note");
headline.addClassName("draggable");
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.
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.
Dialog dialog = new Dialog();
dialog.setWidth("400px");
dialog.setHeight("200px");
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.
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.
new tab
Button closeButton = new Button("Close");
closeButton.addClickListener(e -> dialog.close());
Best Practices
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 when appropriate.
Button Placement
See Buttons in Dialogs.
Related Components
Component | Usage recommendations |
---|---|
Dialog for confirming user actions and decisions | |
A generic overlay whose position is anchored to an element in the UI. |
Technical
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, 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:
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