A lightweight and flexible toast notification library for Svelte, featuring multiple toast types, positioning options, and promise support.
npm install svelte-toast-messages
Add the ToastList component to your app's root (typically App.svelte):
<script>
import { ToastList } from 'svelte-toast-messages';
</script>
<!-- Your app content -->
<ToastList />
Basic Toasts
import { toast } from 'svelte-toast-messages';
// Success toast
toast.success('Operation successful!', {
position: 'top-right',
duration: 3000
});
// Error toast
toast.error('Something went wrong!', {
position: 'bottom-right',
duration: 5000
});
Perfect for async operations:
const myAsyncOperation = async () => {
try {
await toast.promise(
fetchData(), // Your promise
{
loading: 'Loading data...',
success: 'Data loaded successfully!',
error: 'Error loading data'
},
{ position: 'bottom-right' }
);
} catch (error) {
console.error(error);
}
};
The library handles multiple toasts gracefully (maximum 5 at once):
function showMultipleToasts() {
toast.success('First toast', { position: 'top-right' });
toast.error('Second toast', { position: 'top-left' });
toast.success('Third toast', { position: 'bottom-right' });
}
top-right
(default)top-left
bottom-right
bottom-left
3000ms
(3 seconds)1000ms
(1 second)10000ms
(10 seconds)Infinite
(automatically dismissed when promise resolves)interface ToastOptions {
position?: 'top-right' | 'top-left' | 'bottom-right' | 'bottom-left';
duration?: number; // in milliseconds
}
šØ Multiple Toast Types
š Flexible Positioning
ā” Promise Integration
š Queue Management
šØ Animations
šÆ Developer Experience
``` async function handleFormSubmit() { const submitForm = new Promise((resolve) => { setTimeout(() => resolve({ success: true }), 2000); });
try { await toast.promise( submitForm, { loading: 'Submitting form...', success: 'Form submitted!', error: 'Error submitting form' }, { position: 'bottom-right' } ); } catch (error) { console.error(error); }
ā Works in all modern browsers that support ES6:
This project is licensed under the MIT License - see the LICENSE file for details.
We welcome contributions! Here's how you can help:
git checkout -b feature/AmazingFeature
)git commit -m 'Add some AmazingFeature'
)git push origin feature/AmazingFeature
)Feel free to check the issues page if you want to contribute.