Skip to content

Commit 9381f90

Browse files
authored
fix(material/menu): remove dependency on NgClass (#28877)
We can set classes directly through `[class]` instead of having to import `NgClass`. I had to adjust the code a bit, because the built-in `[class]` binding doesn't do diffing on the object literal.
1 parent 8ec47aa commit 9381f90

File tree

2 files changed

+19
-13
lines changed

2 files changed

+19
-13
lines changed

src/material/menu/menu.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<div
33
class="mat-mdc-menu-panel mat-mdc-elevation-specific"
44
[id]="panelId"
5-
[ngClass]="_classList"
5+
[class]="_classList"
66
(keydown)="_handleKeydown($event)"
77
(click)="closed.emit('click')"
88
[@transformMenu]="_panelAnimationState"

src/material/menu/menu.ts

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ import {MenuPositionX, MenuPositionY} from './menu-positions';
5151
import {throwMatMenuInvalidPositionX, throwMatMenuInvalidPositionY} from './menu-errors';
5252
import {MatMenuContent, MAT_MENU_CONTENT} from './menu-content';
5353
import {matMenuAnimations} from './menu-animations';
54-
import {NgClass} from '@angular/common';
5554

5655
let menuPanelUid = 0;
5756

@@ -113,7 +112,6 @@ export function MAT_MENU_DEFAULT_OPTIONS_FACTORY(): MatMenuDefaultOptions {
113112
animations: [matMenuAnimations.transformMenu, matMenuAnimations.fadeInItems],
114113
providers: [{provide: MAT_MENU_PANEL, useExisting: MatMenu}],
115114
standalone: true,
116-
imports: [NgClass],
117115
})
118116
export class MatMenu implements AfterContentInit, MatMenuPanel<MatMenuItem>, OnInit, OnDestroy {
119117
private _keyManager: FocusKeyManager<MatMenuItem>;
@@ -130,7 +128,7 @@ export class MatMenu implements AfterContentInit, MatMenuPanel<MatMenuItem>, OnI
130128
/** Only the direct descendant menu items. */
131129
_directDescendantItems = new QueryList<MatMenuItem>();
132130

133-
/** Config object to be passed into the menu's ngClass */
131+
/** Classes to be applied to the menu panel. */
134132
_classList: {[key: string]: boolean} = {};
135133

136134
/** Current state of the panel animation. */
@@ -225,22 +223,25 @@ export class MatMenu implements AfterContentInit, MatMenuPanel<MatMenuItem>, OnI
225223
@Input('class')
226224
set panelClass(classes: string) {
227225
const previousPanelClass = this._previousPanelClass;
226+
const newClassList = {...this._classList};
228227

229228
if (previousPanelClass && previousPanelClass.length) {
230229
previousPanelClass.split(' ').forEach((className: string) => {
231-
this._classList[className] = false;
230+
newClassList[className] = false;
232231
});
233232
}
234233

235234
this._previousPanelClass = classes;
236235

237236
if (classes && classes.length) {
238237
classes.split(' ').forEach((className: string) => {
239-
this._classList[className] = true;
238+
newClassList[className] = true;
240239
});
241240

242241
this._elementRef.nativeElement.className = '';
243242
}
243+
244+
this._classList = newClassList;
244245
}
245246
private _previousPanelClass: string;
246247

@@ -478,12 +479,15 @@ export class MatMenu implements AfterContentInit, MatMenuPanel<MatMenuItem>, OnI
478479
});
479480

480481
if (!customElevation || customElevation === this._previousElevation) {
482+
const newClassList = {...this._classList};
483+
481484
if (this._previousElevation) {
482-
this._classList[this._previousElevation] = false;
485+
newClassList[this._previousElevation] = false;
483486
}
484487

485-
this._classList[newElevation] = true;
488+
newClassList[newElevation] = true;
486489
this._previousElevation = newElevation;
490+
this._classList = newClassList;
487491
}
488492
}
489493

@@ -495,11 +499,13 @@ export class MatMenu implements AfterContentInit, MatMenuPanel<MatMenuItem>, OnI
495499
* @docs-private
496500
*/
497501
setPositionClasses(posX: MenuPositionX = this.xPosition, posY: MenuPositionY = this.yPosition) {
498-
const classes = this._classList;
499-
classes['mat-menu-before'] = posX === 'before';
500-
classes['mat-menu-after'] = posX === 'after';
501-
classes['mat-menu-above'] = posY === 'above';
502-
classes['mat-menu-below'] = posY === 'below';
502+
this._classList = {
503+
...this._classList,
504+
['mat-menu-before']: posX === 'before',
505+
['mat-menu-after']: posX === 'after',
506+
['mat-menu-above']: posY === 'above',
507+
['mat-menu-below']: posY === 'below',
508+
};
503509

504510
// @breaking-change 15.0.0 Remove null check for `_changeDetectorRef`.
505511
this._changeDetectorRef?.markForCheck();

0 commit comments

Comments
 (0)