Primitives

Popover

Display arbitrary content inside floating panels.

Import

Import the Popover primitives from ng-primitives/popover.

import { NgpPopover, NgpPopoverTrigger, NgpPopoverArrow } from 'ng-primitives/popover';

Usage

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:

Reusable Component

Create a popover component that uses the NgpPopover directive.

Schematics

Generate a reusable popover component using the Angular CLI.

ng g ng-primitives:primitive popover

Options

  • path: The path at which to create the component file.
  • prefix: The prefix to apply to the generated component selector.
  • component-suffix: The suffix to apply to the generated component class name.
  • file-suffix: The suffix to apply to the generated component file name. Defaults to component.
  • styles: How component styles should be generated. css (default) includes the full example styles; unstyled omits them entirely so you can style the component yourself.
  • example-styles (deprecated): still supported for compatibility - true maps to styles: css, false maps to styles: unstyled.

Examples

Custom Offset

You can customize the offset using either a simple number or an object for more precise control:

<!-- Simple number offset -->
<button [ngpPopoverTrigger]="popover" ngpPopoverTriggerOffset="12">Popover with 12px offset</button>

<!-- Object offset for precise control -->
<button
  [ngpPopoverTrigger]="popover"
  [ngpPopoverTriggerOffset]="{mainAxis: 8, crossAxis: 4, alignmentAxis: 2}"
>
  Popover with custom offset
</button>

Custom Shift

You can customize the shift behavior to control how the popover stays within the viewport:

<!-- Disable shift -->
<button [ngpPopoverTrigger]="popover" [ngpPopoverTriggerShift]="false">
  Popover without shift
</button>

<!-- Object shift for precise control -->
<button [ngpPopoverTrigger]="popover" [ngpPopoverTriggerShift]="{padding: 8}">
  Popover with custom shift padding
</button>

Overflow Boundary

By default, the popover is kept within the viewport and its clipping ancestors. Pass boundary or rootBoundary to flip or shift to measure overflow against something else:

<!-- Keep the popover inside a container element -->
<div #boundary>
  <button [ngpPopoverTrigger]="popover" [ngpPopoverTriggerFlip]="{boundary: boundary}">
    Popover constrained to a container
  </button>
</div>

<!-- Keep the popover inside whatever scrolls the trigger -->
<div style="overflow: auto">
  <button [ngpPopoverTrigger]="popover" [ngpPopoverTriggerFlip]="{altBoundary: true}">
    Popover constrained to the trigger's scroll container
  </button>
</div>

<!-- Measure against the whole document rather than the viewport -->
<button [ngpPopoverTrigger]="popover" [ngpPopoverTriggerFlip]="{rootBoundary: 'document'}">
  Popover that does not flip while off-screen
</button>

altBoundary measures against the trigger's clipping ancestors rather than the popover's. The popover is portalled to the body, so its own clipping ancestors are effectively the viewport - set this when the trigger sits in a scroll container. It applies only while boundary is left at its default; an explicit boundary always wins.

crossAxis widens the axis each option checks. For flip it is the alignment axis, on by default - that is how bottom-end becomes bottom-start near an edge. For shift it is the side axis, off by default; enabling it lets the panel move along the placement direction too:

<button [ngpPopoverTrigger]="popover" [ngpPopoverTriggerFlip]="{crossAxis: false}">
  Keep the alignment
</button>
<button [ngpPopoverTrigger]="popover" [ngpPopoverTriggerShift]="{crossAxis: true}">
  Shift on both axes
</button>

The --ngp-popover-available-width and --ngp-popover-available-height custom properties are measured against the flip boundary, or the shift boundary when flip does not set one.

Popover with anchor

The popover can be anchored to a different element than the trigger.

The anchor is live - rebinding it moves an already-open popover to the new element, and it is the new element that counts as inside for dismissal.

Dismiss Guard

Use dismiss guards to prevent a popover from closing when there are unsaved changes. The closeOnOutsideClick and closeOnEscape options accept a guard function that returns a boolean or a Promise<boolean>.

Handling Escape inside the popover

Overlays handle Escape before the controls inside them, so stopping the event in a control is too late. A control that uses Escape itself, such as an inline-edit field that reverts its value, claims the key with claimEscapeKey from ng-primitives/portal. Call it in an injection context; the claim is released when that context is destroyed. A claimed Escape pressed inside the topmost overlay leaves it open. Compare against event.composedPath()[0] rather than event.target, which is retargeted to the shadow host when the overlay renders inside a shadow root.

import { ElementRef, signal, viewChild } from '@angular/core';
import { claimEscapeKey } from 'ng-primitives/portal';

export class InlineEdit {
  private readonly field = viewChild<ElementRef<HTMLInputElement>>('field');
  // true while the value is being edited; Escape then reverts it instead of closing the popover
  readonly editing = signal(false);

