OverlayPanel, also known as Popover, is a container component that can overlay other components on page.
import { OverlayPanel } from 'primereact/overlaypanel';
OverlayPanel is accessed via its reference and visibility is controlled using toggle, show and hide methods with an event of the target.
<Button type="button" icon="pi pi-image" label="Image" onClick={(e) => op.current.toggle(e)} />
<OverlayPanel ref={op}>
<img src="/images/product/bamboo-watch.jpg" alt="Bamboo Watch"></img>
</OverlayPanel>
An example that displays a DataTable inside a popup to select an item.
<Toast ref={toast} />
<Button type="button" icon="pi pi-search" label="Search" onClick={(e) => op.current.toggle(e)} />
{selectedProductContent}
<OverlayPanel ref={op} showCloseIcon>
<DataTable value={products} selectionMode="single" paginator rows={5} selection={selectedProduct} onSelectionChange={(e) => setSelectedProduct(e.value)}>
<Column field="name" header="Name" sortable style={{minWidth: '12rem'}} />
<Column header="Image" body={imageBody} />
<Column field="price" header="Price" sortable body={priceBody} style={{minWidth: '8rem'}} />
</DataTable>
</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. |