Notyf

A minimalistic, responsive, vanilla JavaScript library to show toast notifi...

README

Notyf npm version Cypress.io tests npm downloads size jsdelivr



Notyf is a minimalistic JavaScript library for toast notifications. It's responsive, A11Y compatible, dependency-free and tiny (~3KB). Easy integration with React, Angular, Aurelia, Vue, and Svelte.

demo gif

Features


- 📱 Responsive
- 👓 A11Y compatible
- 🔥 Strongly typed codebase (TypeScript Typings readily available)
- ⚡️ 4 types of bundles exposed: ES6, CommonJS, UMD, and IIFE (for vanilla, framework-free usage).
- 🎯 End-to-end testing with Cypress
- 🎸 Easily plugable to modern frameworks. Recipes available to integrate with React, Angular, Aurelia, Vue, and Svelte.
- ✨ Optional ripple-like fancy revealing effect
- 😈 Simple but highly extensible API. Create your own toast types and customize them.
- 🎃 Support to render custom HTML content within the toasts
- 🐣 Tiny footprint (<3K gzipped)
- 👴🏽 Works on IE11


Installation


  1. ```
  2. npm i notyf
  3. ```

Usage


This section explains the base case using the minified bundle. See the quick recipes section for instructions to plug Notyf into Angular, React, Aurelia, Vue, or Svelte.

Add the css and js files to your main document:

  1. ``` html
  2. <html>
  3.   <head>
  4.     ...
  5.     <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/notyf@3/notyf.min.css">
  6.   </head>
  7.   <body>
  8.     ...
  9.     <script src="https://cdn.jsdelivr.net/npm/notyf@3/notyf.min.js"></script>
  10.   </body>
  11. </html>
  12. ```

Files are delivered via CDN by jsdeliver


Basic


  1. ``` js
  2. // Create an instance of Notyf
  3. var notyf = new Notyf();

  4. // Display an error notification
  5. notyf.error('You must fill out the form before moving forward');

  6. // Display a success notification
  7. notyf.success('Your changes have been successfully saved!');
  8. ```

With module bundlers


Notyf ships with an ES6 bundle referenced from the module key of its package.json. This is the file that module bundlers like Webpack will use when using the package. Notyf is exported as a class under the notyf namespace. Typings are also available.

  1. ``` js
  2. import { Notyf } from 'notyf';
  3. import 'notyf/notyf.min.css'; // for React, Vue and Svelte

  4. // Create an instance of Notyf
  5. const notyf = new Notyf();

  6. // Display an error notification
  7. notyf.error('Please fill out the form');
  8. ```

API


You can set some options when creating a Notyf instance.

new Notyf(options: INotyfOptions)


Param | Type | Default | Details
duration | number | 2000 | Number of miliseconds before hiding the notification. Use 0 for infinite duration.
ripple | boolean | true | Whether to show the notification with a ripple effect
position | [INotyfPosition](#inotyfposition) | {x:'right',y:'bottom'} | Viewport location where notifications are rendered
dismissible | boolean | false | Whether to allow users to dismiss the notification with a button
types | [INotyfNotificationOptions[]](#inotyfnotificationoptions) | Success and error toasts | Array with individual configurations for each type of toast

dismiss(notification: NotyfNotification)


Dismiss a specific notification.

  1. ``` js
  2. const notyf = new Notyf();
  3. const notification = notyf.success('Address updated');
  4. notyf.dismiss(notification);
  5. ```

dismissAll()


Dismiss all the active notifications.

  1. ``` js
  2. const notyf = new Notyf();
  3. notyf.success('Address updated');
  4. notyf.error('Please fill out the form');
  5. notyf.dismissAll();
  6. ```

Events


Every individual notification emits events. You can register listeners using the on method.

'click'


Triggers when the notification is clicked

  1. ``` js
  2. const notyf = new Notyf();
  3. const notification = notyf.success('Address updated. Click here to continue');
  4. notification.on('click', ({target, event}) => {
  5.   // target: the notification being clicked
  6.   // event: the mouseevent
  7.   window.location.href = '/';
  8. });
  9. ```

'dismiss'


Triggers when the notification is manually (not programatically) dismissed.

  1. ``` js
  2. const notyf = new Notyf();
  3. notyf
  4.   .error({
  5.     message: 'There has been an error. Dismiss to retry.',
  6.     dismissible: true
  7.   })
  8.   .on('dismiss', ({target, event}) => foobar.retry());
  9. ```

Interfaces


INotyfPosition


Viewport location where notifications are rendered.

Param | Type | Details
x | left \| center \| right | x-position
y | top \| center \| bottom | y-position

INotyfNotificationOptions


Configuration interface for each individual toast.

Param | Type  | Details
type | string | Notification type for which this configuration will be applied
className | string | Custom class name to be set in the toast wrapper element
duration | number | 2000 | Number of miliseconds before hiding the notification
icon | string [INotyfIcon](#inotyficon) false | Either a string with HTML markup, an object with the properties of the icon, or 'false' to hide the icon
background | string | Background color of the toast
message | string | Message to be rendered inside of the toast. Becomes the default message when used in the global config.
ripple | boolean | Whether or not to render the ripple at revealing
dismissible | boolean | Whether to allow users to dismiss the notification with a button

INotyfIcon


Icon configuration

Param | Type | Details
className | string | Custom class name to be set in the icon element
tagName | string | HTML5 tag used to render the icon
text | string | Inner text rendered within the icon (useful when using ligature icons)
color | string | Icon color. It must be a valid CSS color value. Defaults to background color.

Examples


Global configuration


The following example configures Notyf with the following settings:
- 1s duration
- Render notifications in the top-right corner
- New custom notification called 'warning' with a ligature material icon
- Error notification with custom duration, color and dismiss button

  1. ``` js
  2. const notyf = new Notyf({
  3.   duration: 1000,
  4.   position: {
  5.     x: 'right',
  6.     y: 'top',
  7.   },
  8.   types: [
  9.     {
  10.       type: 'warning',
  11.       background: 'orange',
  12.       icon: {
  13.         className: 'material-icons',
  14.         tagName: 'i',
  15.         text: 'warning'
  16.       }
  17.     },
  18.     {
  19.       type: 'error',
  20.       background: 'indianred',
  21.       duration: 2000,
  22.       dismissible: true
  23.     }
  24.   ]
  25. });
  26. ```

Custom toast type


Register a new toast type and use it by referencing its type name:

  1. ``` js
  2. const notyf = new Notyf({
  3.   types: [
  4.     {
  5.       type: 'info',
  6.       background: 'blue',
  7.       icon: false
  8.     }
  9.   ]
  10. });

  11. notyf.open({
  12.   type: 'info',
  13.   message: 'Send us <b>an email</b> to get support'
  14. });
  15. ```

Warning: Notyf doesn't sanitize the content when rendering your message. To avoid injection attacks, you should either sanitize your HTML messages or make sure you don't render user generated content on the notifications.

Default types with custom configurations


The default types are 'success' and 'error'. You can use them simply by passing a message as its argument, or you can pass a settings object in case you want to modify its behaviour.

  1. ``` js
  2. const notyf = new Notyf();

  3. notyf.error({
  4.   message: 'Accept the terms before moving forward',
  5.   duration: 9000,
  6.   icon: false
  7. })
  8. ```

Recipes


Notyf is well supported in all of the modern frameworks such as Angular, React, Aurelia, Vue, or Svelte. Check out the recipes and learn how to integrate the library to your application.

Contributing


Please see the contributing document and read the contribution guidelines. Thanks in advance for all the help!

Licence


Notyf is under MIT licence