  constructor() {
    claimEscapeKey(
      event => this.editing() && event.composedPath()[0] === this.field()?.nativeElement,
    );
  }
}

Keeping Content Mounted

By default, the popover content is destroyed when the popover closes and created again on the next open, so any one-time setup runs every time. Set ngpPopoverTriggerKeepMounted to keep the content instance alive between opens instead.

The content is still removed from the DOM while the popover is closed, so nothing is left rendered behind the scenes. What survives is the component instance and its DOM nodes: the content is not re-instantiated, so one-time work (such as a network request) is not repeated and unsaved state (such as a partly filled form or a scroll position) is preserved.

Two things to weigh before enabling it:

  • The kept-alive view stays attached to change detection while the popover is hidden, so its bindings continue to be checked.
  • The instance lives until the trigger itself is destroyed, or until the ngpPopoverTrigger content changes, so anything expensive it holds on to stays in memory.

By default a click outside the popover closes it and also reaches what is underneath: a button fires, a link navigates. Set ngpPopoverTriggerModal to make the rest of the page inert while the popover is open, so a click outside only closes the popover.

<button [ngpPopoverTrigger]="popover" ngpPopoverTriggerModal>Open</button>
  • The trigger stays usable, so clicking it again closes the popover. The anchor, when one is set, stays usable too.
  • An outside click closes the topmost modal popover only, one layer at a time. A click fired from code, such as element.click() or a test's fireEvent.click, does not close it.
  • The document still scrolls: use ngpPopoverTriggerScrollBehavior="block" to block it. Other scroll containers, such as the <main> of an app shell, are inert and do not scroll with the wheel, unless they hold the trigger or the anchor.
  • Toasts stay usable. Add the data-ngp-modal-exempt attribute to any other element that must stay usable, such as the container of a third-party overlay library.
  • A closeOnOutsideClick guard receives the nearest element that is not inert, often <body>, rather than the element under the pointer.

End-to-end tests that click something outside an open modal popover have to close it first: the target is inert, so a Playwright click waits for it to take pointer events and times out.

Final focus

When the popover closes, focus returns to the element that was focused when it opened, usually its trigger. Set ngpPopoverFinalFocus to choose another element, to false to leave focus where it is, or to a function that receives the close origin and returns one of these.

<button [ngpPopoverTrigger]="popover">Rename</button>
<input #search />

<ng-template #popover>
  <div ngpPopover [ngpPopoverFinalFocus]="search">...</div>
</ng-template>

API Reference

The following directives are available to import from the ng-primitives/popover package:

NgpPopover

NgpPopoverTrigger

NgpPopoverArrow

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 */
}

Styling

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;
}

With the default reposition scroll behavior, the popover follows its reference element (the anchor, or the trigger when no anchor is set) even once that element is scrolled out of view. data-reference-hidden is applied while that is the case, so you can show that the popover has lost its reference, for example by fading it:

[ngpPopover][data-reference-hidden] {
  opacity: 0.4;
}

Don't hide an open popover with visibility: hidden or display: none: it still traps focus, so a keyboard user would be stuck in content they cannot see. To dismiss the popover when the page scrolls, set ngpPopoverTriggerScrollBehavior="close".

Animations

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;
}

Global Configuration

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',
      cooldown: 0,
      keepMounted: false,
      modal: false,
    }),
  ],
});

NgpPopoverConfig

Accessibility

The popover element is assigned role="dialog" and the trigger element uses aria-expanded to indicate the popover's open state. The trigger is linked to the popover via aria-describedby, and the popover is named by its trigger through aria-labelledby unless an aria-label or aria-labelledby is present when the overlay renders; labels added later do not override the trigger name. Focus is trapped within the popover when open. When it closes, focus returns to the element that was focused when it opened, usually the trigger. An outside click on another control moves focus to that control, and focus the user already moved elsewhere stays there. ngpPopoverFinalFocus can choose another target, or false to leave focus where it is.

Add autofocus to one or more usable elements to focus the first one when the popover opens; otherwise the first tabbable element is focused, or the popover itself if there is none. If focus is already inside the popover when it opens, it stays there and autofocus is ignored.

A modal popover makes the rest of the page inert, so screen readers only reach the popover and its trigger. An outside click reaches no control, so focus returns to the trigger. Elements with data-ngp-modal-exempt, such as the toast region, and aria-live regions placed directly in <body> stay reachable. A live region nested in the page, such as a form error, is inert too while the popover is open.

Keyboard Interactions

  • Enter / Space: Toggle the popover.
  • Esc: Close the popover.
  • Tab: Navigate through focusable elements within the popover.
  • Shift + Tab: Navigate backwards through focusable elements within the popover.

Copyright © 2026 Angular Primitives

This site is powered by Netlify