1.4.0 • Published 4 years ago

react-native-elementary-toast v1.4.0

Weekly downloads
2
License
MIT
Repository
github
Last release
4 years ago

react-native-elementary-toast

A react native module to show toast like android, it works on iOS and Android.

This is based on work at https://github.com/crazycodeboy/react-native-easy-toast.

PRs Welcome NPM version License MIT

Content

Installation

  • 1.Run npm i react-native-elementary-toast --save
  • 2.import Toast, {DURATION} from 'react-native-elementary-toast'

Demo

Screenshots

Getting started

Add react-native-elementary-toast to your js file.

import Toast, {DURATION} from 'react-native-elementary-toast'

Inside your component's render method, use Toast:

 render() {
         return (
             <View style={styles.container}>
                 ...
                 <Toast ref="toast"/>
             </View>
         );
 }

Note: Add it in the bottom of the root view.

Then you can use it like this:

 this.refs.toast.show('hello world!');

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

show a toast, and execute callback function when toast close it:

    this.refs.toast.show('hello world!', 500, () => {
        // something you want to do at close
    });

Show a toast forever until you manually close it:

 this.refs.toast.show('hello world!', DURATION.FOREVER);

Or pass an element:

    this.refs.toast.show(<View><Text>hello world!</Text></View>);

// 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)

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

Basic usage

render() {
        return (
            <View style={styles.container}>
                <TouchableHighlight
                    style={{padding: 10}}
                    onPress={()=>{
                        this.refs.toast.show('hello world!');
                    }}>
                    <Text>Press me</Text>
                </TouchableHighlight>
                <Toast ref="toast"/>
            </View>
        );
    }

Custom Toast

render() {
        return (
            <View style={styles.container}>
                <TouchableHighlight
                    style={{padding: 10}}
                    onPress={()=>{
                        this.refs.toast.show('hello world!',DURATION.LENGTH_LONG);
                    }}>
                    <Text>Press me</Text>
                </TouchableHighlight>
                <Toast
                    ref="toast"
                    style={{backgroundColor:'red'}}
                    position='top'
                    positionValue={200}
                    fadeInDuration={750}
                    fadeOutDuration={1000}
                    opacity={0.8}
                    textStyle={{color:'red'}}
                />
            </View>
        );
    }

API

PropsTypeOptionalDefaultDescription
styleView.propTypes.styletrue{backgroundColor: 'black',opacity: OPACITY,borderRadius: 5,padding: 10,}Custom style toast
positionPropTypes.oneOf('top','center','bottom',)true'bottom'Custom toast position
positionValueReact.PropTypes.numbertrue120Custom toast position value
fadeInDurationReact.PropTypes.numbertrue500Custom toast show duration
fadeOutDurationReact.PropTypes.numbertrue500Custom toast close duration
opacityReact.PropTypes.numbertrue1Custom toast opacity
textStyleView.propTypes.styletrue{color:'white'}Custom style text
MethodTypeOptionalDescription
show(text, duration, callback)functionfalseshow a toast,unit is millisecond,and do callback
close()function-start the close timer

Contribution

Issues are welcome. Please add a screenshot of bug and code snippet. Quickest way to solve issue is to reproduce it on one of the examples.

Pull requests are welcome. If you want to change API or making something big better to create issue and discuss it first.


MIT Licensed