Skip to content

Commit 52a8841

Browse files
committed
feat(menu): add animations and ripples
Closes #1671
1 parent ad3100e commit 52a8841

File tree

10 files changed

+222
-24
lines changed

10 files changed

+222
-24
lines changed

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

Lines changed: 22 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() {
@@ -43,3 +39,25 @@ $md-menu-vertical-padding: 8px !default;
4339
cursor: default;
4440
}
4541
}
42+
43+
/**
44+
* This mixin adds the correct panel transform styles based
45+
* on the direction that the menu panel opens.
46+
*/
47+
@mixin md-menu-positions() {
48+
&.md-menu-after.md-menu-below {
49+
transform-origin: left top;
50+
}
51+
52+
&.md-menu-after.md-menu-above {
53+
transform-origin: left bottom;
54+
}
55+
56+
&.md-menu-before.md-menu-below {
57+
transform-origin: right top;
58+
}
59+
60+
&.md-menu-before.md-menu-above {
61+
transform-origin: right bottom;
62+
}
63+
}

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: 23 additions & 3 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 {
@@ -37,7 +42,7 @@ export class MdMenu implements AfterContentInit, MdMenuPanel, OnDestroy {
3742
private _tabSubscription: Subscription;
3843

3944
/** Config object to be passed into the menu's ngClass */
40-
_classList: Object;
45+
_classList: any = {};
4146

4247
positionX: MenuPositionX = 'after';
4348
positionY: MenuPositionY = 'below';
@@ -49,6 +54,7 @@ export class MdMenu implements AfterContentInit, MdMenuPanel, OnDestroy {
4954
@Attribute('y-position') posY: MenuPositionY) {
5055
if (posX) { this._setPositionX(posX); }
5156
if (posY) { this._setPositionY(posY); }
57+
this._setPositionClasses();
5258
}
5359

5460
// TODO: internal
@@ -77,6 +83,7 @@ export class MdMenu implements AfterContentInit, MdMenuPanel, OnDestroy {
7783
obj[className] = true;
7884
return obj;
7985
}, {});
86+
this._setPositionClasses();
8087
}
8188

8289
@Output() close = new EventEmitter<void>();
@@ -91,11 +98,12 @@ export class MdMenu implements AfterContentInit, MdMenuPanel, OnDestroy {
9198
this.items.first.focus();
9299
this._keyManager.focusedItemIndex = 0;
93100
}
101+
94102
/**
95103
* This emits a close event to which the trigger is subscribed. When emitted, the
96104
* trigger will close the menu.
97105
*/
98-
private _emitCloseEvent(): void {
106+
_emitCloseEvent(): void {
99107
this.close.emit();
100108
}
101109

@@ -112,4 +120,16 @@ export class MdMenu implements AfterContentInit, MdMenuPanel, OnDestroy {
112120
}
113121
this.positionY = pos;
114122
}
123+
124+
/**
125+
* It's necessary to set position-based classes to ensure the menu panel animation
126+
* folds out from the correct direction.
127+
*/
128+
private _setPositionClasses() {
129+
this._classList['md-menu-before'] = this.positionX == 'before';
130+
this._classList['md-menu-after'] = this.positionX == 'after';
131+
this._classList['md-menu-above'] = this.positionY == 'above';
132+
this._classList['md-menu-below'] = this.positionY == 'below';
133+
}
134+
115135
}

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: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,35 @@
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();
12+
@include md-menu-positions();
1013

1114
// max height must be 100% of the viewport height + one row height
1215
max-height: calc(100vh + 48px);
1316
}
1417

18+
.md-menu-content {
19+
padding-top: $md-menu-vertical-padding;
20+
padding-bottom: $md-menu-vertical-padding;
21+
}
22+
1523
[md-menu-item] {
1624
@include md-button-reset();
1725
@include md-menu-item-base();
26+
position: relative;
1827
}
1928

2029
button[md-menu-item] {
2130
width: 100%;
2231
}
32+
33+
.md-menu-ripple {
34+
position: absolute;
35+
top: 0;
36+
left: 0;
37+
bottom: 0;
38+
right: 0;
39+
}

0 commit comments

Comments
 (0)