Dialog
<dialog> element, so focus trapping, stacking and Escape come from the browser.
vig-dialog [vigDialogFooter] ng-template[vigDialogContent]
import { Dialog, DialogFooter, DialogContent, Dialogs } from '@vigdx/ui/dialog';Basic
[(open)] to a signal and give the dialog a heading . Mark the action row with vigDialogFooter ; class="contents" lets its buttons sit directly in the right-aligned footer.
Scrollable content
width sets the width, capped at the viewport.
Dismiss options
closable , closeOnBackdropClick (formerly dismissable ) and closeOnEscape when the user must decide explicitly — and then always offer a button that closes it.
Overlays inside a dialog
provideVigdxUi() moves that container into the open dialog's top layer, so panels stay clickable and Escape closes the open panel before the dialog.
Open from code
Dialogs service opens a component in a vig-dialog without a host element: open() returns a VigDialogRef whose closed promise settles with the result. The content keeps injectDialogRef() in a public dialogRef field, which types the data and the result for the caller, and closes with dialogRef.close(result) ; its beforeClose guard asks before Escape, the backdrop, the X button or Cancel discard edits.
Dialogs service
inject(Dialogs).open(component, options) renders the component in a vig-dialog appended to <body>, with the same native modal, dismissal and exit transition as the declarative dialog. One helper connects the content: injectDialogRef<Result, Data>() gives it the data (data) and the way out (close, requestClose, beforeClose). Declared as a public dialogRef field, it also types open(): the caller must pass matching data, and closed resolves with the result type, with no generics at the call site.
export class EditCustomerDialog {
// R = the result, D = the data. Public, so open() infers both.
readonly dialogRef = injectDialogRef<Customer, Customer>();
save(): void {
this.dialogRef.close({ ...this.dialogRef.data, name: this.name() });
}
}const ref = this.dialogs.open(EditCustomerDialog, {
data: customer, // required and type-checked: EditCustomerDialog declares it
heading: 'Edit customer', // or ariaLabel for a dialog without a heading
size: 'lg', // sm | md (default) | lg: the --container-dialog-* tokens
closeOnBackdropClick: false,
closeOnEscape: true,
closeOnNavigation: true, // default: a route (path) change closes it
beforeClose: (result) => result !== undefined || confirmDiscard(),
injector: this.injector, // = inject(Injector) in a field: this component's providers
providers: [{ provide: CUSTOMER_API, useClass: CustomerApi }],
});
ref.state(); // signal: 'open' | 'closing' | 'closed'
await ref.closed; // Customer | undefined, after the exit transition
ref.requestClose(); // runs beforeClose; ref.close(value) does not Widths are presets from tokens rather than CSS lengths, and there is no z-index option: each dialog opens in the browser's top layer, above everything opened before it. Open an <ng-template> the same way; its context is the ref:
<ng-template #note let-ref>
<p>{{ ref.data }}</p>
<vig-button label="Got it" (click)="ref.close(true)" />
</ng-template>readonly note = viewChild.required<TemplateRef<DialogTemplateContext<boolean, string>>>('note');
open(): void {
this.dialogs.open(this.note(), {
heading: 'Release notes',
data: 'Exports now include archived projects.',
});
}- Focus starts on the content's
autofocuselement, else on its first focusable element, else on the X button, and returns to the element that opened the dialog once it has closed. - Stacking: a dialog opened from another one opens on top of it; Escape closes the newest first.
openDialogs()lists the open ones andcloseAll()closes them. - Closing: Escape, the backdrop, the X button and
requestClose()runbeforeClose;close(result),closeAll(), a navigation and the end of the application do not. A guard that never settles blocks the user's ways out (development builds warn after 10 seconds). - Server rendering: on the server
open()renders nothing and returns a ref that is alreadyclosedand whoseclosedpromise never settles, so a dialog opened while a page initializes appears once the browser takes over, and code awaiting the result runs only there. - Injection:
Dialogsis provided in root. Called from a constructor or field initializer,open()uses the caller's injector; from an event handler it uses the root injector, so passinjector(a field set toinject(Injector)) for the content to reach the opener's component and route providers. - Typing:
open()reads the result and data types from a publicreadonly dialogReffield created byinjectDialogRef(). A private or protected field is invisible to the type system; the ref is thenVigDialogRef<unknown, unknown>. - Navigation: with
closeOnNavigation(the default) a change of the route path closes the dialog withoutbeforeClose; query and fragment changes, and URL changes before the dialog first rendered (initial navigation, redirects), do not. - Native closes: a
<form method="dialog">in the content goes throughrequestClose()with the submit button's value. A close the browser forces (repeated Escape presses without user activation) is accepted as a dismissal rather than re-shown.
Styling hooks
Internal elements carry a stable data-part and the component's data-scope; both are public API, so removing one is a breaking change. Select a part by both, e.g. [data-scope="dialog"][data-part="panel"] { … }: the pair stays within this component when kit components nest, and also reaches parts in overlay panels, which render outside the host. State is exposed through aria-* and, where listed, a data-state; literal values are checked by the API gate, values computed from the component's state follow its documented types. Other data-* attributes listed below (such as data-opening while an overlay animates) are API too. See Customizing.
Scope: [data-scope="dialog"]
| Part | Element | data-state |
|---|---|---|
panel | <dialog> ; style the backdrop with ::backdrop . | open, closed |
header | — | |
heading | <h2> of heading . | — |
close | closable ). | — |
body | — | |
footer | [vigDialogFooter] row. | — |
| Attribute | Meaning |
|---|---|
data-opening | <dialog> ) while its entrance transition plays; removed once it settles (2s at most). Style the entrance with data-opening: utilities. |
data-closing | data-closing: utilities. |
Accessibility
- Follows the WAI-ARIA APG modal dialog pattern using the native
<dialog>andshowModal(): the page behind is inert, focus moves into the dialog and Tab stays inside it. - The dialog is named by its
headingthrougharia-labelledby. Without a header, passariaLabel. - Escape closes the dialog unless
closeOnEscapeis off; when it closes, the browser returns focus to the element that opened it. Dismissal is declared with the nativeclosedbyattribute where supported (closeOnEscapeoff maps toclosedby="none"), with a script fallback elsewhere. - With
closeOnBackdropClick, a backdrop click closes the dialog only when the press and the release both land outside it, so dragging a text selection out of the dialog never loses the user's input. Scrolling over the backdrop or past the end of the content does not scroll the page behind. - The close button is labelled by
closeAriaLabel(localized by default). If you hide it withclosable, keep a visible way to close the dialog. - On open, the browser focuses the first focusable element — the close button while
closableis on. Addautofocusto the field the user should start with (vig-buttontakes anautofocusinput too). - Opening and closing fade (and scale, unless the user prefers reduced motion); the close waits for the fade before the dialog leaves the top layer.
Lazy content
Projected content is created with the vig-dialog, even while it is closed. Wrap a heavy body (a form that loads data, a chart) in <ng-template vigDialogContent> instead: it is created when the dialog opens and destroyed once it has closed, after the exit transition.
API
Dialog
vig-dialogModal dialog on top of the native <dialog> element: the platform provides
the focus trap, top-layer stacking, Escape handling and focus restore —
no overlay library involved. Deliberately not draggable/resizable.
While it plays its entrance or exit transition, the animating element
carries data-opening or data-closing (stable hooks for styling and
for waiting in tests; see the theming guide).
data-opening data-closing
| Name | Type | Default | Description |
|---|---|---|---|
ariaLabel | string | undefined | undefined | — |
closable | boolean | true | Show the X button in the header row. |
closeAriaLabel | string | undefined | undefined | — |
closeOnBackdropClick | boolean | undefined | undefined | Close when the backdrop is clicked (press and release both outside the
box, so dragging a text selection out never dismisses). Default |
closeOnEscape | boolean | true | Close on Escape / the back gesture; off also disables backdrop dismissal natively. |
dismissable | boolean | true | Deprecated: Use `closeOnBackdropClick`; still honored when it is unset. — |
header | string | undefined | undefined | Deprecated: Use `heading`; still honored when `heading` is unset. — |
heading | string | undefined | undefined | Visible title, which also names the panel ( |
open | boolean | false | Two-way: drive with a signal, e.g. |
width | string | 'var(--container-dialog-md)' | CSS width, capped at the viewport by max-width. Defaults to the
|
| Name | Payload | Description |
|---|---|---|
openChange | boolean | Two-way: drive with a signal, e.g. |
Dialogs
Opens dialogs from code (PrimeNG DialogService / CDK Dialog
territory) on top of vig-dialog: native modal <dialog>, top layer,
closedby dismissal and exit transitions. No host element to place.
// EditUser declares `readonly dialogRef = injectDialogRef<User, { id: string }>()`
const ref = inject(Dialogs).open(EditUser, { heading: 'Edit user', data: { id } });
const saved = await ref.closed; // User | undefinedFocus moves to the content's autofocus element, else its first focusable
one, and returns to the opener on close. Dialogs stack: the newest is on
top and Escape closes it first.
Server rendering: open() is a no-op that returns an inert ref (state
closed, closed never settles), so code that opens a dialog while a page
initializes renders nothing on the server and opens it in the browser.
| Signature | Description |
|---|---|
closeAll(): void | Closes every open dialog, newest first, without consulting |
open(component: Type<C>, ...args: DialogOpenArgs<DialogResultOf<C>, DialogDataOf<C>>): VigDialogRef<DialogResultOf<C>, DialogDataOf<C>> | Opens a component in a modal dialog. The result and data types come from
the component's |
open(template: TemplateRef<DialogTemplateContext<R, D>> | TemplateRef<unknown>, ...args: DialogOpenArgs<R, D>): VigDialogRef<R, D> | Opens an |
Templates and directives
| Selector | Export | Description |
|---|---|---|
[vigDialogFooter] | DialogFooter | Marks the footer (action row) content of a vig-dialog. |
ng-template[vigDialogContent] | DialogContent | Body content of a vig-dialog created only while it is shown: when it opens, and destroyed once it has closed (after the exit transition). Projected content, by contrast, lives as long as the vig-dialog does. Example : |