-
Notifications
You must be signed in to change notification settings - Fork 37
/
CinemaEffect.tsx
74 lines (64 loc) · 1.71 KB
/
CinemaEffect.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
import * as React from 'react';
import {extend} from 'lodash';
const height = '12.5%';
const duration = 3;
const bannerBase = {
position: 'absolute' as const,
left: 0,
right: 0,
background: 'black',
};
const animsCSS = `
@keyframes cinemaModeAnimIn {
0% { height: 0; }
100% { height: ${height}; }
}
@keyframes cinemaModeAnimOut {
0% { height: ${height}; }
100% { height: 0; }
}
`;
interface CEProps {
enabled: boolean;
}
interface CEState {
animate: boolean;
}
export default class CinemaEffect extends React.Component<CEProps, CEState> {
timeout?: number;
constructor(props) {
super(props);
this.state = {animate: false};
this.timeout = null;
}
componentWillReceiveProps(newProps) {
if (newProps.enabled !== this.props.enabled) {
this.setState({animate: true});
this.timeout = window.setTimeout(() => {
this.setState({animate: false});
this.timeout = null;
}, duration * 1000);
}
}
componentWillUnmount() {
if (this.timeout) {
clearTimeout(this.timeout);
}
}
render() {
const enabled = this.props.enabled;
const banner = extend({
height: enabled ? height : 0,
animation: this.state.animate
? `cinemaModeAnim${enabled ? 'In' : 'Out'} ${duration}s forwards`
: undefined
}, bannerBase);
const bannerTop = extend({top: 0}, banner);
const bannerBottom = extend({bottom: 0}, banner);
return <div>
<style>{animsCSS}</style>
<div style={bannerTop}/>
<div style={bannerBottom}/>
</div>;
}
}