Skip to content

Commit 3889165

Browse files
authored
refactor(cdk-experimental/dialog): rewrite experimental dialog (#24759)
Rewrites most of the CDK experimental dialog to prepare it for public release. The changes were largely informed by implementing `material/dialog`, `material-experimental/mdc-dialog` and `material/bottom-sheet` using the new API, however the API can be used directly without having to be wrapped. Overview of the changes: * The classes for the dialog container, dialog config and dialog data aren't provided using DI anymore. The previous approach could've been a problem for apps that use multiple different versions of a CDK-based dialog. * Several fixes from the Material dialog that were never backported to the CDK version have been incorporated. * All animations and styles have been removed from the CDK dialog since they were going to be difficult to override by users. Custom animations can be achieved by extending the dialog container class. * The public API has been cleaned up to avoid some of the issues that were inherited from the Material dialog. * A demo has been added to the dev app. Example of how the new APIs was used to implement the Material components: crisbeto@446a552.
1 parent e3825c8 commit 3889165

19 files changed

+902
-858
lines changed

.github/CODEOWNERS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@
153153
/src/dev-app/mdc-autocomplete/** @crisbeto
154154
/src/dev-app/button/** @andrewseguin
155155
/src/dev-app/card/** @andrewseguin
156+
/src/dev-app/cdk-dialog/** @crisbeto
156157
/src/dev-app/cdk-experimental-combobox/** @jelbourn
157158
/src/dev-app/cdk-experimental-listbox/** @jelbourn
158159
/src/dev-app/cdk-experimental-menu/** @jelbourn

goldens/ts-circular-deps.json

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
11
[
2-
[
3-
"src/cdk-experimental/dialog/dialog-config.ts",
4-
"src/cdk-experimental/dialog/dialog-container.ts"
5-
],
62
["src/cdk/drag-drop/directives/drag.ts", "src/cdk/drag-drop/directives/drop-list.ts"],
73
["src/cdk/drag-drop/directives/drag.ts", "src/cdk/drag-drop/drag-events.ts"],
84
[

src/cdk-experimental/dialog/BUILD.bazel

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
load("//tools:defaults.bzl", "ng_module", "ng_test_library", "ng_web_test_suite", "sass_binary")
1+
load("//tools:defaults.bzl", "ng_module", "ng_test_library", "ng_web_test_suite")
22

33
package(default_visibility = ["//visibility:public"])
44

@@ -8,7 +8,7 @@ ng_module(
88
["**/*.ts"],
99
exclude = ["**/*.spec.ts"],
1010
),
11-
assets = [":dialog-container.css"] + glob(["**/*.html"]),
11+
assets = glob(["**/*.html"]),
1212
deps = [
1313
"//src:dev_mode_types",
1414
"//src/cdk/a11y",
@@ -23,11 +23,6 @@ ng_module(
2323
],
2424
)
2525

