Convert Figma logo to code with AI

apvarun logotoastify-js

Pure JavaScript library for better notification messages

2,128
230
2,128
42

Top Related Projects

React notification made easy 🚀 !

11,964

Simple javascript toast notifications

46,822

⏰ Day.js 2kB immutable date-time library alternative to Moment.js with the same modern API

A beautiful replacement for JavaScript's "alert"

Chance - Random generator helper for JavaScript

Growl-style alerts and messages for your app. #hubspot-open-source

Quick Overview

Toastify-js is a lightweight JavaScript library for creating customizable toast notifications. It provides an easy way to display non-blocking notifications in web applications, offering a simple API and various customization options.

Pros

  • Lightweight and dependency-free
  • Highly customizable with numerous options
  • Easy to integrate and use
  • Supports both ES6 modules and CommonJS

Cons

  • Limited built-in animation options
  • No built-in support for queueing multiple notifications
  • Lacks advanced features like progress bars or action buttons
  • May require additional styling for complex layouts

Code Examples

Display a simple toast notification:

Toastify({
  text: "Hello, World!",
  duration: 3000
}).showToast();

Create a toast with custom styling:

Toastify({
  text: "Custom styled toast",
  duration: 3000,
  backgroundColor: "linear-gradient(to right, #00b09b, #96c93d)",
  className: "info",
}).showToast();

Show a toast with a close button:

Toastify({
  text: "Click to close",
  close: true,
  gravity: "top",
  position: "center",
}).showToast();

Getting Started

  1. Install Toastify-js using npm:
npm install --save toastify-js
  1. Import and use in your JavaScript file:
import Toastify from 'toastify-js'
import "toastify-js/src/toastify.css"

Toastify({
  text: "This is a toast",
  duration: 3000,
  destination: "https://github.com/apvarun/toastify-js",
  newWindow: true,
  close: true,
  gravity: "top",
  position: "right",
  stopOnFocus: true,
  style: {
    background: "linear-gradient(to right, #00b09b, #96c93d)",
  },
  onClick: function(){}
}).showToast();

For usage without a module bundler, include the CSS and JS files in your HTML:

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

Competitor Comparisons

React notification made easy 🚀 !

Pros of react-toastify

  • Specifically designed for React applications, offering seamless integration
  • Provides more advanced features like progress bars and update functionality
  • Offers a wider range of customization options and themes

Cons of react-toastify

  • Larger bundle size due to additional features and React-specific code
  • Steeper learning curve for developers not familiar with React ecosystem
  • May be overkill for simple projects or non-React applications

Code Comparison

Toastify-js:

Toastify({
  text: "This is a toast",
  duration: 3000,
  close: true,
  gravity: "top",
  position: "right",
}).showToast();

react-toastify:

import { toast } from 'react-toastify';

toast("This is a toast", {
  position: "top-right",
  autoClose: 3000,
  closeOnClick: true,
});

Both libraries offer simple ways to create toasts, but react-toastify's syntax is more React-friendly and integrates well with component-based architecture. Toastify-js provides a more straightforward approach that can be easily used in any JavaScript project.

11,964

Simple javascript toast notifications

Pros of toastr

  • More customization options for toast appearance and behavior
  • Supports multiple toast types (success, info, warning, error)
  • Extensive documentation and examples

Cons of toastr

  • Larger file size and more dependencies
  • Less modern API design compared to Toastify
  • Requires jQuery as a dependency

Code Comparison

Toastify:

Toastify({
  text: "Hello, World!",
  duration: 3000,
  close: true,
  gravity: "top",
  position: "right",
}).showToast();

toastr:

toastr.options = {
  "closeButton": true,
  "positionClass": "toast-top-right",
  "timeOut": "3000"
};
toastr.success("Hello, World!");

Both libraries offer simple ways to display toast notifications, but Toastify has a more modern and concise API. toastr requires separate configuration of options and provides specific methods for different toast types.

Toastify is a lightweight, dependency-free library with a focus on simplicity and ease of use. It offers basic customization options and a straightforward API. On the other hand, toastr provides more advanced features and customization options but comes with a larger footprint and requires jQuery.

Choose Toastify for a simple, lightweight solution, or opt for toastr if you need more advanced features and don't mind the additional dependencies.

46,822

⏰ Day.js 2kB immutable date-time library alternative to Moment.js with the same modern API

Pros of Day.js

  • More versatile: Day.js is a date and time manipulation library, offering a wide range of functionalities beyond notifications
  • Smaller bundle size: Day.js is lightweight (2KB minified and gzipped) compared to Toastify.js
  • Extensive plugin ecosystem: Day.js has numerous plugins for extended functionality

Cons of Day.js

  • Different purpose: Day.js is not designed for creating toast notifications, which is Toastify.js's primary function
  • Learning curve: Day.js may require more time to learn due to its broader feature set

Code Comparison

Toastify.js (creating a toast notification):

Toastify({
  text: "This is a toast",
  duration: 3000
}).showToast();

Day.js (formatting a date):

dayjs().format('YYYY-MM-DD HH:mm:ss');

