Skip to content
Okaapiأوكابي
Framework for the code
Theme
Okaapi on GitHub

Chapter 06 Overlays

Dialog

A modal window for a task that needs the reader's full attention: a short form, details, terms. Focus moves into it and stays there, the page behind stops scrolling, and Escape, the close button or a click outside closes it.

Use it in Angular
import {
  DialogCloseDirective,
  DialogComponent,

For a yes-or-no question, such as confirming a delete, use the alert dialog: it is built for answers.

A form in a dialog

The title names the dialog and the description is read with it. The actions stay at the bottom.

Angular
<!-- A template (or a component) holds the content -->
<ng-template #editAddress>
  <ui-dialog title="Edit address" description="We deliver your next orders here.">
    <ui-text-field label="Street" [formControl]="street" />
    <ui-text-field label="Building" [formControl]="building" />
    <div ui-dialog-actions>
      <button ui-button variant="secondary" uiDialogClose>Cancel</button>
      <button ui-button [uiDialogClose]="'save'">Save</button>
    </div>
  </ui-dialog>
</ng-template>

<button ui-button (click)="edit(editAddress)">Edit address</button>

// In the class
private readonly dialog = inject(UiDialog);

edit(content: TemplateRef<unknown>) {
  this.dialog.open<string>(content).closed.subscribe((result) => {
    if (result === 'save') { /* ... */ }
  });
}

Long content

When the content is longer than the screen, only the body scrolls: the title and the buttons stay in view. The large size suits reading.

Angular
<ui-dialog title="Terms of ordering" size="lg">
  <p>...</p>
  <div ui-dialog-actions>
    <button ui-button uiDialogClose>Close</button>
  </div>
</ui-dialog>

Only its buttons close it

For a form that a stray click or key must not throw away: a click outside and Escape do nothing, and the close button is hidden, so Cancel and Save are the way out. Works the same on a drawer and a bottom sheet.

Angular
this.dialog.open(content, { dismissable: false });
// a drawer or sheet too: the variant lives on <ui-dialog>, the option on open()

When to use something else

If you needUse
A yes or no question that must be answeredAlert dialog
Content beside the page, such as filtersDrawer
A short note next to the buttonPopover

Inputs

NameTypeDefaultWhat it does
UiDialog.open(content, config?)DialogRef<R> — Opens a component or a template. closed emits the result once.
config.dataD — Read it with inject(DIALOG_DATA) in a component, or let-data in a template.
config.dismissablebooleantruefalse: only your own buttons close it. Escape and a click outside do nothing, and the close button is hidden.
config.initialFocusCSS selectorthe headingThe element to focus first.
<ui-dialog> titlestring — Required. The heading; it names the dialog.
<ui-dialog> descriptionstring — A sentence under the title, read with it.
<ui-dialog> size'sm' | 'md' | 'lg''md'24, 32 or 48rem wide, never wider than the screen.
<ui-dialog> closeLabelstring'Close' from labelsAccessible name of the close button; pass the translated text.
[ui-dialog-actions]content slot — The buttons; they line up at the end and stay in view.
uiDialogClosedirective — On a button: closes the dialog with its value, or none when bare.

Keyboard and screen readers

KeysWhat they do
Tab or Shift + Tab Move between the controls inside; focus stays inside.
Escape Close, and put focus back on the button that opened it.
  • Named by its title; the page behind stops scrolling and cannot be reached.