-
-
Notifications
You must be signed in to change notification settings - Fork 302
/
ResizeObserverExample.tsx
141 lines (123 loc) · 3.31 KB
/
ResizeObserverExample.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import React, {
Fragment,
FC,
useEffect,
useRef,
useState,
CSSProperties,
MutableRefObject,
} from "react";
import { Button } from "@react-md/button";
import {
bem,
ResizeObserver,
ResizeObserverChangeEventHandler,
useToggle,
} from "@react-md/utils";
import { randomInt } from "utils/random";
import "./ResizeObserverExample.scss";
const block = bem("simple-resize");
const DEFAULT_HEIGHT = 100;
const DEFAULT_WIDTH = 150;
interface Size {
height: number;
width: number;
onResize: ResizeObserverChangeEventHandler;
}
/**
* This hook is used to handle the resize events from the `ResizeObserver`. This will update the
* table values with the current `height` and `width` while the new sizes are animating.
*/
function useSize(): Size {
const [size, setSize] = useState({
height: DEFAULT_HEIGHT,
width: DEFAULT_WIDTH,
});
const onResize: ResizeObserverChangeEventHandler = event => {
const { height, width } = event;
setSize({ height, width });
};
const { height, width } = size;
return {
height,
width,
onResize,
};
}
interface RandomStyleReturnValue {
style: CSSProperties;
containerRef: MutableRefObject<HTMLDivElement | null>;
}
/**
* This hook will create a random style for the container element so that
* the `maxHeight` and `maxWidth` can be animated. It'll stop and start
* when the `enabled` value is toggled.
*/
function useRandomStyle(enabled: boolean): RandomStyleReturnValue {
const [style, setStyle] = useState({
maxHeight: DEFAULT_HEIGHT,
maxWidth: DEFAULT_WIDTH,
});
const timeout = useRef<number | undefined>();
const containerRef = useRef<HTMLDivElement | null>(null);
useEffect(() => {
if (!enabled) {
return;
}
const randomize = (): void => {
const maxHeight = randomInt({ min: 100, max: 500 });
const maxWidth = randomInt({
min: 150,
max: containerRef.current ? containerRef.current.offsetWidth : 300,
});
setStyle({ maxHeight, maxWidth });
timeout.current = window.setTimeout(
randomize,
randomInt({ min: 2, max: 8 }) * 1000
);
};
randomize();
return () => {
window.clearTimeout(timeout.current);
};
}, [enabled]);
return { style, containerRef };
}
const SimpleExample: FC = () => {
const [enabled, , , toggle] = useToggle(false);
const { style, containerRef } = useRandomStyle(enabled);
const { height, width, onResize } = useSize();
return (
<Fragment>
<Button
id="start-resizing"
onClick={toggle}
theme="primary"
themeType="contained"
className={block("toggle")}
>
{enabled ? "Stop" : "Start"}
</Button>
<div ref={containerRef} className={block()} style={style}>
<ResizeObserver onResize={onResize} />
<table className={block("table")}>
<tbody>
<tr>
<th scope="row" className={block("th")}>
height:
</th>
<td className={block("td")}>{height}</td>
</tr>
<tr>
<th scope="row" className={block("th")}>
width:
</th>
<td className={block("td")}>{width}</td>
</tr>
</tbody>
</table>
</div>
</Fragment>
);
};
export default SimpleExample;