@codebundlesbyvik/simple-notifier
v3.1.1
Published
A fully featured yet easy to use & lightweight notification library.
Downloads
23
Maintainers
Readme
Simple Notifier
A fully featured yet easy to use & lightweight notification library.
Table of Contents
Nice features
- Show multiple notifications simultaneously, and/or
- Hide older notifications before showing one or more new notifications.
- Great accessibility.
- Is a compact ES module.
Skip to Instance options to get a complete overview of all features!
Usage
For this example I assume the main JavaScript file is processed by a module bundler that can process CSS files.
# Install package from npm
npm install @codebundlesbyvik/simple-notifierIf you're not using a module bundler then:
- Download the latest
@codebundlesbyvik/js-helpersrelease from GitHub or load it directly via jsdelivr. - Download the latest
@codebundlesbyvik/simple-notifierrelease from GitHub or load the CSS and the JavaScript via the jsdelivr CDN.
For the example below I assume the main JavaScript file is processed by a module bundler.
/* style.css */
@import "@codebundlesbyvik/simple-notifier";// index.js
import "./style.css";
import SimpleNotifier from "@codebundlesbyvik/simple-notifier";
const notifier = new SimpleNotifier();
// The following element is inserted as the first child of <body>:
// <div class="simple-notifier simple-notifier--position-y-start simple-notifier--position-x-center">
// </div>
const text = "This is an example notification.";
const variant = "success";
notifier.show(text, variant);
// Notification is shown for 4000 ms.Browser support
Requires an ECMAScript 2022 (ES13) compatible browser. Practically speaking, all browsers released in 2021 and onwards are fully supported.
Instance options
JavaScript
All options listed in the 2 tables below may be provided in an object as parameter on instance creation.
| Property | Type | Default | Description |
| :-------------------------- | :------------------------------------------------- | :----------------------- | :---------------------------------------------------------------------------- |
| parentEl | HTMLElement | document.body | HTML element in which the instance's element will be inserted as first child. |
| position | ["start" \| "end", "start" \| "center" \| "end"] | ["start", "center"] | Logical position in the HTML document to render the instance's HTML element. |
| classNames | string[] | [] | Extra classes to add to the instance's HTML element. |
| hideButtonElAriaLabelText | string | "Dismiss notification" | Text used as aria-label for the notification hide button. |
| theme | "light" | "dark" | "auto" | "auto" | Force color theme. |
| animations | false | "auto" | "auto" | Force animation preference. |
Options below may also be provided via NotificationOptions and if done so take preference.
| Property | Type | Default | Description |
| :-------------- | :-------- | :------ | :------------------------------------------------------------------------------------------------------------------------- |
| hideAfterTime | number | 4000 | Time in milliseconds after which .hide() is automatically called. Set to 0 to disable this behavior. |
| hideOlder | boolean | false | Hide all currently shown notifications before showing the next. |
| dismissible | boolean | false | Render a close button which if pressed calls .hide(). |
CSS
| Variable name | Type | Default | Description |
| :---------------------------------------- | :---------------------------------------------------------------------------------------------- | :--------------------------------------------------------------------------------------- |:----------------------------------------------------------- |
| --simple-notifier-z-index | <integer> | auto | 1090 | Z-index applied to the instance's HTML element. |
| --simple-notifier-font-family | See font-family values | Native font stack | Font family used for all notification content. |
| --simple-notifier-font-size | See font-size values | 1rem | Base font size by which all internal sizes are calculated. |
| --simple-notifier-color-opacity | <alpha-value> | 0.9 | Opacity applied to all available colors. |
| --simple-notifier-color-<name>-channels | <color> | White, grey, black, green, yellow & red (see /src/style.css for exact values) | Available colors. |
| --simple-notifier-animation-duration | <time> | auto | 500ms | Animation duration applied to all transitions & animations. |
Methods
.show(textOrOptions, variant?)
Show a notification.
Parameters
| Parameter | Type | Description |
| :-------------------------------- | :---------------------------------------------------------------------- | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| textOrOptions Required | string | string[] | NotificationOptions | Text to render as notification content or NotificationOptions. Text may be provided as a string or multiple strings in an array. Each string is set as innerHTML of a <p>. |
| variant | See NotificationOptions below. | Colorway of notification to render. Appended as BEM modifier to notification class list. Takes preference over variant provided in NotificationOptions if both are provided. |
NotificationOptions
NotificationOptions always take preference over NotifierOptions.
Notifications are only shown if either text or title is defined.
| Property | Type | Default | Description |
| :-------------- | :----------------------------------------------------------------- | :---------- | :--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| text | string | string[] | undefined | Text to render as notification content. May be provided as a string or multiple strings in an array. Each string is set as innerHTML of a <p>. |
| title | string | [string, string] | undefined | Text to render as notification title. Set as innerHTML, of a <p> if provided as a string or of element of type of title[1] if provided as title[0]. |
| variant | "default" | "success" | "warning" | "error" | string | "default" | Colorway of notification to render. Appended as BEM modifier to notification class list. Overwritten if variant is also provided as .show() parameter. |
| hideAfterTime | number | 4000 | Time in milliseconds after which .hide() is automatically called. Set to 0 to disable this behavior. |
| hideOlder | boolean | false | Hide all currently shown notifications before showing the next. |
| dismissible | boolean | false | Render a close button which if pressed calls .hide(). |
.hide(id: number)
Hide a currently shown notification by its ID. An id can be retrieved via the event fired on the instance element on notification show or the data-notification-id attribute on the notification element.
.hideAll()
Hide all currently shown notifications.
.currentId
Get the current ID, i.e. the one that'll be used for the next notification.
.ids
Get the IDs of all currently shown notifications.
Events
Events are fired on the instance element .simple-notifier. The detail property contains the id of the notification it was fired for.
| Event | Fired after... |
| :---------- | :--------------------------------- |
| shown | Notification is shown. |
| hidden | Notification is hidden. |
| allhidden | Last shown notification is hidden. |
Upgrading from 2.x.x
The following changes are breaking:
- Removed:
instanceId - Renamed:
notificationId>id. Effects getters & eventdetail. - Renamed:
dismissable>dismissible - Changed: Use logical directional keywords instead of physical keywords, i.e.
left>start. - Changed:
notificationOptions.titleLevelmust now be provided asnotificationOptions.title.el(seeNotificationOptions). - Changed: If
variantis provided both as.show()parameter and asnotificationOptions.variant,.show()now takes preference. - Changed: Undocumented but public class field names, visibility & mutability.
License
Mozilla Public License 2.0 © 2025 Viktor Chin-Kon-Sung
