Skip to content
This repository was archived by the owner on Sep 5, 2024. It is now read-only.

Commit 09e7e92

Browse files
committed
feat(panel): configurable animation duration
* Adds the ability to specify the duration of an animation the panel. * Fixes the `MdPanelAnimation` not showing up in the docs. Fixes #9177.
1 parent a3b3e7b commit 09e7e92

File tree

5 files changed

+142
-16
lines changed

5 files changed

+142
-16
lines changed

src/components/panel/demoPanelAnimations/index.html

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11
<div class="demo-md-panel-animation md-padding" ng-controller="AnimationCtrl as ctrl">
22
<h2>Animations</h2>
33
<div layout="row">
4-
<div flex="33">
5-
OpenFrom:
4+
<div flex="25">
5+
<h3>OpenFrom:</h3>
66
<md-radio-group ng-model="ctrl.openFrom">
77
<md-radio-button value="button">Button</md-radio-button>
88
<md-radio-button value="corner">Top/Left Corner</md-radio-button>
99
<md-radio-button value="bottom">Bottom Center</md-radio-button>
1010
</md-radio-group>
1111
</div>
1212

13-
<div flex="33">
14-
CloseTo:
13+
<div flex="25">
14+
<h3>CloseTo:</h3>
1515
<md-radio-group ng-model="ctrl.closeTo">
1616
<md-radio-button value="button">Button</md-radio-button>
1717
<md-radio-button value="corner">Top/Left Corner</md-radio-button>
1818
<md-radio-button value="bottom">Bottom Center</md-radio-button>
1919
</md-radio-group>
2020
</div>
2121

22-
<div flex="33">
23-
AnimationType:
22+
<div flex="25">
23+
<h3>AnimationType:</h3>
2424
<md-radio-group ng-model="ctrl.animationType">
2525
<md-radio-button value="none">None</md-radio-button>
2626
<md-radio-button value="slide">Slide</md-radio-button>
@@ -29,6 +29,33 @@ <h2>Animations</h2>
2929
<md-radio-button value="custom">Custom</md-radio-button>
3030
</md-radio-group>
3131
</div>
32+
33+
<div flex="25">
34+
<h3>Duration:</h3>
35+
<md-input-container>
36+
<label>All animations</label>
37+
<input
38+
type="number"
39+
ng-model="ctrl.duration"
40+
ng-change="ctrl.separateDurations.open = ctrl.separateDurations.close = ctrl.duration">
41+
</md-input-container>
42+
43+
<md-input-container>
44+
<label>Open animation</label>
45+
<input
46+
type="number"
47+
ng-model="ctrl.separateDurations.open"
48+
ng-change="ctrl.duration = null">
49+
</md-input-container>
50+
51+
<md-input-container>
52+
<label>Close animation</label>
53+
<input
54+
type="number"
55+
ng-model="ctrl.separateDurations.close"
56+
ng-change="ctrl.duration = null">
57+
</md-input-container>
58+
</div>
3259
</div>
3360

3461
<div class="demo-md-panel-content">

src/components/panel/demoPanelAnimations/script.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@ function AnimationCtrl($mdPanel) {
1010
this._mdPanel = $mdPanel;
1111
this.openFrom = 'button';
1212
this.closeTo = 'button';
13-
this.animationType = 'none';
13+
this.animationType = 'scale';
14+
this.duration = 300;
15+
this.separateDurations = {
16+
open: this.duration,
17+
close: this.duration
18+
};
1419
}
1520

1621

