Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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;
@felixakiragreen

This comment has been minimized.

Copy link

@felixakiragreen felixakiragreen commented Nov 14, 2014

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:

var transitions = 
{ 'sliding':
  { duration: 250
  , enter: 'slideDown'
  , leave: 'slideUp'
  }
, 'fading':
  { duration: 250
  , enter: 'fadeIn'
  , leave: 'fadeOut'
  }
}

But I've scowled Velocity's documentation and I can't figure how to do it without two separate Velocity calls.

@ccorcos

This comment has been minimized.

Copy link

@ccorcos ccorcos commented Mar 12, 2015

can you give an example of how to use this?

@dongshaohan

This comment has been minimized.

Copy link

@dongshaohan dongshaohan commented Mar 25, 2015

I need an example, please!

@faxioman

This comment has been minimized.

Copy link

@faxioman faxioman commented May 16, 2015

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!!!

@josegranja

This comment has been minimized.

Copy link

@josegranja josegranja commented Jul 6, 2015

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.

@AlexGalays

This comment has been minimized.

Copy link

@AlexGalays AlexGalays commented Aug 2, 2015

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 :)

@phaistonian

This comment has been minimized.

Copy link

@phaistonian phaistonian commented Aug 5, 2015

How does this compare to React Motion or the ReactCSSTransitionGroup?

@brianscroggins24

This comment has been minimized.

Copy link

@brianscroggins24 brianscroggins24 commented Dec 1, 2015

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

This comment has been minimized.

Copy link

@przeor 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