2019-12-20 21:41:07 +00:00
|
|
|
import React from "react";
|
|
|
|
import ReactDom from "react-dom";
|
2017-12-26 01:03:18 +00:00
|
|
|
import PropTypes from 'prop-types';
|
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.
|
|
|
|
*/
|
2019-11-29 02:13:55 +00:00
|
|
|
export default class Velociraptor extends React.Component {
|
|
|
|
static 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,
|
2019-11-29 02:13:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static defaultProps = {
|
|
|
|
startStyles: [],
|
|
|
|
};
|
2016-08-01 15:56:25 +00:00
|
|
|
|
2019-11-29 02:13:55 +00:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
2015-11-27 15:37:40 +00:00
|
|
|
|
|
|
|
this.nodes = {};
|
2016-04-21 13:14:08 +00:00
|
|
|
this._updateChildren(this.props.children);
|
2019-11-29 02:13:55 +00:00
|
|
|
}
|
2015-11-27 15:37:40 +00:00
|
|
|
|
2019-11-29 02:13:55 +00:00
|
|
|
componentDidUpdate() {
|
|
|
|
this._updateChildren(this.props.children);
|
|
|
|
}
|
2016-04-21 13:14:08 +00:00
|
|
|
|
2021-04-08 08:27:41 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param {HTMLElement} node element to apply styles to
|
|
|
|
* @param {object} styles a key/value pair of CSS properties
|
|
|
|
* @returns {Promise<void>} promise resolving when the applied styles have finished transitioning
|
|
|
|
*/
|
|
|
|
_applyStyles(node, styles) {
|
|
|
|
Object.entries(styles).forEach(([property, value]) => {
|
|
|
|
node.style[property] = value;
|
|
|
|
});
|
|
|
|
const transitionEndPromise = new Promise(resolve => {
|
|
|
|
node.addEventListener("transitionend", () => {
|
|
|
|
resolve();
|
|
|
|
}, { once: true });
|
|
|
|
});
|
|
|
|
|
|
|
|
return Promise.race([timeout(300), transitionEndPromise]);
|
|
|
|
}
|
|
|
|
|
2019-11-29 02:13:55 +00:00
|
|
|
_updateChildren(newChildren) {
|
2017-10-11 16:56:17 +00:00
|
|
|
const oldChildren = this.children || {};
|
2015-11-27 15:37:40 +00:00
|
|
|
this.children = {};
|
2019-11-29 02:13:55 +00:00
|
|
|
React.Children.toArray(newChildren).forEach((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];
|
2019-11-29 02:13:55 +00:00
|
|
|
const oldNode = ReactDom.findDOMNode(this.nodes[old.key]);
|
2015-11-27 15:37:40 +00:00
|
|
|
|
2019-11-29 02:13:55 +00:00
|
|
|
if (oldNode && oldNode.style.left !== c.props.style.left) {
|
2021-04-08 08:27:41 +00:00
|
|
|
this._applyStyles(oldNode, { left: c.props.style.left })
|
|
|
|
.then(() => {
|
|
|
|
// 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);
|
2015-11-27 15:37:40 +00:00
|
|
|
}
|
2019-11-29 02:13:55 +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;
|
|
|
|
}
|
2018-10-10 14:15:50 +00:00
|
|
|
// clone the old element with the props (and children) of the new element
|
|
|
|
// so prop updates are still received by the children.
|
2019-11-29 02:13:55 +00:00
|
|
|
this.children[c.key] = React.cloneElement(old, c.props, c.props.children);
|
2015-11-27 15:37:40 +00:00
|
|
|
} 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
|
|
|
|
2019-11-29 02:13:55 +00:00
|
|
|
const startStyles = this.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
|
|
|
|
2019-11-29 02:13:55 +00:00
|
|
|
newProps.ref = ((n) => this._collectNode(
|
2017-10-11 16:56:17 +00:00
|
|
|
c.key, n, restingStyle,
|
2016-08-01 15:56:25 +00:00
|
|
|
));
|
|
|
|
|
2019-11-29 02:13:55 +00:00
|
|
|
this.children[c.key] = React.cloneElement(c, newProps);
|
2015-11-27 15:37:40 +00:00
|
|
|
}
|
|
|
|
});
|
2019-11-29 02:13:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_collectNode(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;
|
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.
|
2021-04-08 08:27:41 +00:00
|
|
|
for (let i = 1; i < startStyles.length; ++i) {
|
|
|
|
this._applyStyles(domNode, startStyles[i]);
|
|
|
|
// console.log("start:"
|
|
|
|
// 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
|
2021-04-08 08:27:41 +00:00
|
|
|
setTimeout(() => {
|
|
|
|
this._applyStyles(domNode, restingStyle);
|
|
|
|
}, 0);
|
2016-04-20 21:21:07 +00:00
|
|
|
|
2021-02-27 05:21:14 +00:00
|
|
|
// console.log("enter:",
|
|
|
|
// JSON.stringify(restingStyle));
|
2015-11-27 15:37:40 +00:00
|
|
|
}
|
|
|
|
this.nodes[k] = node;
|
2019-11-29 02:13:55 +00:00
|
|
|
}
|
2015-11-27 15:37:40 +00:00
|
|
|
|
2019-11-29 02:13:55 +00:00
|
|
|
render() {
|
2015-11-27 15:37:40 +00:00
|
|
|
return (
|
2021-04-08 08:27:41 +00:00
|
|
|
<>{ Object.values(this.children) }</>
|
2015-11-27 15:37:40 +00:00
|
|
|
);
|
2019-11-29 02:13:55 +00:00
|
|
|
}
|
|
|
|
}
|
2021-04-08 08:27:41 +00:00
|
|
|
|
|
|
|
function timeout(time) {
|
|
|
|
return new Promise(resolve =>
|
|
|
|
setTimeout(() => resolve(), time),
|
|
|
|
);
|
|
|
|
}
|