A dead simple, responsive, a11y, dependency-free, vanilla JavaScript toast library.
npm install notyf
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.
- π± 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
Demo: carlosroso.com/notyf
```
npm i notyf
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:
`html`
...
...
> Files are delivered via CDN by jsdeliver
`javascript
// Create an instance of Notyf
var notyf = new Notyf();
// Display an error notification
notyf.error('You must fill out the form before moving forward');
// Display a success notification
notyf.success('Your changes have been successfully saved!');
`
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.
`javascript
import { Notyf } from 'notyf';
import 'notyf/notyf.min.css'; // for React, Vue and Svelte
// Create an instance of Notyf
const notyf = new Notyf();
// Display an error notification
notyf.error('Please fill out the form');
`
You can set some options when creating a Notyf instance.
Param | Type | Default | Details
------------ | ------------- | ------------- | -------------
duration | number | 2000 | Number of miliseconds before hiding the notification. Use 0 for infinite duration.boolean
ripple | | true | Whether to show the notification with a ripple effectINotyfPosition
position | | {x:'right',y:'bottom'} | Viewport location where notifications are renderedboolean
dismissible | |Β false | Whether to allow users to dismiss the notification with a buttonINotyfNotificationOptions[]
types | [](#inotyfnotificationoptions) | Success and error toasts | Array with individual configurations for each type of toast
Dismiss a specific notification.
`javascript`
const notyf = new Notyf();
const notification = notyf.success('Address updated');
notyf.dismiss(notification);
Dismiss all the active notifications.
`javascript`
const notyf = new Notyf();
notyf.success('Address updated');
notyf.error('Please fill out the form');
notyf.dismissAll();
Every individual notification emits events. You can register listeners using the on method.
Triggers when the notification is clicked
`javascript`
const notyf = new Notyf();
const notification = notyf.success('Address updated. Click here to continue');
notification.on('click', ({target, event}) => {
// target: the notification being clicked
// event: the mouseevent
window.location.href = '/';
});
Triggers when the notification is manually (not programatically) dismissed.
`javascript`
const notyf = new Notyf();
notyf
.error({
message: 'There has been an error. Dismiss to retry.',
dismissible: true
})
.on('dismiss', ({target, event}) => foobar.retry());
Viewport location where notifications are rendered.
Param | Type | Details
------------ | ------------- | -------------
x | left \| center \| right | x-positiontop \| center \| bottom
y | | y-position
Configuration interface for each individual toast.
Param | Type | Details
------------ | ------------- | -------------
type | string | Notification type for which this configuration will be appliedstring
className | | Custom class name to be set in the toast wrapper elementnumber
duration | | 2000 | Number of miliseconds before hiding the notificationstring
icon | INotyfIcon false | Either a string with HTML markup, an object with the properties of the icon, or 'false' to hide the iconstring
background | | Background color of the toaststring
message | | Message to be rendered inside of the toast. Becomes the default message when used in the global config.boolean
ripple | | Whether or not to render the ripple at revealingboolean
dismissible | | Whether to allow users to dismiss the notification with a button
Icon configuration
Param | Type | Details
------------ | ------------- | -------------
className | string | Custom class name to be set in the icon elementstring
tagName | | HTML5 tag used to render the iconstring
text | | Inner text rendered within the icon (useful when using ligature icons)string
color | | Icon color. It must be a valid CSS color value. Defaults to background color.
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
`javascript`
const notyf = new Notyf({
duration: 1000,
position: {
x: 'right',
y: 'top',
},
types: [
{
type: 'warning',
background: 'orange',
icon: {
className: 'material-icons',
tagName: 'i',
text: 'warning'
}
},
{
type: 'error',
background: 'indianred',
duration: 2000,
dismissible: true
}
]
});
Register a new toast type and use it by referencing its type name:
`javascript
const notyf = new Notyf({
types: [
{
type: 'info',
background: 'blue',
icon: false
}
]
});
notyf.open({
type: 'info',
message: 'Send us an email to get support'
});
`
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.
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.
`javascript
const notyf = new Notyf();
notyf.error({
message: 'Accept the terms before moving forward',
duration: 9000,
icon: false
})
``
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.
Please see the contributing document and read the contribution guidelines. Thanks in advance for all the help!
Notyf is under MIT licence