-
Notifications
You must be signed in to change notification settings - Fork 6.8k
feat(menu): add menu trigger support #867
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
describe('menu', function () { | ||
beforeEach(function() { | ||
browser.get('/menu'); | ||
}); | ||
|
||
it('should open menu when the trigger is clicked', function () { | ||
expectMenuPresent(false); | ||
element(by.id('trigger')).click(); | ||
|
||
expectMenuPresent(true); | ||
expect(element(by.css('.md-menu')).getText()).toEqual("One\nTwo\nThree"); | ||
}); | ||
|
||
it('should align menu when open', function() { | ||
element(by.id('trigger')).click(); | ||
expectMenuAlignedWith('trigger'); | ||
}); | ||
|
||
it('should close menu when area outside menu is clicked', function () { | ||
element(by.id('trigger')).click(); | ||
element(by.tagName('body')).click(); | ||
expectMenuPresent(false); | ||
}); | ||
|
||
it('should close menu when menu item is clicked', function () { | ||
element(by.id('trigger')).click(); | ||
element(by.id('one')).click(); | ||
expectMenuPresent(false); | ||
}); | ||
|
||
it('should run click handlers on regular menu items', function() { | ||
element(by.id('trigger')).click(); | ||
element(by.id('one')).click(); | ||
expect(element(by.id('text')).getText()).toEqual('one'); | ||
|
||
element(by.id('trigger')).click(); | ||
element(by.id('two')).click(); | ||
expect(element(by.id('text')).getText()).toEqual('two'); | ||
}); | ||
|
||
it('should run not run click handlers on disabled menu items', function() { | ||
element(by.id('trigger')).click(); | ||
element(by.id('three')).click(); | ||
expect(element(by.id('text')).getText()).toEqual(''); | ||
}); | ||
|
||
it('should support multiple triggers opening the same menu', function() { | ||
element(by.id('trigger-two')).click(); | ||
expect(element(by.css('.md-menu')).getText()).toEqual("One\nTwo\nThree"); | ||
expectMenuAlignedWith('trigger-two'); | ||
|
||
element(by.tagName('body')).click(); | ||
expectMenuPresent(false); | ||
|
||
element(by.id('trigger')).click(); | ||
expect(element(by.css('.md-menu')).getText()).toEqual("One\nTwo\nThree"); | ||
expectMenuAlignedWith('trigger'); | ||
|
||
element(by.tagName('body')).click(); | ||
expectMenuPresent(false); | ||
}); | ||
|
||
function expectMenuPresent(bool: boolean) { | ||
return browser.isElementPresent(by.css('.md-menu')).then((isPresent) => { | ||
expect(isPresent).toBe(bool); | ||
}); | ||
} | ||
|
||
function expectMenuAlignedWith(id: string) { | ||
element(by.id(id)).getLocation().then((loc) => { | ||
expectMenuLocation({x: loc.x, y: loc.y}); | ||
}); | ||
} | ||
|
||
function expectMenuLocation({x,y}: {x: number, y: number}) { | ||
element(by.css('.md-menu')).getLocation().then((loc) => { | ||
expect(loc.x).toEqual(x); | ||
expect(loc.y).toEqual(y); | ||
}); | ||
} | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import {MdError} from '@angular2-material/core/errors/error'; | ||
|
||
/** | ||
* Exception thrown when menu trigger doesn't have a valid md-menu instance | ||
*/ | ||
export class MdMenuMissingError extends MdError { | ||
constructor() { | ||
super(`md-menu-trigger: must pass in an md-menu instance. | ||
|
||
Example: | ||
<md-menu #menu="mdMenu"></md-menu> | ||
<button [md-menu-trigger-for]="menu"></button> | ||
`); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import {Directive, Input, HostBinding, HostListener} from '@angular/core'; | ||
|
||
/** | ||
* This directive is intended to be used inside an md-menu tag. | ||
* It exists mostly to set the role attribute. | ||
*/ | ||
@Directive({ | ||
selector: 'button[md-menu-item]', | ||
host: {'role': 'menuitem'} | ||
}) | ||
export class MdMenuItem {} | ||
|
||
/** | ||
* This directive is intended to be used inside an md-menu tag. | ||
* It sets the role attribute and adds support for the disabled property to anchors. | ||
*/ | ||
@Directive({ | ||
selector: 'a[md-menu-item]', | ||
host: {'role': 'menuitem'} | ||
}) | ||
export class MdMenuAnchor { | ||
_disabled: boolean; | ||
|
||
@HostBinding('attr.disabled') | ||
@Input() | ||
get disabled(): boolean { | ||
return this._disabled; | ||
} | ||
|
||
set disabled(value: boolean) { | ||
this._disabled = (value === false || value === undefined) ? null : true; | ||
} | ||
|
||
@HostBinding('attr.aria-disabled') | ||
get isAriaDisabled(): string { | ||
return this.disabled ? 'true' : 'false'; | ||
} | ||
|
||
@HostBinding('tabIndex') | ||
get tabIndex(): number { | ||
return this.disabled ? -1 : 0; | ||
} | ||
|
||
@HostListener('click', ['$event']) | ||
checkDisabled(event: any) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use the |
||
if (this.disabled) { | ||
event.preventDefault(); | ||
event.stopPropagation(); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
import { | ||
Directive, | ||
ElementRef, | ||
Input, | ||
Output, | ||
EventEmitter, | ||
HostListener, | ||
ViewContainerRef, | ||
AfterViewInit, | ||
OnDestroy | ||
} from '@angular/core'; | ||
import {MdMenu} from './menu'; | ||
import {MdMenuItem, MdMenuAnchor} from './menu-item'; | ||
import {MdMenuMissingError} from './menu-errors'; | ||
import { | ||
Overlay, | ||
OverlayState, | ||
OverlayRef, | ||
OVERLAY_PROVIDERS, | ||
TemplatePortal | ||
} from '@angular2-material/core/core'; | ||
import { | ||
ConnectedPositionStrategy | ||
} from '@angular2-material/core/overlay/position/connected-position-strategy'; | ||
|
||
/** | ||
* This directive is intended to be used in conjunction with an md-menu tag. It is | ||
* responsible for toggling the display of the provided menu instance. | ||
*/ | ||
@Directive({ | ||
selector: '[md-menu-trigger-for]', | ||
host: {'aria-haspopup': 'true'}, | ||
providers: [OVERLAY_PROVIDERS], | ||
exportAs: 'mdMenuTrigger' | ||
}) | ||
export class MdMenuTrigger implements AfterViewInit, OnDestroy { | ||
private _portal: TemplatePortal; | ||
private _overlay: OverlayRef; | ||
menuOpen: boolean = false; | ||
|
||
@Input('md-menu-trigger-for') menu: MdMenu; | ||
@Output() onMenuOpen = new EventEmitter(); | ||
@Output() onMenuClose = new EventEmitter(); | ||
|
||
constructor(private _overlayBuilder: Overlay, private _element: ElementRef, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd just call it |
||
private _viewContainerRef: ViewContainerRef) {} | ||
|
||
ngAfterViewInit() { | ||
this._checkMenu(); | ||
this._createOverlay(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I just realized that it might be a problem in Angular Universal if we try to create the overlay on init rather than once a user interaction occurs, since under the hood the overlay creates an element with browser APIs. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, good point. I'll check it out. |
||
this.menu.close.subscribe(() => this.closeMenu()); | ||
} | ||
|
||
ngOnDestroy() { this.destroyMenu(); } | ||
|
||
@HostListener('click') | ||
toggleMenu(): Promise<void> { | ||
return this.menuOpen ? this.closeMenu() : this.openMenu(); | ||
} | ||
|
||
openMenu(): Promise<void> { | ||
return this._overlay.attach(this._portal) | ||
.then(() => this._setMenuState(true)); | ||
} | ||
|
||
closeMenu(): Promise<void> { | ||
return this._overlay.detach() | ||
.then(() => this._setMenuState(false)); | ||
} | ||
|
||
destroyMenu(): void { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this supposed to be part of the public api? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, this was in the doc. Do you think it's unnecessary? I'm on the fence about it. |
||
this._overlay.dispose(); | ||
} | ||
|
||
// set state rather than toggle to support triggers sharing a menu | ||
private _setMenuState(bool: boolean): void { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Also, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fair :) Changing |
||
this.menuOpen = bool; | ||
this.menu._setClickCatcher(bool); | ||
this.menuOpen ? this.onMenuOpen.emit(null) : this.onMenuClose.emit(null); | ||
} | ||
|
||
private _checkMenu() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add function description for these last four functions (I think the rest are pretty obvious) |
||
if (!this.menu || !(this.menu instanceof MdMenu)) { | ||
throw new MdMenuMissingError(); | ||
} | ||
} | ||
|
||
private _createOverlay(): void { | ||
this._portal = new TemplatePortal(this.menu.templateRef, this._viewContainerRef); | ||
this._overlayBuilder.create(this._getOverlayConfig()) | ||
.then(overlay => this._overlay = overlay); | ||
} | ||
|
||
private _getOverlayConfig(): OverlayState { | ||
const overlayState = new OverlayState(); | ||
overlayState.positionStrategy = this._getPosition(); | ||
return overlayState; | ||
} | ||
|
||
private _getPosition(): ConnectedPositionStrategy { | ||
return this._overlayBuilder.position().connectedTo( | ||
this._element, | ||
{originX: 'start', originY: 'top'}, | ||
{overlayX: 'start', overlayY: 'top'} | ||
); | ||
} | ||
} | ||
|
||
export const MD_MENU_DIRECTIVES = [MdMenu, MdMenuItem, MdMenuTrigger, MdMenuAnchor]; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
<div class="md-menu"> | ||
<ng-content></ng-content> | ||
</div> | ||
|
||
<template> | ||
<div class="md-menu" (click)="_emitCloseEvent()"> | ||
<ng-content></ng-content> | ||
</div> | ||
</template> | ||
<div class="md-menu-click-catcher" *ngIf="_showClickCatcher" (click)="_emitCloseEvent()"></div> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,15 @@ | ||
import {Component, Directive, ViewEncapsulation} from '@angular/core'; | ||
// TODO(kara): keyboard events for menu navigation | ||
// TODO(kara): prevent-close functionality | ||
// TODO(kara): set position of menu | ||
|
||
import { | ||
Component, | ||
ViewEncapsulation, | ||
Output, | ||
ViewChild, | ||
TemplateRef, | ||
EventEmitter | ||
} from '@angular/core'; | ||
|
||
@Component({ | ||
moduleId: module.id, | ||
|
@@ -9,13 +20,23 @@ import {Component, Directive, ViewEncapsulation} from '@angular/core'; | |
encapsulation: ViewEncapsulation.None, | ||
exportAs: 'mdMenu' | ||
}) | ||
export class MdMenu {} | ||
export class MdMenu { | ||
private _showClickCatcher: boolean = false; | ||
|
||
@Directive({ | ||
selector: '[md-menu-item]', | ||
host: {'role': 'menuitem'} | ||
}) | ||
export class MdMenuItem {} | ||
@Output() close = new EventEmitter; | ||
@ViewChild(TemplateRef) templateRef: TemplateRef<any>; | ||
|
||
/** | ||
* This function toggles the display of the menu's click catcher element. | ||
* This element covers the viewport when the menu is open to detect clicks outside the menu. | ||
* @internal | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't use |
||
*/ | ||
_setClickCatcher(bool: boolean): void { | ||
this._showClickCatcher = bool; | ||
} | ||
|
||
export const MD_MENU_DIRECTIVES = [MdMenu, MdMenuItem]; | ||
private _emitCloseEvent(): void { | ||
this.close.emit(null); | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just realized we could do this as
String(this.disabled)
, which is slightly shorter.