Created
May 10, 2018 14:29
-
-
Save masiamj/17f84ba57c0a626c29195114fd5c36b5 to your computer and use it in GitHub Desktop.
A generic way to apply enter transitions to a component.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { Component } from 'react' | |
import type { ComponentType } from 'react' | |
import { omit, prop } from 'ramda' | |
import posed from 'react-pose' | |
/** | |
* @const poseProps | |
* @description Represents a fade up transition | |
* @type {{hidden: {y: number, opacity: number}, visible: {y: number, opacity: number}}} | |
*/ | |
const poseProps = { | |
hidden: { | |
y: 10, | |
opacity: 0 | |
}, | |
visible: { | |
y: 0, | |
opacity: 1 | |
} | |
} | |
/** | |
* @const PosedContainer | |
* @description Animated wrapper component | |
*/ | |
const PosedContainer = posed.div(poseProps) | |
/** | |
* @type BaseState | |
*/ | |
type BaseState = { | |
poseState: string, | |
} | |
/** | |
* @const withEnterTransition | |
* @param BaseComponent | |
*/ | |
const withEnterTransition = (BaseComponent: ComponentType<any>) => | |
class Wrapper extends Component<any, BaseState> { | |
constructor (props: any) { | |
super(props) | |
this.state = { | |
poseState: 'hidden' | |
} | |
} | |
/** | |
* @method componentDidMount | |
* @description Triggers the transition | |
*/ | |
componentDidMount () { | |
const timeout = prop('delay', this.props) | |
setTimeout(() => { | |
this.setState({ poseState: 'visible' }) | |
}, timeout || 500) | |
} | |
render () { | |
const { poseState } = this.state | |
const baseProps = omit(['delay'], this.props) | |
return ( | |
<PosedContainer pose={poseState}> | |
<BaseComponent {...baseProps} /> | |
</PosedContainer> | |
) | |
} | |
} | |
export default withEnterTransition |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment