@@ -30,255 +30,45 @@ npm install react-native-toastable
30
30
```
31
31
32
32
## Usage
33
- Place ` Toastable ` component at the root of your app, and use ` showToastable ` function anywhere in your app to show.
33
+ Place ` Toastable ` component at the root of your app, and import ` showToastable ` function anywhere in your app to show.
34
34
35
35
* All examples below assume that you have placed ` Toastable ` component at the root of your app and imported necessary components and functions.*
36
- ### Basic Usage
37
36
38
37
``` js
39
- import * as React from ' react' ;
38
+ import { useSafeAreaInsets } from ' react-native-safe-area-context' ;
39
+ import Toastable from ' react-native-toastable' ;
40
40
41
- import { Button , StyleSheet , View } from ' react-native ' ;
42
- import Toastable , { showToastable } from ' react-native-toastable ' ;
41
+ export default function RootNavigation () {
42
+ const { top } = useSafeAreaInsets () ;
43
43
44
- export default function Example () {
45
44
return (
46
- < View style= {{flex: 1 }}>
47
- < Button
48
- title= " Show Toastable"
49
- onPress= {() =>
50
- showToastable ({
51
- title: ' React Native Heroes' ,
52
- message: ' We are the heroes of React Native 🚀' ,
53
- status: ' info' ,
54
- })
55
- }
56
- / >
57
- < Toastable / >
58
- < / View>
59
- );
60
- }
61
- ```
62
-
63
- ### Queueing
64
-
65
- ``` js
66
- let number = 0
67
-
68
- export default function Example () {
69
- return (
70
- < View style= {{flex: 1 }}>
71
- < Button
72
- title= " Show Toastable"
73
- onPress= {() => {
74
- const status = [' success' , ' info' , ' warning' , ' danger' ][Math .floor (Math .random () * 4 )];
75
- number++ ;
76
- showToastable ({
77
- message: ' Message ' + number,
78
- title: ' React Native Heroes ' + status,
79
- status,
80
- duration: 1500 ,
81
- });
82
- }}
83
- / >
84
- < Toastable / >
85
- < / View>
86
- );
87
- }
88
- ```
89
-
90
- ### Custom Toastable - Local Component
91
- You can pass your own ` renderContent ` function to ` showToastable ` function.
92
-
93
- ``` js
94
- export default function Example () {
95
- return (
96
- < View style= {{flex: 1 }}>
97
- < Button
98
- title= " Show Toastable"
99
- onPress= {() =>
100
- showToastable ({
101
- renderContent : ({ message, title, status = ' info' }) => (
102
- < View
103
- style= {{
104
- flexDirection: ' row' ,
105
- backgroundColor: TOASTABLE_STATUS_MAP [status],
106
- paddingVertical: 12 ,
107
- paddingHorizontal: 16 ,
108
- borderRadius: 12 ,
109
- }}>
110
- < Icon size= {20 } name= " cloud-upload" / >
111
- < View style= {{ marginLeft: 12 , flex: 1 }}>
112
- < Text style= {{ marginTop: 2 }}>
113
- {title}
114
- < / Text >
115
- < Text
116
- numberOfLines= {1 }
117
- adjustsFontSizeToFit
118
- style= {{ marginTop: 8 }}>
119
- {message}
120
- < / Text >
121
- < / View>
122
- < Icon size= {20 } name= " x" / >
123
- < / View>
124
- ),
125
- message: ' filetitle.pdf was uploaded successfully' ,
126
- title: ' Upload successful' ,
127
- status: ' success' ,
128
- })
129
- }
130
- / >
131
- < / View>
132
- );
133
- }
134
-
135
- ```
136
-
137
- ### Custom Toastable - Global Component
138
- You can pass your own ` renderContent ` function to ` Toastable ` component.
139
-
140
- ``` js
141
- export default function Example () {
142
- return (
143
- < View style= {{flex: 1 }}>
144
- < Button
145
- title= " Show Toastable"
146
- onPress= {() =>
147
- showToastable ({
148
- title: ' React Native Heroes' ,
149
- message: ' We are the heroes of React Native 🚀' ,
150
- status: ' success' ,
151
- })
152
- }
153
- / >
154
- < Toastable
155
- renderContent= {(props ) => {
156
- return (
157
- < View
158
- style= {{
159
- flexDirection: ' row' ,
160
- justifyContent: ' space-between' ,
161
- alignItems: ' center' ,
162
- paddingVertical: 16 ,
163
- paddingHorizontal: 12 ,
164
- backgroundColor: TOASTABLE_STATUS_MAP [props .status ?? ' info' ],
165
- borderRadius: 12 ,
166
- borderBottomLeftRadius: 0 ,
167
- borderBottomRightRadius: 0 ,
168
- }}>
169
- < View style= {dw .rowHCenter }>
170
- < ClearText color= " gray-100" > {props .title }< / ClearText>
171
- < ClearText
172
- color= " gray-400"
173
- style= {{ marginTop: 4 }}
174
- variant= " small-none-regular" >
175
- {props .message }
176
- < / ClearText>
177
- < / View>
178
- < / View>
179
- );
180
- }}
181
- / >
182
- < / View>
183
- );
184
- }
185
-
186
- ```
187
- ### Custom Toastable - Custom Status Colors
188
-
189
- ``` js
190
- export default function Example () {
191
- return (
192
- < View style= {{flex: 1 }}>
193
- < Button
194
- title= " Show Toastable"
195
- onPress= {() => showToastable ({ message: ' React Native Heroes is awesome! 🚀' , status: ' success' })}
196
- / >
45
+ < View style= {{ flex: 1 }}>
46
+ < NavigationContainer / >
197
47
< Toastable
198
48
statusMap= {{
199
- success: ' #00BFA6 ' ,
200
- danger: ' #FF5252 ' ,
201
- warning: ' #FFD600 ' ,
202
- info: ' #2962FF ' ,
49
+ success: ' red '
50
+ danger: ' yellow '
51
+ warning: ' green '
52
+ info: ' blue '
203
53
}}
54
+ offset= {top}
55
+ position= " top"
204
56
/ >
205
57
< / View>
206
58
);
207
59
}
208
60
209
- ```
210
-
211
- ## Advanced Usage
212
61
213
- ### Control toastable's content through ` showToastable ` function
214
-
215
- ``` js
216
- export default function Example () {
62
+ export default function HomeScreen () {
217
63
return (
218
64
< View style= {{flex: 1 }}>
219
65
< Button
220
66
title= " Show Toastable"
221
- onPress= {() =>
222
- showToastable ({
223
- message: ' React Native Heroes is awesome! 🚀' ,
224
- alwaysVisible: true ,
225
- animationInTiming: 1000 ,
226
- animationOutTiming: 1000 ,
227
- backgroundColor: ' red' ,
228
- duration: 2000 ,
229
- contentStyle: {
230
- marginHorizontal: 20 ,
231
- },
232
- onPress : () => {
233
- console .log (' onPress' );
234
- },
235
- status: ' success' ,
236
- swipeDirection: ' left' ,
237
- messageColor: ' white' ,
238
- })
239
- }
240
- / >
241
- < Toastable / >
242
- < / View>
243
- );
244
- }
245
-
246
- ```
247
-
248
- ## Control its content through ` Toastable ` component
249
-
250
- ``` js
251
- export default function Example () {
252
- return (
253
- < View style= {{flex: 1 }}>
254
- < Button
255
- title= " Show Toastable"
256
- onPress= {() =>
257
- showToastable ({ message: ' React Native Heroes is awesome! 🚀' , status: ' success' })
258
- }
259
- / >
260
- < Toastable
261
- containerStyle= {{ marginHorizontal: 20 }}
262
- alwaysVisible
263
- animationInTiming= {2000 }
264
- animationOutTiming= {2000 }
265
- duration= {5000 }
266
- onToastableHide= {() => {
267
- console .log (' onToastableHide' );
268
- }}
269
- statusMap= {{
270
- success: ' green' ,
271
- danger: ' red' ,
272
- info: ' blue' ,
273
- warning: ' yellow' ,
274
- }}
275
- renderContent= {(props ) => < ToastableBody {... props} / > }
276
- swipeDirection= {[' left' , ' right' ]}
67
+ onPress= {() => showToastable ({ message: ' React Native Heroes is awesome! 🚀' , status: ' success' })}
277
68
/ >
278
69
< / View>
279
70
);
280
71
}
281
-
282
72
```
283
73
284
74
@@ -290,6 +80,8 @@ Inherit all other props from `ToastableBodyParams` interface. Except `background
290
80
| statusMap | ` StatusMap ` | Status map, used to determine background color based on status | ` success: '#00BFA6', danger: '#FF5252', warning: '#FFD600', info: '#2962FF' ` |
291
81
| onToastableHide | ` Func ` | Callback when toast is dismissed | ` undefined ` |
292
82
| containerStyle | ` ViewProps['style'] ` | Container style for toast container | ` undefined ` |
83
+ position | ` 'top' \| 'bottom'\| 'center' ` | Toast position. | ` 'top' ` |
84
+ offset | ` number ` | Toast offset. | ` 0 ` |
293
85
294
86
## ToastableBodyParams
295
87
@@ -310,6 +102,8 @@ titleColor | `ColorValue` | Custom title color, if this is set. | `'#FFFFFF'` |
310
102
| messageColor | ` ColorValue ` | Custom message color, if this is set. | ` '#FFFFFF' ` |
311
103
titleStyle | ` TextStyle ` | Custom title style. | ` undefined ` |
312
104
messageStyle | ` TextStyle ` | Custom message style. | ` undefined ` |
105
+ position | ` 'top' \| 'bottom'\| 'center' ` | Toast position. | ` 'top' ` |
106
+ offset | ` number ` | Toast offset. | ` 0 ` |
313
107
314
108
## Contributing
315
109
@@ -321,7 +115,6 @@ See the [contributing guide](CONTRIBUTING.md) to learn how to contribute to the
321
115
- Support animationIn and animationOut props
322
116
- Support stackable toasts
323
117
- Support custom animations
324
- - Support snackbars
325
118
- Add custom status support
326
119
327
120
## Inspiration
0 commit comments