-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanimationProvider.js
128 lines (113 loc) · 3.4 KB
/
animationProvider.js
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
import React, { Component } from 'react'
import { animation } from './utils/animate'
import ease from 'ease-component'
const animationProvider = (WrappedComponent) => class extends Component {
constructor (props, context, updater) {
super(props, context, updater)
if (!props.animate) {
throw new Error('Provide animate prop to component')
}
const parsedAnimatedProps = this.getAnimatedPropsForState(props.animate)
this.state = parsedAnimatedProps
this.animations = {}
}
getAnimatedPropsForState (animate) {
return Object.keys(animate).reduce((acc, propKey) => {
const prop = animate[propKey]
if (prop.to !== prop.from) {
acc = {
...acc,
[propKey]: prop.from
}
}
return acc
}, {})
}
createAnimation (key, animate) {
return animation({
draw: (progress) => {
const value = ((progress * (animate[key].to - animate[key].from)) + animate[key].from)
this.setState({
[key]: animate[key].int
? Math.round(value)
: value
})
},
duration: animate[key].duration || 1000,
easing: (() => {
const inputEasing = animate[key].easing
if (typeof inputEasing === 'string') {
return ease[inputEasing]
}
if (typeof inputEasing === 'function') {
return inputEasing
}
return ease.inOutSine
})()
})
}
addAnimation (key, animation) {
this.animations[key] = animation
}
addAllAnimations (animate) {
Object.keys(this.state).forEach((stateKey) => {
const animation = this.createAnimation(stateKey, animate)
this.addAnimation(stateKey, animation)
})
}
runAnimation (key) {
this.animations && this.animations[key] && this.animations[key].start()
}
runAllAnimations () {
Object.keys(this.animations).forEach(animationKey => this.animations[animationKey].start())
}
clearAnimation (animationName) {
this.animations[animationName] && this.animations[animationName].stop()
delete this.animations[animationName]
}
clearAllAnimations () {
Object.keys(this.animations).forEach(this.clearAnimation.bind(this))
}
getUpdatedAnimatedKeys (prevProps, nextProps) {
const prevAnimate = prevProps.animate
const nextAnimate = nextProps.animate
return Object.keys(prevAnimate).reduce((acc, animateKey) => {
const prevProp = prevAnimate[animateKey]
const nextProp = nextAnimate[animateKey]
if (prevProp.to !== nextProp.to) {
acc.push(animateKey)
}
return acc
}, [])
}
componentDidMount () {
const { animate } = this.props
this.addAllAnimations(animate)
this.runAllAnimations()
}
componentWillReceiveProps (nextProps) {
const keys = this.getUpdatedAnimatedKeys(this.props, nextProps)
keys.forEach(key => {
this.clearAnimation(key)
const from = typeof this.state[key] !== 'undefined'
? this.state[key]
: this.props.animate[key].to
const animation = this.createAnimation(key, {
...nextProps.animate,
[key]: {
...nextProps.animate[key],
from
}
})
this.addAnimation(key, animation)
this.runAnimation(key)
})
}
componentWillUnmount () {
this.clearAllAnimations()
}
render () {
return <WrappedComponent {...{...this.props, ...this.state}} />
}
}
export default animationProvider