Skip to content

Commit 88c9bf9

Browse files
committed
feat(menu): add animations and ripple
Closes #1671
1 parent ad3100e commit 88c9bf9

File tree

10 files changed

+134
-22
lines changed

10 files changed

+134
-22
lines changed

src/lib/core/style/_menu-common.scss

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ $md-menu-overlay-max-width: 280px !default; // 56 * 5
1111
$md-menu-item-height: 48px !default;
1212
$md-menu-font-size: 16px !default;
1313
$md-menu-side-padding: 16px !default;
14-
$md-menu-vertical-padding: 8px !default;
1514

1615
@mixin md-menu-base() {
1716
@include md-elevation(2);
@@ -20,9 +19,6 @@ $md-menu-vertical-padding: 8px !default;
2019

2120
overflow: auto;
2221
-webkit-overflow-scrolling: touch; // for momentum scroll on mobile
23-
24-
padding-top: $md-menu-vertical-padding;
25-
padding-bottom: $md-menu-vertical-padding;
2622
}
2723

2824
@mixin md-menu-item-base() {

src/lib/menu/_menu-theme.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
$background: map-get($theme, background);
77
$foreground: map-get($theme, foreground);
88

9-
.md-menu-panel {
9+
.md-menu-content {
1010
background: md-color($background, 'card');
1111
}
1212

src/lib/menu/menu-animations.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import{
2+
AnimationEntryMetadata,
3+
trigger,
4+
state,
5+
style,
6+
animate,
7+
transition
8+
} from '@angular/core';
9+
10+
/**
11+
* Below are all the animations for the md-menu component.
12+
* Animation duration and timing values are based on Material 1.
13+
*/
14+
15+
16+
/**
17+
* This animation controls the menu panel's entry and exit from the page.
18+
*
19+
* When the menu panel is added to the DOM, it scales in and fades in its border.
20+
*
21+
* When the menu panel is removed from the DOM, it simply fades out after a brief
22+
* delay to display the ripple.
23+
*/
24+
export const transformMenu: AnimationEntryMetadata = trigger('transformMenu', [
25+
state('showing', style({
26+
opacity: 1,
27+
transform: `scale(1)`
28+
})),
29+
transition('void => *', [
30+
style({
31+
opacity: 0,
32+
transform: `scale(0)`
33+
}),
34+
animate(`200ms cubic-bezier(0.25, 0.8, 0.25, 1)`)
35+
]),
36+
transition('* => void', [
37+
animate('50ms 100ms linear', style({opacity: 0}))
38+
])
39+
]);
40+
41+
/**
42+
* This animation fades in the background color and content of the menu panel
43+
* after its containing element is scaled in.
44+
*/
45+
export const fadeInItems: AnimationEntryMetadata = trigger('fadeInItems', [
46+
state('showing', style({opacity: 1})),
47+
transition('void => showing', [
48+
style({opacity: 0}),
49+
animate(`200ms 100ms cubic-bezier(0.55, 0, 0.55, 0.2)`)
50+
])
51+
]);

src/lib/menu/menu-directive.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,15 @@ import {
1212
QueryList,
1313
TemplateRef,
1414
ViewChild,
15-
ViewEncapsulation
15+
ViewEncapsulation,
1616
} from '@angular/core';
1717
import {MenuPositionX, MenuPositionY} from './menu-positions';
1818
import {MdMenuInvalidPositionX, MdMenuInvalidPositionY} from './menu-errors';
1919
import {MdMenuItem} from './menu-item';
2020
import {ListKeyManager} from '../core/a11y/list-key-manager';
2121
import {MdMenuPanel} from './menu-panel';
2222
import {Subscription} from 'rxjs/Subscription';
23+
import {transformMenu, fadeInItems} from './menu-animations';
2324

2425
@Component({
2526
moduleId: module.id,
@@ -28,6 +29,10 @@ import {Subscription} from 'rxjs/Subscription';
2829
templateUrl: 'menu.html',
2930
styleUrls: ['menu.css'],
3031
encapsulation: ViewEncapsulation.None,
32+
animations: [
33+
transformMenu,
34+
fadeInItems
35+
],
3136
exportAs: 'mdMenu'
3237
})
3338
export class MdMenu implements AfterContentInit, MdMenuPanel, OnDestroy {
@@ -91,11 +96,12 @@ export class MdMenu implements AfterContentInit, MdMenuPanel, OnDestroy {
9196
this.items.first.focus();
9297
this._keyManager.focusedItemIndex = 0;
9398
}
99+
94100
/**
95101
* This emits a close event to which the trigger is subscribed. When emitted, the
96102
* trigger will close the menu.
97103
*/
98-
private _emitCloseEvent(): void {
104+
_emitCloseEvent(): void {
99105
this.close.emit();
100106
}
101107

src/lib/menu/menu-item.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<ng-content></ng-content>
2+
<div class="md-menu-ripple" *ngIf="!disabled" md-ripple md-ripple-background-color="rgba(0,0,0,0)"
3+
[md-ripple-trigger]="_getHostElement()">
4+
</div>

src/lib/menu/menu-item.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
1-
import {Directive, ElementRef, Input, HostBinding, Renderer} from '@angular/core';
1+
import {Component, ElementRef, Input, HostBinding, Renderer} from '@angular/core';
22
import {MdFocusable} from '../core/a11y/list-key-manager';
33

44
/**
55
* This directive is intended to be used inside an md-menu tag.
66
* It exists mostly to set the role attribute.
77
*/
8-
@Directive({
8+
@Component({
9+
moduleId: module.id,
910
selector: '[md-menu-item]',
1011
host: {
1112
'role': 'menuitem',
1213
'(click)': '_checkDisabled($event)',
1314
'tabindex': '-1'
1415
},
16+
templateUrl: 'menu-item.html',
1517
exportAs: 'mdMenuItem'
1618
})
1719
export class MdMenuItem implements MdFocusable {
@@ -36,12 +38,14 @@ export class MdMenuItem implements MdFocusable {
3638

3739
@HostBinding('attr.aria-disabled')
3840
get isAriaDisabled(): string {
39-
return String(this.disabled);
41+
return String(!!this.disabled);
42+
}
43+
44+
45+
_getHostElement(): HTMLElement {
46+
return this._elementRef.nativeElement;
4047
}
4148

42-
/**
43-
* TODO: internal
44-
*/
4549
_checkDisabled(event: Event) {
4650
if (this.disabled) {
4751
event.preventDefault();

src/lib/menu/menu.html

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
<template>
2-
<div class="md-menu-panel" [ngClass]="_classList"
3-
(click)="_emitCloseEvent()" (keydown)="_keyManager.onKeydown($event)">
4-
<ng-content></ng-content>
2+
<div class="md-menu-panel" [ngClass]="_classList" (keydown)="_keyManager.onKeydown($event)"
3+
(click)="_emitCloseEvent()" [@transformMenu]="'showing'">
4+
<div class="md-menu-content" [@fadeInItems]="'showing'">
5+
<ng-content></ng-content>
6+
</div>
57
</div>
68
</template>
79

src/lib/menu/menu.scss

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,39 @@
55
@import '../core/style/sidenav-common';
66
@import '../core/style/menu-common';
77

8+
$md-menu-vertical-padding: 8px !default;
9+
810
.md-menu-panel {
911
@include md-menu-base();
1012

1113
// max height must be 100% of the viewport height + one row height
1214
max-height: calc(100vh + 48px);
15+
transform-origin: left top;
16+
17+
[dir='rtl'] & {
18+
transform-origin: right top;
19+
}
20+
}
21+
22+
.md-menu-content {
23+
padding-top: $md-menu-vertical-padding;
24+
padding-bottom: $md-menu-vertical-padding;
1325
}
1426

1527
[md-menu-item] {
1628
@include md-button-reset();
1729
@include md-menu-item-base();
30+
position: relative;
1831
}
1932

2033
button[md-menu-item] {
2134
width: 100%;
2235
}
36+
37+
.md-menu-ripple {
38+
position: absolute;
39+
top: 0;
40+
left: 0;
41+
bottom: 0;
42+
right: 0;
43+
}

src/lib/menu/menu.spec.ts

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {TestBed, async} from '@angular/core/testing';
2+
import {By} from '@angular/platform-browser';
23
import {
34
Component,
45
EventEmitter,
@@ -15,7 +16,6 @@ import {
1516
} from './menu';
1617
import {OverlayContainer} from '../core/overlay/overlay-container';
1718

18-
1919
describe('MdMenu', () => {
2020
let overlayContainerElement: HTMLElement;
2121

@@ -42,11 +42,12 @@ describe('MdMenu', () => {
4242
fixture.componentInstance.trigger.openMenu();
4343
fixture.componentInstance.trigger.openMenu();
4444

45-
expect(overlayContainerElement.textContent.trim()).toBe('Simple Content');
45+
expect(overlayContainerElement.textContent).toContain('Simple Content');
46+
expect(overlayContainerElement.textContent).toContain('Disabled Content');
4647
}).not.toThrowError();
4748
});
4849

49-
it('should close the menu when a click occurs outside the menu', () => {
50+
it('should close the menu when a click occurs outside the menu', async(() => {
5051
const fixture = TestBed.createComponent(SimpleMenu);
5152
fixture.detectChanges();
5253
fixture.componentInstance.trigger.openMenu();
@@ -55,8 +56,10 @@ describe('MdMenu', () => {
5556
backdrop.click();
5657
fixture.detectChanges();
5758

58-
expect(overlayContainerElement.textContent).toBe('');
59-
});
59+
fixture.whenStable().then(() => {
60+
expect(overlayContainerElement.textContent).toBe('');
61+
});
62+
}));
6063

6164
it('should open a custom menu', () => {
6265
const fixture = TestBed.createComponent(CustomMenu);
@@ -71,13 +74,37 @@ describe('MdMenu', () => {
7174
}).not.toThrowError();
7275
});
7376

77+
it('should include the ripple on items by default', () => {
78+
const fixture = TestBed.createComponent(SimpleMenu);
79+
fixture.detectChanges();
80+
81+
fixture.componentInstance.trigger.openMenu();
82+
const item = fixture.debugElement.query(By.css('[md-menu-item]'));
83+
const ripple = item.query(By.css('[md-ripple]'));
84+
85+
expect(ripple).not.toBeNull();
86+
});
87+
88+
it('should remove the ripple on disabled items', () => {
89+
const fixture = TestBed.createComponent(SimpleMenu);
90+
fixture.detectChanges();
91+
92+
fixture.componentInstance.trigger.openMenu();
93+
const items = fixture.debugElement.queryAll(By.css('[md-menu-item]'));
94+
95+
// items[1] is disabled, so the ripple should not be present
96+
const ripple = items[1].query(By.css('[md-ripple]'));
97+
expect(ripple).toBeNull();
98+
});
99+
74100
});
75101

76102
@Component({
77103
template: `
78104
<button [md-menu-trigger-for]="menu">Toggle menu</button>
79105
<md-menu #menu="mdMenu">
80106
<button md-menu-item> Simple Content </button>
107+
<button md-menu-item disabled> Disabled Content </button>
81108
</md-menu>
82109
`
83110
})

src/lib/menu/menu.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {OverlayModule, OVERLAY_PROVIDERS} from '../core';
44
import {MdMenu} from './menu-directive';
55
import {MdMenuItem} from './menu-item';
66
import {MdMenuTrigger} from './menu-trigger';
7+
import {MdRippleModule} from '../core/ripple/ripple';
78
export {MdMenu} from './menu-directive';
89
export {MdMenuItem} from './menu-item';
910
export {MdMenuTrigger} from './menu-trigger';
@@ -12,7 +13,7 @@ export {MenuPositionX, MenuPositionY} from './menu-positions';
1213

1314

1415
@NgModule({
15-
imports: [OverlayModule, CommonModule],
16+
imports: [OverlayModule, CommonModule, MdRippleModule],
1617
exports: [MdMenu, MdMenuItem, MdMenuTrigger],
1718
declarations: [MdMenu, MdMenuItem, MdMenuTrigger],
1819
})

0 commit comments

Comments
 (0)