Skip to content

Commit 27a66c9

Browse files
committed
refactor(material/badge): use ID generator
Switches to using the ID generator service to create unique IDs.
1 parent ef5fc3f commit 27a66c9

File tree

2 files changed

+3
-8
lines changed

2 files changed

+3
-8
lines changed

src/material/badge/badge.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* found in the LICENSE file at https://angular.dev/license
77
*/
88

9-
import {AriaDescriber, InteractivityChecker} from '@angular/cdk/a11y';
9+
import {_IdGenerator, AriaDescriber, InteractivityChecker} from '@angular/cdk/a11y';
1010
import {DOCUMENT} from '@angular/common';
1111
import {
1212
booleanAttribute,
@@ -26,8 +26,6 @@ import {
2626
import {ThemePalette} from '@angular/material/core';
2727
import {_CdkPrivateStyleLoader, _VisuallyHiddenLoader} from '@angular/cdk/private';
2828

29-
let nextId = 0;
30-
3129
/** Allowed position options for matBadgePosition */
3230
export type MatBadgePosition =
3331
| 'above after'
@@ -79,6 +77,7 @@ export class MatBadge implements OnInit, OnDestroy {
7977
private _ariaDescriber = inject(AriaDescriber);
8078
private _renderer = inject(Renderer2);
8179
private _animationMode = inject(ANIMATION_MODULE_TYPE, {optional: true});
80+
private _idGenerator = inject(_IdGenerator);
8281

8382
/**
8483
* Theme color of the badge. This API is supported in M2 themes only, it
@@ -135,9 +134,6 @@ export class MatBadge implements OnInit, OnDestroy {
135134
/** Whether the badge is hidden. */
136135
@Input({alias: 'matBadgeHidden', transform: booleanAttribute}) hidden: boolean;
137136

138-
/** Unique id for the badge */
139-
_id: number = nextId++;
140-
141137
/** Visible badge element. */
142138
private _badgeElement: HTMLElement | undefined;
143139

@@ -236,7 +232,7 @@ export class MatBadge implements OnInit, OnDestroy {
236232
const badgeElement = this._renderer.createElement('span');
237233
const activeClass = 'mat-badge-active';
238234

239-
badgeElement.setAttribute('id', `mat-badge-content-${this._id}`);
235+
badgeElement.setAttribute('id', this._idGenerator.getId('mat-badge-content-'));
240236

241237
// The badge is aria-hidden because we don't want it to appear in the page's navigation
242238
// flow. Instead, we use the badge to describe the decorated element with aria-describedby.

tools/public_api_guard/material/badge.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ export class MatBadge implements OnInit, OnDestroy {
2323
disabled: boolean;
2424
getBadgeElement(): HTMLElement | undefined;
2525
hidden: boolean;
26-
_id: number;
2726
isAbove(): boolean;
2827
isAfter(): boolean;
2928
// (undocumented)

0 commit comments

Comments
 (0)