A beautiful toast notification library for React Native and Expo applications.
- 🎨 Beautiful, customizable toast notifications
- 🔄 Promise-based API for async operations
- 🌓 Dark mode support
- 🎯 Multiple toast variants (success, error, warning, info, loading)
- 👆 Swipe to dismiss
- ♿ Accessibility support
- 📱 Safe area aware
- 🔄 Animated transitions
npm install @onesamket/rn-toast
# or
yarn add @onesamket/rn-toastThis library requires the following peer dependencies:
npm install expo-blur expo-haptics @expo/vector-icons react-native-safe-area-context
# or
yarn add expo-blur expo-haptics @expo/vector-icons react-native-safe-area-contextWrap your application with the ToastProvider:
import { ToastProvider } from '@onesamket/rn-toast';
export default function App() {
return (
<ToastProvider>
<YourApp />
</ToastProvider>
);
}import { useToast } from '@onesamket/rn-toast';
function MyComponent() {
const toast = useToast();
const showToast = () => {
toast.toast({
description: 'This is a toast notification',
duration: 3000, // milliseconds
});
};
return (
<Button title="Show Toast" onPress={showToast} />
);
}// Success toast
toast.success({
description: 'Successfully saved!',
});
// Error toast
toast.error({
description: 'An error occurred',
});
// Warning toast
toast.warning({
description: 'Warning: Low battery',
});
// Info toast
toast.info({
description: 'New message received',
});
// Loading toast
const toastId = toast.loading({
description: 'Loading data...',
duration: 0, // Won't auto-dismiss
});
// Later, dismiss the toast manually
toast.dismissToast(toastId);const fetchData = async () => {
return toast.promise(
fetch('https://api.example.com/data'),
{
description: 'Fetching data',
promise: {
loading: 'Loading data...',
success: 'Data loaded successfully!',
error: 'Failed to load data',
},
}
);
};toast.info({
description: 'Your file is ready',
action: {
label: 'Download',
onPress: () => {
// Handle action
downloadFile();
},
},
});The ToastProvider component is required to use the toast functionality.
The useToast hook returns an object with the following methods:
toast(options): Show a default toastsuccess(options): Show a success toasterror(options): Show an error toastwarning(options): Show a warning toastinfo(options): Show an info toastloading(options): Show a loading toastcustom(options): Show a custom toastpromise(promise, options): Show a toast for a promisedismissToast(id): Dismiss a toast by ID
| Property | Type | Description |
|---|---|---|
description |
string |
The content of the toast |
variant |
'default' | 'destructive' | 'success' | 'warning' | 'info' | 'loading' | 'custom' | 'error' |
The visual style of the toast |
duration |
number |
Duration in milliseconds before auto-dismissing (default: 3000, 0 for no auto-dismiss) |
action |
{ label: string, onPress: () => void } |
Optional action button |
promise |
{ loading: string, success: string, error: string } |
Messages for promise states |
swipeToClose |
boolean |
Whether the toast can be dismissed by swiping (default: true) |
position |
'top' | 'bottom' |
Position of the toast (default: 'bottom') |
MIT
Contributions are welcome! Please feel free to submit a Pull Request.
This library was built with React Native and Expo, and was inspired by the need for a simple, customizable toast notification system for React Native applications.
- Initial release
- Basic toast functionality with various variants
- Support for promise-based toasts
- Customizable appearance and behavior