Notification Center is a lightweight JavaScript library for displaying beautiful, customizable notifications powered by Tailwind CSS.
Install via NPM:
npm install @madhusha_99/notification-center
Below are the customizable options for notifications:
Option | Type | Description |
---|---|---|
title |
string |
The title of the notification |
titleColor |
string |
Color of the title text |
text |
string |
The content of the notification |
textColor |
string |
Color of the text content |
type |
string |
Notification type (e.g., success, error, warning) |
toast |
boolean |
Whether the notification should be a toast |
position |
string |
Position of the notification (e.g., top-right, bottom-left) |
duration |
number |
Duration the notification is displayed (in milliseconds) |
icon |
string |
Icon to be displayed in the notification |
alertColor |
string |
Background color of the alert |
alertWidth |
string |
Width of the alert box |
alertHeight |
string |
Height of the alert box |
showCloseButton |
boolean |
Whether to show a close button |
showCancelButton |
boolean |
Whether to show a cancel button |
showConfirmButton |
boolean |
Whether to show a confirm button |
confirmButtonText |
string |
Text for the confirm button |
cancelButtonText |
string |
Text for the cancel button |
confirmButtonColor |
string |
Background color of the confirm button |
cancelButtonColor |
string |
Background color of the cancel button |
confirmButtonClass |
string |
Custom CSS class for the confirm button |
cancelButtonClass |
string |
Custom CSS class for the cancel button |
You can import Toast
and Dialog
in two ways, depending on your preference and project structure.
Import both components from the main module:
import { Toast, Dialog } from '@madhusha_99/notification-center';
This approach is simple and keeps your imports organized in one place.
Import each component separately if you prefer modular imports:
import Toast from '@madhusha_99/notification-center/core/Toast';
import Dialog from '@madhusha_99/notification-center/core/Dialog';
Useful for selective imports and potentially better tree-shaking in certain build setups.
To initialize notifications globally, add the following setup:
import { Toast, Dialog } from '@madhusha_99/notification-center';
declare global {
interface Window {
notify: Toast; // you can use global variable name as you want
dialog: Dialog;
}
}
window.notify = new Toast();
window.dialog = new Dialog();
import { Toast, Dialog } from '@madhusha_99/notification-center';
window.notify = new Toast(); // you can use global variable name as you want
window.dialog = new Dialog();
To use notifications without global initialization, you can create individual instances:
import { Toast, Dialog } from '@madhusha_99/notification-center';
const notify = new Toast(); //you can use constable variable name as you want
const dialog = new Dialog();
You can trigger notifications using:
notify.show({
title: "Success!",
text: "Your action was successful.",
type: "success",
duration: 3000
});
notify.warning({
title: "Warning!",
text: "Please be careful.",
position: "top-3 right-3"
type: "warning",
duration: 3000
});
notify.error({
title: "Error!",
text: "Something went wrong!",
position: "top-3 right-3"
type: "error",
duration: 3000
});
dialog.show({
title: "Are you sure?",
text: "This action cannot be undone.",
showCancelButton: true,
confirmButtonText: "Yes, proceed",
cancelButtonText: "Cancel"
});
The dialog.show()
method returns a Promise, allowing you to handle user actions asynchronously like a true tech geek! 🚀
dialog.show({
title: "Confirm Action",
text: "Are you sure you want to proceed?",
showConfirmButton: true,
showCancelButton: true,
confirmButtonText: "Yes",
cancelButtonText: "No"
}).then((result) => {
if (result) {
console.log("User confirmed the action!");
} else {
console.log("User canceled the action.");
}
});
<button class="bg-green-500 p-3 border-1 border-gray outline-none text-white cursor-pointer"
onclick="notify.success({title: 'Success!', text: 'Your action was successful!', position: 'top-3 right-3', duration: 3000})">
Show Success Notification
</button>
<button class="bg-yellow-500 p-3 border-1 border-gray outline-none text-white cursor-pointer"
onclick="notify.warning({title: 'Warning!', text: 'This is a warning message.', position: 'top-3 left-3', duration: 3000})">
Show Warning Notification
</button>
<button class="bg-red-500 p-3 border-1 border-gray outline-none text-white cursor-pointer"
onclick="notify.error({title: 'Error!', text: 'Something went wrong!', position: 'bottom-3 left-3', duration: 3000})">
Show Error Notification
</button>
This package requires Tailwind CSS to work properly. Ensure Tailwind is included in your project:
npm install -D tailwindcss postcss autoprefixer
npx tailwindcss init
@tailwind base;
@tailwind components;
@tailwind utilities;
top-3 right-3
→ Top Righttop-3 left-3
→ Top Leftbottom-3 right-3
→ Bottom Rightbottom-3 left-3
→ Bottom LeftIf you prefer using Tailwind without NPM, include the following in your HTML:
<script src="https://unpkg.com/@tailwindcss/browser@4"></script>
success(options)
, warning(options)
, error(options)
Shortcut methods for common notifications.
This package is licensed under the MIT License - see the LICENSE file for details.