Skip to content

Commit 65c7cad

Browse files
authored
fix(material/badge): warn if use with mat-icon (#27368)
* fix(material/badge): warn if use with mat-icon prior this commit there was no warning for matBadge being use with <mat-icon> as it is aria-hidden by default which means badge content is invisible to screen readers fixes #27035 * fixup! fix(material/badge): warn if use with mat-icon
1 parent e0608cb commit 65c7cad

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

src/material/badge/badge.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,22 @@ export class MatBadge extends _MatBadgeBase implements OnInit, OnDestroy, CanDis
156156
if (nativeElement.nodeType !== nativeElement.ELEMENT_NODE) {
157157
throw Error('matBadge must be attached to an element node.');
158158
}
159+
160+
const matIconTagName: string = 'mat-icon';
161+
162+
// Heads-up for developers to avoid putting matBadge on <mat-icon>
163+
// as it is aria-hidden by default docs mention this at:
164+
// https://material.angular.io/components/badge/overview#accessibility
165+
if (
166+
nativeElement.tagName.toLowerCase() === matIconTagName &&
167+
nativeElement.getAttribute('aria-hidden') === 'true'
168+
) {
169+
console.warn(
170+
`Detected a matBadge on an "aria-hidden" "<mat-icon>". ` +
171+
`Consider setting aria-hidden="false" in order to surface the information assistive technology.` +
172+
`\n${nativeElement.outerHTML}`,
173+
);
174+
}
159175
}
160176
}
161177

0 commit comments

Comments
 (0)