Skip to content

Commit a3f2ec8

Browse files
authored
fix(material/badge): move warning check later (#32724)
The badge has a check whether it's applied to an `aria-hidden` icon. This check happens very early which can cause false positives. Fixes #27705.
1 parent 06e3d75 commit a3f2ec8

File tree

2 files changed

+26
-19
lines changed

2 files changed

+26
-19
lines changed

goldens/material/badge/index.api.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@
44
55
```ts
66

7+
import { AfterViewInit } from '@angular/core';
78
import * as i0 from '@angular/core';
89
import * as i1 from '@angular/cdk/a11y';
910
import * as i2 from '@angular/cdk/bidi';
1011
import { OnDestroy } from '@angular/core';
1112
import { OnInit } from '@angular/core';
1213

1314
// @public
14-
export class MatBadge implements OnInit, OnDestroy {
15+
export class MatBadge implements OnInit, AfterViewInit, OnDestroy {
1516
constructor(...args: unknown[]);
1617
get color(): ThemePalette;
1718
set color(value: ThemePalette);
@@ -31,6 +32,8 @@ export class MatBadge implements OnInit, OnDestroy {
3132
// (undocumented)
3233
static ngAcceptInputType_overlap: unknown;
3334
// (undocumented)
35+
ngAfterViewInit(): void;
36+
// (undocumented)
3437
ngOnDestroy(): void;
3538
// (undocumented)
3639
ngOnInit(): void;

src/material/badge/badge.ts

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ import {
2121
OnInit,
2222
Renderer2,
2323
ViewEncapsulation,
24-
HOST_TAG_NAME,
2524
DOCUMENT,
25+
AfterViewInit,
2626
} from '@angular/core';
2727
import {_animationsDisabled, ThemePalette} from '../core';
2828
import {_CdkPrivateStyleLoader, _VisuallyHiddenLoader} from '@angular/cdk/private';
@@ -72,7 +72,7 @@ export class _MatBadgeStyleLoader {}
7272
'[class.mat-badge-disabled]': 'disabled',
7373
},
7474
})
75-
export class MatBadge implements OnInit, OnDestroy {
75+
export class MatBadge implements OnInit, AfterViewInit, OnDestroy {
7676
private _ngZone = inject(NgZone);
7777
private _elementRef = inject<ElementRef<HTMLElement>>(ElementRef);
7878
private _ariaDescriber = inject(AriaDescriber);
@@ -162,22 +162,6 @@ export class MatBadge implements OnInit, OnDestroy {
162162
if (nativeElement.nodeType !== nativeElement.ELEMENT_NODE) {
163163
throw Error('matBadge must be attached to an element node.');
164164
}
165-
166-
const tagName = inject(HOST_TAG_NAME);
167-
168-
// Heads-up for developers to avoid putting matBadge on <mat-icon>
169-
// as it is aria-hidden by default docs mention this at:
170-
// https://material.angular.dev/components/badge/overview#accessibility
171-
if (
172-
tagName.toLowerCase() === 'mat-icon' &&
173-
nativeElement.getAttribute('aria-hidden') === 'true'
174-
) {
175-
console.warn(
176-
`Detected a matBadge on an "aria-hidden" "<mat-icon>". ` +
177-
`Consider setting aria-hidden="false" in order to surface the information assistive technology.` +
178-
`\n${nativeElement.outerHTML}`,
179-
);
180-
}
181165
}
182166
}
183167

@@ -213,6 +197,26 @@ export class MatBadge implements OnInit, OnDestroy {
213197
this._isInitialized = true;
214198
}
215199

200+
ngAfterViewInit() {
201+
if (typeof ngDevMode === 'undefined' || ngDevMode) {
202+
const nativeElement = this._elementRef.nativeElement;
203+
204+
// Heads-up for developers to avoid putting matBadge on <mat-icon>
205+
// as it is aria-hidden by default docs mention this at:
206+
// https://material.angular.dev/components/badge/overview#accessibility
207+
if (
208+
nativeElement.tagName.toLowerCase() === 'mat-icon' &&
209+
nativeElement.getAttribute('aria-hidden') === 'true'
210+
) {
211+
console.warn(
212+
`Detected a matBadge on an "aria-hidden" "<mat-icon>". ` +
213+
`Consider setting aria-hidden="false" in order to surface the information assistive technology.` +
214+
`\n${nativeElement.outerHTML}`,
215+
);
216+
}
217+
}
218+
}
219+
216220
ngOnDestroy() {
217221
// ViewEngine only: when creating a badge through the Renderer, Angular remembers its index.
218222
// We have to destroy it ourselves, otherwise it'll be retained in memory.

0 commit comments

Comments
 (0)