2.2.3 • Published 5 months ago

@codebundlesbyvik/simple-notifier v2.2.3

Weekly downloads
-
License
MPL-2.0
Repository
github
Last release
5 months ago

Simple Notifier

npm npm - downloads per week jsdelivr - hits per week

A fully featured yet easy to use & lightweight notification library.

Demo page (interactive)

Table of Contents

  1. Unique features
  2. Usage
  3. Browser support
  4. Instance options
  5. Methods
  6. Events
  7. Quick migration from version 1
  8. License

Unique features

  • Show multiple notifications simultaneously, and/or
  • Hide older notifications before showing one or more new notifications.
  • Fully accessible.
  • Module-based.

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-notifier
/* 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-top 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.

If you're not using a module bundler then either:

In your HTML file, link to the CSS stylesheet and import the JavaScript as a module.

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 can be provided in a NotifierOptions object as parameter on instance creation.

PropertyTypeDefaultDescription
parentElHTMLElementdocument.bodyHTML element in which the instance's element will be inserted as first child.
position["top" \| "bottom", "left" \| "center" \| "right"]["top", "center"]Position in the HTML document to render the instance's HTML element.
classNamesstring[][]Extra classes to add to the instance's HTML element.

Options below can also be provided via NotificationOptions and if done so will take preference.

PropertyTypeDefaultDescription
hideAfterTimenumber4000Time in milliseconds after which .hide() is automatically called. Set to 0 to show notifications until .hide() or .hideAll() is manually called.
hideOlderbooleanfalseHide all previously shown (to be exact, triggered) notifications before showing the one most recently created.
dismissablebooleanfalseRender a close button allowing for manual notification dismissal.

CSS

Variable nameTypeDefaultDescription
--simple-notifier-z-index<integer> | auto1090Z-index applied to the instance's HTML element
--simple-notifier-font-familySee font-family valuesNative font stackFont family used for all notification content.
--simple-notifier-font-sizeSee font-size values1remBase font size by which all internal sizes are calculated.
--simple-notifier-color-opacity<alpha-value>0.9Opacity applied to all available colors
--simple-notifier-color-x<color> | currentColorWhite, black, green, yellow & red (see /src/style.css for exact values)Available colors.
--simple-notifier-animation-duration<time> | auto500 msAnimation duration applied to all animations.

Methods

.show(textOrOptions, variant?)

Show a notification.

Parameters

ParameterTypeDescription
! REQUIRED ! textOrOptionsstring | string[] | NotificationOptionsText to render as notification content or NotificationOptions.
variantSee NotificationOptions below.
NotificationOptions

All options listed in the 2 tables below can be provided in a NotificationOptions object as the textOrOptions parameter.

Either text or title must be defined.

PropertyTypeDefaultDescription
variant"default" | "success" | "warning" | "error" | string"default"Variant (colorway) of notification to render. If you set a custom variant then you'll probably want to define some styling for it.
textstring | string[]undefinedText to render as notification content. Render multiple paragraphs by passing in an array.
titlestringundefinedText to render as notification title.
titleLevel"h1" | "h2" | "h3" | "h4" | "h5" | "h6""h6"title heading level.

Options below can also be provided via NotifierOptions, but those will never take preference.

PropertyTypeDefaultDescription
hideAfterTimenumber4000Time in milliseconds after which .hide() is automatically called. Set to 0 to show notification until .hide() or .hideAll() is manually called.
hideOlderbooleanfalseHide all previously shown (to be exact, triggered) notifications before showing the one just created.
dismissablebooleanfalseRender a close button allowing for manual notification dismissal.

.hide(notificationId: number)

Hide a currently shown notification by its ID. A notificationId can be retrieved via an Event object of the instance element .simple-notifier or the data-notification-id attribute on the notification element .simple-notification.

.hideAll()

Hide all currently shown notifications.

.notificationIds (getter)

Get the IDs of all currently shown notifications.

Events

Events are fired on the instance element .simple-notifier. The Event object contains the notificationId of the notification it was fired for and the instanceId it belongs to.

EventFired when...
shownThe process of showing a notification has completed.
hiddenThe process of hiding a notification has completed.
allhiddenThe last shown notification has been hidden.

Quick migration from version 1

If you were using the previous version as a JavaScript module and want to perform a quick update keeping everything as is, all you need to do is:

  1. Change new SimpleNotifier() to new SimpleNotifier({ hideAfterTime: 3500, hideOlder: true }).
  2. Remove call to .init() since instance initialization is now done when constructing the class.

License

Mozilla Public License 2.0 © 2025 Viktor Chin-Kon-Sung

2.2.1

7 months ago

2.2.0

7 months ago

2.2.3

5 months ago

2.2.2

5 months ago

2.1.0

7 months ago

2.0.1

8 months ago

2.0.0

8 months ago

1.1.3-fix-readme

3 years ago

1.1.3

3 years ago

1.1.1

4 years ago

1.1.0

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago

1.1.2

4 years ago

0.9.0

4 years ago

0.8.2

4 years ago

0.8.1-b

4 years ago

0.8.1

4 years ago

0.8.0

4 years ago