OverlayPanel, also known as Popover, is a container component that can overlay other components on page.
import { OverlayPanelModule } from 'primeng/overlaypanel';
OverlayPanel is accessed via its reference and visibility is controlled using toggle, show and hide methods with an event of the target.
<p-overlayPanel #op>
<img src="https://primefaces.org/cdn/primeng/images/demo/product/bamboo-watch.jpg" alt="product" />
</p-overlayPanel>
<p-button (click)="op.toggle($event)" icon="pi pi-image" label="Show"></p-button>
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 target.
<p-button (click)="op.show($event, targetEl)" icon="pi pi-image" label="Show"></p-button>
<div #targetEl class="mt-5 w-10rem h-5rem border-1 surface-border border-round flex align-items-center justify-content-center">
<span>Target Element</span>
</div>
<p-overlayPanel #op>
<img src="https://primefaces.org/cdn/primeng/images/demo/product/bamboo-watch.jpg" alt="product" />
</p-overlayPanel>
Content of the OverlayPanel is defined by content template.
<p-overlayPanel #op>
<ng-template pTemplate="content">
<h4>Custom Content</h4>
</ng-template>
</p-overlayPanel>
<p-button (click)="op.toggle($event)" icon="pi pi-image" label="Show"></p-button>
An example that displays a DataTable inside a popup to select an item.
<p-toast></p-toast>
<p-button (click)="op.toggle($event)" icon="pi pi-search" label="Search"></p-button>
<div *ngIf="selectedProduct" class="p-5 surface-card shadow-2 border-round">
<div class="relative">
<img src="https://primefaces.org/cdn/primeng/images/demo/product/{{ selectedProduct.image }}" [alt]="selectedProduct.name" />
</div>
<div class="flex align-items-center justify-content-between mt-3 mb-2">
<span class="text-900 font-medium text-xl">{{selectedProduct.name}}</span>
<span class="text-900 text-xl ml-3">{{'$' + selectedProduct.price}}</span>
</div>
<span class="text-600">{{selectedProduct.category}}</span>
</div>
<p-overlayPanel #op [style]="{'width': '450px'}" [showCloseIcon]="true">
<ng-template pTemplate="content">
<p-table [value]="products" selectionMode="single" [(selection)]="selectedProduct" (onRowSelect)="onRowSelect($event, op)" [paginator]="true" [rows]="5" responsiveLayout="scroll">
<ng-template pTemplate="header">
<tr>
<th pSortableColumn="name">Name<p-sortIcon field="name"></p-sortIcon></th>
<th>Image</th>
<th pSortableColumn="price">Price <p-sortIcon field="price"></p-sortIcon></th>
</tr>
</ng-template>
<ng-template pTemplate="body" let-rowData let-product>
<tr [pSelectableRow]="rowData">
<td>{{product.name}}</td>
<td><img src="https://primefaces.org/cdn/primeng/images/demo/product/{{ product.image }}"[alt]="product.image" class="w-5rem shadow-2"/></td>
<td>{{product.price}}</td>
</tr>
</ng-template>
</p-table>
</ng-template>
</p-overlayPanel>
Following is the list of structural style classes, for theming classes visit theming page.
Name | Element |
---|---|
p-overlaypanel | Container element. |
p-overlaypanel-content | Content of the panel. |
p-overlaypanel-close | Close icon. |
OverlayPanel component uses dialog role and since any attribute is passed to the root element you may define attributes like aria-label or aria-labelledby to describe the popup contents. 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. OverlayPanel adds aria-expanded state attribute and aria-controls to the trigger so that the relation between the trigger and the popup is defined.
When the popup gets opened, the first focusable element receives the focus and this can be customized by adding autofocus to an element within the popup.
Key | Function |
---|---|
tab | Moves focus to the next the focusable element within the popup. |
shift + tab | Moves focus to the previous the focusable element within the popup. |
escape | Closes the popup and moves focus to the trigger. |
Key | Function |
---|---|
enter | Closes the popup and moves focus to the trigger. |
space | Closes the popup and moves focus to the trigger. |
API defines helper props, events and others for the PrimeNG OverlayPanel module.
Name | Type | Default | Description |
---|---|---|---|
dismissable | boolean | true | Enables to hide the overlay when outside is clicked. |
showCloseIcon | boolean | false | When enabled, displays a close icon at top right corner. |
ariaCloseLabel | string | close | Aria label of the close icon. |
style | string | null | Inline style of the component. |
styleClass | string | null | Style class of the component. |
appendTo | any | null | Target element to attach the panel, 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). |
baseZIndex | number | 0 | Base zIndex value to use in layering. |
autoZIndex | boolean | true | Whether to automatically manage layering. |
showTransitionOptions | string | .12s cubic-bezier(0, 0, 0.2, 1) | Transition options of the show animation. |
hideTransitionOptions | string | .1s linear | Transition options of the hide animation. |
focusOnShow | boolean | true | When enabled, first button receives focus on show. |
Name | Parameters | Description |
---|---|---|
onShow | - | Callback to invoke when an overlay becomes visible. |
onHide | - | Callback to invoke after overlay gets hidden. |
Name | Parameters | Description |
---|---|---|
toggle | event: browser event target?: target element to align the panel, defaults to event.target | Toggles the visibility of the panel. |
show | event: browser event target?: target element to align the panel to | Displays the panel. |
hide | - | Hides the panel. |
Name | Parameters |
---|---|
content | - |
closeicon | - |