Pure JavaScript library for better notification messages

Overview

Toastify

Built with JavaScript

toastify-js MIT License

Toastify is a lightweight, vanilla JS toast notification library.

Demo

Click here

Features

  • Multiple stacked notifications
  • Customizable
  • No blocking of execution thread

Customization options

  • Notification Text
  • Duration
  • Toast background color
  • Close icon display
  • Display position
  • Offset position

Installation

Toastify now supports installation via NPM

  • Run the below command to add toastify-js to your exisitng or new project.
npm install --save toastify-js

or

yarn add toastify-js -S
  • Import toastify-js into your module to start using it.
import Toastify from 'toastify-js'

You can use the default CSS from Toastify as below and later override it or choose to write your own CSS.

import "toastify-js/src/toastify.css"

Adding ToastifyJs to HTML page using the traditional method

To start using Toastify, add the following CSS on to your page.

<link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/toastify-js/src/toastify.min.css">

And the script at the bottom of the page

<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/toastify-js"></script>

Files are delivered via the CDN service provided by jsdeliver

Documentation

Toastify({
  text: "This is a toast",
  duration: 3000,
  destination: "https://github.com/apvarun/toastify-js",
  newWindow: true,
  close: true,
  gravity: "top", // `top` or `bottom`
  position: "left", // `left`, `center` or `right`
  stopOnFocus: true, // Prevents dismissing of toast on hover
  style: {
    background: "linear-gradient(to right, #00b09b, #96c93d)",
  },
  onClick: function(){} // Callback after click
}).showToast();

Toast messages will be centered on devices with screen width less than 360px.

Add own custom classes

If you want to use custom classes on the toast for customizing (like info or warning for example), you can do that as follows:

Toastify({
  text: "This is a toast",
  className: "info",
  style: {
    background: "linear-gradient(to right, #00b09b, #96c93d)",
  }
}).showToast();

Multiple classes also can be assigned as a string, with spaces between class names.

Add some offset

If you want to add offset to the toast, you can do that as follows:

Toastify({
  text: "This is a toast with offset",
  offset: {
    x: 50, // horizontal axis - can be a number or a string indicating unity. eg: '2em'
    y: 10 // vertical axis - can be a number or a string indicating unity. eg: '2em'
  },
}).showToast();

Toast will be pushed 50px from right in x axis and 10px from top in y axis.

Note:

If position is equals to left, it will be pushed from left. If gravity is equals to bottom, it will be pushed from bottom.

API

Option Key type Usage Defaults
text string Message to be displayed in the toast "Hi there!"
node ELEMENT_NODE Provide a node to be mounted inside the toast. node takes higher precedence over text
duration number Duration for which the toast should be displayed.
-1 for permanent toast
3000
selector string | ELEMENT_NODE ShadowRoot CSS Selector or Element Node on which the toast should be added
destination URL string URL to which the browser should be navigated on click of the toast
newWindow boolean Decides whether the destination should be opened in a new window or not false
close boolean To show the close icon or not false
gravity "top" or "bottom" To show the toast from top or bottom "top"
position "left" or "right" To show the toast on left or right "right"
backgroundColor CSS background value To be deprecated, use style.background option instead. Sets the background color of the toast
avatar URL string Image/icon to be shown before text
className string Ability to provide custom class name for further customization
stopOnFocus boolean To stop timer when hovered over the toast (Only if duration is set) true
callback Function Invoked when the toast is dismissed
onClick Function Invoked when the toast is clicked
offset Object Ability to add some offset to axis
escapeMarkup boolean Toggle the default behavior of escaping HTML markup true
style object Use the HTML DOM Style properties to add any style directly to toast
oldestFirst boolean Set the order in which toasts are stacked in page true

Deprecated properties: backgroundColor - use style.background option instead

Browsers support


IE / Edge

Firefox

Chrome

Safari

Opera
IE10, IE11, Edge last 10 versions last 10 versions last 10 versions last 10 versions

Contributors

AStoker
AStoker
caiomoura1994
caiomoura1994
rndevfx
rndevfx
1ess
1ess
d4rn0k
d4rn0k
danielkaiser80
danielkaiser80
skjnldsv
skjnldsv
chasedeanda
chasedeanda
chrisgraham
chrisgraham
Wachiwi
Wachiwi
FeixuRuins
FeixuRuins
gavinhungry
gavinhungry
haydster7
haydster7
joaquinwojcik
joaquinwojcik
juliushaertl
juliushaertl
mort3za
mort3za
Sandip124
Sandip124
Tadaz
Tadaz
t12ung
t12ung
victorfeijo
victorfeijo
fiatjaf
fiatjaf
prousseau-korem
prousseau-korem

