Skip to content

Commit 60e63ea

Browse files
committed
feat(cdk): switch injectables to new scope API
1 parent 877eb85 commit 60e63ea

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+1330
-784
lines changed

package-lock.json

Lines changed: 998 additions & 535 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/cdk/a11y/a11y-module.ts

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,12 @@
99
import {PlatformModule} from '@angular/cdk/platform';
1010
import {CommonModule} from '@angular/common';
1111
import {NgModule} from '@angular/core';
12-
import {ARIA_DESCRIBER_PROVIDER, AriaDescriber} from './aria-describer/aria-describer';
13-
import {CdkMonitorFocus, FOCUS_MONITOR_PROVIDER} from './focus-monitor/focus-monitor';
14-
import {
15-
CdkTrapFocus,
16-
FocusTrapDeprecatedDirective,
17-
FocusTrapFactory,
18-
} from './focus-trap/focus-trap';
19-
import {InteractivityChecker} from './interactivity-checker/interactivity-checker';
20-
import {LIVE_ANNOUNCER_PROVIDER} from './live-announcer/live-announcer';
12+
import {CdkMonitorFocus} from './focus-monitor/focus-monitor';
13+
import {CdkTrapFocus, FocusTrapDeprecatedDirective} from './focus-trap/focus-trap';
2114

2215
@NgModule({
2316
imports: [CommonModule, PlatformModule],
2417
declarations: [CdkTrapFocus, FocusTrapDeprecatedDirective, CdkMonitorFocus],
2518
exports: [CdkTrapFocus, FocusTrapDeprecatedDirective, CdkMonitorFocus],
26-
providers: [
27-
InteractivityChecker,
28-
FocusTrapFactory,
29-
AriaDescriber,
30-
LIVE_ANNOUNCER_PROVIDER,
31-
ARIA_DESCRIBER_PROVIDER,
32-
FOCUS_MONITOR_PROVIDER,
33-
]
3419
})
3520
export class A11yModule {}

src/cdk/a11y/aria-describer/aria-describer.ts

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

9-
import {Injectable, Inject, InjectionToken, Optional, SkipSelf} from '@angular/core';
109
import {DOCUMENT} from '@angular/common';
10+
import {
11+
APP_ROOT_SCOPE,
12+
Inject,
13+
Injectable,
14+
InjectionToken,
15+
Optional,
16+
SkipSelf,
17+
} from '@angular/core';
1118
import {addAriaReferencedId, getAriaReferenceIds, removeAriaReferencedId} from './aria-reference';
1219

