Skip to content

Commit df00d75

Browse files
authored
refactor(cdk/dialog): changes to accommodate standalone (#27693)
Reworks the CDK dialog to use a standalone component and to use `providedIn: 'root'` for its providers.
1 parent cf456a2 commit df00d75

File tree

5 files changed

+31
-15
lines changed

5 files changed

+31
-15
lines changed

src/cdk/dialog/dialog-container.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
CdkPortalOutlet,
2121
ComponentPortal,
2222
DomPortal,
23+
PortalModule,
2324
TemplatePortal,
2425
} from '@angular/cdk/portal';
2526
import {DOCUMENT} from '@angular/common';
@@ -54,6 +55,8 @@ export function throwDialogContentAlreadyAttachedError() {
5455
// Using OnPush for dialogs caused some G3 sync issues. Disabled until we can track them down.
5556
// tslint:disable-next-line:validate-decorators
5657
changeDetection: ChangeDetectionStrategy.Default,
58+
standalone: true,
59+
imports: [PortalModule],
5760
host: {
5861
'class': 'cdk-dialog-container',
5962
'tabindex': '-1',

src/cdk/dialog/dialog-injectors.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,20 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {InjectionToken} from '@angular/core';
9+
import {InjectionToken, inject} from '@angular/core';
1010
import {Overlay, ScrollStrategy} from '@angular/cdk/overlay';
1111
import {DialogConfig} from './dialog-config';
1212

1313
/** Injection token for the Dialog's ScrollStrategy. */
1414
export const DIALOG_SCROLL_STRATEGY = new InjectionToken<() => ScrollStrategy>(
1515
'DialogScrollStrategy',
16+
{
17+
providedIn: 'root',
18+
factory: () => {
19+
const overlay = inject(Overlay);
20+
return () => overlay.scrollStrategies.block();
21+
},
22+
},
1623
);
1724

1825
/** Injection token for the Dialog's Data. */
@@ -21,12 +28,20 @@ export const DIALOG_DATA = new InjectionToken<any>('DialogData');
2128
/** Injection token that can be used to provide default options for the dialog module. */
2229
export const DEFAULT_DIALOG_CONFIG = new InjectionToken<DialogConfig>('DefaultDialogConfig');
2330

24-
/** @docs-private */
31+
/**
32+
* @docs-private
33+
* @deprecated No longer used. To be removed.
34+
* @breaking-change 19.0.0
35+
*/
2536
export function DIALOG_SCROLL_STRATEGY_PROVIDER_FACTORY(overlay: Overlay): () => ScrollStrategy {
2637
return () => overlay.scrollStrategies.block();
2738
}
2839

29-
/** @docs-private */
40+
/**
41+
* @docs-private
42+
* @deprecated No longer used. To be removed.
43+
* @breaking-change 19.0.0
44+
*/
3045
export const DIALOG_SCROLL_STRATEGY_PROVIDER = {
3146
provide: DIALOG_SCROLL_STRATEGY,
3247
deps: [Overlay],

src/cdk/dialog/dialog-module.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,15 @@ import {PortalModule} from '@angular/cdk/portal';
1212
import {A11yModule} from '@angular/cdk/a11y';
1313
import {Dialog} from './dialog';
1414
import {CdkDialogContainer} from './dialog-container';
15-
import {DIALOG_SCROLL_STRATEGY_PROVIDER} from './dialog-injectors';
1615

1716
@NgModule({
18-
imports: [OverlayModule, PortalModule, A11yModule],
17+
imports: [OverlayModule, PortalModule, A11yModule, CdkDialogContainer],
1918
exports: [
2019
// Re-export the PortalModule so that people extending the `CdkDialogContainer`
2120
// don't have to remember to import it or be faced with an unhelpful error.
2221
PortalModule,
2322
CdkDialogContainer,
2423
],
25-
declarations: [CdkDialogContainer],
26-
providers: [Dialog, DIALOG_SCROLL_STRATEGY_PROVIDER],
24+
providers: [Dialog],
2725
})
2826
export class DialogModule {}

src/cdk/dialog/dialog.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ import {CdkDialogContainer} from './dialog-container';
3939
/** Unique id for the created dialog. */
4040
let uniqueId = 0;
4141

42-
@Injectable()
42+
@Injectable({providedIn: 'root'})
4343
export class Dialog implements OnDestroy {
4444
private _openDialogsAtThisLevel: DialogRef<any, any>[] = [];
4545
private readonly _afterAllClosedAtThisLevel = new Subject<void>();

tools/public_api_guard/cdk/dialog.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ import { FocusMonitor } from '@angular/cdk/a11y';
1818
import { FocusOrigin } from '@angular/cdk/a11y';
1919
import { FocusTrapFactory } from '@angular/cdk/a11y';
2020
import * as i0 from '@angular/core';
21-
import * as i2 from '@angular/cdk/overlay';
22-
import * as i3 from '@angular/cdk/portal';
23-
import * as i4 from '@angular/cdk/a11y';
21+
import * as i1 from '@angular/cdk/overlay';
22+
import * as i2 from '@angular/cdk/portal';
23+
import * as i3 from '@angular/cdk/a11y';
2424
import { InjectionToken } from '@angular/core';
2525
import { Injector } from '@angular/core';
2626
import { InteractivityChecker } from '@angular/cdk/a11y';
@@ -70,7 +70,7 @@ export class CdkDialogContainer<C extends DialogConfig = DialogConfig> extends B
7070
_recaptureFocus(): void;
7171
protected _trapFocus(): void;
7272
// (undocumented)
73-
static ɵcmp: i0.ɵɵComponentDeclaration<CdkDialogContainer<any>, "cdk-dialog-container", never, {}, {}, never, never, false, never>;
73+
static ɵcmp: i0.ɵɵComponentDeclaration<CdkDialogContainer<any>, "cdk-dialog-container", never, {}, {}, never, never, true, never>;
7474
// (undocumented)
7575
static ɵfac: i0.ɵɵFactoryDeclaration<CdkDialogContainer<any>, [null, null, { optional: true; }, null, null, null, null, null]>;
7676
}
@@ -104,14 +104,14 @@ export const DIALOG_DATA: InjectionToken<any>;
104104
// @public
105105
export const DIALOG_SCROLL_STRATEGY: InjectionToken<() => ScrollStrategy>;
106106

107-
// @public
107+
// @public @deprecated
108108
export const DIALOG_SCROLL_STRATEGY_PROVIDER: {
109109
provide: InjectionToken<() => ScrollStrategy>;
110110
deps: (typeof Overlay)[];
111111
useFactory: typeof DIALOG_SCROLL_STRATEGY_PROVIDER_FACTORY;
112112
};
113113

114-
// @public
114+
// @public @deprecated
115115
export function DIALOG_SCROLL_STRATEGY_PROVIDER_FACTORY(overlay: Overlay): () => ScrollStrategy;
116116

117117
// @public
@@ -164,7 +164,7 @@ export class DialogModule {
164164
// (undocumented)
165165
static ɵinj: i0.ɵɵInjectorDeclaration<DialogModule>;
166166
// (undocumented)
167-
static ɵmod: i0.ɵɵNgModuleDeclaration<DialogModule, [typeof i1.CdkDialogContainer], [typeof i2.OverlayModule, typeof i3.PortalModule, typeof i4.A11yModule], [typeof i3.PortalModule, typeof i1.CdkDialogContainer]>;
167+
static ɵmod: i0.ɵɵNgModuleDeclaration<DialogModule, never, [typeof i1.OverlayModule, typeof i2.PortalModule, typeof i3.A11yModule, typeof i4.CdkDialogContainer], [typeof i2.PortalModule, typeof i4.CdkDialogContainer]>;
168168
}
169169

170170
// @public

0 commit comments

Comments
 (0)