Merge pull request #3687 from matrix-org/travis/rr-velocity

Convert Velociraptor component to a class
This commit is contained in:
Travis Ralston 2019-11-29 07:41:17 -07:00 committed by GitHub
commit 7b1ee944c8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 37 additions and 51 deletions

View file

@ -1,7 +1,6 @@
const React = require('react'); const React = require('react');
const ReactDom = require('react-dom'); const ReactDom = require('react-dom');
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import createReactClass from 'create-react-class';
const Velocity = require('velocity-animate'); const Velocity = require('velocity-animate');
/** /**
@ -11,10 +10,8 @@ const Velocity = require('velocity-animate');
* from DOM order. This makes it a lot simpler and lighter: if you need fully * 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. * automatic positional animation, look at react-shuffle or similar libraries.
*/ */
module.exports = createReactClass({ export default class Velociraptor extends React.Component {
displayName: 'Velociraptor', static propTypes = {
propTypes: {
// either a list of child nodes, or a single child. // either a list of child nodes, or a single child.
children: PropTypes.any, children: PropTypes.any,
@ -26,82 +23,71 @@ module.exports = createReactClass({
// a list of transition options from the corresponding startStyle // a list of transition options from the corresponding startStyle
enterTransitionOpts: PropTypes.array, enterTransitionOpts: PropTypes.array,
}, };
getDefaultProps: function() { static defaultProps = {
return { startStyles: [],
startStyles: [], enterTransitionOpts: [],
enterTransitionOpts: [], };
};
}, constructor(props) {
super(props);
componentWillMount: function() {
this.nodes = {}; this.nodes = {};
this._updateChildren(this.props.children); this._updateChildren(this.props.children);
}, }
componentWillReceiveProps: function(nextProps) { componentDidUpdate() {
this._updateChildren(nextProps.children); this._updateChildren(this.props.children);
}, }
/** _updateChildren(newChildren) {
* update `this.children` according to the new list of children given
*/
_updateChildren: function(newChildren) {
const self = this;
const oldChildren = this.children || {}; const oldChildren = this.children || {};
this.children = {}; this.children = {};
React.Children.toArray(newChildren).forEach(function(c) { React.Children.toArray(newChildren).forEach((c) => {
if (oldChildren[c.key]) { if (oldChildren[c.key]) {
const old = oldChildren[c.key]; const old = oldChildren[c.key];
const oldNode = ReactDom.findDOMNode(self.nodes[old.key]); const oldNode = ReactDom.findDOMNode(this.nodes[old.key]);
if (oldNode && oldNode.style.left != c.props.style.left) { if (oldNode && oldNode.style.left !== c.props.style.left) {
Velocity(oldNode, { left: c.props.style.left }, self.props.transition).then(function() { Velocity(oldNode, { left: c.props.style.left }, this.props.transition).then(() => {
// special case visibility because it's nonsensical to animate an invisible element // special case visibility because it's nonsensical to animate an invisible element
// so we always hidden->visible pre-transition and visible->hidden after // so we always hidden->visible pre-transition and visible->hidden after
if (oldNode.style.visibility == 'visible' && c.props.style.visibility == 'hidden') { if (oldNode.style.visibility === 'visible' && c.props.style.visibility === 'hidden') {
oldNode.style.visibility = c.props.style.visibility; oldNode.style.visibility = c.props.style.visibility;
} }
}); });
//console.log("translation: "+oldNode.style.left+" -> "+c.props.style.left); //console.log("translation: "+oldNode.style.left+" -> "+c.props.style.left);
} }
if (oldNode && oldNode.style.visibility == 'hidden' && c.props.style.visibility == 'visible') { if (oldNode && oldNode.style.visibility === 'hidden' && c.props.style.visibility === 'visible') {
oldNode.style.visibility = c.props.style.visibility; oldNode.style.visibility = c.props.style.visibility;
} }
// clone the old element with the props (and children) of the new element // clone the old element with the props (and children) of the new element
// so prop updates are still received by the children. // so prop updates are still received by the children.
self.children[c.key] = React.cloneElement(old, c.props, c.props.children); this.children[c.key] = React.cloneElement(old, c.props, c.props.children);
} else { } else {
// new element. If we have a startStyle, use that as the style and go through // new element. If we have a startStyle, use that as the style and go through
// the enter animations // the enter animations
const newProps = {}; const newProps = {};
const restingStyle = c.props.style; const restingStyle = c.props.style;
const startStyles = self.props.startStyles; const startStyles = this.props.startStyles;
if (startStyles.length > 0) { if (startStyles.length > 0) {
const startStyle = startStyles[0]; const startStyle = startStyles[0];
newProps.style = startStyle; newProps.style = startStyle;
// console.log("mounted@startstyle0: "+JSON.stringify(startStyle)); // console.log("mounted@startstyle0: "+JSON.stringify(startStyle));
} }
newProps.ref = ((n) => self._collectNode( newProps.ref = ((n) => this._collectNode(
c.key, n, restingStyle, c.key, n, restingStyle,
)); ));
self.children[c.key] = React.cloneElement(c, newProps); this.children[c.key] = React.cloneElement(c, newProps);
} }
}); });
}, }
/** _collectNode(k, node, restingStyle) {
* 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) {
if ( if (
node && node &&
this.nodes[k] === undefined && this.nodes[k] === undefined &&
@ -125,12 +111,12 @@ module.exports = createReactClass({
// and then we animate to the resting state // and then we animate to the resting state
Velocity(domNode, restingStyle, Velocity(domNode, restingStyle,
transitionOpts[i-1]) transitionOpts[i-1])
.then(() => { .then(() => {
// once we've reached the resting state, hide the element if // once we've reached the resting state, hide the element if
// appropriate // appropriate
domNode.style.visibility = restingStyle.visibility; domNode.style.visibility = restingStyle.visibility;
}); });
/* /*
console.log("enter:", console.log("enter:",
@ -153,13 +139,13 @@ module.exports = createReactClass({
if (domNode) Velocity.Utilities.removeData(domNode); if (domNode) Velocity.Utilities.removeData(domNode);
} }
this.nodes[k] = node; this.nodes[k] = node;
}, }
render: function() { render() {
return ( return (
<span> <span>
{ Object.values(this.children) } { Object.values(this.children) }
</span> </span>
); );
}, }
}); }

View file