|
1 |
| -<!-- TODO --> |
| 1 | +The `Dialog` service can be used to open unstyled modal dialogs and to build your own dialog |
| 2 | +services. |
| 3 | + |
| 4 | +<!-- example(cdk-dialog-overview) --> |
| 5 | + |
| 6 | +You can open a dialog by calling the `open` method either with a component or with a `TemplateRef` |
| 7 | +representing the dialog content. The method additionally accepts an optional configuration object. |
| 8 | +The `open` method returns a `DialogRef` instance: |
| 9 | + |
| 10 | +```ts |
| 11 | +const dialogRef = dialog.open(UserProfileComponent, { |
| 12 | + height: '400px', |
| 13 | + width: '600px', |
| 14 | + panelClass: 'my-dialog', |
| 15 | +}); |
| 16 | +``` |
| 17 | + |
| 18 | +The `DialogRef` provides a reference to the opened dialog. You can use the `DialogRef` to close the |
| 19 | +dialog, subscribe to dialog events, and modify dialog state. All `Observable` instances on the |
| 20 | +`DialogRef` complete when the dialog closes. |
| 21 | + |
| 22 | +```ts |
| 23 | +dialogRef.closed.subscribe(result => { |
| 24 | + console.log(`Dialog result: ${result}`); // Pizza! |
| 25 | +}); |
| 26 | + |
| 27 | +dialogRef.close('Pizza!'); |
| 28 | +``` |
| 29 | + |
| 30 | +Components created via `Dialog` can _inject_ `DialogRef` and use it to close the dialog |
| 31 | +in which they are contained. When closing, an optional result value can be provided. This result |
| 32 | +value is forwarded as the result of the `closed` Observable. |
| 33 | + |
| 34 | +```ts |
| 35 | +@Component({/* ... */}) |
| 36 | +export class YourDialog { |
| 37 | + constructor(public dialogRef: DialogRef<string>) {} |
| 38 | + |
| 39 | + closeDialog() { |
| 40 | + this.dialogRef.close('Pizza!'); |
| 41 | + } |
| 42 | +} |
| 43 | +``` |
| 44 | + |
| 45 | +### Dialog styling |
| 46 | + |
| 47 | +The `Dialog` service includes an intentionally limited set of structural styles. You can customize |
| 48 | +the dialog's appearance using one of the following approaches. |
| 49 | + |
| 50 | +#### `panelClass` option |
| 51 | +The `panelClass` property of `DialogConfig` allows you to apply one or more CSS classes to the |
| 52 | +overlay element that contains the custom dialog content. Any styles targeting these CSS classes |
| 53 | +must be global styles. |
| 54 | + |
| 55 | +#### Styling the dialog component |
| 56 | +You can use the `styles` or `styleUrls` of a custom component to style the dialog content: |
| 57 | + |
| 58 | +```ts |
| 59 | +// MyDialog is rendered via `dialog.open(MyDialog)` |
| 60 | +@Component({ |
| 61 | + selector: 'my-dialog', |
| 62 | + styles: [` |
| 63 | + :host { |
| 64 | + display: block; |
| 65 | + background: #fff; |
| 66 | + border-radius: 8px; |
| 67 | + padding: 16px; |
| 68 | + } |
| 69 | + `] |
| 70 | +}) |
| 71 | +class MyDialog {} |
| 72 | +``` |
| 73 | + |
| 74 | +<!-- example(cdk-dialog-styling) --> |
| 75 | + |
| 76 | +#### Providing a custom dialog contaier |
| 77 | +If you want more control over the dialog's behavior and styling, you can provide your own dialog |
| 78 | +container component using the `container` option in `DialogConfig`. This approach requires more |
| 79 | +code up-front, but it allows you to customize the DOM structure and behavior of the container |
| 80 | +around the dialog content. Custom container components can optionally extend `CdkDialogContainer` |
| 81 | +to inherit standard behaviors, such as accessible focus management. |
| 82 | + |
| 83 | +```ts |
| 84 | +import {CdkDialogContainer} from '@angular/cdk/dialog'; |
| 85 | + |
| 86 | +@Component({ |
| 87 | + selector: 'my-dialog-container', |
| 88 | + styles: [` |
| 89 | + :host { |
| 90 | + display: block; |
| 91 | + background: #fff; |
| 92 | + border-radius: 8px; |
| 93 | + padding: 16px; |
| 94 | + } |
| 95 | + `] |
| 96 | +}) |
| 97 | +class MyDialogContainer extends CdkDialogContainer {} |
| 98 | +``` |
| 99 | + |
| 100 | +### Specifying global configuration defaults |
| 101 | +Default dialog options can be specified by providing an instance of `DialogConfig` for |
| 102 | +`DEFAULT_DIALOG_CONFIG` in your application's root module. |
| 103 | + |
| 104 | +```ts |
| 105 | +@NgModule({ |
| 106 | + providers: [ |
| 107 | + {provide: DEFAULT_DIALOG_CONFIG, useValue: {hasBackdrop: false}} |
| 108 | + ] |
| 109 | +}) |
| 110 | +``` |
| 111 | + |
| 112 | +### Sharing data with the Dialog component. |
| 113 | +You can use the `data` option to pass information to the dialog component. |
| 114 | + |
| 115 | +```ts |
| 116 | +const dialogRef = dialog.open(YourDialog, { |
| 117 | + data: {name: 'frodo'}, |
| 118 | +}); |
| 119 | +``` |
| 120 | + |
| 121 | +Access the data in your dialog component with the `DIALOG_DATA` injection token: |
| 122 | + |
| 123 | +```ts |
| 124 | +import {Component, Inject} from '@angular/core'; |
| 125 | +import {DIALOG_DATA} from '@angular/cdk/dialog'; |
| 126 | + |
| 127 | +@Component({ |
| 128 | + selector: 'your-dialog', |
| 129 | + template: 'passed in {{ data.name }}', |
| 130 | +}) |
| 131 | +export class YourDialog { |
| 132 | + constructor(@Inject(DIALOG_DATA) public data: {name: string}) { } |
| 133 | +} |
| 134 | +``` |
| 135 | + |
| 136 | +If you're using a `TemplateRef` for your dialog content, the data is available in the template: |
| 137 | + |
| 138 | +```html |
| 139 | +<ng-template let-data> |
| 140 | + Hello, {{data.name}} |
| 141 | +</ng-template> |
| 142 | +``` |
| 143 | + |
| 144 | +<!-- example(cdk-dialog-data) --> |
| 145 | + |
| 146 | +### Accessibility |
| 147 | + |
| 148 | +`Dialog` creates modal dialogs that implement the ARIA `role="dialog"` pattern by default. |
| 149 | +You can change the dialog's role to `alertdialog` via the `DialogConfig`. |
| 150 | + |
| 151 | +You should provide an accessible label to this root dialog element by setting the `ariaLabel` or |
| 152 | +`ariaLabelledBy` properties of `DialogConfig`. You can additionally specify a description element |
| 153 | +ID via the `ariaDescribedBy` property of `DialogConfig`. |
| 154 | + |
| 155 | +#### Keyboard interaction |
| 156 | + |
| 157 | +By default, the escape key closes `Dialog`. While you can disable this behavior via the |
| 158 | +`disableClose` property of `DialogConfig`, doing this breaks the expected interaction pattern |
| 159 | +for the ARIA `role="dialog"` pattern. |
| 160 | + |
| 161 | +#### Focus management |
| 162 | + |
| 163 | +When opened, `Dialog` traps browser focus such that it cannot escape the root |
| 164 | +`role="dialog"` element. By default, the first tabbable element in the dialog receives focus. |
| 165 | +You can customize which element receives focus with the `autoFocus` property of |
| 166 | +`DialogConfig`, which supports the following values. |
| 167 | + |
| 168 | +| Value | Behavior | |
| 169 | +|------------------|--------------------------------------------------------------------------| |
| 170 | +| `first-tabbable` | Focus the first tabbable element. This is the default setting. | |
| 171 | +| `first-header` | Focus the first header element (`role="heading"`, `h1` through `h6`) | |
| 172 | +| `dialog` | Focus the root `role="dialog"` element. | |
| 173 | +| Any CSS selector | Focus the first element matching the given selector. | |
| 174 | + |
| 175 | +While the default setting applies the best behavior for most applications, special cases may benefit |
| 176 | +from these alternatives. Always test your application to verify the behavior that works best for |
| 177 | +your users. |
| 178 | + |
| 179 | +#### Focus restoration |
| 180 | + |
| 181 | +When closed, `Dialog` restores focus to the element that previously held focus when the |
| 182 | +dialog opened by default. You can customize the focus restoration behavior using the `restoreFocus` |
| 183 | +property of `DialogConfig`. It supports the following values. |
| 184 | + |
| 185 | +| Value type | Behavior | |
| 186 | +|------------------|------------------------------------------------------------------------------------------------------------------| |
| 187 | +| `boolean` | When `true`, focus will be restored to the previously-focused element, otherwise focus won't be restored at all. | |
| 188 | +| `string` | Value is treated as a CSS selector. Focus will be restored to the element matching the selector. | |
| 189 | +| `HTMLElement` | Specific element that focus should be restored to. | |
0 commit comments