Skip to content

Commit 89ed2ef

Browse files
committed
refactor(cdk/menu): use ID generator
Switches to using the ID generator service to create unique IDs.
1 parent 55268a2 commit 89ed2ef

File tree

3 files changed

+8
-15
lines changed

3 files changed

+8
-15
lines changed

src/cdk/menu/menu-base.ts

Lines changed: 2 additions & 5 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 {FocusKeyManager, FocusOrigin} from '@angular/cdk/a11y';
9+
import {_IdGenerator, FocusKeyManager, FocusOrigin} from '@angular/cdk/a11y';
1010
import {Directionality} from '@angular/cdk/bidi';
1111
import {
1212
AfterContentInit,
@@ -30,9 +30,6 @@ import {CdkMenuItem} from './menu-item';
3030
import {MENU_STACK, MenuStack, MenuStackItem} from './menu-stack';
3131
import {PointerFocusTracker} from './pointer-focus-tracker';
3232

33-
/** Counter used to create unique IDs for menus. */
34-
let nextId = 0;
35-
3633
/**
3734
* Abstract directive that implements shared logic common to all menus.
3835
* This class can be extended to create custom menu types.
@@ -70,7 +67,7 @@ export abstract class CdkMenuBase
7067
protected readonly dir = inject(Directionality, {optional: true});
7168

7269
/** The id of the menu's host element. */
73-
@Input() id = `cdk-menu-${nextId++}`;
70+
@Input() id: string = inject(_IdGenerator).getId('cdk-menu-');
7471

7572
/** All child MenuItem elements nested in this Menu. */
7673
@ContentChildren(CdkMenuItem, {descendants: true})

src/cdk/menu/menu-item-radio.ts

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

9-
import {UniqueSelectionDispatcher} from '@angular/cdk/collections';
109
import {Directive, inject, OnDestroy} from '@angular/core';
10+
import {UniqueSelectionDispatcher} from '@angular/cdk/collections';
11+
import {_IdGenerator} from '@angular/cdk/a11y';
1112
import {CdkMenuItemSelectable} from './menu-item-selectable';
1213
import {CdkMenuItem} from './menu-item';
1314

14-
/** Counter used to set a unique id and name for a selectable item */
15-
let nextId = 0;
16-
1715
/**
1816
* A directive providing behavior for the "menuitemradio" ARIA role, which behaves similarly to
1917
* a conventional radio-button. Any sibling `CdkMenuItemRadio` instances within the same `CdkMenu`
@@ -36,7 +34,7 @@ export class CdkMenuItemRadio extends CdkMenuItemSelectable implements OnDestroy
3634
private readonly _selectionDispatcher = inject(UniqueSelectionDispatcher);
3735

3836
/** An ID to identify this radio item to the `UniqueSelectionDispatcher`. */
39-
private _id = `${nextId++}`;
37+
private _id = inject(_IdGenerator).getId('cdk-menu-item-radio-');
4038

4139
/** Function to unregister the selection dispatcher */
4240
private _removeDispatcherListener: () => void;

src/cdk/menu/menu-stack.ts

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

9-
import {Inject, Injectable, InjectionToken, Optional, SkipSelf} from '@angular/core';
9+
import {inject, Inject, Injectable, InjectionToken, Optional, SkipSelf} from '@angular/core';
10+
import {_IdGenerator} from '@angular/cdk/a11y';
1011
import {Observable, Subject} from 'rxjs';
1112
import {debounceTime, distinctUntilChanged, startWith} from 'rxjs/operators';
1213

@@ -58,9 +59,6 @@ export interface MenuStackCloseEvent {
5859
focusParentTrigger?: boolean;
5960
}
6061

61-
/** The next available menu stack ID. */
62-
let nextId = 0;
63-
6462
/**
6563
* MenuStack allows subscribers to listen for close events (when a MenuStackItem is popped off
6664
* of the stack) in order to perform closing actions. Upon the MenuStack being empty it emits
@@ -70,7 +68,7 @@ let nextId = 0;
7068
@Injectable()
7169
export class MenuStack {
7270
/** The ID of this menu stack. */
73-
readonly id = `${nextId++}`;
71+
readonly id = inject(_IdGenerator).getId('cdk-menu-stack-');
7472

7573
/** All MenuStackItems tracked by this MenuStack. */
7674
private readonly _elements: MenuStackItem[] = [];

0 commit comments

Comments
 (0)