Primitives

Dialog

A dialog is a floating window that can be used to display information or prompt the user for input.

Import

Import the Dialog primitives from ng-primitives/dialog.

import {
  NgpDialog,
  NgpDialogTitle,
  NgpDialogDescription,
  NgpDialogTrigger,
  NgpDialogOverlay,
} from 'ng-primitives/dialog';

Usage

Assemble the dialog directives in your template.

<button [ngpDialogTrigger]="dialog" ngpButton>Launch Dialog</button>

<ng-template #dialog let-close="close">
  <div ngpDialogOverlay>
    <div ngpDialog>
      <h1 ngpDialogTitle>Publish this article?</h1>
      <p ngpDialogDescription>
        Are you sure you want to publish this article? This action is irreversible.
      </p>
      <div class="dialog-footer">
        <button (click)="close()" ngpButton>Cancel</button>
        <button (click)="close()" ngpButton>Confirm</button>
      </div>
    </div>
  </div>
</ng-template>

Reusable Component

Create reusable components that uses the NgpDialog and NgpDialogTrigger directives.

Schematics

Generate a reusable dialog component using the Angular CLI.

ng g ng-primitives:primitive dialog

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

Dismiss Guard

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

Handling Escape inside the dialog

A control inside the dialog that uses Escape itself, such as an inline-edit field that reverts its value, claims the key with claimEscapeKey from ng-primitives/portal so the dialog stays open. A claim only applies to Escape pressed inside the topmost overlay and skips closeOnEscape guards. The Popover page has an example; it works the same for every overlay.

Dialog with external data

Data can be passed to the dialog using the NgpDialogManager.

Drawer

A drawer is a type of dialog that slides in from the side of the screen.

Non-modal drawer

A drawer that acts on the page beside it, such as view settings or filters next to a table, should leave that page usable. Set ngpDialogTriggerModal to false (or pass modal: false to NgpDialogManager.open() or provideDialogConfig). While a non-modal dialog is open:

  • the page stays scrollable, clickable and readable by screen readers, unless a modal dialog below it still covers the page;
  • focus moves to the dialog element on open, unless an element inside it already took focus. It is not trapped. On close it returns to the trigger only if it was still inside the dialog, or in an overlay opened from it;
  • a second click on the trigger closes the dialog, unless disableClose is set;
  • closeOnEscape applies only to an Escape pressed inside the dialog. Elsewhere, Escape goes to the overlay below;
  • an outside click does not close it. Leave out ngpDialogOverlay, so nothing covers the page.
<button [ngpDialogTrigger]="settings" ngpDialogTriggerModal="false">View settings</button>

<ng-template #settings let-close="close">
  <div ngpDialog>
    <h2 ngpDialogTitle>View settings</h2>
    <button (click)="close()">Done</button>
  </div>
</ng-template>

modal is read when the dialog opens. ngpDialogModal on the dialog only sets aria-modal. Inside the dialog, injectDialogRef().config.modal tells whether it is modal.

API Reference

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

NgpDialog

NgpDialogTitle

NgpDialogDescription

NgpDialogTrigger

NgpDialogOverlay

NgpDialogManager

The NgpDialogManager can be used as an alternative to the NgpDialogTrigger directive for programmatically opening dialogs.

The manager provides methods to open, close, and query dialogs, and accepts a component or template reference to display.

NgpDialogRef

Reference returned by NgpDialogManager.open(). Provides methods to interact with the opened dialog.

Closes the dialog, optionally returning a result value. Observable that emits immediately when `close()` is called, before exit animations run. Observable that emits after exit animations have completed. The data passed to the dialog via `NgpDialogConfig.data`. The unique ID for the dialog. Whether the user is allowed to close the dialog. Whether the escape key is allowed to close the dialog, or a guard function. When a function is provided, it receives the keyboard event and should return a boolean or `Promise`. Whether clicking outside (on the overlay) is allowed to close the dialog, or a guard function. When a function is provided, it receives the target element and should return a boolean or `Promise`. Observable that emits keyboard events dispatched within the dialog. Observable that emits pointer events dispatched outside of the dialog. Updates the position of the dialog. Currently a no-op as dialogs are CSS-centered.

Global Configuration

You can configure the default options for all dialogs in your application by using the provideDialogConfig function in a providers array.

import { provideDialogConfig } from 'ng-primitives/dialog';

bootstrapApplication(AppComponent, {
  providers: [
    provideDialogConfig({
      container: '#app-root',
      role: 'dialog',
      modal: true,
    }),
  ],
});

By default dialogs render into document.body. The container option accepts an element or a selector, which is useful when the dialog must live inside a specific subtree - for example a micro-frontend root or an element carrying your theme class. It can be overridden per dialog through NgpDialogTrigger's ngpDialogTriggerContainer input, or through the container option passed to NgpDialogManager.open().

A dialog opened by NgpDialogTrigger stays open when its trigger is removed, for example by an @if. Set lifetime: 'trigger' to close it along with its trigger. A trigger inside a menu or popover follows that overlay's trigger instead, so a dialog opened from a menu item stays open when the menu closes. With lifetime: 'trigger', a dialog opened from inside another dialog closes when that dialog closes, since its trigger goes with it. Dialogs opened with NgpDialogManager.open() are not affected.

provideDialogConfig({ lifetime: 'trigger' });

NgpDialogConfig

Accessibility

Adheres to the WAI-ARIA design pattern.

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

When a dialog closes, focus returns to the element that opened it. If that element is gone, for example a menu item whose menu closed on select, focus goes to the trigger of the menu or popover it was in.

Keyboard Interactions

  • Esc: Close the dialog, as set by closeOnEscape. A non-modal dialog only takes an Escape pressed inside it.
  • Tab: Navigate through focusable elements within the dialog. In a non-modal dialog, focus can move on to the page.
  • Shift + Tab: Navigate backwards through focusable elements within the dialog. In a non-modal dialog, focus can move on to the page.

Copyright © 2026 Angular Primitives

This site is powered by Netlify