Skip to content

Added a 'show-forever' mode and added a delay for manually close() #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,26 @@ Then you can use it like this:

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

Show a toast forever until you manually close it:
```javascript
this.refs.toast.show('hello world!', DURATION.FOREVER);

// later on:
this.refs.toast.close('hello world!');
```

Optional you can pass a delay in seconds to the close()-method:
```javascript
this.refs.toast.close('hello world!', 500);
```

Currently, the default delay for close() in FOREVER-mode is set to 250 ms (or this.props.defaultCloseDelay, which you can pass with)

```jsx
<Toast ... defaultCloseDelay={100} />
```



### Basic usage

Expand Down
27 changes: 19 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,29 @@ import {
Dimensions,
Text,
} from 'react-native'
export const DURATION = { LENGTH_LONG: 2000, LENGTH_SHORT: 500 };

export const DURATION = {
LENGTH_LONG: 2000,
LENGTH_SHORT: 500,
FOREVER: 0,
};

const {height, width} = Dimensions.get('window');

export default class Toast extends Component {

constructor(props) {
super(props);

this.state = {
isShow: false,
text: '',
opacityValue: new Animated.Value(this.props.opacity),
}
}

show(text, duration) {
this.duration = duration || DURATION.LENGTH_SHORT;
this.duration = typeof duration === 'number' ? duration : DURATION.LENGTH_SHORT;

this.setState({
isShow: true,
Expand All @@ -43,14 +51,16 @@ export default class Toast extends Component {
}
).start(() => {
this.isShow = true;
this.close();
if(duration !== DURATION.FOREVER) this.close();
});
}

close() {
let delay = this.duration;

if (!this.isShow) return;
close( duration ) {
let delay = typeof duration === 'undefined' ? this.duration : duration;

if(delay === DURATION.FOREVER) delay = this.props.defaultCloseDelay || 250;

if (!this.isShow && !this.state.isShow) return;
this.timer && clearTimeout(this.timer);
this.timer = setTimeout(() => {
Animated.timing(
Expand Down Expand Up @@ -85,7 +95,8 @@ export default class Toast extends Component {
pos = height - this.props.positionValue;
break;
}
let view = this.state.isShow ?

const view = this.state.isShow ?
<View
style={[styles.container, { top: pos }]}
pointerEvents="none"
Expand Down