New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

awesome-notifications

Package Overview
Dependencies
Maintainers
1
Versions
37
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

awesome-notifications

Lightweight library for beautifull and smooth notifications

  • 2.2.8
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

npm npm npm

Awesome Notifications

It's a lightweight, fully customizable JavaScript library for showing notifications.

Advantages: 5kb gzipped sizeno dependenciesadvanced async supportfully customizable>95% test coverage.

Demo: https://f3oall.github.io/awesome-notifications/

Changelog: changelog.md

Demo gif

Install

Attention! This library uses FontAwesome 4 icons, so you either need to make sure that FontAwesome is connected to your project or disable icons, passing the icons: {enabled: false} property to options. Also you can preserve icons setting up a custom template for them via editing options.icons.prefix and options.icons.suffix

Via NPM

npm install --save awesome-notifications

In browser

Download index.var.js and style.css, then put them in your html:

<head>
  <link rel="stylesheet" href="path/to/style.css"></link>
  <script src="path/to/index.var.js"></script>
</head>

Vue.js version

You can learn more in the Vue.js version repository: https://github.com/f3oall/vue-awesome-notifications

Usage

Node.js

import AWN from "awesome-notifications"

let notifier = new AWN(options)

In browser

<script>
  var notifier = new AWN(options);
</script>
<button onclick="notifier.success('Success message');">Show Success</button>

Available functions

You can pass any valid HTML to html functions params.

FunctionParamsDescriptionExample
tip(html)html - String, requiredshows a gray toast with specified htmltip('First line text<br>Second line text')
info(html)html - String, requiredshows a blue toast with specified htmlinfo('<b>You can put any HTML here</b>')
success(html)html - String, requiredshows a green toast with specified htmlsuccess('Simple none-HTML message')
warning(html)html - String, requiredshows an orange toast with specified htmlwarning('Simple none HTML message')
alert(html)html - String, requiredshows a red toast with specified htmlalert('Simple none HTML message')
async(promise, onResolve, onReject, html)promise - Promise, required;
onResolve - Function, String, optional, either callback or message;
onReject - Function, String, optional, either callback or message;
html - String, optional, html for async toast
shows an async gray toast untill promise will be completed, then run a callback or show new toastasync(somePromise, 'success toast msg', rejectCallback , 'Custom async msg')
asyncBlock(promise, onResolve, onReject, html)promise - Promise, required;
onResolve - Function, String, optional, either callback or message;
onReject - Function, String, optional, either callback or message;
html - String, optional, html for async toast
shows loader and blocks the screen untill promise will be completed, then run a callback or show new toastasyncBlock(somePromise, resolveCallback, 'custom message for alert toast' , 'Custom async msg')
confirm(html, onOk, onCancel)html - String, required
onOk - Function, optional
onCancel - Function, optional
shows a modal dialog, which is waiting for users confirmationconfirm('Are you sure?', runIfOkClicked, runIfCancelClicked)
modal(html, className)html - String, required
className - String, required
shows a custom modal dialog with which contains only value of htmlmodal('<h2>Your custom title</h2><p>Your custom text</p>', 'custom-class-name')

How to use async

There are two types of async functions in this library: async and asyncBlock. They differ in appearance but work with promises identically.

If your promise was rejected, alert toast will be shown to the user. New toast message will contain error returned by promise. Make sure that error returned by promise has type of string

Take a look on function behaviour after promise rejection:

notifier.async(Promise.reject("some error")) // will show a new alert toast with message "some error"

