Skip to content

Instantly share code, notes, and snippets.

@hpierre74
Last active August 8, 2018 22:50
Show Gist options
  • Save hpierre74/3738f02bf426c1e9ac1a6a3197656001 to your computer and use it in GitHub Desktop.
Save hpierre74/3738f02bf426c1e9ac1a6a3197656001 to your computer and use it in GitHub Desktop.
import React, { Component } from "react";
import PropTypes from "prop-types";
import styled, { keyframes } from "styled-components";
const AnimationWrapper = styled.div`
animation: ${({ duration }) => duration}s ${({ animation }) => animation};
`;
const withAnim = ComponentToAnimate => {
class WithAnimHoc extends Component {
constructor(props) {
super(props);
this.state = {
AsyncComponent: () => null,
animation: this.props.animation,
duration: this.props.duration
};
}
async componentDidMount() {
const module = await import(`react-animations/lib/${
this.state.animation
}`);
const AsyncComponent = () => (
<AnimationWrapper
duration={this.state.duration}
animation={keyframes`${module.default}`}
>
<ComponentToAnimate {...this.props} />
</AnimationWrapper>
);
this.setState({ AsyncComponent });
}
render() {
const { AsyncComponent } = this.state;
return <AsyncComponent />;
}
}
WithAnimHoc.defaultProps = {
duration: "1.5"
};
WithAnimHoc.propTypes = {
animation: PropTypes.string.isRequired,
duration: PropTypes.string
};
return WithAnimHoc;
};
export default withAnim;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment