ToastMyNuts

A buttery smooth toast notification library for the browser.

This library was heavily inspired by Sonner by Emilkowalski and its main feature of stackable toasts are from that library. It is for React, so this library was made mostly for Vanilla JavaScript. Go check it out.

Github

How to install

There are two ways to install ToastMyNuts

  • Via CDN like Unpkg
<link rel="stylesheet" href="https://unpkg.com/toastmynuts@2.0.0/dist/styles.css" integrity="sha256-7eQ6VAEIduMDO57CXZFgs0BHnl2Yra0kWf189xqn+pM=" crossorigin="anonymous">
<script src="https://unpkg.com/toastmynuts@2.0.0/dist/index.umd.js" integrity="sha256-NE6HCAqAOOVeap80K1HtFztPRJQLtkWLDIWJhxC711Q=" crossorigin="anonymous"></script>
  • Via ES Modules

First install them using your preferred package manager as long as they can install deps from npm.

npm install toastmynuts

Note: This can only be used on the client, so please take note if you are using server side stuff. Also, there can only be one instance of a toaster, so if you call initializeToast again, the returned instance will be the same as the one from the very first invocation.

import { initializeToast } from "toastmynuts";
import "toastmynuts/styles.css";

// The default config
export const toast = initializeToast({
	// maxVisibleToasts: 3,
	// richColors: true,
	// stackable: true,
	// toastDuration: 10_000,
	// dir: undefined,
	// theme: "system",
	// closeOnSwipe: true,
	// position: { x: "middle", y: "top" }
});

Usage

How to use the library.

  • Via CDN like Unpkg

Note: The toaster will automatically be initialized for you once you load the script for the library. There can only be one instance of this toaster, and it is exposed under the window object.

<!-- When the script loads, it will look for a configuration in JSON format from a meta tag. Below is an example -->
<meta name="toastmynuts:config" content='{"richColors": true}' />

To render toasts

<script>
	// Rendering a default toast
	const toastId = window.ToastMyNuts("Hello, World!");
	// Then to remove that toast manually. Note that if the toast has been removed
	// when you call this, an error will be thrown.
	window.ToastMyNuts.remove(toastId);
</script>
  • Via ES Modules
import { toast } from "path where you initialized the toast";

// Render a default toast;
const toastId = toast("Hello, World!");
// Then to remove that toast manually. Note that if the toast has been removed
// when you call this, an error will be thrown.
toast.remove(toastId);

Promises

The library makes use of the behavior of promises to allow you to dynamically set a message for either a successful or failed asynchronous operation.

try {
	const [toastId, data] = await toast.promise(
		// The loading message
		"Loading...",
		// The success message.
		// Either a string or a callback fn that returns a string.
		// This fn will have access to the data.
		(data) => {
			return `Welcome, ${data.name}! Your data has been registered.`;
		},
		// The error message.
		// Either a string or a callback fn that returns a string.
		// This fn will have access to the data.
		(err) => {
			return "Something went wrong. Please try again.";
		},
		// Can either be an asynchronous function or a promise
		async () => {
			const res = await fetch(
				"https://jsonplaceholder.typicode.com/users/1",
			);
			return res.json();
		},
		{ /** Toast options... */ }
	);
} catch (res) {
	const [toastId, err] = res;

	console.error(err);
}

Demo

Test out all the available toasts and options

All the available toasts

All the options. Note that some options can only be changed if the initiated toaster is empty. For example, the stackable property cannot have its value changed until all toasts in a toaster are gone. This is because, behind the scenes, event listeners are attached to the toast container when the first toast is added to handle the expansion of stackable toasts.

Rich colors

Theme to adjust to light or dark themes. If you want to check for how the default rich colors look like for either themes, you can toggle the page to either light or dark via the header.

Various locations for the toast.

About

About this project

Making this was fairly difficult, and it seemed even harder when trying to abstract the logic. Therefore, contributions are welcome to this library.

Since this was inspired from Sonner, an open-source library, and I got the stacking logic from it, this library has a MIT license, so have at it.

Do you want to hear more or learn from me? Then, check out my YouTube channel