All files / src/Animate Animate.js

0% Statements 0/28
0% Branches 0/37
0% Functions 0/2
0% Lines 0/25

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                                                                                                                                                                                                                                                                 
import React, { Component, cloneElement } from 'preact';
import PropTypes from 'prop-types';
import styled, { css } from 'styled-components';
import Animations from './../Animations';
 
/**
 * Wrapper for animating the external component
 *
 * @example ./../../docs/components/Animate.md
 */
class Animate extends Component {
    static propTypes = {
        /**
         * Component to be animated
         */
        component: PropTypes.element.isRequired,
 
        /**
         * Animation properties
         */
        animation: PropTypes.shape({
            active: PropTypes.bool,
            duration: PropTypes.number,
            timingFunction: PropTypes.string,
            delay: PropTypes.string,
            iterationCount: PropTypes.number,
            direction: PropTypes.string,
            fillMode: PropTypes.string,
            playState: PropTypes.string,
            onStart: PropTypes.func,
            onComplete: PropTypes.func,
            onIteration: PropTypes.func,
        }),
 
        /**
         * Element attributes
         */
        attrs: PropTypes.object,
    };
 
    static defaultProps = {
        animation: {},
        attrs: {},
    };
 
    componentWillMount() {
        const { component } = this.props;
        this.component = props =>
            cloneElement(component, {
                ...props,
            });
    }
 
    componentDidMount() {
        if (this.comp) {
            this.comp
                .getDOMNode()
                .addEventListener('animationstart', this.handleAnimationStart);
            this.comp
                .getDOMNode()
                .addEventListener('animationend', this.handleAnimationComplete);
            this.comp
                .getDOMNode()
                .addEventListener(
                    'animationiteration',
                    this.handleAnimationIteration
                );
        }
    }
 
    handleAnimationStart = () => {
        const { animation } = this.props;
        if (animation && typeof animation.onStart === 'function') {
            animation.onStart(this.comp, animation);
        }
    };
 
    handleAnimationComplete = () => {
        const { animation } = this.props;
        if (animation && typeof animation.onComplete === 'function') {
            animation.onComplete(this.comp, animation);
        }
    };
 
    handleAnimationIteration = () => {
        const { animation } = this.props;
        if (animation && typeof animation.onIteration === 'function') {
            animation.onIteration(this.comp, animation);
        }
    };
 
    render() {
        const { animation, attrs = {} } = this.props;
 
        const {
            duration = '500ms',
            timingFunction = 'linear',
            delay = '0s',
            iterationCount = 0,
            direction = '',
            fillMode = '',
            playState = '',
            active = true,
        } = animation;
 
        const name = Animations[animation.name] || animation.name || '';
 
        const AnimatedComponent = styled(this.component).attrs({
            ...attrs,
        })`
            ${active &&
                css`
                    animation-name: ${name};
                    animation-duration: ${duration};
                    animation-timing-function: ${timingFunction};
                    animation-delay: ${delay};
                    animation-iteration-count: ${iterationCount};
                    animation-direction: ${direction};
                    animation-fill-mode: ${fillMode};
                    animation-play-state: ${playState};
                `};
        `;
 
        return <AnimatedComponent innerRef={comp => (this.comp = comp)} />;
    }
}
 
export default Animate;