Skip to content

Commit e594d40

Browse files
docs(cdk): use inject() and remove unnecessary module references (#32830)
Update CDK documentation to use the inject() function instead of constructor injection, and remove misleading module import requirements for services that are providedIn: 'root' (LiveAnnouncer, Clipboard, MediaMatcher, BreakpointObserver). Also replace A11yModule references with direct directive import guidance for CdkTrapFocus.
1 parent 50403ce commit e594d40

File tree

7 files changed

+32
-30
lines changed

7 files changed

+32
-30
lines changed

src/cdk/a11y/a11y.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ be used to create accessible experience for components like
8787
[modal dialogs](https://www.w3.org/WAI/ARIA/apg/patterns/dialog-modal/), where focus must be
8888
constrained.
8989

90-
This directive is declared in `A11yModule`.
90+
Import `CdkTrapFocus` in your component to use this directive.
9191

9292
#### Example
9393

@@ -139,10 +139,11 @@ for more information on aria-live regions.
139139
```ts
140140
@Component({...})
141141
export class MyComponent {
142+
private liveAnnouncer = inject(LiveAnnouncer);
142143

143-
constructor(liveAnnouncer: LiveAnnouncer) {
144-
liveAnnouncer.announce("Hey Google");
145-
}
144+
announceMessage() {
145+
this.liveAnnouncer.announce("Hey Google");
146+
}
146147
}
147148
```
148149

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ be used to create accessible experience for components like
55
[modal dialogs](https://www.w3.org/WAI/ARIA/apg/patterns/dialog-modal/), where focus must be
66
constrained.
77

8-
This directive is declared in `A11yModule`.
8+
Import `CdkTrapFocus` in your component to use this directive.
99

1010
#### Example
1111

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,16 @@ Announce the given message via aria-live region. The politeness argument determi
1010
`aria-live` attribute on the announcer element, defaulting to 'polite'.
1111

1212
#### Examples
13-
The LiveAnnouncer is injected into a component:
13+
The `LiveAnnouncer` is injected into a component:
1414
```ts
1515
@Component({
16-
selector: 'my-component'
17-
providers: [LiveAnnouncer]
16+
selector: 'my-component',
1817
})
1918
export class MyComponent {
19+
private liveAnnouncer = inject(LiveAnnouncer);
2020

21-
constructor(liveAnnouncer: LiveAnnouncer) {
22-
liveAnnouncer.announce("Hey Google");
21+
announceMessage() {
22+
this.liveAnnouncer.announce("Hey Google");
2323
}
2424
}
2525
```

src/cdk/clipboard/clipboard.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ directly to place it on the clipboard.
2121
import {Clipboard} from '@angular/cdk/clipboard';
2222

2323
class HeroProfile {
24-
constructor(private clipboard: Clipboard) {}
24+
private clipboard = inject(Clipboard);
2525

2626
copyHeroName() {
2727
this.clipboard.copy('Alphonso');
@@ -39,9 +39,9 @@ the text that was buffered. Please note, if you call `beginCopy`, you must clean
3939
import {Clipboard} from '@angular/cdk/clipboard';
4040

4141
class HeroProfile {
42-
lifetimeAchievements: string;
42+
private clipboard = inject(Clipboard);
4343

44-
constructor(private clipboard: Clipboard) {}
44+
lifetimeAchievements: string;
4545

4646
copyAchievements() {
4747
const pending = this.clipboard.beginCopy(this.lifetimeAchievements);

src/cdk/layout/breakpoints-observer.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
### BreakpointsModule
1+
### BreakpointObserver
22

3-
When including the CDK's `LayoutModule`, components can inject `BreakpointsObserver` to request
4-
the matching state of a CSS Media Query.
3+
`BreakpointObserver` is an injectable service that lets you evaluate media queries to determine
4+
the current screen size and react to changes when the viewport size crosses a breakpoint.
55

66
A set of breakpoints is provided based on the Material Design
77
[breakpoint system](https://material.io/guidelines/layout/responsive-ui.html#responsive-ui-breakpoints).
@@ -10,10 +10,10 @@ A set of breakpoints is provided based on the Material Design
1010
```ts
1111
@Component({ ... })
1212
export class MyWidget {
13-
isHandset: Observable<BreakpointState>;
13+
private breakpointObserver = inject(BreakpointObserver);
1414

15-
constructor(bm: BreakpointObserver) {
16-
bm.observe(Handset).subscribe((state: BreakpointState) => {
15+
constructor() {
16+
this.breakpointObserver.observe(Handset).subscribe((state: BreakpointState) => {
1717
if (state.matches) {
1818
this.makeEverythingFitOnSmallScreen();
1919
} else {

src/cdk/layout/layout.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,7 @@ The `matchMedia` method can be used to get a native
7979
```ts
8080
@Component({...})
8181
class MyComponent {
82-
constructor(mediaMatcher: MediaMatcher) {
83-
const mediaQueryList = mediaMatcher.matchMedia('(min-width: 1px)');
84-
}
82+
private mediaMatcher = inject(MediaMatcher);
83+
private mediaQueryList = this.mediaMatcher.matchMedia('(min-width: 1px)');
8584
}
8685
```

src/cdk/layout/media-matcher.md

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
### MediaMatcher
2-
3-
When including the CDK's `LayoutModule`, components can inject `MediaMatcher` to access the
4-
matchMedia method, if available on the platform.
2+
3+
`MediaMatcher` is an injectable service that provides access to the `matchMedia` method, if
4+
available on the platform.
55

66
#### Example
77
```ts
8-
@Component({ ... })
9-
export class MyWidget {
10-
constructor(mm: MediaMatcher) {
11-
mm.matchMedia('(orientation: landscape)').matches ?
8+
@Component({ ... })
9+
export class MyWidget {
10+
private mediaMatcher = inject(MediaMatcher);
11+
12+
checkOrientation() {
13+
this.mediaMatcher.matchMedia('(orientation: landscape)').matches ?
1214
this.setLandscapeMode() :
1315
this.setPortraitMode();
1416
}
15-
}
17+
}
1618
```
1719

0 commit comments

Comments
 (0)