aboutsummaryrefslogtreecommitdiffstats
path: root/packages/dev-tools-pages/ts/components/Animations/index.tsx
blob: bf35c0784c2b7f4018c6eae3c8695645649b1034 (plain) (blame)
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
import * as React from 'react';
import Lottie from 'react-lottie';
import styled from 'styled-components';

import { media } from 'ts/variables';

interface AnimationProps {
    animationData: object;
    width: number;
    height: number;
}

interface AnimationState {
    width?: number;
    height?: number;
}

class Animation extends React.PureComponent<AnimationProps, AnimationState> {
    timeout = null as any;
    constructor(props: AnimationProps) {
        super(props);

        this.state = {
            height: undefined,
            width: undefined,
        };

        this.handleResize = this.handleResize.bind(this);
        this.updateAnimationSize = this.updateAnimationSize.bind(this);
    }

    componentDidMount() {
        this.updateAnimationSize();
        window.addEventListener('resize', this.handleResize);
    }

    componentWillUnmount() {
        window.removeEventListener('resize', this.handleResize);
    }

    handleResize() {
        clearTimeout(this.timeout);
        this.timeout = setTimeout(this.updateAnimationSize, 200);
    }

    updateAnimationSize() {
        const windowWidth = window.innerWidth;
        let width = undefined;
        let height = undefined;
        if (windowWidth < 1200) {
            const maxWidth = windowWidth + 250;
            const ratio = maxWidth / this.props.width;

            height = Math.round(this.props.height * ratio);
            width = Math.round(this.props.width * ratio);
        }

        this.setState({ width, height });
    }

    render() {
        let { animationData } = this.props;
        const height = this.state.height || this.props.height;
        const width = this.state.width || this.props.width;

        return (
            <Container height={height}>
                <InnerContainer>
                    <Lottie
                        width={width}
                        height={height}
                        options={{
                            loop: true,
                            autoplay: true,
                            animationData: animationData,
                        }}
                    />
                </InnerContainer>
            </Container>
        );
    }
}

const Container = styled.div`
    width: 100%;
    height: ${(props: { height: number }) => props.height}px;
    position: absolute;
    top: 40%;
    left: 0;
    z-index: -1;
    overflow: hidden;
    ${media.medium`
        top: auto;
        bottom: -3rem;
    `};
`;

const InnerContainer = styled.div`
    position: absolute;
    top: 0;
    left: 50%;
    transform: translateX(-50%);
`;

export default Animation;