While both libraries are useful, they serve different purposes. Toastify.js is specifically for creating toast notifications, while Day.js is a more general-purpose date and time manipulation library. The choice between them depends on the specific needs of your project.

A beautiful replacement for JavaScript's "alert"

Pros of SweetAlert

  • More customizable alert designs with rich content support
  • Built-in input types for gathering user information
  • Supports chaining multiple alerts and promise-based usage

Cons of SweetAlert

  • Larger file size and potentially heavier on resources
  • Steeper learning curve due to more complex API
  • May be overkill for simple notification needs

Code Comparison

SweetAlert:

Swal.fire({
  title: 'Are you sure?',
  text: "You won't be able to revert this!",
  icon: 'warning',
  showCancelButton: true,
  confirmButtonText: 'Yes, delete it!'
}).then((result) => {
  if (result.isConfirmed) {
    // Deletion logic here
  }
})

Toastify:

Toastify({
  text: "Are you sure you want to delete this?",
  duration: 3000,
  close: true,
  gravity: "top",
  position: "right",
  backgroundColor: "linear-gradient(to right, #ff416c, #ff4b2b)",
}).showToast();

SweetAlert offers more complex interactions and customization, while Toastify focuses on simpler, non-blocking notifications. SweetAlert is better suited for important user decisions or data input, whereas Toastify excels at providing quick, unobtrusive feedback. The choice between them depends on the specific needs of your project and the level of user interaction required.

Chance - Random generator helper for JavaScript

Pros of Chancejs

  • More versatile: Generates random data for various use cases, not limited to notifications
  • Larger community: More stars, forks, and contributors, indicating wider adoption
  • Extensive documentation: Comprehensive API reference and examples

Cons of Chancejs

  • Larger file size: Heavier library compared to Toastify-js
  • Not focused on UI: Doesn't provide ready-to-use visual components
  • Steeper learning curve: More complex API due to its broader functionality

Code Comparison

Toastify-js (creating a notification):

Toastify({
  text: "This is a toast",
  duration: 3000
}).showToast();

Chancejs (generating random data):

var chance = new Chance();
var randomName = chance.name();
var randomEmail = chance.email();

Summary

Chancejs is a powerful library for generating random data, offering a wide range of functions for various data types. It's well-suited for testing, mock data generation, and simulations. Toastify-js, on the other hand, is a lightweight library specifically designed for creating toast notifications in web applications. While Chancejs provides more versatility in data generation, Toastify-js excels in its focused approach to UI notifications.

Growl-style alerts and messages for your app. #hubspot-open-source

Pros of Messenger

  • More comprehensive messaging solution with advanced features like message actions and custom styling
  • Better documentation and examples for various use cases
  • Actively maintained by HubSpot with regular updates and bug fixes

Cons of Messenger

  • Larger file size and potentially more complex to implement
  • Less focused on simple toast notifications, which may be overkill for basic use cases
  • Requires jQuery as a dependency, which may not be ideal for all projects

Code Comparison

Toastify-js:

Toastify({
  text: "This is a toast",
  duration: 3000,
  close: true,
  gravity: "top",
  position: "right",
}).showToast();

Messenger:

Messenger().post({
  message: "This is a message",
  type: "info",
  showCloseButton: true,
  hideAfter: 3,
  position: "top right"
});

Both libraries offer similar functionality for displaying notifications, but Messenger provides more options for customization and advanced features. Toastify-js is more lightweight and focused specifically on toast notifications, while Messenger offers a broader range of messaging capabilities. The choice between the two depends on the specific needs of your project and the level of complexity you require in your notification system.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

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 existing 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 KeytypeUsageDefaults
textstringMessage to be displayed in the toast"Hi there!"
nodeELEMENT_NODEProvide a node to be mounted inside the toast. node takes higher precedence over text
durationnumberDuration for which the toast should be displayed.
-1 for permanent toast
3000
selectorstring | ELEMENT_NODEShadowRootCSS Selector or Element Node on which the toast should be added
destinationURL stringURL to which the browser should be navigated on click of the toast
newWindowbooleanDecides whether the destination should be opened in a new window or notfalse
closebooleanTo show the close icon or notfalse
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"
backgroundColorCSS background valueTo be deprecated, use style.background option instead. Sets the background color of the toast
avatarURL stringImage/icon to be shown before text
classNamestringAbility to provide custom class name for further customization
stopOnFocusbooleanTo stop timer when hovered over the toast (Only if duration is set)true
callbackFunctionInvoked when the toast is dismissed
onClickFunctionInvoked when the toast is clicked
offsetObjectAbility to add some offset to axis
escapeMarkupbooleanToggle the default behavior of escaping HTML markuptrue
styleobjectUse the HTML DOM Style properties to add any style directly to toast
ariaLivestringAnnounce the toast to screen readers, see https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/ARIA_Live_Regions for options"polite"
oldestFirstbooleanSet the order in which toasts are stacked in pagetrue

Deprecated properties: backgroundColor - use style.background option instead

Browsers support


IE / Edge

Firefox

Chrome

Safari

Opera
IE10, IE11, Edgelast 10 versionslast 10 versionslast 10 versionslast 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

NPM DownloadsLast 30 Days