OverlayPanelOverlayPanel is a container component that can overlay other components on page.

OverlayPanel - 图1

Documentation

Import

  1. import {OverlayPanelModule} from 'primeng/overlaypanel';
  2.  

Getting Started

OverlayPanel is defined using p-overlayPanel element and is displayed using the show or toggle method of a local ng-template variable.

  1. <p-overlayPanel #op>
  2. Content
  3. </p-overlayPanel>
  4. <button type="text" pButton label="Basic" (click)="op.toggle($event)"></button>
  5.  

Show and Hide

show method takes two parameters, first one is the event and it is mandatory. By default the target component to align the overlay is the event target, if you'd like to align it to another element, provide it as the second parameter. Similarly calling hide() hides the overlay panel and the toggle method toggles the visibility of the panel. In example below, clicking the button displays the overlayPanel aligned to the actualTarget div, not the button itself.

  1. <p-overlayPanel #op>
  2. Content
  3. </p-overlayPanel>
  4. <button type="text" pButton label="Custom Target" (click)="op.show($event, actualTarget)"></button>
  5. <div #actualTarget></div>
  6.  

Dismissable and CloseIcon

Clicking outside the overlay hides the panel, setting dismissable to false disables this behavior. Additionally enablign showCloseIcon property displays a close icon at the top right corner to close the panel

  1. <p-overlayPanel #op [dismissable]="true" [showCloseIcon]="true">
  2. Content
  3. </p-overlayPanel>
  4.  

Animation Configuration

Transition of the open and hide animations can be customized using the showTransitionOptions and hideTransitionOptions properties, example below disables the animations altogether.

  1. <p-overlayPanel [showTransitionOptions]="'0ms'" [hideTransitionOptions]="'0ms'" #op [dismissable]="true" [showCloseIcon]="true">
  2. Content
  3. </p-overlayPanel>
  4.  

Properties

NameTypeDefaultDescription
dismissablebooleantrueEnables to hide the overlay when outside is clicked.
showCloseIconbooleanfalseWhen enabled, displays a close icon at top right corner.
stylestringnullInline style of the component.
styleClassstringnullStyle class of the component.
appendToanynullTarget element to attach the panel, valid values are "body" or a local ng-template variable of another element.
baseZIndexnumber0Base zIndex value to use in layering.
autoZIndexbooleantrueWhether to automatically manage layering.
showTransitionOptionsstring225ms ease-outTransition options of the show animation.
hideTransitionOptionsstring195ms ease-inTransition options of the hide animation.

Events

NameParametersDescription
onShow-Callback to invoke when an overlay becomes visible.
onHide-Callback to invoke after overlay gets hidden.

Methods

NameParametersDescription
toggleevent: browser event target?: target element to align the panel, defaults to event.target Toggles the visibility of the panel.
showevent: browser event target?: target element to align the panel to Displays the panel.
hide-Hides the panel.

Styling

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

NameElement
ui-overlaypanelContainer element.
ui-overlaypanel-contentContent of the panel.
ui-overlaypanel-closeClose icon.

Dependencies

None.

Source

View on GitHub

  1. <h3 class="first">Basic</h3>
  2. <p>Click the button to show the panel.</p>
  3. <button type="text" pButton label="Basic" (click)="op1.toggle($event)"></button>
  4. <p-overlayPanel #op1>
  5. <img src="assets/showcase/images/demo/galleria/galleria1.jpg" alt="Galleria 1" />
  6. </p-overlayPanel>
  7. <h3>Customized</h3>
  8. <p>This OverlayPanel gets displayed on hover of the icon, is not dismissable and displays a close icon.</p>
  9. <i class="pi pi-search" (mouseenter)="op2.show($event)" style="font-size:24px"></i>
  10. <p-overlayPanel #op2 [showCloseIcon]="true" [dismissable]="false">
  11. <p-table [value]="cars1" [style]="{width: '500px'} [paginator]="true" [rows]="5"">
  12. <ng-template pTemplate="header">
  13. <tr>
  14. <th pSortableColumn="vin">Vin <p-sortIcon field="vin"></p-sortIcon></th>
  15. <th pSortableColumn="year">Year <p-sortIcon field="year"></p-sortIcon></th>
  16. <th pSortableColumn="brand">Brand <p-sortIcon field="brand"></p-sortIcon></th>
  17. <th pSortableColumn="color">Color <p-sortIcon field="color"></p-sortIcon></th>
  18. </tr>
  19. </ng-template>
  20. <ng-template pTemplate="body" let-car>
  21. <tr>
  22. <td>{{car.vin}}</td>
  23. <td>{{car.year}}</td>
  24. <td>{{car.brand}}</td>
  25. <td>{{car.color}}</td>
  26. </tr>
  27. </ng-template>
  28. </p-table>
  29. </p-overlayPanel>
  30. <h3>Table Integration</h3>
  31. <p-table [value]="cars2">
  32. <ng-template pTemplate="header">
  33. <tr>
  34. <th style="width: 4em"></th>
  35. <th>Vin</th>
  36. <th>Year</th>
  37. <th>Brand</th>
  38. <th>Color</th>
  39. </tr>
  40. </ng-template>
  41. <ng-template pTemplate="body" let-car>
  42. <tr>
  43. <td>
  44. <button type="button" pButton (click)="selectCar($event,car,op3)" icon="pi pi-search"></button>
  45. </td>
  46. <td>{{car.vin}}</td>
  47. <td>{{car.year}}</td>
  48. <td>{{car.brand}}</td>
  49. <td>{{car.color}}</td>
  50. </tr>
  51. </ng-template>
  52. </p-table>
  53. <p-overlayPanel #op3>
  54. <img src="assets/showcase/images/demo/car/{{selectedCar.brand}}.png" *ngIf="selectedCar"/>
  55. </p-overlayPanel>
  56.  
  1. export class OverlayPanelDemo {
  2. cars1: Car[];
  3. cars2: Car[];
  4. selectedCar: Car;
  5. constructor(private carService: CarService) { }
  6. ngOnInit() {
  7. this.carService.getCarsSmall().then(cars => this.cars1 = cars);
  8. this.carService.getCarsSmall().then(cars => this.cars2 = cars);
  9. }
  10. selectCar(event,car: Car, overlaypanel: OverlayPanel) {
  11. this.selectedCar = car;
  12. overlaypanel.toggle(event);
  13. }
  14. }
  15.