Class Dialog
- All Implemented Interfaces:
com.vaadin.flow.component.AttachNotifier,com.vaadin.flow.component.DetachNotifier,com.vaadin.flow.component.HasComponents,com.vaadin.flow.component.HasElement,com.vaadin.flow.component.HasEnabled,com.vaadin.flow.component.HasSize,com.vaadin.flow.component.HasStyle,com.vaadin.flow.component.HasTheme,HasThemeVariant<DialogVariant>,Serializable
Dialogs can be made modal or non-modal. A modal Dialog blocks the user from interacting with the rest of the user interface while the Dialog is open, as opposed to a non-modal Dialog, which does not block interaction.
Dialogs can be made draggable and resizable. When draggable, the user is able to move them around using a pointing device. It is recommended to make non-modal Dialogs draggable so that the user can interact with content that might otherwise be obscured by the Dialog. A resizable Dialog allows the user to resize the Dialog by dragging from the edges of the Dialog with a pointing device. Dialogs are not resizable by default.
Dialogs automatically become scrollable when their content overflows. Custom scrollable areas can be created using the Scroller component.
Best Practices:
Dialogs are disruptive by nature and should be used sparingly. Do not use
them to communicate nonessential information, such as success messages like
“Logged in”, “Copied”, and so on. Instead, use Notifications when
appropriate.
- Author:
- Vaadin Ltd
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classEvent that is fired after the dialog's closing animation has finished.static class`vaadin-dialog-close-action` is sent when the user clicks outside the dialog or presses the escape key.static class`dragged` event is sent when the user finishes dragging the dialog.static final classClass for adding and removing components to the footer part of a dialog.static final classClass for adding and removing components to the header part of a dialog.static class`resize` event is sent when the user finishes resizing the dialog.static classEvent that is fired when the dialog's opened state changes. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidadd(Collection<com.vaadin.flow.component.Component> components) Adds the given components into this dialog.com.vaadin.flow.shared.RegistrationaddAttachListener(com.vaadin.flow.component.ComponentEventListener<com.vaadin.flow.component.AttachEvent> listener) com.vaadin.flow.shared.RegistrationaddClosedListener(com.vaadin.flow.component.ComponentEventListener<Dialog.ClosedEvent> listener) Add a lister for when the dialog's closing animation has finished.voidaddComponentAtIndex(int index, com.vaadin.flow.component.Component component) Adds the given component into this dialog at the given index.com.vaadin.flow.shared.RegistrationaddDetachListener(com.vaadin.flow.component.ComponentEventListener<com.vaadin.flow.component.DetachEvent> listener) com.vaadin.flow.shared.RegistrationaddDialogCloseActionListener(com.vaadin.flow.component.ComponentEventListener<Dialog.DialogCloseActionEvent> listener) Add a listener that controls whether the dialog should be closed or not.com.vaadin.flow.shared.RegistrationaddDraggedListener(com.vaadin.flow.component.ComponentEventListener<Dialog.DialogDraggedEvent> listener) Adds a listener that is called after the user finishes dragging the dialog.com.vaadin.flow.shared.RegistrationaddOpenedChangeListener(com.vaadin.flow.component.ComponentEventListener<Dialog.OpenedChangeEvent> listener) Add a listener for when the dialog's opened state changes.com.vaadin.flow.shared.RegistrationaddResizeListener(com.vaadin.flow.component.ComponentEventListener<Dialog.DialogResizeEvent> listener) Adds a listener that is called after the user finishes resizing the dialog.voidclose()Closes the dialog.protected StringSet thearia-labelattribute for assistive technologies like screen readers.Gets the object from which components can be added or removed from the dialog footer area.Gets the object from which components can be added or removed from the dialog header area.Gets the title set for the dialog header.getLeft()Gets the left position of the dialog.Deprecated, for removal: This API element is subject to removal in a future version.getRole()Gets the ARIA role for the dialog element, used by screen readers.com.vaadin.flow.dom.StylegetStyle()getTop()Gets the top position of the dialog.getWidth()booleanGets whether this dialog can be closed by hitting the esc-key or not.booleanGets whether this dialog can be closed by clicking outside of it or not.booleanGets whether dialog is enabled to be dragged or not.booleanisModal()Gets whether component is set as modal or modeless dialog.booleanisOpened()Gets the open state from the dialog.booleanGets whether dialog is enabled to be resized or not.protected voidonAttach(com.vaadin.flow.component.AttachEvent attachEvent) voidopen()Opens the dialog.protected voidsetAriaLabel(String ariaLabel) Set thearia-labelattribute for assistive technologies like screen readers.voidsetCloseOnEsc(boolean closeOnEsc) Sets whether this dialog can be closed by hitting the esc-key or not.voidsetCloseOnOutsideClick(boolean closeOnOutsideClick) Sets whether this dialog can be closed by clicking outside of it or not.voidsetDraggable(boolean draggable) Sets whether dialog is enabled to be dragged by the user or not.voidsetHeaderTitle(String title) Sets the title to be rendered on the dialog header.voidvoidSets the distance of the dialog from the left of its container.voidsetMaxHeight(String value) voidsetMaxWidth(String value) voidsetMinHeight(String value) voidsetMinWidth(String value) voidsetModal(boolean modal) Sets whether component will open modal or modeless dialog.voidsetOpened(boolean opened) Opens or closes the dialog.voidsetOverlayRole(String role) Deprecated, for removal: This API element is subject to removal in a future version.UsesetRole(String)insteadvoidsetResizable(boolean resizable) Sets whether dialog can be resized by user or not.voidSets the ARIA role for the dialog element, used by screen readers.voidSets the top position of the dialog.voidsetVisible(boolean visible) Set the visibility of the dialog.voidMethods inherited from class com.vaadin.flow.component.Component
addListener, findAncestor, fireEvent, from, get, getChildren, getElement, getEventBus, getId, getListeners, getLocale, getParent, getTranslation, getTranslation, getTranslation, getTranslation, getTranslation, getTranslation, getUI, hasListener, isAttached, isTemplateMapped, isVisible, onDetach, onEnabledStateChanged, removeFromParent, scrollIntoView, scrollIntoView, set, setElement, setIdMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface com.vaadin.flow.component.HasComponents
add, add, addComponentAsFirst, remove, remove, removeAllMethods inherited from interface com.vaadin.flow.component.HasElement
getElementMethods inherited from interface com.vaadin.flow.component.HasEnabled
isEnabled, setEnabledMethods inherited from interface com.vaadin.flow.component.HasSize
getHeightUnit, getWidthUnit, setHeight, setHeightFull, setMaxHeight, setMaxWidth, setMinHeight, setMinWidth, setSizeFull, setSizeUndefined, setWidth, setWidthFullMethods inherited from interface com.vaadin.flow.component.HasStyle
addClassName, addClassNames, getClassName, getClassNames, hasClassName, removeClassName, removeClassNames, setClassName, setClassNameMethods inherited from interface com.vaadin.flow.component.HasTheme
addThemeName, addThemeNames, getThemeName, getThemeNames, hasThemeName, removeThemeName, removeThemeNames, setThemeName, setThemeNameMethods inherited from interface com.vaadin.flow.component.shared.HasThemeVariant
addThemeVariants, removeThemeVariants
-
Constructor Details
-
Dialog
public Dialog()Creates an empty dialog. -
Dialog
public Dialog(com.vaadin.flow.component.Component... components) Creates a dialog with given components inside.- Parameters:
components- the components inside the dialog- See Also:
-
HasComponents.add(Component...)
-
Dialog
Creates a dialog with given title.- Parameters:
title- the title of the component
-
Dialog
Creates a dialog with given title and components inside.- Parameters:
title- the title of the componentcomponents- the components inside the dialog
-
-
Method Details
-
getTop
Gets the top position of the dialog.- Returns:
- the top position of the dialog
-
setTop
Sets the top position of the dialog. If a unitless number is provided, pixels are assumed.Note that the dialog top edge may not be the same as the viewport top edge (e.g. the "Lumo" theme defines some spacing to prevent the dialog from stretching all the way to the top of the viewport).
- Parameters:
top- the top position of the dialog
-
getLeft
Gets the left position of the dialog.- Returns:
- the left position of the dialog
-
setLeft
Sets the distance of the dialog from the left of its container. If a unitless number is provided, pixels are assumed.Note that the dialog left edge may not be the same as the viewport left edge (e.g. the "Lumo" theme defines some spacing to prevent the dialog from stretching all the way to the left of the viewport).
- Parameters:
left- the left position of the dialog
-
setWidth
- Specified by:
setWidthin interfacecom.vaadin.flow.component.HasSize
-
setMinWidth
- Specified by:
setMinWidthin interfacecom.vaadin.flow.component.HasSize
-
setMaxWidth
- Specified by:
setMaxWidthin interfacecom.vaadin.flow.component.HasSize
-
setHeight
- Specified by:
setHeightin interfacecom.vaadin.flow.component.HasSize
-
setMinHeight
- Specified by:
setMinHeightin interfacecom.vaadin.flow.component.HasSize
-
setMaxHeight
- Specified by:
setMaxHeightin interfacecom.vaadin.flow.component.HasSize
-
getWidth
- Specified by:
getWidthin interfacecom.vaadin.flow.component.HasSize
-
getMinWidth
- Specified by:
getMinWidthin interfacecom.vaadin.flow.component.HasSize
-
getMaxWidth
- Specified by:
getMaxWidthin interfacecom.vaadin.flow.component.HasSize
-
getHeight
- Specified by:
getHeightin interfacecom.vaadin.flow.component.HasSize
-
getMinHeight
- Specified by:
getMinHeightin interfacecom.vaadin.flow.component.HasSize
-
getMaxHeight
- Specified by:
getMaxHeightin interfacecom.vaadin.flow.component.HasSize
-
addDialogCloseActionListener
public com.vaadin.flow.shared.Registration addDialogCloseActionListener(com.vaadin.flow.component.ComponentEventListener<Dialog.DialogCloseActionEvent> listener) Add a listener that controls whether the dialog should be closed or not.The listener is informed when the user wants to close the dialog by clicking outside the dialog, or by pressing escape. Then you can decide whether to close or to keep opened the dialog. It means that dialog won't be closed automatically unless you call
close()method explicitly in the listener implementation.NOTE: adding this listener changes behavior of the dialog. Dialog is closed automatically in case there are no any close listeners. And the
close()method should be called explicitly to close the dialog in case there are close listeners.- Parameters:
listener- the listener to add- Returns:
- registration for removal of listener
- See Also:
-
addResizeListener
public com.vaadin.flow.shared.Registration addResizeListener(com.vaadin.flow.component.ComponentEventListener<Dialog.DialogResizeEvent> listener) Adds a listener that is called after the user finishes resizing the dialog. It is called only if resizing is enabled (seesetResizable(boolean)).Note: By default, the component will sync the width/height and top/left values after every resizing.
- Parameters:
listener- the listener to add- Returns:
- registration for removal of listener
-
addDraggedListener
public com.vaadin.flow.shared.Registration addDraggedListener(com.vaadin.flow.component.ComponentEventListener<Dialog.DialogDraggedEvent> listener) Adds a listener that is called after the user finishes dragging the dialog. It is called only if dragging is enabled (seesetDraggable(boolean)).Note: By default, the component will sync the top/left values after every dragging.
- Parameters:
listener- the listener to add- Returns:
- registration for removal of listener
-
add
Adds the given components into this dialog.- Specified by:
addin interfacecom.vaadin.flow.component.HasComponents- Parameters:
components- the components to add
-
addComponentAtIndex
public void addComponentAtIndex(int index, com.vaadin.flow.component.Component component) Adds the given component into this dialog at the given index.- Specified by:
addComponentAtIndexin interfacecom.vaadin.flow.component.HasComponents- Parameters:
index- the index, where the component will be added.component- the component to add
-
isCloseOnEsc
public boolean isCloseOnEsc()Gets whether this dialog can be closed by hitting the esc-key or not.By default, the dialog is closable with esc.
- Returns:
trueif this dialog can be closed with the esc-key,falseotherwise
-
setCloseOnEsc
public void setCloseOnEsc(boolean closeOnEsc) Sets whether this dialog can be closed by hitting the esc-key or not.By default, the dialog is closable with esc.
- Parameters:
closeOnEsc-trueto enable closing this dialog with the esc-key,falseto disable it
-
isCloseOnOutsideClick
public boolean isCloseOnOutsideClick()Gets whether this dialog can be closed by clicking outside of it or not.By default, the dialog is closable with an outside click.
- Returns:
trueif this dialog can be closed by an outside click,falseotherwise
-
setCloseOnOutsideClick
public void setCloseOnOutsideClick(boolean closeOnOutsideClick) Sets whether this dialog can be closed by clicking outside of it or not.By default, the dialog is closable with an outside click.
- Parameters:
closeOnOutsideClick-trueto enable closing this dialog with an outside click,falseto disable it
-
open
public void open()Opens the dialog.If a dialog was not added manually to a parent component, it will be automatically added to the
UIwhen opened, and automatically removed from the UI when closed. Note that the dialog is then scoped to the UI, and not the current view. As such, when navigating away from a view, the dialog will still be opened or stay open. In order to close the dialog when navigating away from a view, it should either be explicitly added as a child to the view, or it should be explicitly closed when leaving the view. -
close
public void close()Closes the dialog.This automatically removes the dialog from the
UI, unless it was manually added to a parent component. -
setModal
public void setModal(boolean modal) Sets whether component will open modal or modeless dialog.Note: When dialog is set to be modeless, then it's up to you to provide means for it to be closed (eg. a button that calls
close()). The reason being that a modeless dialog allows user to interact with the interface under it and won't be closed by clicking outside or the ESC key.- Parameters:
modal-falseto enable dialog to open as modeless modal,trueotherwise.
-
isModal
public boolean isModal()Gets whether component is set as modal or modeless dialog.- Returns:
trueif modal dialog (default),falseotherwise.
-
setDraggable
public void setDraggable(boolean draggable) Sets whether dialog is enabled to be dragged by the user or not.To allow an element inside the dialog to be dragged by the user (for instance, a header inside the dialog), a class
"draggable"can be added to it (seeHasStyle.addClassName(String)).Note: If draggable is enabled and dialog is opened without first being explicitly attached to a parent, then it won't restore its last position in the case the user closes and opens it again. Reason being that a self attached dialog is removed from the DOM when it's closed and position is not synched.
- Parameters:
draggable-trueto enable dragging of the dialog,falseotherwise
-
isDraggable
public boolean isDraggable()Gets whether dialog is enabled to be dragged or not.- Returns:
trueif dragging is enabled,falseotherwise (default).
-
setResizable
public void setResizable(boolean resizable) Sets whether dialog can be resized by user or not.- Parameters:
resizable-trueto enabled resizing of the dialog,falseotherwise.
-
isResizable
public boolean isResizable()Gets whether dialog is enabled to be resized or not.- Returns:
trueif resizing is enabled,falsoeotherwiser (default).
-
setHeaderTitle
Sets the title to be rendered on the dialog header.- Parameters:
title- title to be rendered
-
getHeaderTitle
Gets the title set for the dialog header.- Returns:
- the title or an empty string, if a header title is not defined.
-
getHeader
Gets the object from which components can be added or removed from the dialog header area. The header is displayed only if there's agetHeaderTitle()or at least one component added withHasComponents.add(Component...).- Returns:
- the header object
-
setVisible
public void setVisible(boolean visible) Set the visibility of the dialog.For a modal dialog the server-side modality will be removed when dialog is not visible so that interactions can be made in the application.
- Overrides:
setVisiblein classcom.vaadin.flow.component.Component- Parameters:
visible- dialog visibility- See Also:
-
Component.setVisible(boolean)
-
setOpened
public void setOpened(boolean opened) Opens or closes the dialog.If a dialog was not added manually to a parent component, it will be automatically added to the
UIwhen opened, and automatically removed from the UI when closed. Note that the dialog is then scoped to the UI, and not the current view. As such, when navigating away from a view, the dialog will still be opened or stay open. In order to close the dialog when navigating away from a view, it should either be explicitly added as a child to the view, or it should be explicitly closed when leaving the view.- Parameters:
opened-trueto open the dialog,falseto close it
-
isOpened
@Synchronize(property="opened", value="opened-changed", allowInert=true) public boolean isOpened()Gets the open state from the dialog.- Returns:
- the
openedproperty from the dialog
-
addOpenedChangeListener
public com.vaadin.flow.shared.Registration addOpenedChangeListener(com.vaadin.flow.component.ComponentEventListener<Dialog.OpenedChangeEvent> listener) Add a listener for when the dialog's opened state changes.Note that this event fires immediately when the opened property changes, which, when closing the dialog, is before the closing animation has finished. To wait for the animation to finish, use
addClosedListener(ComponentEventListener).- Parameters:
listener- the listener to add- Returns:
- a Registration for removing the event listener
-
addClosedListener
public com.vaadin.flow.shared.Registration addClosedListener(com.vaadin.flow.component.ComponentEventListener<Dialog.ClosedEvent> listener) Add a lister for when the dialog's closing animation has finished. Can be used to remove the dialog from the UI afterward.- Parameters:
listener- the listener to add- Returns:
- a Registration for removing the event listener
-
addAttachListener
public com.vaadin.flow.shared.Registration addAttachListener(com.vaadin.flow.component.ComponentEventListener<com.vaadin.flow.component.AttachEvent> listener) Note: To listen for opening the dialog, you should use
addOpenedChangeListener(ComponentEventListener).- Specified by:
addAttachListenerin interfacecom.vaadin.flow.component.AttachNotifier
-
addDetachListener
public com.vaadin.flow.shared.Registration addDetachListener(com.vaadin.flow.component.ComponentEventListener<com.vaadin.flow.component.DetachEvent> listener) Note: To listen for closing the dialog, you should use
addClosedListener(ComponentEventListener), as the component is not necessarily removed from the DOM when closing.- Specified by:
addDetachListenerin interfacecom.vaadin.flow.component.DetachNotifier
-
onAttach
protected void onAttach(com.vaadin.flow.component.AttachEvent attachEvent) - Overrides:
onAttachin classcom.vaadin.flow.component.Component
-
setRole
Sets the ARIA role for the dialog element, used by screen readers.- Parameters:
role- the role to set
-
setOverlayRole
Deprecated, for removal: This API element is subject to removal in a future version.UsesetRole(String)insteadSets the ARIA role for the dialog element, used by screen readers.- Parameters:
role- the role to set
-
getRole
Gets the ARIA role for the dialog element, used by screen readers. Defaults todialog.- Returns:
- the role
-
getOverlayRole
Deprecated, for removal: This API element is subject to removal in a future version.UsegetRole()insteadGets the ARIA role for the dialog element, used by screen readers. Defaults todialog.- Returns:
- the role
-
getAriaLabel
Set thearia-labelattribute for assistive technologies like screen readers. Anundefinedvalue for this property (the default) means that thearia-labelattribute is not present at all.This property is not synchronized automatically from the client side, so the returned value may not be the same as in client side.
- Returns:
- the
ariaLabelproperty from the webcomponent
-
setAriaLabel
Set thearia-labelattribute for assistive technologies like screen readers. Anundefinedvalue for this property (the default) means that thearia-labelattribute is not present at all.- Parameters:
ariaLabel- the String value to set
-
getStyle
public com.vaadin.flow.dom.Style getStyle()- Specified by:
getStylein interfacecom.vaadin.flow.component.HasStyle- Throws:
UnsupportedOperationException- Dialog does not support adding styles
-
getRole()instead