Skip to content

Commit 1a27b55

Browse files
authored
Merge pull request #7 from djock/master
Props for fadeIn and fadeOut; Set fadeIn effect for show()
2 parents cfdfb96 + 3bacaff commit 1a27b55

File tree

2 files changed

+43
-24
lines changed

2 files changed

+43
-24
lines changed

README.md

+9-1
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@ render() {
9494
ref="toast"
9595
style={{backgroundColor:'red'}}
9696
position='top'
97+
positionValue={200}
98+
fadeInDuration={750}
99+
fadeOutDuration={1000}
100+
opacity={0.8}
97101
textStyle={{color:'red'}}
98102
/>
99103
</View>
@@ -114,13 +118,17 @@ Props | Type | Optional | Default | Description
114118
----------------- | -------- | -------- | ----------- | -----------
115119
style | View.propTypes.style | true | {backgroundColor: 'black',opacity: OPACITY,borderRadius: 5,padding: 10,} | Custom style toast
116120
position | PropTypes.oneOf(['top','center','bottom',]) |true | 'bottom' | Custom toast position
121+
positionValue | React.PropTypes.number | true | 120 | Custom toast position value
122+
fadeInDuration | React.PropTypes.number | true | 500 | Custom toast show duration
123+
fadeOutDuration | React.PropTypes.number | true | 500 | Custom toast close duration
124+
opacity | React.PropTypes.number | true | 1 | Custom toast opacity
117125
textStyle | View.propTypes.style | true | {color:'white'} | Custom style text
118126

119127

120128
Method | Type | Optional | Description
121129
----------------- | -------- | -------- | ----------- | -----------
122130
show(text, duration) | function | false | show a toast,unit is millisecond
123-
close(instantly) | function | true | pass true to close instantly, empty or false to start the close timer
131+
close() | function | - | start the close timer
124132

125133

126134
## Contribution

index.js

+34-23
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,8 @@ import {
1414
Dimensions,
1515
Text,
1616
} from 'react-native'
17-
export const DURATION = { LENGTH_LONG: 2000, LENGTH_SHORT: 1000 };
17+
export const DURATION = { LENGTH_LONG: 2000, LENGTH_SHORT: 500 };
1818
const {height, width} = Dimensions.get('window');
19-
const OPACITY = 0.6;
2019

2120
export default class Toast extends Component {
2221

@@ -25,65 +24,70 @@ export default class Toast extends Component {
2524
this.state = {
2625
isShow: false,
2726
text: '',
28-
opacityValue: new Animated.Value(OPACITY),
27+
opacityValue: new Animated.Value(this.props.opacity),
2928
}
3029
}
3130
show(text, duration) {
3231
this.duration = duration || DURATION.LENGTH_SHORT;
32+
3333
this.setState({
3434
isShow: true,
3535
text: text,
3636
});
37-
this.isShow = true;
38-
this.state.opacityValue.setValue(OPACITY)
39-
this.close();
40-
}
4137

42-
close(instant) {
43-
var animationDuration = 500, closeDuration = this.duration;
44-
if (instant == true) {
45-
animationDuration = 0;
46-
closeDuration = 0;
47-
}
38+
Animated.timing(
39+
this.state.opacityValue,
40+
{
41+
toValue: this.props.opacity,
42+
duration: this.props.fadeInDuration,
43+
}
44+
).start(() => {
45+
this.isShow = true;
46+
this.close();
47+
});
48+
}
4849

50+
close() {
51+
let delay = this.duration;
52+
4953
if (!this.isShow) return;
5054
this.timer && clearTimeout(this.timer);
5155
this.timer = setTimeout(() => {
5256
Animated.timing(
5357
this.state.opacityValue,
5458
{
5559
toValue: 0.0,
56-
duration: animationDuration,
60+
duration: this.props.fadeOutDuration,
5761
}
5862
).start(() => {
5963
this.setState({
6064
isShow: false,
6165
});
6266
this.isShow = false;
6367
});
64-
}, closeDuration);
68+
}, delay);
6569
}
6670

6771
componentWillUnmount() {
6872
this.timer && clearTimeout(this.timer);
6973
}
7074

7175
render() {
72-
let top;
76+
let pos;
7377
switch (this.props.position) {
7478
case 'top':
75-
top = 120;
79+
pos = this.props.positionValue;
7680
break;
7781
case 'center':
78-
top = height / 2;
82+
pos = height / 2;
7983
break;
8084
case 'bottom':
81-
top = height - 160;
85+
pos = height - this.props.positionValue;
8286
break;
8387
}
8488
let view = this.state.isShow ?
8589
<View
86-
style={[styles.container, { top: top }]}
90+
style={[styles.container, { top: pos }]}
8791
pointerEvents="none"
8892
>
8993
<Animated.View
@@ -105,7 +109,6 @@ const styles = StyleSheet.create({
105109
},
106110
content: {
107111
backgroundColor: 'black',
108-
opacity: OPACITY,
109112
borderRadius: 5,
110113
padding: 10,
111114
},
@@ -121,10 +124,18 @@ Toast.propTypes = {
121124
'center',
122125
'bottom',
123126
]),
124-
textStyle: Text.propTypes.style
127+
textStyle: Text.propTypes.style,
128+
positionValue: React.PropTypes.number,
129+
fadeInDuration: React.PropTypes.number,
130+
fadeOutDuration: React.PropTypes.number,
131+
opacity: React.PropTypes.number
125132
}
126133

127134
Toast.defaultProps = {
128135
position: 'bottom',
129-
textStyle: styles.text
136+
textStyle: styles.text,
137+
positionValue: 120,
138+
fadeInDuration: 500,
139+
fadeOutDuration: 500,
140+
opacity: 1
130141
}

0 commit comments

Comments
 (0)