Primitives
Display arbitrary content inside floating panels.
Import the Popover primitives from ng-primitives/popover
.
import { NgpPopover, NgpPopoverTrigger, NgpPopoverArrow } from 'ng-primitives/popover';
Assemble the popover directives in your template.
<button [ngpPopoverTrigger]="popover" (ngpPopoverTriggerOpenChange)="onPopoverStateChange($event)">
Click me
</button>
<ng-template #popover>
<div ngpPopover>Popover content</div>
</ng-template>
You can listen to the ngpPopoverTriggerOpenChange
event to perform actions when the popover state changes. The event emits a boolean value indicating whether the popover is open or closed:
Create a popover component that uses the NgpPopover
directive.
The following directives are available to import from the ng-primitives/popover
package:
Attribute | Description |
---|---|
data-enter |
Applied when the popover is being added to the DOM. This can be used to trigger animations. |
data-exit |
Applied when the popover is being removed from the DOM. This can be used to trigger animations. |
data-placement |
The final rendered placement of the popover. |
The following CSS custom properties are applied to the ngpPopover
directive:
Property | Description |
---|---|
--ngp-popover-transform-origin |
The transform origin of the popover for animations. |
--ngp-popover-trigger-width |
The width of the trigger element. |
Attribute | Description |
---|---|
data-open |
Applied when the popover is open. |
data-disabled |
Applied when the popover is disabled. |
The NgpPopoverArrow
directive is used to add an arrow to the popover. It should be placed inside the popover content. It will receive inset-inline-start
or inset-block-start
styles to position the arrow based on the popover's placement. As a result it should be positioned absolutely within the popover content.
The arrow can be styled conditionally based on the popover's final placement using the data-placement
attribute:
[ngpPopoverArrow][data-placement='top'] {
/* Arrow styles when popover is positioned on top */
}
[ngpPopoverArrow][data-placement='bottom'] {
/* Arrow styles when popover is positioned on bottom */
}
Attribute | Description |
---|---|
data-placement |
The final rendered placement of the popover. |
For the popover to be positioned correctly relative to the trigger element, it must use absolute or fixed positioning. For example, you can use the following CSS:
[ngpPopover] {
position: absolute;
}
The ngpPopover
primitive adds a CSS custom property --ngp-popover-transform-origin
to the element that can be used to animate the popover from the trigger element.
The ngpPopover
will also add the data-enter
and data-exit
attributes to the element when it is being added or removed from the DOM. This can be used to trigger animations.
:host[data-enter] {
animation: fade-in 0.2s ease-in-out;
}
:host[data-exit] {
animation: fade-out 0.2s ease-in-out;
}
You can configure the default options for all popovers in your application by using the providePopoverConfig
function in a providers array.
import { providePopoverConfig } from 'ng-primitives/popover';
bootstrapApplication(AppComponent, {
providers: [
providePopoverConfig({
offset: 4,
placement: 'top',
showDelay: 0,
hideDelay: 0,
flip: true,
container: document.body,
closeOnOutsideClick: true,
scrollBehavior: 'reposition',
}),
],
});
Copyright © 2025 Angular Primitives