-
Notifications
You must be signed in to change notification settings - Fork 18
/
CircleTransition.js
186 lines (172 loc) · 4.39 KB
/
CircleTransition.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
import React, { Component } from 'react'
import PropTypes from 'prop-types'
import { Easing, Modal, Dimensions, Animated } from 'react-native'
const { width, height } = Dimensions.get('window')
const reactMixin = require('react-mixin')
import TimerMixin from 'react-timer-mixin'
class CircleTransition extends Component {
constructor (props) {
super(props)
const {
scaleShrink,
scaleExpand,
expand
} = this.props
const initialScale = expand ? scaleShrink : scaleExpand
this.state = {
visible: false,
scale: new Animated.Value(initialScale)
}
this.setVisible = this.setVisible.bind(this)
this.setScale = this.setScale.bind(this)
this.resetCircle = this.resetCircle.bind(this)
}
start (callback) {
const {expand} = this.props
this.setVisible(true, () => this.animate(expand, callback))
}
animate (expand, callback) {
const {
scaleShrink,
scaleExpand,
easing
} = this.props
let toValue = expand ? scaleExpand : scaleShrink
Animated.timing(this.state.scale, {
toValue: toValue,
duration: this.props.duration,
easing: easing
}).start(() => {
callback()
this.hideCircle()
})
}
hideCircle () {
const { transitionBuffer } = this.props
// the circle disappears only when the transition is completed...
this.setTimeout(() => {
this.resetCircle()
}, transitionBuffer)
}
resetCircle () {
this.setVisible(false, () => {
this.setScale(0)
})
}
setVisible (visible, callback) {
this.setState({
visible: visible
}, callback)
}
setScale (scale) {
this.setState({
scale: new Animated.Value(scale)
})
}
getLeftPosition (position) {
const {size, customLeftMargin} = this.props
const halfSize = size / 2
const halfWidth = width / 2
let marginHorizontalTopLeft = -halfSize
switch (position) {
case 'center':
case 'top':
case 'bottom':
return marginHorizontalTopLeft + halfWidth
case 'topRight':
case 'bottomRight':
case 'right':
return marginHorizontalTopLeft + width
case 'custom':
return marginHorizontalTopLeft + customLeftMargin
default:
return marginHorizontalTopLeft
}
}
getTopPosition (position) {
const {size, customTopMargin} = this.props
const halfSize = size / 2
const halfHeight = height / 2
let marginVerticalTopLeft = -halfSize
switch (position) {
case 'center':
case 'left':
case 'right':
return marginVerticalTopLeft + halfHeight
case 'bottomLeft':
case 'bottomRight':
case 'bottom':
return marginVerticalTopLeft + height
case 'custom':
return marginVerticalTopLeft + customTopMargin
default:
return marginVerticalTopLeft
}
}
render () {
const {scale, visible} = this.state
const { size, color, position } = this.props
let topPosition = this.getTopPosition(position)
let leftPosition = this.getLeftPosition(position)
return (
<Modal
animationType='none'
transparent
visible={visible}
onRequestClose={() => {}}>
<Animated.View style={{
position: 'absolute',
backgroundColor: color,
top: topPosition,
left: leftPosition,
width: size,
height: size,
borderRadius: size / 2,
transform: [{
scale: scale
}]
}} />
</Modal>
)
}
}
reactMixin(CircleTransition.prototype, TimerMixin)
CircleTransition.propTypes = {
color: PropTypes.string,
size: PropTypes.number,
scaleShrink: PropTypes.number,
scaleExpand: PropTypes.number,
duration: PropTypes.number,
transitionBuffer: PropTypes.number,
position: PropTypes.oneOf([
'topLeft',
'topRight',
'bottomLeft',
'bottomRight',
'center',
'left',
'right',
'top',
'bottom',
'custom'
]),
customLeftMargin: PropTypes.number,
customTopMargin: PropTypes.number,
expand: PropTypes.bool,
easing: PropTypes.func,
sizeBeforeExpanding: PropTypes.number
}
CircleTransition.defaultProps = {
color: 'orange',
size: Math.min(width, height) - 1,
scaleShrink: 0,
scaleExpand: 4,
duration: 800,
transitionBuffer: 5,
position: 'topLeft',
expand: true,
customLeftMargin: 0,
customTopMargin: 0,
easing: Easing.linear
}
export default CircleTransition