forked from Nubescope/react-native-floating-hearts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FloatingHearts.js
215 lines (174 loc) · 4.57 KB
/
FloatingHearts.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import React, { Component } from 'react'
import PropTypes from 'prop-types'
import { View, Animated, StyleSheet, ViewPropTypes } from 'react-native'
import HeartShape from './HeartShape'
/**
* @class FloatingHearts
*/
class FloatingHearts extends Component {
state = {
hearts: [],
height: null,
}
createHeart(index) {
return {
id: index,
right: getRandomNumber(50, 150),
}
}
removeHeart(id) {
this.setState({ hearts: this.state.hearts.filter(heart => heart.id !== id) })
}
componentWillUpdate(nextProps) {
const oldCount = this.props.count
const newCount = nextProps.count
const numHearts = newCount - oldCount
if (numHearts <= 0) {
return
}
const items = Array(numHearts).fill()
const newHearts = items.map((item, i) => oldCount + i).map(this.createHeart)
this.setState({ hearts: this.state.hearts.concat(newHearts) })
}
handleOnLayout = e => {
const height = e.nativeEvent.layout.height
this.setState({ height })
}
render() {
const { height } = this.state
const { color, renderCustomShape } = this.props
const isReady = height !== null
let heartProps = {}
if (color !== null) {
heartProps.color = color
}
return (
<View style={[styles.container, this.props.style]} onLayout={this.handleOnLayout} pointerEvents="none">
{isReady &&
this.state.hearts.map(({ id, right }) => (
<AnimatedShape key={id} height={height} style={{ right }} onComplete={this.removeHeart.bind(this, id)}>
{renderCustomShape ? renderCustomShape(id) : <HeartShape {...heartProps} />}
</AnimatedShape>
))}
</View>
)
}
}
FloatingHearts.propTypes = {
style: ViewPropTypes.style,
count: PropTypes.number,
color: PropTypes.string,
renderCustomShape: PropTypes.func,
}
FloatingHearts.defaultProps = {
count: -1,
}
/**
* @class AnimatedShape
*/
class AnimatedShape extends Component {
constructor(props) {
super(props)
this.state = {
position: new Animated.Value(0),
shapeHeight: null,
enabled: false,
animationsReady: false,
}
}
componentDidMount() {
Animated.timing(this.state.position, {
duration: 2000,
useNativeDriver: true,
toValue: this.props.height * -1,
}).start(this.props.onComplete)
}
getAnimationStyle() {
if (!this.state.animationsReady) {
return { opacity: 0 }
}
return {
transform: [
{ translateY: this.state.position },
{ translateX: this.xAnimation },
{ scale: this.scaleAnimation },
{ rotate: this.rotateAnimation },
],
opacity: this.opacityAnimation,
}
}
handleOnLayout = e => {
if (this.rendered) {
return null
}
this.rendered = true
const height = Math.ceil(this.props.height)
const negativeHeight = height * -1
const shapeHeight = e.nativeEvent.layout.height
this.yAnimation = this.state.position.interpolate({
inputRange: [negativeHeight, 0],
outputRange: [height, 0],
})
this.opacityAnimation = this.yAnimation.interpolate({
inputRange: [0, height - shapeHeight],
outputRange: [1, 0],
})
this.scaleAnimation = this.yAnimation.interpolate({
inputRange: [0, 15, 30, height],
outputRange: [0, 1.2, 1, 1],
})
this.xAnimation = this.yAnimation.interpolate({
inputRange: [0, height / 2, height],
outputRange: [0, 15, 0],
})
this.rotateAnimation = this.yAnimation.interpolate({
inputRange: [0, height / 4, height / 3, height / 2, height],
outputRange: ['0deg', '-2deg', '0deg', '2deg', '0deg'],
})
setTimeout(() => this.setState({ animationsReady: true }), 16)
}
render() {
return (
<Animated.View
style={[styles.shapeWrapper, this.getAnimationStyle(), this.props.style]}
onLayout={this.handleOnLayout}
>
{this.props.children}
</Animated.View>
)
}
}
AnimatedShape.propTypes = {
height: PropTypes.number.isRequired,
onComplete: PropTypes.func.isRequired,
style: View.propTypes.style,
children: PropTypes.node.isRequired,
}
AnimatedShape.defaultProps = {
onComplete: () => {},
}
/**
* Styles
*/
const styles = StyleSheet.create({
container: {
top: 0,
left: 0,
right: 0,
bottom: 0,
position: 'absolute',
},
shapeWrapper: {
position: 'absolute',
bottom: 0,
backgroundColor: 'transparent',
},
})
/**
* Helpers
*/
const getRandomNumber = (min, max) => Math.random() * (max - min) + min
/**
* Exports
*/
export default FloatingHearts