@@ -21,8 +21,8 @@ import {
2121 OnInit ,
2222 Renderer2 ,
2323 ViewEncapsulation ,
24- HOST_TAG_NAME ,
2524 DOCUMENT ,
25+ AfterViewInit ,
2626} from '@angular/core' ;
2727import { _animationsDisabled , ThemePalette } from '../core' ;
2828import { _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