rn-toastify 1.0.9 → 1.0.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.MD +96 -190
- package/package.json +1 -1
- package/src/context/ToastContainer.js +12 -4
package/README.MD
CHANGED
|
@@ -1,47 +1,63 @@
|
|
|
1
|
+
# React Native Toastify
|
|
1
2
|
|
|
2
|
-
|
|
3
|
+
A highly customizable and performant toast notification library for React Native. Featuring smooth animations, swipe gestures, and flexible styling options.
|
|
3
4
|
|
|
5
|
+

|
|
4
6
|
|
|
5
|
-
|
|
7
|
+
---
|
|
6
8
|
|
|
7
|
-
|
|
8
|
-
# rn-toastify (react-native-toast)
|
|
9
|
+
## 🚀 Features
|
|
9
10
|
|
|
10
|
-
|
|
11
|
+
- **Single-root container**: Mount once at the app root.
|
|
12
|
+
- **Imperative API**: Use `useToast` hook anywhere in your app.
|
|
13
|
+
- **Light/Dark Theme**: System-aware with optional forced theme.
|
|
14
|
+
- **Promise Helper**: Show loading, success, and error states.
|
|
15
|
+
- **Customizable**: Add custom content, styles, and emojis.
|
|
11
16
|
|
|
12
|
-
|
|
17
|
+
---
|
|
13
18
|
|
|
14
|
-
##
|
|
19
|
+
## 📦 Installation
|
|
15
20
|
|
|
16
|
-
|
|
17
|
-
- Imperative hook API (useToast) — call from anywhere in your component tree
|
|
18
|
-
- System-aware light/dark theme with optional forced theme
|
|
19
|
-
- Promise helper (loading → success/error)
|
|
20
|
-
- Custom content and emoji toasts
|
|
21
|
-
|
|
22
|
-
## Installation
|
|
21
|
+
Install the package using npm or yarn:
|
|
23
22
|
|
|
24
23
|
```bash
|
|
25
24
|
npm install rn-toastify
|
|
26
25
|
```
|
|
27
26
|
|
|
28
|
-
Or
|
|
27
|
+
Or:
|
|
29
28
|
|
|
30
29
|
```bash
|
|
31
30
|
yarn add rn-toastify
|
|
32
31
|
```
|
|
33
32
|
|
|
34
|
-
|
|
33
|
+
### Peer Dependencies
|
|
34
|
+
|
|
35
|
+
Ensure the following peer dependencies are installed:
|
|
36
|
+
|
|
37
|
+
- `react`
|
|
38
|
+
- `react-native`
|
|
39
|
+
- `react-native-reanimated` (v2+)
|
|
40
|
+
- `lottie-react-native`
|
|
41
|
+
|
|
42
|
+
For iOS, run:
|
|
43
|
+
|
|
44
|
+
```bash
|
|
45
|
+
cd ios && pod install
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
---
|
|
35
49
|
|
|
36
|
-
|
|
50
|
+
## 🛠️ Quick Start
|
|
37
51
|
|
|
38
|
-
|
|
52
|
+
### Step 1: Mount the Toast Container
|
|
53
|
+
|
|
54
|
+
Add the `ToastContainer` to your app's root component (e.g., `App.js`):
|
|
39
55
|
|
|
40
56
|
```javascript
|
|
41
57
|
import React from 'react';
|
|
42
58
|
import { NavigationContainer } from '@react-navigation/native';
|
|
43
59
|
import MainNavigator from './src/navigation';
|
|
44
|
-
import
|
|
60
|
+
import { ToastContainer } from 'rn-toastify';
|
|
45
61
|
|
|
46
62
|
export default function App() {
|
|
47
63
|
return (
|
|
@@ -49,15 +65,15 @@ export default function App() {
|
|
|
49
65
|
<NavigationContainer>
|
|
50
66
|
<MainNavigator />
|
|
51
67
|
</NavigationContainer>
|
|
52
|
-
|
|
53
|
-
{/* Mount once at root. Optionally force theme: <ToastContainer theme="dark" /> */}
|
|
54
|
-
<ToastContainer />
|
|
68
|
+
<ToastContainer theme="dark" /> {/* Optional: Force theme */}
|
|
55
69
|
</>
|
|
56
70
|
);
|
|
57
71
|
}
|
|
58
72
|
```
|
|
59
73
|
|
|
60
|
-
2
|
|
74
|
+
### Step 2: Use the `useToast` Hook
|
|
75
|
+
|
|
76
|
+
Call toast methods anywhere in your app:
|
|
61
77
|
|
|
62
78
|
```javascript
|
|
63
79
|
import React from 'react';
|
|
@@ -65,44 +81,62 @@ import { View, Button } from 'react-native';
|
|
|
65
81
|
import useToast from 'rn-toastify';
|
|
66
82
|
|
|
67
83
|
export default function HomeScreen() {
|
|
68
|
-
const { success, error
|
|
84
|
+
const { success, error } = useToast();
|
|
69
85
|
|
|
70
86
|
return (
|
|
71
87
|
<View>
|
|
72
|
-
<Button title="Success" onPress={() => success('
|
|
73
|
-
<Button title="Error" onPress={() => error('
|
|
88
|
+
<Button title="Show Success" onPress={() => success('Operation successful!')} />
|
|
89
|
+
<Button title="Show Error" onPress={() => error('Something went wrong.', { position: 'top' })} />
|
|
74
90
|
</View>
|
|
75
91
|
);
|
|
76
92
|
}
|
|
77
93
|
```
|
|
78
94
|
|
|
79
|
-
|
|
95
|
+
---
|
|
96
|
+
|
|
97
|
+
## 📖 API Reference
|
|
98
|
+
|
|
99
|
+
### `useToast` Hook
|
|
100
|
+
|
|
101
|
+
The `useToast` hook provides the following methods:
|
|
102
|
+
|
|
103
|
+
| Method | Description |
|
|
104
|
+
|----------|--------------------------------------|
|
|
105
|
+
| `show` | Display a custom toast. |
|
|
106
|
+
| `success`| Show a success toast. |
|
|
107
|
+
| `error` | Show an error toast. |
|
|
108
|
+
| `custom` | Render a custom React element. |
|
|
109
|
+
| `emoji` | Display a toast with an emoji. |
|
|
110
|
+
| `promise`| Handle promise states with toasts. |
|
|
80
111
|
|
|
81
|
-
|
|
112
|
+
### Common Options
|
|
82
113
|
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
- `promise(promise, { loading, success, error }, options)`
|
|
114
|
+
| Option | Type | Description |
|
|
115
|
+
|------------|----------|--------------------------------------|
|
|
116
|
+
| `duration` | `number` | Duration in milliseconds. |
|
|
117
|
+
| `position` | `string` | `'top' | 'bottom' | 'center'`. |
|
|
118
|
+
| `style` | `object` | Custom styles for the toast wrapper. |
|
|
89
119
|
|
|
90
|
-
### Options
|
|
120
|
+
### `promise` Method Options
|
|
91
121
|
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
122
|
+
| Option | Type | Description |
|
|
123
|
+
|------------|----------|--------------------------------------|
|
|
124
|
+
| `loading` | `string` | Message during loading. |
|
|
125
|
+
| `success` | `string` | Message on success. |
|
|
126
|
+
| `error` | `string` | Message on error. |
|
|
95
127
|
|
|
96
|
-
|
|
128
|
+
---
|
|
97
129
|
|
|
98
|
-
|
|
130
|
+
## 🌈 Examples
|
|
131
|
+
|
|
132
|
+
### Success Toast
|
|
99
133
|
|
|
100
134
|
```javascript
|
|
101
135
|
const { success } = useToast();
|
|
102
|
-
success('Operation successful', { duration: 1500, position: 'top' });
|
|
136
|
+
success('Operation successful!', { duration: 1500, position: 'top' });
|
|
103
137
|
```
|
|
104
138
|
|
|
105
|
-
Promise
|
|
139
|
+
### Promise Helper
|
|
106
140
|
|
|
107
141
|
```javascript
|
|
108
142
|
const { promise } = useToast();
|
|
@@ -111,174 +145,46 @@ const myPromise = fetch('/api/save');
|
|
|
111
145
|
|
|
112
146
|
promise(myPromise, {
|
|
113
147
|
loading: 'Saving…',
|
|
114
|
-
success: 'Saved!',
|
|
115
|
-
error: 'Save failed',
|
|
148
|
+
success: 'Saved successfully!',
|
|
149
|
+
error: 'Save failed.',
|
|
116
150
|
});
|
|
117
151
|
```
|
|
118
152
|
|
|
119
|
-
Custom
|
|
153
|
+
### Custom Content
|
|
120
154
|
|
|
121
155
|
```javascript
|
|
122
156
|
const { custom } = useToast();
|
|
123
|
-
custom(<
|
|
157
|
+
custom(<MyCustomComponent />, { duration: 2000 });
|
|
124
158
|
```
|
|
125
159
|
|
|
126
|
-
Emoji
|
|
160
|
+
### Emoji Toast
|
|
127
161
|
|
|
128
162
|
```javascript
|
|
129
163
|
const { emoji } = useToast();
|
|
130
|
-
emoji('Great job!', '
|
|
131
|
-
```
|
|
132
|
-
|
|
133
|
-
## Theme
|
|
134
|
-
|
|
135
|
-
- By default `ToastContainer` detects the system color scheme (via `Appearance`) and passes a `theme` prop to visual toast components.
|
|
136
|
-
- To force a theme, mount the container with `theme`:
|
|
137
|
-
|
|
138
|
-
```jsx
|
|
139
|
-
<ToastContainer theme="dark" />
|
|
140
|
-
```
|
|
141
|
-
|
|
142
|
-
If the system color scheme is unavailable, the toast theme defaults to `light`.
|
|
143
|
-
|
|
144
|
-
## Compatibility & installation notes
|
|
145
|
-
|
|
146
|
-
- Peer dependencies: `react`, `react-native`, `react-native-reanimated` (v2+), `lottie-react-native` (if using animations). Install and link those in your app. See `package.json`.
|
|
147
|
-
- Reanimated setup: add `react-native-reanimated/plugin` to `babel.config.js` as the last plugin.
|
|
148
|
-
- iOS: run `cd ios && pod install` on macOS after installing native dependencies.
|
|
149
|
-
|
|
150
|
-
## Import styles
|
|
151
|
-
|
|
152
|
-
This package exports a default `useToast` hook and named exports. Both of the following work:
|
|
153
|
-
|
|
154
|
-
```javascript
|
|
155
|
-
// ES default + named
|
|
156
|
-
import useToast, { ToastContainer } from 'rn-toastify';
|
|
157
|
-
|
|
158
|
-
// CommonJS
|
|
159
|
-
const rnToastify = require('rn-toastify');
|
|
160
|
-
const useToast = rnToastify.useToast || rnToastify.default;
|
|
161
|
-
const { ToastContainer } = rnToastify;
|
|
164
|
+
emoji('Great job!', '🎉', { duration: 1300 });
|
|
162
165
|
```
|
|
163
166
|
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
- Crash on app start / `_rnToastify.default is not a function`:
|
|
167
|
-
- Clear Metro cache and rebuild: `npx react-native start --reset-cache` and rebuild the app.
|
|
168
|
-
- Ensure your app resolves the package's `index.js` correctly (this package exports a CommonJS-friendly entry to avoid interop issues).
|
|
169
|
-
|
|
170
|
-
- Reanimated-related errors:
|
|
171
|
-
- Add the Reanimated babel plugin and rebuild the app. See Reanimated installation docs.
|
|
172
|
-
|
|
173
|
-
- Lottie errors on iOS:
|
|
174
|
-
- Run `cd ios && pod install` and rebuild from Xcode.
|
|
175
|
-
|
|
176
|
-
- Toasts not showing:
|
|
177
|
-
- Make sure `ToastContainer` is mounted once at the app root.
|
|
178
|
-
- Ensure native peer dependencies are installed and app rebuilt.
|
|
179
|
-
|
|
180
|
-
## Publishing & versioning
|
|
181
|
-
|
|
182
|
-
When you're ready to release this change to npm:
|
|
183
|
-
|
|
184
|
-
1. Update the package version in `package.json` (or run `npm version patch|minor|major`).
|
|
185
|
-
2. Run tests and build (if any): `npm test`.
|
|
186
|
-
3. Pack and inspect: `npm pack`.
|
|
187
|
-
4. Publish: `npm publish --access public` (make sure you're logged in via `npm login`).
|
|
188
|
-
|
|
189
|
-
## Contributing
|
|
190
|
-
|
|
191
|
-
PRs welcome. Please open an issue if you find bugs or want features like scoped containers, configurable color tokens, or example apps.
|
|
192
|
-
|
|
193
|
-
## License
|
|
194
|
-
|
|
195
|
-
MIT
|
|
196
|
-
<Button title="Show Custom Toast" onPress={() => custom(
|
|
197
|
-
<View style={styles.customContent}>
|
|
198
|
-
<Text>Custom Toast Content</Text>
|
|
199
|
-
</View>,
|
|
200
|
-
{ duration: 1500, position: 'top' }
|
|
201
|
-
)} />
|
|
202
|
-
<Button title="Show Emoji Toast" onPress={() => emoji('Great job!', '👍', { duration: 1500, position: 'top' })} />
|
|
203
|
-
<ToastContainer />
|
|
204
|
-
</View>
|
|
205
|
-
);
|
|
206
|
-
};
|
|
207
|
-
|
|
208
|
-
const styles = StyleSheet.create({
|
|
209
|
-
container: {
|
|
210
|
-
flex: 1,
|
|
211
|
-
justifyContent: 'center',
|
|
212
|
-
alignItems: 'center',
|
|
213
|
-
},
|
|
214
|
-
customContent: {
|
|
215
|
-
padding: 10,
|
|
216
|
-
backgroundColor: '#fff',
|
|
217
|
-
borderRadius: 5,
|
|
218
|
-
},
|
|
219
|
-
});
|
|
220
|
-
|
|
221
|
-
export default AppContent;
|
|
222
|
-
```
|
|
223
|
-
This example integrates multiple toast types and demonstrates how to trigger each one. It also includes the necessary ToastContainer to display the toasts.
|
|
224
|
-
|
|
225
|
-
Theme and single-root notes
|
|
226
|
-
- Mount `ToastContainer` once at the app root (recommended). The container detects the system color scheme by default and passes a `theme` prop to all toast components.
|
|
227
|
-
- To force a theme, pass `theme="dark"` or `theme="light"` to `<ToastContainer />`.
|
|
228
|
-
- If the system color scheme is unavailable, the toast theme defaults to `light`.
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
## API Reference
|
|
232
|
-
|
|
233
|
-
#### useToast Hook Methods
|
|
234
|
-
All toast methods accept the following parameters:
|
|
235
|
-
|
|
236
|
-
| Parameter | Type | Description |
|
|
237
|
-
| :-------- | :------- | :------------------------- |
|
|
238
|
-
| `message` | `string` | **Required**. The message to display in the toast. |
|
|
239
|
-
| `options` | `object` | **Optional**. Configuration options for the toast (e.g., duration, position). |
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
- Methods
|
|
243
|
-
|
|
244
|
-
- success : Displays a success toast.
|
|
245
|
-
|
|
246
|
-
- error : Displays an error toast.
|
|
247
|
-
|
|
248
|
-
- promise : Handles a promise and displays loading, success, and error toasts based on the promise's state.
|
|
249
|
-
|
|
250
|
-
Additional options for promise method:
|
|
251
|
-
| Option | Type | Description |
|
|
252
|
-
| :-------- | :------- | :------------------------- |
|
|
253
|
-
| `loading` | `string` | The message to display in the toast. |
|
|
254
|
-
| `success` | `string` | Message to display if the promise resolves. |
|
|
255
|
-
| `error` | `string` | Message to display if the promise is rejected. |
|
|
256
|
-
|
|
257
|
-
- custom
|
|
167
|
+
---
|
|
258
168
|
|
|
259
|
-
|
|
169
|
+
## 🎨 Theme Support
|
|
260
170
|
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
| `content` | `element` | **Required**. React element to render in the toast. |
|
|
171
|
+
- **Automatic**: Detects system theme (light/dark).
|
|
172
|
+
- **Forced**: Pass `theme="light"` or `theme="dark"` to `ToastContainer`.
|
|
264
173
|
|
|
265
|
-
|
|
174
|
+
If the system theme is unavailable, the default is `light`.
|
|
266
175
|
|
|
267
|
-
|
|
176
|
+
---
|
|
268
177
|
|
|
269
|
-
|
|
270
|
-
| :-------- | :------- | :------------------------- |
|
|
271
|
-
| `emoji` | `string` | **Required**. The emoji to display alongside the message. |
|
|
178
|
+
## 🐛 Troubleshooting
|
|
272
179
|
|
|
273
|
-
|
|
180
|
+
### Common Issues
|
|
274
181
|
|
|
275
|
-
|
|
182
|
+
- **Toasts not showing**: Ensure `ToastContainer` is mounted at the app root.
|
|
183
|
+
- **Reanimated errors**: Add the Reanimated Babel plugin and rebuild.
|
|
184
|
+
- **Lottie issues on iOS**: Run `cd ios && pod install`.
|
|
276
185
|
|
|
277
|
-
|
|
278
|
-
| :-------- | :------- | :------------------------- |
|
|
279
|
-
| `duration` | `number` |**Optional**. Duration in milliseconds for which the toast is visible. |
|
|
280
|
-
| `position` | `string` | **Optional**. Position of the toast on the screen (top, bottom, center). |
|
|
186
|
+
---
|
|
281
187
|
|
|
282
|
-
## License
|
|
188
|
+
## 📜 License
|
|
283
189
|
|
|
284
|
-
[
|
|
190
|
+
MIT License. See [LICENSE](./LICENSE) for details.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "rn-toastify",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.10",
|
|
4
4
|
"description": "A customizable and performant toast notification library for React Native, featuring smooth animations, swipe gestures, and flexible styling options.",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"repository": {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import React, { useEffect, useState } from 'react';
|
|
2
|
-
import { View, StyleSheet, Appearance } from 'react-native';
|
|
2
|
+
import { View, StyleSheet, Appearance, StatusBar, Platform, SafeAreaView } from 'react-native';
|
|
3
3
|
import Toast from '../Toast';
|
|
4
4
|
import toastManagerInstance from './ToastManager';
|
|
5
5
|
import {
|
|
@@ -45,7 +45,15 @@ const ToastContainer = ({ theme: forcedTheme } = {}) => {
|
|
|
45
45
|
return (
|
|
46
46
|
<>
|
|
47
47
|
{/* Top positioned toasts */}
|
|
48
|
-
<
|
|
48
|
+
<SafeAreaView
|
|
49
|
+
style={[
|
|
50
|
+
styles.topContainer,
|
|
51
|
+
{
|
|
52
|
+
top: Platform.OS === 'android' ? StatusBar.currentHeight || 0 : 0,
|
|
53
|
+
},
|
|
54
|
+
]}
|
|
55
|
+
pointerEvents="box-none"
|
|
56
|
+
>
|
|
49
57
|
{topToasts.map((toast, index) => (
|
|
50
58
|
<Toast
|
|
51
59
|
key={toast.id}
|
|
@@ -53,13 +61,13 @@ const ToastContainer = ({ theme: forcedTheme } = {}) => {
|
|
|
53
61
|
duration={toast?.options?.duration}
|
|
54
62
|
position="top"
|
|
55
63
|
theme={theme}
|
|
56
|
-
style={[toast?.options?.style || {}, {
|
|
64
|
+
style={[toast?.options?.style || {}, { marginTop: index * 60 }]} // Adjust spacing between top toasts
|
|
57
65
|
onHide={() => toastManagerInstance.remove(toast.id)}
|
|
58
66
|
>
|
|
59
67
|
{toast.content}
|
|
60
68
|
</Toast>
|
|
61
69
|
))}
|
|
62
|
-
</
|
|
70
|
+
</SafeAreaView>
|
|
63
71
|
|
|
64
72
|
{/* Center positioned toasts */}
|
|
65
73
|
<View style={styles.centerContainer} pointerEvents="box-none">
|