Skip to content

Commit aa0ea72

Browse files
authored
Merge pull request #10 from itinance/forever_mode
Added a 'show-forever' mode and added a delay for manually close()
2 parents 2daa706 + dd6d745 commit aa0ea72

File tree

2 files changed

+39
-8
lines changed

2 files changed

+39
-8
lines changed

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,26 @@ Then you can use it like this:
5757

5858
That's it, you're ready to go!
5959

60+
Show a toast forever until you manually close it:
61+
```javascript
62+
this.refs.toast.show('hello world!', DURATION.FOREVER);
63+
64+
// later on:
65+
this.refs.toast.close('hello world!');
66+
```
67+
68+
Optional you can pass a delay in seconds to the close()-method:
69+
```javascript
70+
this.refs.toast.close('hello world!', 500);
71+
```
72+
73+
Currently, the default delay for close() in FOREVER-mode is set to 250 ms (or this.props.defaultCloseDelay, which you can pass with)
74+
75+
```jsx
76+
<Toast ... defaultCloseDelay={100} />
77+
```
78+
79+
6080

6181
### Basic usage
6282

index.js

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,29 @@ import {
1414
Dimensions,
1515
Text,
1616
} from 'react-native'
17-
export const DURATION = { LENGTH_LONG: 2000, LENGTH_SHORT: 500 };
17+
18+
export const DURATION = {
19+
LENGTH_LONG: 2000,
20+
LENGTH_SHORT: 500,
21+
FOREVER: 0,
22+
};
23+
1824
const {height, width} = Dimensions.get('window');
1925

2026
export default class Toast extends Component {
2127

2228
constructor(props) {
2329
super(props);
30+
2431
this.state = {
2532
isShow: false,
2633
text: '',
2734
opacityValue: new Animated.Value(this.props.opacity),
2835
}
2936
}
37+
3038
show(text, duration) {
31-
this.duration = duration || DURATION.LENGTH_SHORT;
39+
this.duration = typeof duration === 'number' ? duration : DURATION.LENGTH_SHORT;
3240

3341
this.setState({
3442
isShow: true,
@@ -43,14 +51,16 @@ export default class Toast extends Component {
4351
}
4452
).start(() => {
4553
this.isShow = true;
46-
this.close();
54+
if(duration !== DURATION.FOREVER) this.close();
4755
});
4856
}
4957

50-
close() {
51-
let delay = this.duration;
52-
53-
if (!this.isShow) return;
58+
close( duration ) {
59+
let delay = typeof duration === 'undefined' ? this.duration : duration;
60+
61+
if(delay === DURATION.FOREVER) delay = this.props.defaultCloseDelay || 250;
62+
63+
if (!this.isShow && !this.state.isShow) return;
5464
this.timer && clearTimeout(this.timer);
5565
this.timer = setTimeout(() => {
5666
Animated.timing(
@@ -85,7 +95,8 @@ export default class Toast extends Component {
8595
pos = height - this.props.positionValue;
8696
break;
8797
}
88-
let view = this.state.isShow ?
98+
99+
const view = this.state.isShow ?
89100
<View
90101
style={[styles.container, { top: pos }]}
91102
pointerEvents="none"

0 commit comments

Comments
 (0)