Skip to content

Commit 9323bf4

Browse files
committed
refactor(material/badge): use common service for loading structural styles
Switches the badge to use the common style loading service to load its structural styles.
1 parent da65ef2 commit 9323bf4

File tree

2 files changed

+5
-25
lines changed

2 files changed

+5
-25
lines changed

src/material/badge/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ ng_module(
2222
deps = [
2323
"//src:dev_mode_types",
2424
"//src/cdk/a11y",
25+
"//src/cdk/private",
2526
"//src/material/core",
2627
"@npm//@angular/animations",
2728
"@npm//@angular/common",

src/material/badge/badge.ts

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,11 @@
99
import {AriaDescriber, InteractivityChecker} from '@angular/cdk/a11y';
1010
import {DOCUMENT} from '@angular/common';
1111
import {
12-
ApplicationRef,
1312
booleanAttribute,
1413
ChangeDetectionStrategy,
1514
Component,
16-
createComponent,
1715
Directive,
1816
ElementRef,
19-
EnvironmentInjector,
2017
inject,
2118
Inject,
2219
Input,
@@ -29,6 +26,7 @@ import {
2926
ANIMATION_MODULE_TYPE,
3027
} from '@angular/core';
3128
import {ThemePalette} from '@angular/material/core';
29+
import {_CdkPrivateStyleLoader} from '@angular/cdk/private';
3230

3331
let nextId = 0;
3432

@@ -48,9 +46,6 @@ export type MatBadgeSize = 'small' | 'medium' | 'large';
4846

4947
const BADGE_CONTENT_CLASS = 'mat-badge-content';
5048

51-
/** Keeps track of the apps currently containing badges. */
52-
const badgeApps = new Set<ApplicationRef>();
53-
5449
/**
5550
* Component used to load the structural styles of the badge.
5651
* @docs-private
@@ -162,36 +157,20 @@ export class MatBadge implements OnInit, OnDestroy {
162157
private _renderer: Renderer2,
163158
@Optional() @Inject(ANIMATION_MODULE_TYPE) private _animationMode?: string,
164159
) {
165-
const appRef = inject(ApplicationRef);
166-
167-
if (!badgeApps.has(appRef)) {
168-
badgeApps.add(appRef);
169-
170-
const componentRef = createComponent(_MatBadgeStyleLoader, {
171-
environmentInjector: inject(EnvironmentInjector),
172-
});
173-
174-
appRef.onDestroy(() => {
175-
badgeApps.delete(appRef);
176-
if (badgeApps.size === 0) {
177-
componentRef.destroy();
178-
}
179-
});
180-
}
160+
const styleLoader = inject(_CdkPrivateStyleLoader);
161+
styleLoader.load(_MatBadgeStyleLoader);
181162

182163
if (typeof ngDevMode === 'undefined' || ngDevMode) {
183164
const nativeElement = _elementRef.nativeElement;
184165
if (nativeElement.nodeType !== nativeElement.ELEMENT_NODE) {
185166
throw Error('matBadge must be attached to an element node.');
186167
}
187168

188-
const matIconTagName: string = 'mat-icon';
189-
190169
// Heads-up for developers to avoid putting matBadge on <mat-icon>
191170
// as it is aria-hidden by default docs mention this at:
192171
// https://material.angular.io/components/badge/overview#accessibility
193172
if (
194-
nativeElement.tagName.toLowerCase() === matIconTagName &&
173+
nativeElement.tagName.toLowerCase() === 'mat-icon' &&
195174
nativeElement.getAttribute('aria-hidden') === 'true'
196175
) {
197176
console.warn(

0 commit comments

Comments
 (0)