element-web/src/Velociraptor.js

146 lines
5.3 KiB
JavaScript
Raw Normal View History

import React from "react";
import ReactDom from "react-dom";
import PropTypes from 'prop-types';
/**
* 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 = {
// either a list of child nodes, or a single child.
children: PropTypes.any,
// optional transition information for changing existing children
transition: PropTypes.object,
// a list of state objects to apply to each child node in turn
startStyles: PropTypes.array,
2019-11-29 02:13:55 +00:00
};
static defaultProps = {
startStyles: [],
};
2019-11-29 02:13:55 +00:00
constructor(props) {
super(props);
this.nodes = {};
this._updateChildren(this.props.children);
2019-11-29 02:13:55 +00:00
}
2019-11-29 02:13:55 +00:00
componentDidUpdate() {
this._updateChildren(this.props.children);
}
/**
*
* @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) {
const oldChildren = this.children || {};
this.children = {};
2019-11-29 02:13:55 +00:00
React.Children.toArray(newChildren).forEach((c) => {
if (oldChildren[c.key]) {
const old = oldChildren[c.key];
2019-11-29 02:13:55 +00:00
const oldNode = ReactDom.findDOMNode(this.nodes[old.key]);
2019-11-29 02:13:55 +00:00
if (oldNode && oldNode.style.left !== c.props.style.left) {
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);
}
2019-11-29 02:13:55 +00:00
if (oldNode && oldNode.style.visibility === 'hidden' && c.props.style.visibility === 'visible') {
oldNode.style.visibility = c.props.style.visibility;
}
// 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);
} else {
// new element. If we have a startStyle, use that as the style and go through
// the enter animations
const newProps = {};
const restingStyle = c.props.style;
2019-11-29 02:13:55 +00:00
const startStyles = this.props.startStyles;
if (startStyles.length > 0) {
const startStyle = startStyles[0];
newProps.style = startStyle;
// console.log("mounted@startstyle0: "+JSON.stringify(startStyle));
}
2019-11-29 02:13:55 +00:00
newProps.ref = ((n) => this._collectNode(
c.key, n, restingStyle,
));
2019-11-29 02:13:55 +00:00
this.children[c.key] = React.cloneElement(c, newProps);
}
});
2019-11-29 02:13:55 +00:00
}
_collectNode(k, node, restingStyle) {
if (
2016-01-08 16:43:25 +00:00
node &&
this.nodes[k] === undefined &&
this.props.startStyles.length > 0
) {
const startStyles = this.props.startStyles;
const domNode = ReactDom.findDOMNode(node);
// start from startStyle 1: 0 is the one we gave it
// to start with, so now we animate 1 etc.
for (let i = 1; i < startStyles.length; ++i) {
this._applyStyles(domNode, startStyles[i]);
// console.log("start:"
// JSON.stringify(startStyles[i]),
// );
}
// and then we animate to the resting state
setTimeout(() => {
this._applyStyles(domNode, restingStyle);
}, 0);
// console.log("enter:",
// JSON.stringify(restingStyle));
}
this.nodes[k] = node;
2019-11-29 02:13:55 +00:00
}
2019-11-29 02:13:55 +00:00
render() {
return (
<>{ Object.values(this.children) }</>
);
2019-11-29 02:13:55 +00:00
}
}
function timeout(time) {
return new Promise(resolve =>
setTimeout(() => resolve(), time),
);
}