Skip to content

Instantly share code, notes, and snippets.

@tkafka
Last active May 17, 2024 02:08
Show Gist options
  • Save tkafka/0d94c6ec94297bb67091 to your computer and use it in GitHub Desktop.
Save tkafka/0d94c6ec94297bb67091 to your computer and use it in GitHub Desktop.
Drop-in replacement for ReactCSSTransitionGroup that uses velocity.js instead of CSS transforms. Add your own transitions to `transitions` hash.
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;
@phaistonian
Copy link

How does this compare to React Motion or the ReactCSSTransitionGroup?

@brianscroggins24
Copy link

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

@przeor
Copy link

przeor commented Aug 29, 2016

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment