Dialog is a container to display content in an overlay window.
import { DialogModule } from 'primeng/dialog';
Dialog is used as a container and visibility is controlled with visible property. Dialogs are draggable and resizable by default.
<p-button (click)="showDialog()" icon="pi pi-external-link" label="Show"></p-button>
<p-dialog header="Header" [(visible)]="visible" [style]="{width: '50vw'}">
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</p>
</p-dialog>
Dialog automatically displays a scroller when content exceeeds viewport.
<p-button (click)="showDialog()" icon="pi pi-external-link" label="Show"></p-button>
<p-dialog header="Long Content" [(visible)]="visible" [style]="{ width: '50vw', height: '300px' }">
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</p>
<p>
"Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim
ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur,
adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea
commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?
</p>
<p>
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui
officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime
placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Temporibus autem quibusdam et aut officiis debitis aut rerum necessitatibus saepe eveniet ut et voluptates repudiandae sint et molestiae non
recusandae. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.
</p>
</p-dialog>
Mask layer behind the Dialog can be turned on by setting the modal property to true.
<p-button (click)="showDialog()" icon="pi pi-external-link" label="Show"></p-button>
<p-dialog header="Header" [(visible)]="visible" [modal]="true" [style]="{ width: '50vw' }" [draggable]="false" [resizable]="false">
<p class="m-0">
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</p>
</p-dialog>
Dialog width can be adjusted per screen size with the breakpoints option where a key defines the max-width for the breakpoint and value for the corresponding width. When no breakpoint matches width defined in style property is used.
<p-button (click)="showDialog()" icon="pi pi-external-link" label="Show"></p-button>
<p-dialog header="Header" [(visible)]="visible" [breakpoints]="{ '960px': '75vw' }" [style]="{ width: '50vw' }" [draggable]="false" [resizable]="false">
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</p>
</p-dialog>
The position property is used to display a Dialog at all edges and corners of the screen.
<div class="flex flex-wrap gap-2">
<p-button (click)="showDialog('left')" icon="pi pi-arrow-right" label="Left" styleClass="p-button-help"></p-button>
<p-button (click)="showDialog('right')" icon="pi pi-arrow-left" label="Right" styleClass="p-button-help"></p-button>
</div>
<div class="flex flex-wrap gap-2">
<p-button (click)="showDialog('top-left')" icon="pi pi-arrow-down-right" label="TopLeft" styleClass="p-button-warning"></p-button>
<p-button (click)="showDialog('top')" icon="pi pi-arrow-down" label="Top" styleClass="p-button-warning"></p-button>
<p-button (click)="showDialog('top-right')" icon="pi pi-arrow-down-left" label="TopRight" styleClass="p-button-warning"></p-button>
</div>
<div class="flex flex-wrap gap-2">
<p-button (click)="showDialog('bottom-left')" icon="pi pi-arrow-up-right" label="BottomLeft" styleClass="p-button-success"></p-button>
<p-button (click)="showDialog('bottom')" icon="pi pi-arrow-up" label="Bottom" styleClass="p-button-success"></p-button>
<p-button (click)="showDialog('bottom-right')" icon="pi pi-arrow-up-left" label="BottomRight" styleClass="p-button-success"></p-button>
</div>
<p-dialog header="Header" [(visible)]="visible" [position]="position" [style]="{ width: '50vw' }">
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</p>
</p-dialog>
Setting maximizable property to true enables the full screen mode.
<p-button (click)="showDialog()" icon="pi pi-external-link" label="Show"></p-button>
<p-dialog header="Header" [(visible)]="visible" [style]="{width: '50vw'}" [maximizable]="true">
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</p>
</p-dialog>
Dialog can be customized using header and footer templates.
<p-button (click)="showDialog()" icon="pi pi-external-link" label="Show"></p-button>
<p-dialog header="Header" [(visible)]="visible" [style]="{ width: '50vw' }">
<ng-template pTemplate="header">
<span class="text-xl font-bold">Header Content</span>
</ng-template>
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</p>
<ng-template pTemplate="footer">
<p-button icon="pi pi-check" (click)="visible = false" label="Ok" styleClass="p-button-text"></p-button>
</ng-template>
</p-dialog>
When dialog includes other components with overlays such as dropdown, the overlay part cannot exceed dialog boundaries due to overflow. In order to solve this, you can either append the overlay to the body by using appendTo property or allow overflow in dialog.
<p-button (click)="showDialog()" icon="pi pi-external-link" label="Show"></p-button>
<p-dialog header="Header" [(visible)]="visible" [style]="{ width: '50vw' }">
<div class="flex py-2 justify-content-center">
<p-dropdown appendTo="body" [options]="cities" [(ngModel)]="selectedCity" placeholder="Select a City" optionLabel="name"></p-dropdown>
</div>
</p-dialog>
Following is the list of structural style classes, for theming classes visit theming page.
Name | Element |
---|---|
p-dialog | Container element |
p-dialog-titlebar | Container of header. |
p-dialog-title | Header element. |
p-dialog-titlebar-icon | Icon container inside header. |
p-dialog-titlebar-close | Close icon element. |
p-dialog-content | Content element. |
p-dialog-footer | Footer element. |
Dialog component uses dialog role along with aria-labelledby referring to the header element however any attribute is passed to the root element so you may use aria-labelledby to override this default behavior. In addition aria-modal is added since focus is kept within the popup.
It is recommended to use a trigger component that can be accessed with keyboard such as a button, if not adding tabIndex would be necessary.
Trigger element also requires aria-expanded and aria-controls to be handled explicitly.
Close element is a button with an aria-label that refers to the aria.close property of the locale API by default, you may usecloseButtonProps to customize the element and override the default aria-label.
<p-button
icon="pi pi-external-link"
(click)="visible = true"
aria-controls="{{visible ? 'dialog' : null}}"
aria-expanded="{{visible ? true : false}}"
></p-button>
<p-dialog id="dialog" header="Header" [(visible)]="visible" [style]="{ width: '50vw' }" (onHide)="visible = false">
<p>Content</p>
</p-dialog>
Key | Function |
---|---|
tab | Moves focus to the next the focusable element within the dialog. |
shift + tab | Moves focus to the previous the focusable element within the dialog. |
escape | Closes the dialog if closeOnEscape is true. |
Key | Function |
---|---|
enter | Closes the dialog. |
space | Closes the dialog. |
API defines helper props, events and others for the PrimeNG Dialog module.
Name | Type | Default | Description |
---|---|---|---|
header | string | null | Title text of the dialog. |
draggable | boolean | true | Enables dragging to change the position using header. |
keepInViewport | boolean | true | Keeps dialog in the viewport. |
resizable | boolean | true | Enables resizing of the content. |
contentStyle | object | null | Style of the content section. |
visible | boolean | false | Specifies the visibility of the dialog. |
modal | boolean | false | Defines if background should be blocked when dialog is displayed. |
position | string | center | Position of the dialog, options are "center", "top", "bottom", "left", "right", "top-left", "top-right", "bottom-left" or "bottom-right". |
blockScroll | boolean | false | Whether background scroll should be blocked when dialog is visible. |
closeOnEscape | boolean | true | Specifies if pressing escape key should hide the dialog. |
dismissableMask | boolean | false | Specifies if clicking the modal background should hide the dialog. |
rtl | boolean | false | When enabled dialog is displayed in RTL direction. |
closable | boolean | true | Adds a close icon to the header to hide the dialog. |
appendTo | any | null | Target element to attach the dialog, valid values are "body" or a local ng-template variable of another element (note: use binding with brackets for template variables, e.g. [appendTo]="mydiv" for a div element having #mydiv as variable name). |
style | object | null | Inline style of the component. |
styleClass | string | null | Style class of the component. |
maskStyleClass | string | null | Style class of the mask. |
contentStyle | object | null | Inline style of the content. |
contentStyleClass | string | null | Style class of the content. |
showHeader | boolean | true | Whether to show the header or not. |
baseZIndex | number | 0 | Base zIndex value to use in layering. |
autoZIndex | boolean | true | Whether to automatically manage layering. |
minX | number | 0 | Minimum value for the left coordinate of dialog in dragging. |
minY | number | 0 | Minimum value for the top coordinate of dialog in dragging. |
focusOnShow | boolean | true | When enabled, first button receives focus on show. |
focusTrap | boolean | true | When enabled, can only focus on elements inside the dialog. |
maximizable | boolean | false | Whether the dialog can be displayed full screen. |
breakpoints | object | null | Object literal to define widths per screen size. |
transitionOptions | string | 150ms cubic-bezier(0, 0, 0.2, 1) | Transition options of the animation. |
closeIcon | string | null | Name of the close icon. |
closeAriaLabel | string | null | Defines a string that labels the close button for accessibility. |
closeTabindex | string | null | Index of the close button in tabbing order. |
minimizeIcon | string | null | Name of the minimize icon. |
maximizeIcon | string | null | Name of the maximize icon. |
Name | Parameters | Description |
---|---|---|
onShow | event: Event object | Callback to invoke when dialog is shown. |
onHide | event: Event object | Callback to invoke when dialog is hidden. |
onResizeInit | event: Event object | Callback to invoke when dialog resizing is initiated. |
onResizeEnd | event: Event object | Callback to invoke when dialog resizing is completed. |
onDragEnd | event: Event object | Callback to invoke when dialog dragging is completed. |
onMaximize | event: Event object | Callback to invoke when dialog maximized or unmaximized. |