DialogDialog is a container to display content in an overlay window.

Dialog - 图1

Documentation

Import

  1. import {DialogModule} from 'primeng/dialog';
  2.  

Getting Started

Dialog is used as a container and visibility is controlled with visible property.

  1. <p-dialog header="Title" [(visible)]="display">
  2. Content
  3. </p-dialog>
  4. <button type="button" (click)="showDialog()" pButton icon="pi pi-info-circle" label="Show"></button>
  5.  
  1. export class ModelComponent {
  2. display: boolean = false;
  3. showDialog() {
  4. this.display = true;
  5. }
  6. }
  7.  

By default dialog is hidden and enabling the visible property displays the dialog. As visible supports two-way binding, hiding the dialog with close button updates the visible state as false.

Dimensions

Use style property to define the dimensions of the Dialog.

  1. <p-dialog header="Title" [(visible)]="display" [style]="{width: '300px', height: '200px'}">
  2. Content
  3. </p-dialog>
  4.  

Header and Footer sections are useful to include custom content. Note that Header and Footer components should be imported and defined at directives section of your component for this to work.

  1. <p-dialog [(visible)]="display">
  2. <p-header>
  3. Header content here
  4. </p-header>
  5. Content
  6. <p-footer>
  7. //buttons
  8. </p-footer>
  9. </p-dialog>
  10.  

Overlays Inside

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 or allow overflow in dialog.

  1. <p-dialog>
  2. <p-dropdown appendTo="body"></p-dropdown>
  3. </p-dialog>
  4.  
  1. <p-dialog [contentStyle]="{'overflow':'visible'}">
  2. <p-dropdown></p-dropdown>
  3. </p-dialog>
  4.  

Dynamic Content

Dynamic content may move the dialog boundaries outside of the viewport. Common solution is defining max-height at content section to let the content overflow. You may also call the center() method of Dialog to re-center it after the content changes.

  1. <p-dialog [contentStyle]="{'max-height':'200px'}">
  2. <ul>
  3. <li *ngFor="let item of items">{{item}}<li>
  4. </ul>
  5. </p-dialog>
  6.  

RTL Support

RTL orientation is enabled by adding "ui-rtl" to an ancestor element of dialog in combination with standard dir="rtl" option.

  1. <div class="ui-rtl" dir="rtl">
  2. <p-dialog [(visible)]="display">
  3. <p-header>
  4. Header content here
  5. </p-header>
  6. Content
  7. <p-footer>
  8. Footer content here
  9. </p-footer>
  10. </p-dialog>
  11. </div>
  12.  

Animation Configuration

Transition of the Dialog open and hide animations can be customized using the transitionOptions property with a default value as 150ms cubic-bezier(0, 0, 0.2, 1), example below disables the animation altogether.

  1. <p-dialog [(visible)]="display" [transitionOptions]="'0ms'"></p-dialog>
  2.  

Properties

NameTypeDefaultDescription
headerstringnullTitle text of the dialog.
draggablebooleantrueEnables dragging to change the position using header.
resizablebooleantrueEnables resizing of the content.
minWidth (deprecated, use style property)numberautoMinimum width of a resizable dialog.
minHeight (deprecated, use style property)numberautoMinimum height of a resizable dialog.
width (deprecated, use style property)intautoWidth of the dialog.
height (deprecated, use style property)intautoHeight of the dialog.
contentStyleobjectnullStyle of the content section.
visiblebooleanfalseSpecifies the visibility of the dialog.
modalbooleanfalseDefines if background should be blocked when dialog is displayed.
blockScrollbooleanfalseWhether background scroll should be blocked when dialog is visible.
closeOnEscapebooleantrueSpecifices if pressing escape key should hide the dialog.
dismissableMaskbooleanfalseSpecifices if clicking the modal background should hide the dialog.
rtlbooleanfalseWhen enabled dialog is displayed in RTL direction.
closablebooleantrueAdds a close icon to the header to hide the dialog.
responsivebooleantrueIn responsive mode, dialog adjusts itself to screen width.
breakpointnumber640Maximum screen width to display the dialog with 100% width in responsive mode.
appendToanynullTarget element to attach the dialog, valid values are "body" or a local ng-template variable of another element.
stylestringnullInline style of the component.
styleClassstringnullStyle class of the component.
showHeaderbooleantrueWhether to show the header or not.
positionLeftnumbernullLeft coordinate value of the dialog.
positionTopnumbernullTop coordinate value of the dialog.
baseZIndexnumber0Base zIndex value to use in layering.
autoZIndexbooleantrueWhether to automatically manage layering.
minXnumber0Minimum value for the left coordinate of dialog in dragging.
minYnumber0Minimum value for the top coordinate of dialog in dragging.
focusOnShowbooleantrueWhen enabled, first button receives focus on show.
focusTrapbooleantrueWhen enabled, can only focus on elements inside the dialog.
maximizablebooleanfalseWhether the dialog can be displayed full screen.
transitionOptionsstring150ms cubic-bezier(0, 0, 0.2, 1)Transition options of the animation.
closeIconstringpi pi-timesName of the close icon.
minimizeIconstringpi pi-window-minimizeName of the minimize icon.
maximizeIconstringpi pi-window-maximizeName of the maximize icon.

Events

NameParametersDescription
onShowevent: Event objectCallback to invoke when dialog is shown.
onHideevent: Event objectCallback to invoke when dialog is hidden.

Styling

Following is the list of structural style classes, for theming classes visit theming page.

NameElement
ui-dialogContainer element
ui-dialog-titlebarContainer of header.
ui-dialog-titleHeader element.
ui-dialog-titlebar-iconIcon container inside header.
ui-dialog-titlebar-closeClose icon element.
ui-dialog-contentContent element.
ui-dialog-footerFooter element.

Dependencies

None.

Source

View on GitHub

  1. <p-dialog header="Godfather I" [(visible)]="display" [modal]="true" [responsive]="true" [style]="{width: '350px', minWidth: '200px'}" [minY]="70"
  2. [maximizable]="true" [baseZIndex]="10000">
  3. <p>The story begins as Don Vito Corleone, the head of a New York Mafia family, oversees his daughter's wedding.
  4. His beloved son Michael has just come home from the war, but does not intend to become part of his father's business.
  5. Through Michael's life the nature of the family business becomes clear. The business of the family is just like the head of the family,
  6. kind and benevolent to those who give respect,
  7. but given to ruthless violence whenever anything stands against the good of the family.</p>
  8. <p-footer>
  9. <button type="button" pButton icon="pi pi-check" (click)="display=false" label="Yes"></button>
  10. <button type="button" pButton icon="pi pi-close" (click)="display=false" label="No" class="ui-button-secondary"></button>
  11. </p-footer>
  12. </p-dialog>
  13. <button type="button" (click)="showDialog()" pButton icon="pi pi-info-circle" label="Show"></button>
  14.  
  1. export class DialogDemo {
  2. display: boolean = false;
  3. showDialog() {
  4. this.display = true;
  5. }
  6. }
  7.