notifier.async(Promise.reject({message: "some error")) // will throw an error, because returned value is Object.

notifier.async(Promise.reject("some error"), null, "custom error") // you can pass a string as `onResolve`, it will be used as message for alert toast

notifier.async(Promise.reject("some error"), null, err => console.log(err)) // you can pass a function as `onResolve`, no new toast will be added
notifier.async(Promise.reject("some error")).then(() => {},err => console.log(err)) // run same function with preserving new alert toast

If most of your promises returns similar objects, you can set handleReject function in options, to transform your objects to the strings:

let notifier = new AWN({
  handleReject(value) {
    // value is returned value of your promises reject
    if (typeof value === "string") return value // optional check to preserve values which are already strings
    // any code which should be applied to all reject values
    return value.errors.message //choose string property of object that should be shown as error
  }
})
notifer.async(Promise.reject({ errors: { message: "custom message" } })) // there is no error anymore, alert toast will be shown.

If your promise was resolved, nothing will happen by default. Customization here is similar:

notifier.async(Promise.resolve("all done")) // won't show anything after promise resolved

notifier.async(Promise.resolve("all done"), "your custom message") // will show a new success toast with message "your custom message"
notifier.async(Promise.resolve("all done"), result => {
  notifier.success(`${result} and your custom message`)
}) // will show a new success toast with message "all done and your custom message"
notifier.async(Promise.resolve("all done")).then(result => {
  notifier.success(`${result} and your custom message`)
}) // same as above, written without callback

*How to use modal

modal function will create a new modal window with HTML that you provided. All your HTML will be put into the div with class awn-modal-${className}, where className is the second parameter in modal function.

Customization

Options

You can pass your own options when you're initializing a library, e.g.

var options = {
  labels: {
    tip: "Your custom tip box label"
  }
}
var notifier = new AWN(options)

Available options

All labels properties support HTML.

NameTypeDefaultDescription
positionString"bottom-right"position of notifications
durationNumber5000determines how long notification exists, ms
animationDurationNumber300determines speed of animation, ms
asyncBlockMinDurationNumber500minimal time to show asyncBlock modal window, prevents blinking, when async function completes too fast
maxNotificationsNumber10max amount of notifications
handleRejectFunctionsee in source codehandles returned value of promise in async functions if it was rejected;
By default will throw an error, if value type isn't a String
labelsObjectSee properties belowdefault labels for notifications
labels.tipString"Tip"default label for tip toast
labels.infoString"Info"default label for info toast
labels.successString"Success"default label for success toast
labels.warningString"Attention"default label for warning toast
labels.alertString"Error"default label for alert toast
labels.asyncString"Loading"default label for async toast
labels.confirmString"Confirmation required"confrim window title
iconsObjectSee properties belowdefault Font Awesome icons for notifications
icons.tipString"question-circle"FontAwesome icon classes for tip toast, first one without fa-
icons.infoString"info-circle"FontAwesome icon classes for info toast, first one without fa-
icons.successString"check-circle"FontAwesome icon classes for success toast, first one without fa-
icons.warningString"exclamation-circle"FontAwesome icon classes for warning toast, first one without fa-
icons.alertString"warning"FontAwesome icon classes for alert toast, first one without fa-
icons.asyncString"cof fa-spin"FontAwesome icon classes for async toast, first one without fa-
icons.confirmString"warning"FontAwesome icon classes for confirm window, first one without fa-
icons.enabledBooleantrueDetermines icons existence
icons.prefixString"<i class='fa fa-fw fa"HTML before any icons[value] (e.g. icons.tip)
icons.suffixString"'></i>"HTML after any icons[value] (e.g. icons.tip)
modalObjectSee properties belowmodal windows settings
modal.okLabelString"OK"confirm window success button label
modal.cancelLabelString"Cancel"confirm window cancel button label
modal.maxWidthString"500px"confirm window max-width CSS property
messagesObjectSee properties belowdefault messages
messages.asyncString"Please, wait..."default async toast message, supports HTML
messages["async-block"]String"Loading"default asyncBlock modal message, supports HTML
replacementsObjectSee properties belowcontains rules of replacement for html
each rule is Object
where keys are first param for replace function
and values are second param.
replacements.generalObject{ "<script>": "", "</script>": "" }rules for all event types
replacements.tipObject""rules for tip events
replacements.infoObject""rules for info events
replacements.successObject""rules for success events
replacements.warningObject""rules for warning events
replacements.alertObject""rules for alert events
replacements.asyncObject""rules for async events
replacements.asyncBlockObject""rules for asyncBlock modal window
replacements.modalObject""rules for custom modal window
replacements.confirmObject""rules for confirm window

Styles

The most convinient and quick way to change styles is dowload styles folder which contains .scss files. Then you have to edit variables.scss, compile your scss to css and add new css file to your project.

Also, you can just add default style.css to your project and override it in your styles file. To learn more about default styles, look at styles folder.

Security notes

Make sure that you pass safe HTML to msg param. Sending data which can be directly or indirectly edited by user (e.g. name of account), provides a possibility for HTML Injections. You can set up replacements in options to filter msg variable.

Browser support

Last 2 versions.

License

This project is licensed under the terms of the MIT license.

Keywords

FAQs

Package last updated on 03 Apr 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc