2022-05-06 12:09:28 +03:00
|
|
|
/*
|
|
|
|
Copyright 2022 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2019-12-21 00:41:07 +03:00
|
|
|
import React from "react";
|
|
|
|
import ReactDom from "react-dom";
|
2021-09-27 13:38:56 +03:00
|
|
|
|
|
|
|
interface IChildProps {
|
|
|
|
style: React.CSSProperties;
|
|
|
|
ref: (node: React.ReactInstance) => void;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface IProps {
|
|
|
|
// either a list of child nodes, or a single child.
|
|
|
|
children: React.ReactNode;
|
|
|
|
|
|
|
|
// optional transition information for changing existing children
|
|
|
|
transition?: object;
|
|
|
|
|
|
|
|
// a list of state objects to apply to each child node in turn
|
|
|
|
startStyles: React.CSSProperties[];
|
|
|
|
}
|
2015-11-27 18:37:40 +03:00
|
|
|
|
|
|
|
/**
|
2021-04-14 12:18:45 +03:00
|
|
|
* The NodeAnimator contains components and animates transitions.
|
2015-11-27 18:37:40 +03:00
|
|
|
* 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.
|
|
|
|
*/
|
2021-09-27 13:38:56 +03:00
|
|
|
export default class NodeAnimator extends React.Component<IProps> {
|
|
|
|
private nodes = {};
|
|
|
|
private children: { [key: string]: React.DetailedReactHTMLElement<any, HTMLElement> };
|
2021-09-28 08:48:56 +03:00
|
|
|
public static defaultProps: Partial<IProps> = {
|
2019-11-29 05:13:55 +03:00
|
|
|
startStyles: [],
|
|
|
|
};
|
2016-08-01 18:56:25 +03:00
|
|
|
|
2021-09-27 13:38:56 +03:00
|
|
|
constructor(props: IProps) {
|
2019-11-29 05:13:55 +03:00
|
|
|
super(props);
|
2015-11-27 18:37:40 +03:00
|
|
|
|
2021-09-27 13:38:56 +03:00
|
|
|
this.updateChildren(this.props.children);
|
2019-11-29 05:13:55 +03:00
|
|
|
}
|
2015-11-27 18:37:40 +03:00
|
|
|
|
2021-09-27 13:38:56 +03:00
|
|
|
public componentDidUpdate(): void {
|
|
|
|
this.updateChildren(this.props.children);
|
2019-11-29 05:13:55 +03:00
|
|
|
}
|
2016-04-21 16:14:08 +03:00
|
|
|
|
2021-04-08 11:27:41 +03:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param {HTMLElement} node element to apply styles to
|
2021-09-27 13:38:56 +03:00
|
|
|
* @param {React.CSSProperties} styles a key/value pair of CSS properties
|
2021-04-08 13:43:13 +03:00
|
|
|
* @returns {void}
|
2021-04-08 11:27:41 +03:00
|
|
|
*/
|
2021-09-27 13:38:56 +03:00
|
|
|
private applyStyles(node: HTMLElement, styles: React.CSSProperties): void {
|
2021-04-08 11:27:41 +03:00
|
|
|
Object.entries(styles).forEach(([property, value]) => {
|
|
|
|
node.style[property] = value;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-09-27 13:38:56 +03:00
|
|
|
private updateChildren(newChildren: React.ReactNode): void {
|
2017-10-11 19:56:17 +03:00
|
|
|
const oldChildren = this.children || {};
|
2015-11-27 18:37:40 +03:00
|
|
|
this.children = {};
|
2021-09-27 13:38:56 +03:00
|
|
|
React.Children.toArray(newChildren).forEach((c: any) => {
|
2015-11-27 18:37:40 +03:00
|
|
|
if (oldChildren[c.key]) {
|
2017-10-11 19:56:17 +03:00
|
|
|
const old = oldChildren[c.key];
|
2019-11-29 05:13:55 +03:00
|
|
|
const oldNode = ReactDom.findDOMNode(this.nodes[old.key]);
|
2015-11-27 18:37:40 +03:00
|
|
|
|
2021-09-27 13:38:56 +03:00
|
|
|
if (oldNode && (oldNode as HTMLElement).style.left !== c.props.style.left) {
|
|
|
|
this.applyStyles(oldNode as HTMLElement, { left: c.props.style.left });
|
2015-11-27 18:37:40 +03:00
|
|
|
}
|
2018-10-10 17:15:50 +03: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 05:13:55 +03:00
|
|
|
this.children[c.key] = React.cloneElement(old, c.props, c.props.children);
|
2015-11-27 18:37:40 +03:00
|
|
|
} else {
|
2016-08-01 18:56:25 +03:00
|
|
|
// new element. If we have a startStyle, use that as the style and go through
|
2015-11-27 18:37:40 +03:00
|
|
|
// the enter animations
|
2021-09-27 13:38:56 +03:00
|
|
|
const newProps: Partial<IChildProps> = {};
|
2017-10-11 19:56:17 +03:00
|
|
|
const restingStyle = c.props.style;
|
2016-08-01 18:56:25 +03:00
|
|
|
|
2019-11-29 05:13:55 +03:00
|
|
|
const startStyles = this.props.startStyles;
|
2016-08-01 18:56:25 +03:00
|
|
|
if (startStyles.length > 0) {
|
2017-10-11 19:56:17 +03:00
|
|
|
const startStyle = startStyles[0];
|
2015-11-27 18:37:40 +03:00
|
|
|
newProps.style = startStyle;
|
|
|
|
}
|
2016-08-01 18:56:25 +03:00
|
|
|
|
2021-09-27 13:38:56 +03:00
|
|
|
newProps.ref = (n) => this.collectNode(c.key, n, restingStyle);
|
2016-08-01 18:56:25 +03:00
|
|
|
|
2019-11-29 05:13:55 +03:00
|
|
|
this.children[c.key] = React.cloneElement(c, newProps);
|
2015-11-27 18:37:40 +03:00
|
|
|
}
|
|
|
|
});
|
2019-11-29 05:13:55 +03:00
|
|
|
}
|
|
|
|
|
2021-09-27 13:38:56 +03:00
|
|
|
private collectNode(k: string, node: React.ReactInstance, restingStyle: React.CSSProperties): void {
|
2016-08-01 18:56:25 +03:00
|
|
|
if (node && this.nodes[k] === undefined && this.props.startStyles.length > 0) {
|
2017-10-11 19:56:17 +03:00
|
|
|
const startStyles = this.props.startStyles;
|
2017-01-20 17:22:27 +03:00
|
|
|
const domNode = ReactDom.findDOMNode(node);
|
2015-11-27 18:37:40 +03:00
|
|
|
// start from startStyle 1: 0 is the one we gave it
|
|
|
|
// to start with, so now we animate 1 etc.
|
2021-04-08 11:27:41 +03:00
|
|
|
for (let i = 1; i < startStyles.length; ++i) {
|
2021-09-27 13:38:56 +03:00
|
|
|
this.applyStyles(domNode as HTMLElement, startStyles[i]);
|
2015-11-27 18:37:40 +03:00
|
|
|
}
|
2016-08-01 18:56:25 +03:00
|
|
|
|
2015-11-27 18:37:40 +03:00
|
|
|
// and then we animate to the resting state
|
2022-11-30 14:32:56 +03:00
|
|
|
window.setTimeout(() => {
|
2021-09-27 13:38:56 +03:00
|
|
|
this.applyStyles(domNode as HTMLElement, restingStyle);
|
2021-04-08 11:27:41 +03:00
|
|
|
}, 0);
|
2015-11-27 18:37:40 +03:00
|
|
|
}
|
|
|
|
this.nodes[k] = node;
|
2019-11-29 05:13:55 +03:00
|
|
|
}
|
2015-11-27 18:37:40 +03:00
|
|
|
|
2021-09-27 13:38:56 +03:00
|
|
|
public render(): JSX.Element {
|
2021-04-08 11:27:41 +03:00
|
|
|
return <>{Object.values(this.children)}</>;
|
2019-11-29 05:13:55 +03:00
|
|
|
}
|
|
|
|
}
|