This repository has been archived by the owner on Dec 6, 2022. It is now read-only.
forked from cdvntr/react-native-confetti
-
Notifications
You must be signed in to change notification settings - Fork 0
/
confettiView.js
78 lines (69 loc) · 1.82 KB
/
confettiView.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
import React, {Component} from 'react';
import {
AppRegistry,
StyleSheet,
View,
Animated
} from 'react-native';
import Confetti from "./confetti.js";
class ConfettiView extends Component {
constructor(props) {
super(props);
this.state = {confettis: []};
this.confettiIndex = 0;
this.stopConfetti = false;
}
startConfetti() {
let {confettis} = this.state;
let {confettiCount, timeout} = this.props;
this.stopConfetti = false;
if(this.confettiIndex < confettiCount) {
setTimeout(() => {
if (this.stopConfetti) {
return;
} else {
confettis.push({key: this.confettiIndex});
this.confettiIndex++;
this.setState({confettis});
this.startConfetti();
}
}, timeout);
}
}
removeConfetti(key) {
let {confettis} = this.state;
let {confettiCount} = this.props;
let index = confettis.findIndex(confetti => {return confetti.key === key});
confettis.splice(index, 1);
this.setState({confettis});
if(key === confettiCount - 1) {
this.confettiIndex = 0;
}
}
stopConfetti ()
{
this.stopConfetti = true;
}
render() {
let {confettis} = this.state;
let {...otherProps} = this.props
return <View style={styles.container}>
{confettis.map(confetti => {
return <Confetti key={confetti.key} index={confetti.key} onComplete={this.removeConfetti.bind(this, confetti.key)} colors={this.props.colors} {...otherProps}/>
})}
</View>
}
}
ConfettiView.defaultProps = {
confettiCount: 100,
timeout: 30
}
const styles = StyleSheet.create({
container: {
position: 'absolute',
top: 0,
left: 0,
right: 0
}
});
export default ConfettiView;