Toast notifications with a simple and easy-to-use.
npm install @dhafitf/toast-it
or
yarn add @dhafitf/toast-it
<script src="https://cdn.jsdelivr.net/npm/@dhafitf/toast-it/dist/toast-it.umd.min.js"></script>
Files are delivered via the CDN service provided by jsdelivr.
Add a toast to page. The toast will be appended to the body element.
const toast = new ToastIt();
const notifyButton = document.getElementById("notify-btn");
notifyButton.addEventListener("click", () => {
toast.success("Hello, World!");
});
Calling the ToastIt
constructor will create a new instance of the toast notification. The constructor accepts an optional configuration object. This will be used to set the default options for the toast.
const toast = new ToastIt({
// Will auto close the toast after the duration. Set to false to disable
autoClose: true,
// Duration of the toast to be visible. Not applicable if autoClose is false
duration: 5000,
// Position of the toast
position: "bottom-right",
// Space between toasts
gutter: 8,
});
toast.success("Hello, World!");
Creates a notification with an animated checkmark.
toast.error("Hello, World!");
Creates a notification with an animated error icon.
toast.open({
type: "blank", // You can also use "success" or "error"
message: "Hello, World!",
});
A toast can be created with options properties to override the default options.
toast.success("Hello, World!", {
duration: 3000,
position: "top-left",
});
or
toast.open({
type: "success",
message: "Hello, World!",
duration: 3000,
position: "top-left",
});
You can manually dismiss a notification with toast.dismiss
. Toasts will auto-remove after 3 second by default.
// Generate a random id for the toast
const toastId = Math.random().toString(36).substring(2, 9);
const showButton = document.getElementById("show");
showButton.addEventListener("click", () => {
toast.success("Hello World!", {
id: toastId, // Set the id of the toast
autoClose: false,
position: "top-right",
});
});
const dismissButton = document.getElementById("dismiss");
dismissButton.addEventListener("click", () => {
toast.dismiss(toastId); // Dismiss the toast with the given id
});
toast.dismiss();
This project is inspired by