20+
1321
/**
1422
* Interface used to register message elements and keep a count of how many registrations have
1523
* the same message and the reference to the message element used for the `aria-describedby`.
@@ -46,7 +54,7 @@ let messagesContainer: HTMLElement | null = null;
4654
* content.
4755
* @docs-private
4856
*/
49-
@Injectable()
57+
@Injectable({scope: APP_ROOT_SCOPE})
5058
export class AriaDescriber {
5159
private _document: Document;
5260

@@ -204,12 +212,13 @@ export class AriaDescriber {
204212

205213
}
206214

207-
/** @docs-private */
215+
216+
/** @docs-private @deprecated @deletion-target 7.0.0 */
208217
export function ARIA_DESCRIBER_PROVIDER_FACTORY(parentDispatcher: AriaDescriber, _document: any) {
209218
return parentDispatcher || new AriaDescriber(_document);
210219
}
211220

212-
/** @docs-private */
221+
/** @docs-private @deprecated @deletion-target 7.0.0 */
213222
export const ARIA_DESCRIBER_PROVIDER = {
214223
// If there is already an AriaDescriber available, use that. Otherwise, provide a new one.
215224
provide: AriaDescriber,

src/cdk/a11y/focus-monitor/focus-monitor.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
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+
89
import {Platform, supportsPassiveEventListeners} from '@angular/cdk/platform';
910
import {
11+
APP_ROOT_SCOPE,
1012
Directive,
1113
ElementRef,
1214
EventEmitter,
@@ -40,7 +42,7 @@ type MonitoredElementInfo = {
4042

4143

4244
/** Monitors mouse and keyboard events to determine the cause of focus events. */
43-
@Injectable()
45+
@Injectable({scope: APP_ROOT_SCOPE})
4446
export class FocusMonitor implements OnDestroy {
4547
/** The focus origin that the next focus event is a result of. */
4648
private _origin: FocusOrigin = null;
@@ -398,13 +400,13 @@ export class CdkMonitorFocus implements OnDestroy {
398400
}
399401
}
400402

401-
/** @docs-private */
403+
/** @docs-private @deprecated @deletion-target 7.0.0 */
402404
export function FOCUS_MONITOR_PROVIDER_FACTORY(
403405
parentDispatcher: FocusMonitor, ngZone: NgZone, platform: Platform) {
404406
return parentDispatcher || new FocusMonitor(ngZone, platform);
405407
}
406408

407-
/** @docs-private */
409+
/** @docs-private @deprecated @deletion-target 7.0.0 */
408410
export const FOCUS_MONITOR_PROVIDER = {
409411
// If there is already a FocusMonitor available, use that. Otherwise, provide a new one.
410412
provide: FocusMonitor,

src/cdk/a11y/focus-trap/focus-trap.ts

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

9+
import {coerceBooleanProperty} from '@angular/cdk/coercion';
10+
import {DOCUMENT} from '@angular/common';
911
import {
12+
AfterContentInit,
13+
APP_ROOT_SCOPE,
1014
Directive,
1115
ElementRef,
16+
Inject,
17+
Injectable,
1218
Input,
1319
NgZone,
1420
OnDestroy,
15-
AfterContentInit,
16-
Injectable,
17-
Inject,
1821
} from '@angular/core';
19-
import {coerceBooleanProperty} from '@angular/cdk/coercion';
2022
import {take} from 'rxjs/operators/take';
2123
import {InteractivityChecker} from '../interactivity-checker/interactivity-checker';
22-
import {DOCUMENT} from '@angular/common';
2324

2425

2526
/**
@@ -278,7 +279,7 @@ export class FocusTrap {
278279

279280

280281
/** Factory that allows easy instantiation of focus traps. */
281-
@Injectable()
282+
@Injectable({scope: APP_ROOT_SCOPE})
282283
export class FocusTrapFactory {
283284
private _document: Document;
284285

src/cdk/a11y/interactivity-checker/interactivity-checker.ts

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

9-
import {Injectable} from '@angular/core';
109
import {Platform} from '@angular/cdk/platform';
10+
import {APP_ROOT_SCOPE, Injectable} from '@angular/core';
1111

1212

1313
// The InteractivityChecker leans heavily on the ally.js accessibility utilities.
@@ -18,7 +18,7 @@ import {Platform} from '@angular/cdk/platform';
1818
* Utility for checking the interactivity of an element, such as whether is is focusable or
1919
* tabbable.
2020
*/
21-
@Injectable()
21+
@Injectable({scope: APP_ROOT_SCOPE})
2222
export class InteractivityChecker {
2323

2424
constructor(private _platform: Platform) {}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
import {APP_ROOT_SCOPE, InjectionToken} from '@angular/core';
10+
11+
// The token for the live announcer element is defined in a separate file from LiveAnnouncer
12+
// as a workaround for https://github.com/angular/angular/issues/22559
13+
14+
export const LIVE_ANNOUNCER_ELEMENT_TOKEN =
15+
new InjectionToken<HTMLElement | null>('liveAnnouncerElement', {
16+
scope: APP_ROOT_SCOPE,
17+
factory: () => null,
18+
});

src/cdk/a11y/live-announcer/live-announcer.spec.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import {inject, fakeAsync, tick, ComponentFixture, TestBed} from '@angular/core/testing';
21
import {Component} from '@angular/core';
2+
import {ComponentFixture, fakeAsync, inject, TestBed, tick} from '@angular/core/testing';
33
import {By} from '@angular/platform-browser';
4-
import {LiveAnnouncer, LIVE_ANNOUNCER_ELEMENT_TOKEN} from './live-announcer';
54
import {A11yModule} from '../index';
5+
import {LiveAnnouncer} from './live-announcer';
6+
import {LIVE_ANNOUNCER_ELEMENT_TOKEN} from './live-announcer-token';
67

78

89
describe('LiveAnnouncer', () => {

src/cdk/a11y/live-announcer/live-announcer.ts

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

9+
import {DOCUMENT} from '@angular/common';
910
import {
11+
APP_ROOT_SCOPE,
12+
Inject,
1013
Injectable,
11-
InjectionToken,
14+
OnDestroy,
1215
Optional,
13-
Inject,
16+
Provider,
1417
SkipSelf,
15-
OnDestroy,
1618
} from '@angular/core';
17-
import {DOCUMENT} from '@angular/common';
19+
import {LIVE_ANNOUNCER_ELEMENT_TOKEN} from './live-announcer-token';
1820

1921

20-
export const LIVE_ANNOUNCER_ELEMENT_TOKEN = new InjectionToken<HTMLElement>('liveAnnouncerElement');
21-
2222
/** Possible politeness levels. */
2323
export type AriaLivePoliteness = 'off' | 'polite' | 'assertive';
2424

25-
@Injectable()
25+
@Injectable({scope: APP_ROOT_SCOPE})
2626
export class LiveAnnouncer implements OnDestroy {
27-
private _liveElement: Element;
27+
private readonly _liveElement: Element;
2828

2929
constructor(
3030
@Optional() @Inject(LIVE_ANNOUNCER_ELEMENT_TOKEN) elementToken: any,
@@ -81,14 +81,15 @@ export class LiveAnnouncer implements OnDestroy {
8181

8282
}
8383

84-
/** @docs-private */
84+
85+
/** @docs-private @deprecated @deletion-target 7.0.0 */
8586
export function LIVE_ANNOUNCER_PROVIDER_FACTORY(
8687
parentDispatcher: LiveAnnouncer, liveElement: any, _document: any) {
8788
return parentDispatcher || new LiveAnnouncer(liveElement, _document);
8889
}
8990

90-
/** @docs-private */
91-
export const LIVE_ANNOUNCER_PROVIDER = {
91+
/** @docs-private @deprecated @deletion-target 7.0.0 */
92+
export const LIVE_ANNOUNCER_PROVIDER: Provider = {
9293
// If there is already a LiveAnnouncer available, use that. Otherwise, provide a new one.
9394
provide: LiveAnnouncer,
9495
deps: [

src/cdk/a11y/public-api.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export * from './key-manager/list-key-manager';
1515
export * from './focus-trap/focus-trap';
1616
export * from './interactivity-checker/interactivity-checker';
1717
export * from './live-announcer/live-announcer';
18+
export * from './live-announcer/live-announcer-token';
1819
export * from './focus-monitor/focus-monitor';
1920
export * from './fake-mousedown';
2021
export * from './a11y-module';

src/cdk/accordion/accordion-module.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,12 @@
77
*/
88

99
import {NgModule} from '@angular/core';
10-
import {UNIQUE_SELECTION_DISPATCHER_PROVIDER} from '@angular/cdk/collections';
1110
import {CdkAccordion} from './accordion';
1211
import {CdkAccordionItem} from './accordion-item';
1312

13+
1414
@NgModule({
1515
exports: [CdkAccordion, CdkAccordionItem],
1616
declarations: [CdkAccordion, CdkAccordionItem],
17-
providers: [UNIQUE_SELECTION_DISPATCHER_PROVIDER],
1817
})
1918
export class CdkAccordionModule {}

src/cdk/bidi/BUILD.bazel

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package(default_visibility=["//visibility:public"])
22
load("@angular//:index.bzl", "ng_module")
3+
load("@build_bazel_rules_typescript//:defs.bzl", "ts_library", "ts_web_test")
4+
35

46
ng_module(
57
name = "bidi",
@@ -8,3 +10,30 @@ ng_module(
810
deps = ["@rxjs"],
911
tsconfig = ":tsconfig-build.json",
1012
)
13+
14+
ts_library(
15+
name = "bidi_test_sources",
16+
testonly = 1,
17+
srcs = glob(["**/*.spec.ts"]),
18+
deps = [
19+
":bidi",
20+
"@rxjs",
21+
],
22+
tsconfig = ":tsconfig-build.json",
23+
)
24+
25+
ts_web_test(
26+
name = "unit_tests",
27+
bootstrap = [
28+
"//:web_test_bootstrap_scripts",
29+
],
30+
31+
# Do not sort
32+
deps = [
33+
"//:tslib_bundle",
34+
"//:angular_bundles",
35+
"//:angular_test_bundles",
36+
"//test:angular_test_init",
37+
":bidi_test_sources",
38+
],
39+
)

src/cdk/bidi/bidi-module.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,11 @@
77
*/
88

99
import {NgModule} from '@angular/core';
10-
import {DOCUMENT} from '@angular/common';
1110
import {Dir} from './dir';
12-
import {DIR_DOCUMENT, Directionality} from './directionality';
1311

1412

1513
@NgModule({
1614
exports: [Dir],
1715
declarations: [Dir],
18-
providers: [
19-
{provide: DIR_DOCUMENT, useExisting: DOCUMENT},
20-
Directionality,
21-
]
2216
})
2317
export class BidiModule { }

src/cdk/bidi/dir-document-token.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
import {DOCUMENT} from '@angular/common';
10+
import {APP_ROOT_SCOPE, inject, InjectionToken} from '@angular/core';
11+
12+
13+
/**
14+
* Injection token used to inject the document into Directionality.
15+
* This is used so that the value can be faked in tests.
16+
*
17+
* We can't use the real document in tests because changing the real `dir` causes geometry-based
18+
* tests in Safari to fail.
19+
*
20+
* We also can't re-provide the DOCUMENT token from platform-brower because the unit tests
21+
* themselves use things like `querySelector` in test code.
22+
*
23+
* This token is defined in a separate file from Directionality as a workaround for
24+
* https://github.com/angular/angular/issues/22559
25+
*
26+
* @docs-private
27+
*/
28+
export const DIR_DOCUMENT = new InjectionToken<Document>('cdk-dir-doc', {
29+
scope: APP_ROOT_SCOPE,
30+
factory: () => inject(DOCUMENT),
31+
});

src/cdk/bidi/directionality.ts

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

9-
import {
10-
EventEmitter,
11-
Injectable,
12-
Optional,
13-
Inject,
14-
InjectionToken,
15-
} from '@angular/core';
9+
import {APP_ROOT_SCOPE, EventEmitter, Inject, Injectable, Optional} from '@angular/core';
10+
import {DIR_DOCUMENT} from './dir-document-token';
1611

1712

1813
export type Direction = 'ltr' | 'rtl';
1914

20-
/**
21-
* Injection token used to inject the document into Directionality.
22-
* This is used so that the value can be faked in tests.
23-
*
24-
* We can't use the real document in tests because changing the real `dir` causes geometry-based
25-
* tests in Safari to fail.
26-
*
27-
* We also can't re-provide the DOCUMENT token from platform-brower because the unit tests
28-
* themselves use things like `querySelector` in test code.
29-
*/
30-
export const DIR_DOCUMENT = new InjectionToken<Document>('cdk-dir-doc');
3115

3216
/**
3317
* The directionality (LTR / RTL) context for the application (or a subtree of it).
3418
* Exposes the current direction and a stream of direction changes.
3519
*/
36-
@Injectable()
20+
@Injectable({scope: APP_ROOT_SCOPE})
3721
export class Directionality {
3822
/** The current 'ltr' or 'rtl' value. */
3923
readonly value: Direction = 'ltr';

0 commit comments

Comments
 (0)