Skip to content

Commit

Permalink
fix(material/menu): remove dependency on NgClass (#28877)
Browse files Browse the repository at this point in the history
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.

(cherry picked from commit 9381f90)
  • Loading branch information
crisbeto committed Apr 15, 2024
1 parent 4a56d6a commit 7a085c6
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/material/menu/menu.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<div
class="mat-mdc-menu-panel mat-mdc-elevation-specific"
[id]="panelId"
[ngClass]="_classList"
[class]="_classList"
(keydown)="_handleKeydown($event)"
(click)="closed.emit('click')"
[@transformMenu]="_panelAnimationState"
Expand Down
30 changes: 18 additions & 12 deletions src/material/menu/menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ import {MenuPositionX, MenuPositionY} from './menu-positions';
import {throwMatMenuInvalidPositionX, throwMatMenuInvalidPositionY} from './menu-errors';
import {MatMenuContent, MAT_MENU_CONTENT} from './menu-content';
import {matMenuAnimations} from './menu-animations';
import {NgClass} from '@angular/common';

let menuPanelUid = 0;

Expand Down Expand Up @@ -109,7 +108,6 @@ export function MAT_MENU_DEFAULT_OPTIONS_FACTORY(): MatMenuDefaultOptions {
animations: [matMenuAnimations.transformMenu, matMenuAnimations.fadeInItems],
providers: [{provide: MAT_MENU_PANEL, useExisting: MatMenu}],
standalone: true,
imports: [NgClass],
})
export class MatMenu implements AfterContentInit, MatMenuPanel<MatMenuItem>, OnInit, OnDestroy {
private _keyManager: FocusKeyManager<MatMenuItem>;
Expand All @@ -126,7 +124,7 @@ export class MatMenu implements AfterContentInit, MatMenuPanel<MatMenuItem>, OnI
/** Only the direct descendant menu items. */
_directDescendantItems = new QueryList<MatMenuItem>();

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

/** Current state of the panel animation. */
Expand Down Expand Up @@ -221,22 +219,25 @@ export class MatMenu implements AfterContentInit, MatMenuPanel<MatMenuItem>, OnI
@Input('class')
set panelClass(classes: string) {
const previousPanelClass = this._previousPanelClass;
const newClassList = {...this._classList};

if (previousPanelClass && previousPanelClass.length) {
previousPanelClass.split(' ').forEach((className: string) => {
this._classList[className] = false;
newClassList[className] = false;
});
}

this._previousPanelClass = classes;

if (classes && classes.length) {
classes.split(' ').forEach((className: string) => {
this._classList[className] = true;
newClassList[className] = true;
});

this._elementRef.nativeElement.className = '';
}

this._classList = newClassList;
}
private _previousPanelClass: string;

Expand Down Expand Up @@ -465,12 +466,15 @@ export class MatMenu implements AfterContentInit, MatMenuPanel<MatMenuItem>, OnI
});

if (!customElevation || customElevation === this._previousElevation) {
const newClassList = {...this._classList};

if (this._previousElevation) {
this._classList[this._previousElevation] = false;
newClassList[this._previousElevation] = false;
}

this._classList[newElevation] = true;
newClassList[newElevation] = true;
this._previousElevation = newElevation;
this._classList = newClassList;
}
}

Expand All @@ -482,11 +486,13 @@ export class MatMenu implements AfterContentInit, MatMenuPanel<MatMenuItem>, OnI
* @docs-private
*/
setPositionClasses(posX: MenuPositionX = this.xPosition, posY: MenuPositionY = this.yPosition) {
const classes = this._classList;
classes['mat-menu-before'] = posX === 'before';
classes['mat-menu-after'] = posX === 'after';
classes['mat-menu-above'] = posY === 'above';
classes['mat-menu-below'] = posY === 'below';
this._classList = {
...this._classList,
['mat-menu-before']: posX === 'before',
['mat-menu-after']: posX === 'after',
['mat-menu-above']: posY === 'above',
['mat-menu-below']: posY === 'below',
};

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

0 comments on commit 7a085c6

Please sign in to comment.