Skip to content

Instantly share code, notes, and snippets.

@masiamj
Created May 10, 2018 14:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save masiamj/17f84ba57c0a626c29195114fd5c36b5 to your computer and use it in GitHub Desktop.
Save masiamj/17f84ba57c0a626c29195114fd5c36b5 to your computer and use it in GitHub Desktop.
A generic way to apply enter transitions to a component.
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