26-
sass_binary(
27-
name = "dialog_container_scss",
28-
src = "dialog-container.scss",
29-
)
30-
3126
ng_test_library(
3227
name = "unit_test_sources",
3328
srcs = glob(

src/cdk-experimental/dialog/dialog-config.ts

Lines changed: 87 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,26 @@
55
* Use of this source code is governed by an MIT-style license that can be
66
* found in the LICENSE file at https://angular.io/license
77
*/
8-
import {Injector, ViewContainerRef} from '@angular/core';
8+
9+
import {
10+
ViewContainerRef,
11+
ComponentFactoryResolver,
12+
Injector,
13+
StaticProvider,
14+
Type,
15+
} from '@angular/core';
916
import {Direction} from '@angular/cdk/bidi';
10-
import {ComponentType} from '@angular/cdk/overlay';
11-
import {CdkDialogContainer} from './dialog-container';
17+
import {PositionStrategy, ScrollStrategy} from '@angular/cdk/overlay';
18+
import {BasePortalOutlet} from '@angular/cdk/portal';
1219

1320
/** Options for where to set focus to automatically on dialog open */
1421
export type AutoFocusTarget = 'dialog' | 'first-tabbable' | 'first-heading';
1522

16-
/** Valid ARIA roles for a dialog element. */
23+
/** Valid ARIA roles for a dialog. */
1724
export type DialogRole = 'dialog' | 'alertdialog';
1825

19-
/** Possible overrides for a dialog's position. */
20-
export interface DialogPosition {
21-
top?: string;
22-
bottom?: string;
23-
left?: string;
24-
right?: string;
25-
}
26-
27-
export class DialogConfig<D = any> {
28-
/** Component to use as the container for the dialog. */
29-
containerComponent?: ComponentType<CdkDialogContainer>;
30-
26+
/** Configuration for opening a modal dialog. */
27+
export class DialogConfig<D = unknown, R = unknown, C extends BasePortalOutlet = BasePortalOutlet> {
3128
/**
3229
* Where the attached component should live in Angular's *logical* component tree.
3330
* This affects what is available for injection and the change detection order for the
@@ -42,67 +39,115 @@ export class DialogConfig<D = any> {
4239
*/
4340
injector?: Injector;
4441

45-
/** The id of the dialog. */
42+
/** ID for the dialog. If omitted, a unique one will be generated. */
4643
id?: string;
4744

48-
/** The ARIA role of the dialog. */
45+
/** The ARIA role of the dialog element. */
4946
role?: DialogRole = 'dialog';
5047

51-
/** Custom class(es) for the overlay panel. */
48+
/** Optional CSS class or classes applied to the overlay panel. */
5249
panelClass?: string | string[] = '';
5350

54-
/** Whether the dialog has a background. */
51+
/** Whether the dialog has a backdrop. */
5552
hasBackdrop?: boolean = true;
5653

57-
/** Custom class(es) for the backdrop. */
58-
backdropClass?: string | undefined = '';
54+
/** Optional CSS class or classes applied to the overlay backdrop. */
55+
backdropClass?: string | string[] = '';
5956

60-
/** Whether the dialog can be closed by user interaction. */
57+
/** Whether the dialog closes with the escape key or pointer events outside the panel element. */
6158
disableClose?: boolean = false;
6259

63-
/** The width of the dialog. */
60+
/** Width of the dialog. */
6461
width?: string = '';
6562

66-
/** The height of the dialog. */
63+
/** Height of the dialog. */
6764
height?: string = '';
6865

69-
/** The minimum width of the dialog. */
70-
minWidth?: string | number = '';
66+
/** Min-width of the dialog. If a number is provided, assumes pixel units. */
67+
minWidth?: number | string;
7168

72-
/** The minimum height of the dialog. */
73-
minHeight?: string | number = '';
69+
/** Min-height of the dialog. If a number is provided, assumes pixel units. */
70+
minHeight?: number | string;
7471

75-
/** The maximum width of the dialog. */
76-
maxWidth?: string | number = '80vw';
72+
/** Max-width of the dialog. If a number is provided, assumes pixel units. Defaults to 80vw. */
73+
maxWidth?: number | string;
7774

78-
/** The maximum height of the dialog. */
79-
maxHeight?: string | number = '';
75+
/** Max-height of the dialog. If a number is provided, assumes pixel units. */
76+
maxHeight?: number | string;
8077

81-
/** The position of the dialog. */
82-
position?: DialogPosition;
78+
/** Strategy to use when positioning the dialog. Defaults to centering it on the page. */
79+
positionStrategy?: PositionStrategy;
8380

84-
/** Data to be injected into the dialog content. */
81+
/** Data being injected into the child component. */
8582
data?: D | null = null;
8683

87-
/** The layout direction for the dialog content. */
84+
/** Layout direction for the dialog's content. */
8885
direction?: Direction;
8986

9087
/** ID of the element that describes the dialog. */
9188
ariaDescribedBy?: string | null = null;
9289

93-
/** Aria label to assign to the dialog element */
90+
/** ID of the element that labels the dialog. */
91+
ariaLabelledBy?: string | null = null;
92+
93+
/** Dialog label applied via `aria-label` */
9494
ariaLabel?: string | null = null;
9595

96+
/** Whether this a modal dialog. Used to set the `aria-modal` attribute. */
97+
ariaModal?: boolean = true;
98+
9699
/**
97100
* Where the dialog should focus on open.
98101
* @breaking-change 14.0.0 Remove boolean option from autoFocus. Use string or
99102
* AutoFocusTarget instead.
100103
*/
101104
autoFocus?: AutoFocusTarget | string | boolean = 'first-tabbable';
102105

103-
/** Duration of the enter animation. Has to be a valid CSS value (e.g. 100ms). */
104-
enterAnimationDuration?: string = '225ms';
106+
/**
107+
* Whether the dialog should restore focus to the
108+
* previously-focused element upon closing.
109+
*/
110+
restoreFocus?: boolean = true;
111+
112+
/**
113+
* Scroll strategy to be used for the dialog. This determines how
114+
* the dialog responds to scrolling underneath the panel element.
115+
*/
116+
scrollStrategy?: ScrollStrategy;
105117

106-
/** Duration of the exit animation. Has to be a valid CSS value (e.g. 50ms). */
107-
exitAnimationDuration?: string = '225ms';
118+
/**
119+
* Whether the dialog should close when the user navigates backwards or forwards through browser
120+
* history. This does not apply to navigation via anchor element unless using URL-hash based
121+
* routing (`HashLocationStrategy` in the Angular router).
122+
*/
123+
closeOnNavigation?: boolean = true;
124+
125+
/** Alternate `ComponentFactoryResolver` to use when resolving the associated component. */
126+
componentFactoryResolver?: ComponentFactoryResolver;
127+
128+
/**
129+
* Providers that will be exposed to the contents of the dialog. Can also
130+
* be provided as a function in order to generate the providers lazily.
131+
*/
132+
providers?:
133+
| StaticProvider[]
134+
| ((dialogRef: R, config: DialogConfig<D, R, C>, container: C) => StaticProvider[]);
135+
136+
/**
137+
* Component into which the dialog content will be rendered. Defaults to `CdkDialogContainer`.
138+
* A configuration object can be passed in to customize the providers that will be exposed
139+
* to the dialog container.
140+
*/
141+
container?:
142+
| Type<C>
143+
| {
144+
type: Type<C>;
145+
providers: (config: DialogConfig<D, R, C>) => StaticProvider[];
146+
};
147+
148+
/**
149+
* Context that will be passed to template-based dialogs.
150+
* A function can be passed in to resolve the context lazily.
151+
*/
152+
templateContext?: Record<string, any> | (() => Record<string, any>);
108153
}

src/cdk-experimental/dialog/dialog-container.scss

Lines changed: 0 additions & 6 deletions
This file was deleted.

0 commit comments

Comments
 (0)