License

MIT © Varun A P

Buy Me A Coffee

Comments
  • Add support for offset

    Add support for offset

    Add some basic support for offset as requested on #48

    Usage:

    Toastify({
      text: "This is a toast with offset",
      offset: {
        x: 50, // horizontal axis - can be a number or a string indicating unity. eg: '2em'
        y: 10 // vertical axis - can be a number or a string indicating unity. eg: '2em'
      },
    }).showToast();
    

    Toast will be pushed 50px from right in x axis and 10px from top in y axis.

    Example: image

    If position is equals to left, it will be pushed from left. If gravity is equals to bottom, it will be pushed from bottom.

    Example: image

    Also updated the docs.

    opened by joaquinwojcik 7
  • Toast should always visible

    Toast should always visible

    Hello, I have the problem that the toast is coming from the bottom of the top of the page and not top or bottom of the screen so if I am scrolling the page or using it on a tall page, I cannot see the toast anymore, is there a fix? Currently, the website using it is embedded within an iframe thought I am not sure if that is matter or not. Please kindly advise,

    opened by carloftg 7
  • Doesn't play well with TailwindCSS (or any other utility framework)

    Doesn't play well with TailwindCSS (or any other utility framework)

    ... because of the style background: linear-gradient(135deg, #73a5ff, #5477f5) applied by default.

    TailwindCSS uses the bg- utility class to change the background color. Passing the option className doesn't work of course, because of the background property:

    toastify

    This is annoying because most of the time the primary purpose is to change the background color. Is there a way to unset (maybe using some options) the background itself, and use my bg-green-500 class?

    opened by gremo 5
  • Style Options:

    Style Options: "backgroundColor" not working!

    Toastify({ style: { backgroundColor: '#dc3545' } }).showToast(); Not Working...

    default: background: -webkit-linear-gradient(315deg, #73a5ff, #5477f5); background: linear-gradient(135deg, #73a5ff, #5477f5);

    Hacktoberfest 
    opened by ghost 5
  • Any plans to support arbitrary HTML content?

    Any plans to support arbitrary HTML content?

    For ultimate control and styling, I would like to optionally pass HTML div with arbitrary content as arg. In my case I'd like to include small chart or buttons, small table etc. Any plans for such a feature?

    opened by hipitihop 5
  • stop requiring the CSS file from JavaScript.

    stop requiring the CSS file from JavaScript.

    I think we should leave that as an option for people who like to require CSS files and have a build system and process that allows that. For all other cases, it should be an option not to.

    opened by fiatjaf 5
  • How to use this inside chrome extension ?

    How to use this inside chrome extension ?

    I am currently creating a single chrome extension. One of its functions is to send a toast to the user. I want to do this using this library, can you help?

    opened by AbbosRakhmonov 4
  • Property style.background has stopped working

    Property style.background has stopped working

    I am pulling Toastify JS from the CDN. Just now I've realized that the property style.background is no longer working, and my toasts are using the built-in style from toastify.css instead. (I did force reload all my resources to ensure that it wasn't a stale version.) The property backgroundColor still works, but this gives me a deprecation warning.

    opened by catempire 4
  • Convert to ES6 compatible library

    Convert to ES6 compatible library

    ES6 Conversion + Features

    Resolves #15 Added a new file (toastify-es.js) which contains an ES6 compliant version of Toastify.
    Few improvements and changes to behavior:

    • Based off comments, positionLeft was finally removed
    • Expanded functionality for selector that allows you to pass in an element as well (defaults to document.body)

    Notable changes to code:

    • Made use of arrow functions to keep context the same (replaced (function(){...}).bind(this))
    • let instead of var
    • Toastify class rather than function. Exported function creates an instance of Toastify
    • When checking classes, use the built in classList functionality on elements
    • rootElement is now stored on the class so when we remove toasts we know where to look specifically (this enhanced the ability of putting toasts inside elements and allows you to use Toastify inside shadowDom nodes)
    • Added ability to use escapeMarkup to allow optionally unsafe innerHTML
    • Added ability to use the HTML DOM style property to directly style the toast (this further extends backgroundColor functionality, and therefore backgroundColor should be deprecated in the future in favor of using style.background). Note, since previous functionality took backgroundColor and applied it to the elements background style (which changes all background styles), to prevent breaking changes, backgroundColor takes precedence over style.backgroundColor.

    Things that should still happen:

    • [x] Get maintainer feedback (I'm only human and spent a few hours quickly changing things, don't want to miss something)
    • [x] Prevent XSS issues when we pass text into the innerHTML of the toast (probably just want to use innerText, as there's already node for when you want to pass in an element)
    • [x] Cleanup of default values and states
    • [x] Adequate testing of all use cases
    • [x] Test across browsers
    • [x] Include added functionality in legacy versions
    opened by AStoker 4
  • multiple messages do not dissapear

    multiple messages do not dissapear

    If I show one message it disappears. No problem but if i show multiple messages then only first one disappears rest are keeps alive.

    here is my message below

    toast.lib.init({ text: message, duration: 3000, newWindow: false, close: true, gravity: "top", position: 'right', backgroundColor: "linear-gradient(to right, #00b09b, #96c93d)", stopOnFocus: true, }).showToast();

    opened by merkancam 4
  • Improve accessibility by defaulting and allowing `aria-live`

    Improve accessibility by defaulting and allowing `aria-live`

    Hi there, greetings from https://github.com/nextcloud/nextcloud-dialogs/issues/255 We are in the process of doing an A11y validation and one of the remarks was that or errors and messages (which are using toastify) are not read by screen readers. This can be easily achieved by setting aria-live="polite" on the HTMLElement.

    For errors and important information it is even recommended to use aria-live="assertive", however this interrupts whatever is currently being read out by the screenreader, so it should be "opt in" so it can be used for errors and similar stuff, but is not forced for "Your message was posted" kind of messages.

    Currently I implemented a PR in our lib which just manipulates the HTMLElement afterwards: https://github.com/nextcloud/nextcloud-dialogs/pull/622/files

    But of course it would be better if the library would allow this directly, so all users of the lib benefit from it.

    Are you interested in fixing this and would accept a pull request + publish a new version afterwards?

    opened by nickvergessen 3
  • Stop the timeout if the window is not focused

    Stop the timeout if the window is not focused

    Fixes #110. This adds a new option, stopOnWindowBlur, that - similarly to the stopOnFocus option - stops the timeout timer if the user navigates away from the page and restarts the timer once the window regains focus. This prevents notifications from disappearing without the user actually having seen it. I set it to be enabled by default.

    opened by DieterHolvoet 0
  • Reposition function is not working with shadow roots (selector option)

    Reposition function is not working with shadow roots (selector option)

    Actual behavior

    You provide a useful feature for shadow roots which is the selector property, however the reposition function is not working due to shadow roots. You are using document.getElementsByClassName which gets blocked by the shadow roots so whenever we use shadow roots nothing get selected, so nothing is repositioned.

    Expected behavior

    I expect the reposition function to work in shadow root by checking if a selector is passed to the toast and using it to do the getElements located here. I have managed to make it work locally by doing the following (I haven't tested any other usecases so it may broke other usecases but it's a good track of solution) :

    I have passed the this object as props to the reposition function to get access to the selector and check if any is provided, if so we do the equivalent of getElementsByClassName but with shadow roots way, which is the querySelectorAll.

    We get the following for the lime 363: var allToasts = toast.options.selector ? toast.options.selector.querySelectorAll('.toastify') : document.getElementsByClassName("toastify");

    Don't hesitate if you need more information.

    opened by thomleclerc 0
  • Only start the countdown timer if the page is visible

    Only start the countdown timer if the page is visible

    Actual behaviour

    I'm using this library with the Drupal module. This module shows messages e.g. after saving a page. It happens often that I save the page and while waiting for the new page to load, I navigate to another tab. Once I come back to the previous tab where the toast appeared, I notice that the toast has already disappeared.

    Expected behaviour

    Only start the countdown timer if the tab where the toast appears is open. This way, no messages can be missed. Could be made an option instead of the default behaviour.

    opened by DieterHolvoet 0
  • Add a progress bar showing the time until the toast closes

    Add a progress bar showing the time until the toast closes

    Implemented the progress bar that shows the time until the toast closes (#94)

    How to use:

    Toastify({
            progressBar: true,
            progressBarColor: 'coral',
            text: "With progress bar",
            gravity: "top",
            position: 'left'
        }).showToast();
    

    Animation is done with CSS animation property.

    opened by konclave 2
  • Choose Between Displaying Multiple Toasts Or Just One

    Choose Between Displaying Multiple Toasts Or Just One

    Description

    This PR will allow users to choose between displaying a single toast on the screen vs. displaying multiple toasts, with one line of code.

    Displaying multiple toasts is still the default setting, so anyone using Toastify wouldn't be affected by these changes. The only difference is that with the new "single toast" approach, the toast duration is not honored. Meaning, if a new toast is about to appear, any toast(s) that are currently on the screen are then removed, regardless of how long they were supposed to be displayed for.

    How To Use

    Toastify.multiple = true; // Allows multiple toasts (default)
    Toastify.multiple = false; // Only one toast can be on the screen
    Toastify({
        ...
    }).showToast()
    

    Super easy! It can be toggled back and forth if it needs to be, but it only needs to be set once at the top of the script, before any toasts are created.

    Testing

    I want to start off by saying I have not tested this on all browsers. However, can confirm that it is working on these browsers:

    • Google Chrome (version 104.0.5112.102)
    • Firefox (version 80.0.1)

    The code I added is very short and sweet so I would not worry too much about compatibility issues. The only functions I use that are worth looking into are:

    indexOf  - https://caniuse.com/?search=indexOf
    splice   - https://caniuse.com/?search=splice
    forEach  - https://caniuse.com/?search=forEach
    

    Based on the results from caniuse.com, I don't think this will cause any issues.

    Additional Comments

    I took the liberty of updating the demo to include this "toggle" feature by adding a checkbox to toggle between the 2 states. Toggling the checkbox also updates the code displayed in the box. It can be viewed at https://trevorslobodnick.github.io/toastify-js/

    I also updated the readme so the documentation now includes Toastify.multiple = true; at the top, and I also added myself to the contributors list.

    Im new to this so please let me know if I need to add, edit, or remove anything.

    Thanks, Trevor Slobodnick

    opened by TrevorSlobodnick 0
Releases(1.12.0)
  • 1.12.0(Jul 21, 2022)

  • 1.11.2(Oct 6, 2021)

  • 1.11.1(Jul 15, 2021)

  • 1.11.0(Apr 25, 2021)

  • 1.10.0(Mar 25, 2021)

    • selector now supports a DOM Node, along with ID string (#65)
    • New property - escapeMarkup - Toggle the default behavior of escaping HTML markup
    • New property - style - Use the HTML DOM Style properties to add any style directly to toast
    • Adds toastify-es.js, to be used from node_modules until there are no compatibility issues

    Deprecations:

    • backgroundColor is deprecated. Use style.background instead
    Source code(tar.gz)
    Source code(zip)
  • 1.9.3(Oct 10, 2020)

  • 1.9.2(Oct 10, 2020)

  • 1.9.1(Aug 13, 2020)

  • 1.8.0(Jun 12, 2020)

  • 1.7.0(Mar 1, 2020)

    • To be able to set stopOnFocus for toasts without close icon
    • Bugfix: duration can be infinite by setting as 0
    • Bugfix: Prevent errors when parent node is removed from DOM while using frameworks
    • Bugfix: IE 9/10 compatibility fix
    Source code(tar.gz)
    Source code(zip)
  • 1.6.1(Jun 29, 2019)

    • Deprecation Warning: Migrating from positionLeft property to position - positionLeft is still supported but may be removed in future versions
    • Property position to support center as a value along with left and right - Useful for centering toast messages in the page
    • Bugfix: Disabling stopOnFocus
    Source code(tar.gz)
    Source code(zip)
  • 1.5.0(May 30, 2019)

  • 1.4.0(May 12, 2019)

    • Breaking Change: Manually import CSS while using as module in your modern JavaScript applications.
    • Ability to pause the toast dismiss timer on hover (Using stopOnFocus property)
    Source code(tar.gz)
    Source code(zip)
  • 1.3.1(Nov 13, 2018)

    • Fix issue when destination and close options is used at the same time
    • Added support for Classes. Now custom classes can be added to the toast while creating it
    Source code(tar.gz)
    Source code(zip)
  • 1.1.0(Feb 18, 2018)

  • 1.0.0(Feb 17, 2018)

  • 0.0.1(Sep 3, 2017)

Owner
Varun A P
Frontend Engineer, Web perf expert, AWS Community Builder. Spends time learning, building & speeding up stuff on the internet.
Varun A P
🔔 a clean and simple notification, input, and selection suite for javascript, with no dependencies

notie notie is a clean and simple notification, input, and selection suite for javascript, with no dependencies Live demo: https://jaredreich.com/noti

Jared Reich 6.3k Dec 26, 2022
Elegant, responsive, flexible and lightweight notification plugin with no dependencies.

iziToast Elegant, responsive, flexible and lightweight notification plugin with no dependencies. izitoast.marcelodolza.com Fast Responsive Animated Li

Marcelo Dolza 2.5k Jan 2, 2023
A simple, modern, browser notification system

humane.js This project was heavily inspired by humanmsg project for jQuery. I really liked the project but I wanted to remove the jQuery dependency, a

Marc Harter 2.1k Dec 10, 2022
Growl-style alerts and messages for your app. #hubspot-open-source

Messenger Demo and Usage Docs Show messages in your app. Wrap AJAX requests with progress, success and error messages, and add retry to your failed re

HubSpot 4k Jan 2, 2023
🚀 The ultimate library for managing multi-channel notifications with a single API.

?? The ultimate library for managing multi-channel notifications with a single API.

Notifire 16.3k Jan 4, 2023
Beautiful JavaScript notifications with Web Notifications support.

v4: v5: A JavaScript/TypeScript notification, confirmation, and prompt library. Notifications can display as toast style, snackbar style, banners, dia

SciActive 3.6k Dec 30, 2022
Simple javascript toast notifications

toastr toastr is a Javascript library for non-blocking notifications. jQuery is required. The goal is to create a simple core library that can be cust

null 11.5k Jan 6, 2023
framework-agnostic styled alert system for javascript

SMOKE.JS - 0.1.3 Notify or get approval from your users quickly and with style. This alert system uses css animations and background (so there are no

j youngblood 934 Dec 25, 2022
Grupprojekt för kurserna 'Javascript med Ramverk' och 'Agil Utveckling'

JavaScript-med-Ramverk-Laboration-3 Grupprojektet för kurserna Javascript med Ramverk och Agil Utveckling. Utvecklingsguide För information om hur utv

Svante Jonsson IT-Högskolan 3 May 18, 2022
⛔️ DEPRECATED - Dependency-free notification library that makes it easy to create alert - success - error - warning - information - confirmation messages as an alternative the standard alert dialog.

DEPRECATED This repository is no longer supported, please consider using alternatives. Dependency-free notification library. Documentation » Hi NOTY i

Nedim Arabacı 6.7k Dec 21, 2022
Hemsida för personer i Sverige som kan och vill erbjuda boende till människor på flykt

Getting Started with Create React App This project was bootstrapped with Create React App. Available Scripts In the project directory, you can run: np

null 4 May 3, 2022
Kurs-repo för kursen Webbserver och Databaser

Webbserver och databaser This repository is meant for CME students to access exercises and codealongs that happen throughout the course. I hope you wi

null 14 Jan 3, 2023
A chat logs online saver for discord bots to save messages history & cleared messages online

Chat Logs NPM package that saves messages online to view it later Useful for bots where users can save messages history & cleared messages online Supp

TARIQ 8 Dec 28, 2022
Lightweight and simple notification library in Vanilla JavaScript.

SimpleNotification SimpleNotification is a library to display simple yet customizable notifications. You can stylize text with a simple syntax, add bu

null 14 Dec 11, 2022
Mosha-vue-toastify - A light weight and fun Vue 3 toast or notification or snack bar or however you wanna call it library.

Mosha Vue Toastify A lightweight and fun Vue 3 toast or notification or snack bar or however you wanna call it library. English | 简体中文 Talk is cheap,

Baidi Liu 187 Jan 2, 2023
Notification handler. KISS, light and library free.

Notification.js Notification.js is a standalone ES6 module that allows you to easily handle several notifications from different types at the same tim

Arthur Beaulieu 2 Nov 25, 2020
A simple and light jquery library for toast notification!

What is notify message? Notify message it's a simple jquery library for create a simple and light push notification in your website! How does this wor

Ivan Persiani 3 Feb 23, 2022
🔔 a clean and simple notification, input, and selection suite for javascript, with no dependencies

notie notie is a clean and simple notification, input, and selection suite for javascript, with no dependencies Live demo: https://jaredreich.com/noti

Jared Reich 6.3k Dec 26, 2022
JavaScript Alert/Notification System

alertify.js - browser dialogs never looked so good The end of maintenance... at least for now Unfortunately, I will no longer be maintaining alertify.

Fabien Doiron 4.3k Dec 22, 2022
Elegant, responsive, flexible and lightweight notification plugin with no dependencies.

iziToast Elegant, responsive, flexible and lightweight notification plugin with no dependencies. izitoast.marcelodolza.com Fast Responsive Animated Li

Marcelo Dolza 2.5k Jan 2, 2023