-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.tsx
82 lines (65 loc) · 1.81 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import React, { ReactElement } from "react";
type Props = {
children: ReactElement;
className?: string;
style?: React.CSSProperties;
transition?: number;
};
type State = {};
export default class AutoTransition extends React.Component<Props, State> {
static defaultProps = {
style: {},
transition: 0.2,
};
name!: string;
private rootRef: React.RefObject<HTMLDivElement>;
constructor(props: Props) {
super(props);
this.rootRef = React.createRef();
}
getSnapshotBeforeUpdate() {
const element = this.rootRef.current;
if (element) {
return [
window.getComputedStyle(element).width,
window.getComputedStyle(element).height,
];
}
}
componentDidUpdate(prevProps: Props, prevState: State, snapshot: string[]) {
const element = this.rootRef.current;
if (!element) {
return;
}
element.style.width = "auto";
element.style.height = "auto";
const afterHeight = window.getComputedStyle(element).height;
const afterWidth = window.getComputedStyle(element).width;
const originTransition = window.getComputedStyle(element).transition;
element.style.transition = "none";
element.style.width = snapshot[0];
element.style.height = snapshot[1];
element.offsetWidth;
// element.offsetWidth = element.offsetWidth;
element.style.transition = originTransition;
element.style.width = afterWidth;
element.style.height = afterHeight;
}
render() {
const { transition, style, children, className } = this.props;
return (
<div
className={className}
ref={this.rootRef}
style={{
overflow: "hidden",
transition: `all ${transition}s`,
display: "inline-block",
...style,
}}
>
{children}
</div>
);
}
}