Skip to content

Commit

Permalink
fix(menu): remove classes from inert element (#4800)
Browse files Browse the repository at this point in the history
Removes the classes from the inert `md-menu` element, after they're transferred to the menu panel.

Fixes #4484.
  • Loading branch information
crisbeto authored and andrewseguin committed Jun 8, 2017
1 parent 8d9bbbf commit 93a21c7
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
17 changes: 12 additions & 5 deletions src/lib/menu/menu-directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
TemplateRef,
ViewChild,
ViewEncapsulation,
ElementRef,
} from '@angular/core';
import {MenuPositionX, MenuPositionY} from './menu-positions';
import {throwMdMenuInvalidPositionX, throwMdMenuInvalidPositionY} from './menu-errors';
Expand Down Expand Up @@ -85,16 +86,22 @@ export class MdMenu implements AfterContentInit, MdMenuPanel, OnDestroy {
*/
@Input('class')
set classList(classes: string) {
this._classList = classes.split(' ').reduce((obj: any, className: string) => {
obj[className] = true;
return obj;
}, {});
this.setPositionClasses();
if (classes && classes.length) {
this._classList = classes.split(' ').reduce((obj: any, className: string) => {
obj[className] = true;
return obj;
}, {});

this._elementRef.nativeElement.className = '';
this.setPositionClasses();
}
}

/** Event emitted when the menu is closed. */
@Output() close = new EventEmitter<void>();

constructor(private _elementRef: ElementRef) { }

ngAfterContentInit() {
this._keyManager = new FocusKeyManager(this.items).withWrap();
this._tabSubscription = this._keyManager.tabOut.subscribe(() => this._emitCloseEvent());
Expand Down
18 changes: 17 additions & 1 deletion src/lib/menu/menu.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,22 @@ describe('MdMenu', () => {
expect(overlayPane.getAttribute('dir')).toEqual('rtl');
});

it('should transfer any custom classes from the host to the overlay', () => {
const fixture = TestBed.createComponent(SimpleMenu);

fixture.detectChanges();
fixture.componentInstance.trigger.openMenu();

const menuEl = fixture.debugElement.query(By.css('md-menu')).nativeElement;
const panel = overlayContainerElement.querySelector('.mat-menu-panel');

expect(menuEl.classList).not.toContain('custom-one');
expect(menuEl.classList).not.toContain('custom-two');

expect(panel.classList).toContain('custom-one');
expect(panel.classList).toContain('custom-two');
});

describe('positions', () => {
let fixture: ComponentFixture<PositionedMenu>;
let panel: HTMLElement;
Expand Down Expand Up @@ -462,7 +478,7 @@ describe('MdMenu', () => {
@Component({
template: `
<button [mdMenuTriggerFor]="menu" #triggerEl>Toggle menu</button>
<md-menu #menu="mdMenu" (close)="closeCallback()">
<md-menu class="custom-one custom-two" #menu="mdMenu" (close)="closeCallback()">
<button md-menu-item> Item </button>
<button md-menu-item disabled> Disabled </button>
</md-menu>
Expand Down

0 comments on commit 93a21c7

Please sign in to comment.