Class Dialog
- All Implemented Interfaces:
AttachNotifier,DetachNotifier,HasComponents,HasElement,HasEnabled,HasSize,HasStyle,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 class`vaadin-dialog-close-action` is sent when the user clicks outside the overlay or presses the escape key.static class`dragged` event is sent when the user finishes dragging the overlay.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 overlay.static classopened-changedevent is sent when the overlay opened state changes. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidadd(Collection<Component> components) Adds the given components into this dialog.addAttachListener(ComponentEventListener<AttachEvent> listener) Adds a attach listener to this component.voidaddComponentAtIndex(int index, Component component) Adds the given component into this dialog at the given index.addDetachListener(ComponentEventListener<DetachEvent> listener) Adds a detach listener to this component.Add a listener that controls whether the dialog should be closed or not.Adds a listener that is called after user finishes dragging the overlay.Add a lister for event fired by theopened-changedevents.Adds a listener that is called after user finishes resizing the overlay.voidclose()Closes the dialog.protected StringSet thearia-labelattribute for assistive technologies like screen readers.Gets the set of CSS class names used for this element.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.Gets the height defined for the component.getLeft()Gets the left position of the overlay.Gets the max-height defined for the component.Gets the max-width defined for the component.Gets the min-height defined for the component.Gets the min-width defined for the component.Gets the ARIA role for the overlay element, used by screen readers.getStyle()Gets the style instance for managing inline styles for the element of this component.getTop()Gets the top position of the overlay.getWidth()Gets the width defined for the component.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(AttachEvent attachEvent) Called when the component is attached to a UI.voidopen()Opens the dialog.protected voidsetAriaLabel(String ariaLabel) Set thearia-labelattribute for assistive technologies like screen readers.voidsetClassName(String className) Sets the CSS class names of the dialog overlay element.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.voidSets the height of the component.voidSets the distance of the overlay from the left of its container.voidsetMaxHeight(String value) Sets the max-height of the component.voidsetMaxWidth(String value) Sets the max-width of the component.voidsetMinHeight(String value) Sets the min-height of the component.voidsetMinWidth(String value) Sets the min-width of the component.voidsetModal(boolean modal) Sets whether component will open modal or modeless dialog.voidsetOpened(boolean opened) Opens or closes the dialog.voidsetOverlayRole(String role) Sets the ARIA role for the overlay element, used by screen readers.voidsetResizable(boolean resizable) Sets whether dialog can be resized by user or not.voidSets the top position of the overlay.voidsetVisible(boolean visible) Set the visibility of the dialog.voidSets the width of the component.Methods 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, hasClassName, removeClassName, removeClassNames, 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
Creates a dialog with given components inside.- Parameters:
components- the components inside the dialog- See Also:
-
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 overlay.- Returns:
- the top position of the overlay
-
setTop
Sets the top position of the overlay. If a unitless number is provided, pixels are assumed.Note that the overlay top edge may not be the same as the viewport top edge (e.g. the "Lumo" theme defines some spacing to prevent the overlay from stretching all the way to the top of the viewport).
- Parameters:
top- the top position of the overlay
-
getLeft
Gets the left position of the overlay.- Returns:
- the left position of the overlay
-
setLeft
Sets the distance of the overlay from the left of its container. If a unitless number is provided, pixels are assumed.Note that the overlay left edge may not be the same as the viewport left edge (e.g. the "Lumo" theme defines some spacing to prevent the overlay from stretching all the way to the left of the viewport).
- Parameters:
left- the left position of the overlay
-
setWidth
Description copied from interface:HasSizeSets the width of the component.The width should be in a format understood by the browser, e.g. "100px" or "2.5em".
If the provided
widthvalue is null then width is removed. -
setMinWidth
Description copied from interface:HasSizeSets the min-width of the component.The width should be in a format understood by the browser, e.g. "100px" or "2.5em".
If the provided
minWidthvalue is null then min-width is removed.- Specified by:
setMinWidthin interfaceHasSize- Parameters:
value- the min-width value (ifnull, the property will be removed)
-
setMaxWidth
Description copied from interface:HasSizeSets the max-width of the component.The width should be in a format understood by the browser, e.g. "100px" or "2.5em".
If the provided
maxWidthvalue is null then max-width is removed.- Specified by:
setMaxWidthin interfaceHasSize- Parameters:
value- the max-width value (ifnull, the property will be removed)
-
setHeight
Description copied from interface:HasSizeSets the height of the component.The height should be in a format understood by the browser, e.g. "100px" or "2.5em".
If the provided
heightvalue is null then height is removed. -
setMinHeight
Description copied from interface:HasSizeSets the min-height of the component.The height should be in a format understood by the browser, e.g. "100px" or "2.5em".
If the provided
minHeightvalue is null then min-height is removed.- Specified by:
setMinHeightin interfaceHasSize- Parameters:
value- the min-height value (ifnull, the property will be removed)
-
setMaxHeight
Description copied from interface:HasSizeSets the max-height of the component.The height should be in a format understood by the browser, e.g. "100px" or "2.5em".
If the provided
maxHeightvalue is null then max-height is removed.- Specified by:
setMaxHeightin interfaceHasSize- Parameters:
value- the max-height value (ifnull, the property will be removed)
-
getWidth
Description copied from interface:HasSizeGets the width defined for the component.Note that this does not return the actual size of the component but the width which has been set using
HasSize.setWidth(String). -
getMinWidth
Description copied from interface:HasSizeGets the min-width defined for the component.Note that this does not return the actual size of the component but the min-width which has been set using
HasSize.setMinWidth(String).- Specified by:
getMinWidthin interfaceHasSize- Returns:
- the min-width which has been set for the component
-
getMaxWidth
Description copied from interface:HasSizeGets the max-width defined for the component.Note that this does not return the actual size of the component but the max-width which has been set using
HasSize.setMaxWidth(String).- Specified by:
getMaxWidthin interfaceHasSize- Returns:
- the max-width which has been set for the component
-
getHeight
Description copied from interface:HasSizeGets the height defined for the component.Note that this does not return the actual size of the component but the height which has been set using
HasSize.setHeight(String). -
getMinHeight
Description copied from interface:HasSizeGets the min-height defined for the component.Note that this does not return the actual size of the component but the min-height which has been set using
HasSize.setMinHeight(String).- Specified by:
getMinHeightin interfaceHasSize- Returns:
- the min-height which has been set for the component
-
getMaxHeight
Description copied from interface:HasSizeGets the max-height defined for the component.Note that this does not return the actual size of the component but the max-height which has been set using
HasSize.setMaxHeight(String).- Specified by:
getMaxHeightin interfaceHasSize- Returns:
- the max-height which has been set for the component
-
addDialogCloseActionListener
public Registration addDialogCloseActionListener(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
Adds a listener that is called after user finishes resizing the overlay. 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
Adds a listener that is called after user finishes dragging the overlay. 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.The elements in the DOM will not be children of the
<vaadin-dialog>element, but will be inserted into an overlay that is attached into the<body>.- Specified by:
addin interfaceHasComponents- Parameters:
components- the components to add
-
addComponentAtIndex
Adds the given component into this dialog at the given index.The element in the DOM will not be child of the
<vaadin-dialog>element, but will be inserted into an overlay that is attached into the<body>.- Specified by:
addComponentAtIndexin interfaceHasComponents- 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 classComponent- Parameters:
visible- dialog visibility- See Also:
-
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
Gets the open state from the dialog.- Returns:
- the
openedproperty from the dialog
-
addOpenedChangeListener
public Registration addOpenedChangeListener(ComponentEventListener<Dialog.OpenedChangeEvent> listener) Add a lister for event fired by theopened-changedevents.- Parameters:
listener- the listener to add- Returns:
- a Registration for removing the event listener
-
addAttachListener
Adds a attach listener to this component.Note: To listen for opening the dialog, you should use
addOpenedChangeListener(ComponentEventListener).- Specified by:
addAttachListenerin interfaceAttachNotifier- Parameters:
listener- the listener to add, notnull- Returns:
- a handle that can be used for removing the listener
-
addDetachListener
Adds a detach listener to this component.Note: To listen for closing the dialog, you should use
addOpenedChangeListener(ComponentEventListener), as the component is not necessarily removed from the DOM when closing.- Specified by:
addDetachListenerin interfaceDetachNotifier- Parameters:
listener- the listener to add, notnull- Returns:
- a handle that can be used for removing the listener
-
onAttach
Description copied from class:ComponentCalled when the component is attached to a UI.This method is invoked before the
Make sure to callAttachEventis fired for the component.super.onAttachwhen overriding this method. -
setOverlayRole
Sets the ARIA role for the overlay element, used by screen readers.- Parameters:
role- the role to set
-
getOverlayRole
Gets the ARIA role for the overlay 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
-
setClassName
Sets the CSS class names of the dialog overlay element. This method overwrites any previous set class names.- Specified by:
setClassNamein interfaceHasStyle- Parameters:
className- a space-separated string of class names to set, ornullto remove all class names
-
getClassNames
Description copied from interface:HasStyleGets the set of CSS class names used for this element. The returned set can be modified to add or remove class names. The contents of the set is also reflected in the value of theclassattribute.Despite the name implying a list being returned, the return type is actually a
Setsince the in-browser return value behaves like aSetin Java.- Specified by:
getClassNamesin interfaceHasStyle- Returns:
- a list of class names, never
null - See Also:
-
getStyle
Description copied from interface:HasStyleGets the style instance for managing inline styles for the element of this component.- Specified by:
getStylein interfaceHasStyle- Returns:
- the style object for the element, not
null - Throws:
UnsupportedOperationException- Dialog does not support adding styles to overlay
-