2022-05-06 11:09:28 +02: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.
|
|
|
|
*/
|
|
|
|
|
2024-01-03 12:36:11 +01:00
|
|
|
import React, { Key, MutableRefObject, ReactElement, ReactFragment, ReactInstance, ReactPortal } from "react";
|
2019-12-20 22:41:07 +01:00
|
|
|
import ReactDom from "react-dom";
|
2021-09-27 12:38:56 +02: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[];
|
2024-01-03 12:36:11 +01:00
|
|
|
|
|
|
|
innerRef?: MutableRefObject<any>;
|
2021-09-27 12:38:56 +02:00
|
|
|
}
|
2015-11-27 16:37:40 +01:00
|
|
|
|
2023-06-29 13:17:05 +02:00
|
|
|
function isReactElement(c: ReactElement | ReactFragment | ReactPortal): c is ReactElement {
|
|
|
|
return typeof c === "object" && "type" in c;
|
|
|
|
}
|
|
|
|
|
2015-11-27 16:37:40 +01:00
|
|
|
/**
|
2021-04-14 11:18:45 +02:00
|
|
|
* The NodeAnimator contains components and animates transitions.
|
2015-11-27 16:37:40 +01: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 12:38:56 +02:00
|
|
|
export default class NodeAnimator extends React.Component<IProps> {
|
2023-02-13 12:39:16 +01:00
|
|
|
private nodes: Record<string, ReactInstance> = {};
|
2023-05-05 10:11:14 +02:00
|
|
|
private children: { [key: string]: ReactElement } = {};
|
2021-09-28 07:48:56 +02:00
|
|
|
public static defaultProps: Partial<IProps> = {
|
2019-11-29 03:13:55 +01:00
|
|
|
startStyles: [],
|
|
|
|
};
|
2016-08-01 17:56:25 +02:00
|
|
|
|
2022-12-16 13:29:59 +01:00
|
|
|
public constructor(props: IProps) {
|
2019-11-29 03:13:55 +01:00
|
|
|
super(props);
|
2015-11-27 16:37:40 +01:00
|
|
|
|
2021-09-27 12:38:56 +02:00
|
|
|
this.updateChildren(this.props.children);
|
2019-11-29 03:13:55 +01:00
|
|
|
}
|
2015-11-27 16:37:40 +01:00
|
|
|
|
2021-09-27 12:38:56 +02:00
|
|
|
public componentDidUpdate(): void {
|
|
|
|
this.updateChildren(this.props.children);
|
2019-11-29 03:13:55 +01:00
|
|
|
}
|
2016-04-21 15:14:08 +02:00
|
|
|
|
2021-04-08 10:27:41 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param {HTMLElement} node element to apply styles to
|
2021-09-27 12:38:56 +02:00
|
|
|
* @param {React.CSSProperties} styles a key/value pair of CSS properties
|
2021-04-08 12:43:13 +02:00
|
|
|
* @returns {void}
|
2021-04-08 10:27:41 +02:00
|
|
|
*/
|
2021-09-27 12:38:56 +02:00
|
|
|
private applyStyles(node: HTMLElement, styles: React.CSSProperties): void {
|
2021-04-08 10:27:41 +02:00
|
|
|
Object.entries(styles).forEach(([property, value]) => {
|
2023-02-13 12:39:16 +01:00
|
|
|
node.style[property as keyof Omit<CSSStyleDeclaration, "length" | "parentRule">] = value;
|
2021-04-08 10:27:41 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-09-27 12:38:56 +02:00
|
|
|
private updateChildren(newChildren: React.ReactNode): void {
|
2017-10-11 18:56:17 +02:00
|
|
|
const oldChildren = this.children || {};
|
2015-11-27 16:37:40 +01:00
|
|
|
this.children = {};
|
2023-06-29 13:17:05 +02:00
|
|
|
React.Children.toArray(newChildren).forEach((c) => {
|
|
|
|
if (!isReactElement(c)) return;
|
2023-03-07 14:19:18 +01:00
|
|
|
if (oldChildren[c.key!]) {
|
|
|
|
const old = oldChildren[c.key!];
|
|
|
|
const oldNode = ReactDom.findDOMNode(this.nodes[old.key!]);
|
2015-11-27 16:37:40 +01:00
|
|
|
|
2021-09-27 12:38:56 +02: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 16:37:40 +01:00
|
|
|
}
|
2018-10-10 16:15:50 +02:00
|
|
|
// clone the old element with the props (and children) of the new element
|
|
|
|
// so prop updates are still received by the children.
|
2023-03-07 14:19:18 +01:00
|
|
|
this.children[c.key!] = React.cloneElement(old, c.props, c.props.children);
|
2015-11-27 16:37:40 +01:00
|
|
|
} else {
|
2016-08-01 17:56:25 +02:00
|
|
|
// new element. If we have a startStyle, use that as the style and go through
|
2015-11-27 16:37:40 +01:00
|
|
|
// the enter animations
|
2021-09-27 12:38:56 +02:00
|
|
|
const newProps: Partial<IChildProps> = {};
|
2017-10-11 18:56:17 +02:00
|
|
|
const restingStyle = c.props.style;
|
2016-08-01 17:56:25 +02:00
|
|
|
|
2019-11-29 03:13:55 +01:00
|
|
|
const startStyles = this.props.startStyles;
|
2016-08-01 17:56:25 +02:00
|
|
|
if (startStyles.length > 0) {
|
2017-10-11 18:56:17 +02:00
|
|
|
const startStyle = startStyles[0];
|
2015-11-27 16:37:40 +01:00
|
|
|
newProps.style = startStyle;
|
|
|
|
}
|
2016-08-01 17:56:25 +02:00
|
|
|
|
2023-03-07 14:19:18 +01:00
|
|
|
newProps.ref = (n) => this.collectNode(c.key!, n, restingStyle);
|
2016-08-01 17:56:25 +02:00
|
|
|
|
2023-03-07 14:19:18 +01:00
|
|
|
this.children[c.key!] = React.cloneElement(c, newProps);
|
2015-11-27 16:37:40 +01:00
|
|
|
}
|
|
|
|
});
|
2019-11-29 03:13:55 +01:00
|
|
|
}
|
|
|
|
|
2023-03-07 14:19:18 +01:00
|
|
|
private collectNode(k: Key, node: React.ReactInstance, restingStyle: React.CSSProperties): void {
|
2022-12-12 12:24:14 +01:00
|
|
|
if (node && this.nodes[k] === undefined && this.props.startStyles.length > 0) {
|
2017-10-11 18:56:17 +02:00
|
|
|
const startStyles = this.props.startStyles;
|
2017-01-20 15:22:27 +01:00
|
|
|
const domNode = ReactDom.findDOMNode(node);
|
2015-11-27 16:37:40 +01:00
|
|
|
// start from startStyle 1: 0 is the one we gave it
|
|
|
|
// to start with, so now we animate 1 etc.
|
2021-04-08 10:27:41 +02:00
|
|
|
for (let i = 1; i < startStyles.length; ++i) {
|
2021-09-27 12:38:56 +02:00
|
|
|
this.applyStyles(domNode as HTMLElement, startStyles[i]);
|
2015-11-27 16:37:40 +01:00
|
|
|
}
|
2016-08-01 17:56:25 +02:00
|
|
|
|
2015-11-27 16:37:40 +01:00
|
|
|
// and then we animate to the resting state
|
2022-11-30 12:32:56 +01:00
|
|
|
window.setTimeout(() => {
|
2021-09-27 12:38:56 +02:00
|
|
|
this.applyStyles(domNode as HTMLElement, restingStyle);
|
2021-04-08 10:27:41 +02:00
|
|
|
}, 0);
|
2015-11-27 16:37:40 +01:00
|
|
|
}
|
|
|
|
this.nodes[k] = node;
|
2024-01-03 12:36:11 +01:00
|
|
|
|
|
|
|
if (this.props.innerRef) {
|
|
|
|
this.props.innerRef.current = node;
|
|
|
|
}
|
2019-11-29 03:13:55 +01:00
|
|
|
}
|
2015-11-27 16:37:40 +01:00
|
|
|
|
2023-02-13 18:01:43 +01:00
|
|
|
public render(): React.ReactNode {
|
2022-12-12 12:24:14 +01:00
|
|
|
return <>{Object.values(this.children)}</>;
|
2019-11-29 03:13:55 +01:00
|
|
|
}
|
|
|
|
}
|