@@ -22,6 +27,8 @@ AnimationCtrl.prototype.showDialog = function() {
2227

2328
var animation = this._mdPanel.newPanelAnimation();
2429

30+
animation.duration(this.duration || this.separateDurations);
31+
2532
switch(this.openFrom) {
2633
case 'button':
2734
animation.openFrom('.animation-target');

src/components/panel/panel.js

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -592,24 +592,27 @@ angular
592592
* MdPanelAnimation *
593593
*****************************************************************************/
594594

595-
596595
/**
597596
* @ngdoc type
598597
* @name MdPanelAnimation
598+
* @module material.components.panel
599599
* @description
600600
* Animation configuration object. To use, create an MdPanelAnimation with the
601601
* desired properties, then pass the object as part of $mdPanel creation.
602602
*
603-
* Example:
603+
* @usage
604604
*
605+
* <hljs lang="js">
605606
* var panelAnimation = new MdPanelAnimation()
606607
* .openFrom(myButtonEl)
608+
* .duration(1337)
607609
* .closeTo('.my-button')
608610
* .withAnimation($mdPanel.animation.SCALE);
609611
*
610612
* $mdPanel.create({
611613
* animation: panelAnimation
612614
* });
615+
* </hljs>
613616
*/
614617

615618
/**
@@ -658,6 +661,18 @@ angular
658661
* @returns {!MdPanelAnimation}
659662
*/
660663

664+
/**
665+
* @ngdoc method
666+
* @name MdPanelAnimation#duration
667+
* @description
668+
* Specifies the duration of the animation in milliseconds. The `duration`
669+
* method accepts either a number or an object with separate open and close
670+
* durations.
671+
*
672+
* @param {number|{open: number, close: number}} duration
673+
* @returns {!MdPanelAnimation}
674+
*/
675+
661676

662677
/*****************************************************************************
663678
* IMPLEMENTATION *
@@ -1399,13 +1414,19 @@ MdPanelRef.prototype._createBackdrop = function() {
13991414
open: '_md-opaque-enter',
14001415
close: '_md-opaque-leave'
14011416
});
1417+
1418+
if (this.config.animation) {
1419+
backdropAnimation.duration(this.config.animation._rawDuration);
1420+
}
1421+
14021422
var backdropConfig = {
14031423
animation: backdropAnimation,
14041424
attachTo: this.config.attachTo,
14051425
focusOnOpen: false,
14061426
panelClass: '_md-panel-backdrop',
14071427
zIndex: this.config.zIndex - 1
14081428
};
1429+
14091430
this._backdropRef = this._$mdPanel.create(backdropConfig);
14101431
}
14111432
if (!this._backdropRef.isAttached) {
@@ -1543,7 +1564,7 @@ MdPanelRef.prototype._configureScrollListener = function() {
15431564
* @private
15441565
*/
15451566
MdPanelRef.prototype._configureTrapFocus = function() {
1546-
// Focus doesn't remain instead of the panel without this.
1567+
// Focus doesn't remain inside of the panel without this.
15471568
this.panelEl.attr('tabIndex', '-1');
15481569
if (this.config['trapFocus']) {
15491570
var element = this.panelEl;
@@ -2279,6 +2300,15 @@ function MdPanelAnimation($injector) {
22792300

22802301
/** @private {string|{open: string, close: string}} */
22812302
this._animationClass = '';
2303+
2304+
/** @private {number} */
2305+
this._openDuration;
2306+
2307+
/** @private {number} */
2308+
this._closeDuration;
2309+
2310+
/** @private {number|{open: number, close: number}} */
2311+
this._rawDuration;
22822312
}
22832313

22842314

@@ -2326,6 +2356,30 @@ MdPanelAnimation.prototype.closeTo = function(closeTo) {
23262356
return this;
23272357
};
23282358

2359+
/**
2360+
* Specifies the duration of the animation in milliseconds.
2361+
* @param {number|{open: number, close: number}} duration
2362+
* @returns {!MdPanelAnimation}
2363+
*/
2364+
MdPanelAnimation.prototype.duration = function(duration) {
2365+
if (duration) {
2366+
if (angular.isNumber(duration)) {
2367+
this._openDuration = this._closeDuration = toSeconds(duration);
2368+
} else if (angular.isObject(duration)) {
2369+
this._openDuration = toSeconds(duration.open);
2370+
this._closeDuration = toSeconds(duration.close);
2371+
}
2372+
}
2373+
2374+
// Save the original value so it can be passed to the backdrop.
2375+
this._rawDuration = duration;
2376+
2377+
return this;
2378+
2379+
function toSeconds(value) {
2380+
if (angular.isNumber(value)) return value / 1000;
2381+
}
2382+
};
23292383

23302384
/**
23312385
* Returns the element and bounds for the animation target.
@@ -2428,6 +2482,8 @@ MdPanelAnimation.prototype.animateOpen = function(panelEl) {
24282482
}
24292483
}
24302484

2485+
animationOptions.duration = this._openDuration;
2486+
24312487
return animator
24322488
.translate3d(panelEl, openFrom, openTo, animationOptions);
24332489
};
@@ -2491,6 +2547,8 @@ MdPanelAnimation.prototype.animateClose = function(panelEl) {
24912547
}
24922548
}
24932549

2550+
reverseAnimationOptions.duration = this._closeDuration;
2551+
24942552
return animator
24952553
.translate3d(panelEl, closeFrom, closeTo, reverseAnimationOptions);
24962554
};

src/components/panel/panel.spec.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2149,6 +2149,38 @@ describe('$mdPanel', function() {
21492149
expect(animation._closeTo.bounds).toEqual(inputRect);
21502150
});
21512151
});
2152+
2153+
describe('should determine the animation duration when', function() {
2154+
it('provided a value in milliseconds', function() {
2155+
var animation = mdPanelAnimation.duration(1300);
2156+
2157+
expect(animation._openDuration).toBe(1.3);
2158+
});
2159+
2160+
it('provided a number', function() {
2161+
var animation = mdPanelAnimation.duration(2000);
2162+
2163+
expect(animation._openDuration).toEqual(animation._closeDuration);
2164+
expect(animation._openDuration).toBe(2);
2165+
});
2166+
2167+
it('provided an object', function() {
2168+
var animation = mdPanelAnimation.duration({
2169+
open: 1200,
2170+
close: 600
2171+
});
2172+
2173+
expect(animation._openDuration).toBe(1.2);
2174+
expect(animation._closeDuration).toBe(0.6);
2175+
});
2176+
2177+
it('provided an invalid value', function() {
2178+
var animation = mdPanelAnimation.duration('very fast');
2179+
2180+
expect(animation._openDuration).toBeFalsy();
2181+
expect(animation._closeDuration).toBeFalsy();
2182+
});
2183+
});
21522184
});
21532185

21542186
/**

src/core/util/animation/animate.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,12 @@ function AnimateDomUtils($mdUtil, $q, $timeout, $mdConstant, $animateCss) {
2121
*
2222
*/
2323
translate3d : function( target, from, to, options ) {
24-
return $animateCss(target,{
25-
from:from,
26-
to:to,
27-
addClass:options.transitionInClass,
28-
removeClass:options.transitionOutClass
24+
return $animateCss(target, {
25+
from: from,
26+
to: to,
27+
addClass: options.transitionInClass,
28+
removeClass: options.transitionOutClass,
29+
duration: options.duration
2930
})
3031
.start()
3132
.then(function(){
@@ -40,7 +41,8 @@ function AnimateDomUtils($mdUtil, $q, $timeout, $mdConstant, $animateCss) {
4041
return $animateCss(target, {
4142
to: newFrom || from,
4243
addClass: options.transitionOutClass,
43-
removeClass: options.transitionInClass
44+
removeClass: options.transitionInClass,
45+
duration: options.duration
4446
}).start();
4547

4648
}

0 commit comments

Comments
 (0)