as-toast
Version:
Simple and customizable toast notifications for Svelte
29 lines (28 loc) • 722 B
JavaScript
import { writable } from 'svelte/store';
export const toasts = writable([]);
/**
*
* @param msg Toast message, can be any HTML.
* @param type Toast type ('info' or 'warn')
* @param removeAfter Time in milliseconds the toast will be displayed
* @returns id of Toast
*/
export function addToast(msg, type = 'info', removeAfter = 5000) {
const id = new Date().valueOf() + msg;
toasts.update((all) => [
{
id,
msg,
type,
removeAfter
},
...all
]);
setTimeout(() => {
removeToast(id);
}, removeAfter);
return id;
}
export function removeToast(id) {
toasts.update((all) => all.filter((toast) => toast.id !== id));
}