#
Toasts
NidamJS provides a flexible and easy-to-use Toast notification system. It allows you to display temporary messages to the user, with customizable positions, durations, and severity levels.
#
Quick Start
To use toasts, you must include the toast stack container in your HTML. Without this, NidamJS will log a warning and won't know where to render the toasts.
<!-- Usually placed near the end of the <body> -->
<div nd-toast-stack data-position="bottom-right"></div>
You can then import and call the toast functions:
import { showToast } from "nidamjs"; // Use your actual import path
// A simple informative toast
showToast("info", "Your profile has been updated.");
#
Core Attributes
The toast system relies on a few data attributes to manage the display and positioning of notifications.
#
1. The Stack Container (nd-toast-stack)
Applied to an empty <div>, this acts as the anchor for all incoming notifications.
#
2. The Position (data-position)
Defines the default position for all toasts within the stack.
Allowed values are:
top-righttop-leftbottom-rightbottom-left
#
API Reference
The core functionality is exposed via JavaScript exports.
#
showToast(type, messages, options)
The core function used to display a toast notification.
Parameters:
Message Structures:
- If a String is provided, a standard text toast is displayed.
- If an Array of strings is provided, it will render them as a bulleted list (
<ul>). - If an Object is provided, it automatically extracts message values (or an
.errorsproperty array if one is present).
Options (options object):
Examples:
// A success toast with a custom duration
showToast("success", "Operation successful!", { duration: 5000 });
// An error toast that stays until the user manually closes it
showToast("error", "Failed to connect to the server.", {
duration: 0,
closable: true,
});
// Displaying multiple errors, rendering beautifully as an unordered list
showToast("warning", ["Password is too short", "Email is invalid"]);
#
Technical Specifications
#
Attribute Reference
#
Styling & Customization
Toasts use the .nd-toast class, with the data-type attribute representing their visual states. You can override styles using regular CSS.
#
Technical Classes Reference
Example CSS Overrides:
/* Styling the main stack container */
[nd-toast-stack] {
/* position, z-index, flexbox gap... */
}
/* Individual toast styling */
.nd-toast {
/* padding, background, border-radius, box-shadow... */
}
/* Severity based colors */
.nd-toast[data-type="success"] { ... }
.nd-toast[data-type="error"] { ... }
.nd-toast[data-type="warning"] { ... }
.nd-toast[data-type="info"] { ... }