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