2017-10-11 16:56:17 +00:00
|
|
|
const React = require('react');
|
|
|
|
const ReactDom = require('react-dom');
|
2017-12-26 01:03:18 +00:00
|
|
|
import PropTypes from 'prop-types';
|
2017-10-11 16:56:17 +00:00
|
|
|
const Velocity = require('velocity-vector');
|
2015-11-27 15:37:40 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The Velociraptor contains components and animates transitions with velocity.
|
|
|
|
* It will only pick up direct changes to properties ('left', currently), and so
|
|
|
|
* will not work for animating positional changes where the position is implicit
|
|
|
|
* from DOM order. This makes it a lot simpler and lighter: if you need fully
|
|
|
|
* automatic positional animation, look at react-shuffle or similar libraries.
|
|
|
|
*/
|
|
|
|
module.exports = React.createClass({
|
|
|
|
displayName: 'Velociraptor',
|
|
|
|
|
|
|
|
propTypes: {
|
2016-04-20 21:21:07 +00:00
|
|
|
// either a list of child nodes, or a single child.
|
2017-12-26 01:03:18 +00:00
|
|
|
children: PropTypes.any,
|
2016-04-20 21:21:07 +00:00
|
|
|
|
|
|
|
// optional transition information for changing existing children
|
2017-12-26 01:03:18 +00:00
|
|
|
transition: PropTypes.object,
|
2016-08-01 15:56:25 +00:00
|
|
|
|
|
|
|
// a list of state objects to apply to each child node in turn
|
2017-12-26 01:03:18 +00:00
|
|
|
startStyles: PropTypes.array,
|
2016-08-01 15:56:25 +00:00
|
|
|
|
|
|
|
// a list of transition options from the corresponding startStyle
|
2017-12-26 01:03:18 +00:00
|
|
|
enterTransitionOpts: PropTypes.array,
|
2016-08-01 15:56:25 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
getDefaultProps: function() {
|
|
|
|
return {
|
|
|
|
startStyles: [],
|
|
|
|
enterTransitionOpts: [],
|
|
|
|
};
|
2015-11-27 15:37:40 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
componentWillMount: function() {
|
|
|
|
this.nodes = {};
|
2016-04-21 13:14:08 +00:00
|
|
|
this._updateChildren(this.props.children);
|
2015-11-27 15:37:40 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
componentWillReceiveProps: function(nextProps) {
|
2016-04-21 13:14:08 +00:00
|
|
|
this._updateChildren(nextProps.children);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* update `this.children` according to the new list of children given
|
|
|
|
*/
|
|
|
|
_updateChildren: function(newChildren) {
|
2017-10-11 16:56:17 +00:00
|
|
|
const self = this;
|
|
|
|
const oldChildren = this.children || {};
|
2015-11-27 15:37:40 +00:00
|
|
|
this.children = {};
|
2016-04-21 13:14:08 +00:00
|
|
|
React.Children.toArray(newChildren).forEach(function(c) {
|
2015-11-27 15:37:40 +00:00
|
|
|
if (oldChildren[c.key]) {
|
2017-10-11 16:56:17 +00:00
|
|
|
const old = oldChildren[c.key];
|
|
|
|
const oldNode = ReactDom.findDOMNode(self.nodes[old.key]);
|
2015-11-27 15:37:40 +00:00
|
|
|
|
2016-01-08 16:43:25 +00:00
|
|
|
if (oldNode && oldNode.style.left != c.props.style.left) {
|
2015-11-27 15:37:40 +00:00
|
|
|
Velocity(oldNode, { left: c.props.style.left }, self.props.transition).then(function() {
|
|
|
|
// special case visibility because it's nonsensical to animate an invisible element
|
|
|
|
// so we always hidden->visible pre-transition and visible->hidden after
|
|
|
|
if (oldNode.style.visibility == 'visible' && c.props.style.visibility == 'hidden') {
|
|
|
|
oldNode.style.visibility = c.props.style.visibility;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
//console.log("translation: "+oldNode.style.left+" -> "+c.props.style.left);
|
|
|
|
}
|
2017-07-10 14:07:22 +00:00
|
|
|
if (oldNode && oldNode.style.visibility == 'hidden' && c.props.style.visibility == 'visible') {
|
2017-02-02 18:21:53 +00:00
|
|
|
oldNode.style.visibility = c.props.style.visibility;
|
|
|
|
}
|
2015-11-27 15:37:40 +00:00
|
|
|
self.children[c.key] = old;
|
|
|
|
} else {
|
2016-08-01 15:56:25 +00:00
|
|
|
// new element. If we have a startStyle, use that as the style and go through
|
2015-11-27 15:37:40 +00:00
|
|
|
// the enter animations
|
2017-10-11 16:56:17 +00:00
|
|
|
const newProps = {};
|
|
|
|
const restingStyle = c.props.style;
|
2016-08-01 15:56:25 +00:00
|
|
|
|
2017-10-11 16:56:17 +00:00
|
|
|
const startStyles = self.props.startStyles;
|
2016-08-01 15:56:25 +00:00
|
|
|
if (startStyles.length > 0) {
|
2017-10-11 16:56:17 +00:00
|
|
|
const startStyle = startStyles[0];
|
2015-11-27 15:37:40 +00:00
|
|
|
newProps.style = startStyle;
|
2016-08-01 15:56:25 +00:00
|
|
|
// console.log("mounted@startstyle0: "+JSON.stringify(startStyle));
|
2015-11-27 15:37:40 +00:00
|
|
|
}
|
2016-08-01 15:56:25 +00:00
|
|
|
|
2017-10-11 16:56:17 +00:00
|
|
|
newProps.ref = ((n) => self._collectNode(
|
|
|
|
c.key, n, restingStyle,
|
2016-08-01 15:56:25 +00:00
|
|
|
));
|
|
|
|
|
2015-11-27 15:37:40 +00:00
|
|
|
self.children[c.key] = React.cloneElement(c, newProps);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2016-08-01 15:56:25 +00:00
|
|
|
/**
|
|
|
|
* called when a child element is mounted/unmounted
|
|
|
|
*
|
|
|
|
* @param {string} k key of the child
|
|
|
|
* @param {null|Object} node On mount: React node. On unmount: null
|
|
|
|
* @param {Object} restingStyle final style
|
|
|
|
*/
|
|
|
|
_collectNode: function(k, node, restingStyle) {
|
2015-11-27 15:37:40 +00:00
|
|
|
if (
|
2016-01-08 16:43:25 +00:00
|
|
|
node &&
|
2015-11-27 15:37:40 +00:00
|
|
|
this.nodes[k] === undefined &&
|
2016-08-01 15:56:25 +00:00
|
|
|
this.props.startStyles.length > 0
|
2015-11-27 15:37:40 +00:00
|
|
|
) {
|
2017-10-11 16:56:17 +00:00
|
|
|
const startStyles = this.props.startStyles;
|
|
|
|
const transitionOpts = this.props.enterTransitionOpts;
|
2017-01-20 14:22:27 +00:00
|
|
|
const domNode = ReactDom.findDOMNode(node);
|
2015-11-27 15:37:40 +00:00
|
|
|
// start from startStyle 1: 0 is the one we gave it
|
|
|
|
// to start with, so now we animate 1 etc.
|
|
|
|
for (var i = 1; i < startStyles.length; ++i) {
|
|
|
|
Velocity(domNode, startStyles[i], transitionOpts[i-1]);
|
2016-08-01 15:56:25 +00:00
|
|
|
/*
|
|
|
|
console.log("start:",
|
|
|
|
JSON.stringify(transitionOpts[i-1]),
|
|
|
|
"->",
|
|
|
|
JSON.stringify(startStyles[i]),
|
|
|
|
);
|
|
|
|
*/
|
2015-11-27 15:37:40 +00:00
|
|
|
}
|
2016-08-01 15:56:25 +00:00
|
|
|
|
2015-11-27 15:37:40 +00:00
|
|
|
// and then we animate to the resting state
|
2016-08-01 15:56:25 +00:00
|
|
|
Velocity(domNode, restingStyle,
|
2016-04-20 21:21:07 +00:00
|
|
|
transitionOpts[i-1])
|
|
|
|
.then(() => {
|
|
|
|
// once we've reached the resting state, hide the element if
|
|
|
|
// appropriate
|
2016-08-01 15:56:25 +00:00
|
|
|
domNode.style.visibility = restingStyle.visibility;
|
2016-04-20 21:21:07 +00:00
|
|
|
});
|
|
|
|
|
2016-08-01 15:56:25 +00:00
|
|
|
/*
|
|
|
|
console.log("enter:",
|
|
|
|
JSON.stringify(transitionOpts[i-1]),
|
|
|
|
"->",
|
|
|
|
JSON.stringify(restingStyle));
|
|
|
|
*/
|
2016-05-26 12:51:51 +00:00
|
|
|
} else if (node === null) {
|
2016-05-26 15:30:04 +00:00
|
|
|
// Velocity stores data on elements using the jQuery .data()
|
|
|
|
// method, and assumes you'll be using jQuery's .remove() to
|
|
|
|
// remove the element, but we don't use jQuery, so we need to
|
|
|
|
// blow away the element's data explicitly otherwise it will leak.
|
|
|
|
// This uses Velocity's internal jQuery compatible wrapper.
|
|
|
|
// See the bug at
|
2016-05-26 12:51:51 +00:00
|
|
|
// https://github.com/julianshapiro/velocity/issues/300
|
2016-05-26 15:30:04 +00:00
|
|
|
// and the FAQ entry, "Preventing memory leaks when
|
|
|
|
// creating/destroying large numbers of elements"
|
|
|
|
// (https://github.com/julianshapiro/velocity/issues/47)
|
2017-01-20 14:22:27 +00:00
|
|
|
const domNode = ReactDom.findDOMNode(this.nodes[k]);
|
2018-04-13 09:44:37 +00:00
|
|
|
if (domNode) Velocity.Utilities.removeData(domNode);
|
2015-11-27 15:37:40 +00:00
|
|
|
}
|
|
|
|
this.nodes[k] = node;
|
|
|
|
},
|
|
|
|
|
|
|
|
render: function() {
|
|
|
|
return (
|
|
|
|
<span>
|
2017-10-11 16:56:17 +00:00
|
|
|
{ Object.values(this.children) }
|
2015-11-27 15:37:40 +00:00
|
|
|
</span>
|
|
|
|
);
|
|
|
|
},
|
|
|
|
});
|