-
-
Save tkafka/0d94c6ec94297bb67091 to your computer and use it in GitHub Desktop.
The MIT License (MIT) | |
Copyright (c) 2014 Tomas Kafka | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all | |
copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
SOFTWARE. |
/** | |
* Adapted from https://github.com/Khan/react-components/blob/master/js/timeout-transition-group.jsx | |
*/ | |
var React = require('react/addons'); | |
var ReactTransitionGroup = React.addons.TransitionGroup; | |
var Velocity = require('velocity-animate'); | |
var transitions = { | |
// Forcefeeding: property order = [after, before] | |
'slide-forward': { | |
duration: 200, | |
enter: { | |
translateX: [ '0%', '100%' ], | |
}, | |
leave: { | |
translateX: [ '-100%', '0%' ], | |
} | |
}, | |
'slide-back': { | |
duration: 200, | |
enter: { | |
translateX: [ '0%', '-100%' ], | |
}, | |
leave: { | |
translateX: [ '100%', '0%' ], | |
} | |
}, | |
'slideover-forward': { | |
duration: 200, | |
enter: { | |
translateX: [ '0%', '100%' ], | |
zIndex: [ 1, 1 ] | |
}, | |
leave: { | |
// translateX: [ '0%', '0%' ], | |
zIndex: [ 0, 0 ] | |
} | |
}, | |
'slideover-back': { | |
duration: 200, | |
enter: { | |
// translateX: [ '0%', '0%' ], | |
zIndex: [ 0, 0 ] | |
}, | |
leave: { | |
translateX: [ '100%', '0%' ], | |
zIndex: [ 1, 1 ] | |
} | |
}, | |
default: { | |
duration: 200, | |
enter: { | |
opacity: [ 1, 0 ], | |
}, | |
leave: { | |
opacity: [ 0, 1 ], | |
} | |
} | |
}; | |
var VelocityTransitionGroupChild = React.createClass({ | |
propTypes: { | |
transitionName: React.PropTypes.string.isRequired, | |
}, | |
_getTransition: function() { | |
if (!transitions[this.props.transitionName]) { | |
console.warn('TransitionName ' + this.props.transitionName + ' wasn\'t found in VelocityTransitionGroupChild transitions.'); | |
} | |
return transitions[this.props.transitionName] || transitions.default; | |
}, | |
componentWillEnter: function(done) { | |
var node = this.getDOMNode(); | |
var transition = this._getTransition(); | |
Velocity( | |
node, | |
transition.enter, | |
{ | |
duration: transition.duration, | |
complete: done | |
}); | |
}, | |
componentWillLeave: function(done) { | |
var node = this.getDOMNode(); | |
var transition = this._getTransition(); | |
Velocity( | |
node, | |
transition.leave, | |
{ | |
duration: transition.duration, | |
complete: done | |
}); | |
}, | |
render: function() { | |
return React.Children.only(this.props.children); | |
} | |
}); | |
var VelocityTransitionGroup = React.createClass({ | |
propTypes: { | |
transitionName: React.PropTypes.string.isRequired, | |
}, | |
_wrapChild: function(child) { | |
return ( | |
<VelocityTransitionGroupChild | |
transitionName={this.props.transitionName} | |
> | |
{child} | |
</VelocityTransitionGroupChild> | |
); | |
}, | |
render: function() { | |
return ( | |
<ReactTransitionGroup | |
{...this.props} | |
childFactory={this._wrapChild} | |
/> | |
); | |
} | |
}); | |
module.exports = VelocityTransitionGroup; |
can you give an example of how to use this?
I need an example, please!
An ES6 example:
import VelocityTransitionGroup from './velocity-transition.js';
Then in your render method:
<VelocityTransitionGroup transitionName="slide-forward">
<your_changing_content />
</VelocityTransitionGroup>
It works great! It's a must if you want hassle-free transitions in iOS/Safari!!!
There is a problem with using Velocity. Apparently the nodes are still referenced in Velocity and are not completely deleted from the DOM. There is a node leaking in result. You can see that with chrome tools.
You need to call Velocity.Utilities.removeData(node, ['velocity']);
before calling the react callback for the leave animation to ensure the nodes are GC' ed. A bit obscure... But still better than using dumb CSS transitions as animations :)
How does this compare to React Motion or the ReactCSSTransitionGroup?
ReactCSSTransitionGroup have been horrible thus far because they are using css transitions. Oftentimes the browsers doesn't recognize when the transitionEnd event occurs and doesn't remove the element from the DOM
https://reactjs.co This is the official free online convention and tutorial book for React.JS Developers. React is not only the View (in MVC) anymore. ReactJS For Dummies: Why & How to Learn React Redux, the Right Way.
Any idea how to pass through multiple transitions? I want to fade and slide using velocity's built in functions. (Because the heights are all sizes so I can't specify it) I've gotten one or the other to work by doing:
But I've scowled Velocity's documentation and I can't figure how to do it without two separate Velocity calls.