Top Related Projects
The iconic SVG, font, and CSS toolkit
β¨ A beautiful, responsive, highly customizable and accessible (WAI-ARIA) replacement for JavaScript's popup boxes. Zero dependencies. πΊπ¦
A beautiful replacement for JavaScript's "alert"
β° Day.js 2kB immutable date-time library alternative to Moment.js with the same modern API
Parse, validate, manipulate, and display dates in javascript.
Quick Overview
Toastr is a lightweight, easy-to-use JavaScript library for non-blocking notifications. It provides a simple way to create toast notifications that can be customized in terms of appearance, position, and behavior. Toastr is widely used in web applications to display informative messages to users.
Pros
- Easy to implement and use with minimal setup
- Highly customizable with various options for appearance and behavior
- Supports multiple notification types (success, info, warning, error)
- Responsive and works well on different screen sizes
Cons
- Limited built-in animation options
- May require additional styling for complex designs
- Dependency on jQuery (though a non-jQuery version is available)
- Limited accessibility features out of the box
Code Examples
- Basic usage:
// Display a success message
toastr.success('Data saved successfully!');
// Display an error message
toastr.error('An error occurred while processing your request.');
- Customizing options:
// Set global options
toastr.options = {
closeButton: true,
progressBar: true,
positionClass: 'toast-bottom-right',
timeOut: 5000
};
// Display an info message with custom options
toastr.info('Your session will expire in 5 minutes.', 'Session Expiry', {timeOut: 10000});
- Chaining methods:
// Display a warning message with chained methods
toastr.warning('You are about to delete this item.')
.css('background-color', '#FF6347')
.addClass('custom-toast');
Getting Started
- Include toastr CSS and JS files in your HTML:
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/toastr.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/toastr.min.js"></script>
- Use toastr in your JavaScript code:
// Display a simple notification
toastr.success('Hello, World!');
// Customize options
toastr.options.positionClass = 'toast-top-center';
toastr.info('This is an info message.');
Competitor Comparisons
The iconic SVG, font, and CSS toolkit
Pros of Font-Awesome
- Extensive icon library with thousands of scalable vector icons
- Regular updates and additions to the icon set
- Supports both web fonts and SVG with JavaScript
Cons of Font-Awesome
- Larger file size, potentially impacting page load times
- Requires more setup and configuration compared to Toastr
- May be overkill for projects that only need a few icons
Code Comparison
Font-Awesome (HTML usage):
<i class="fas fa-user"></i>
<i class="far fa-envelope"></i>
<i class="fab fa-twitter"></i>
Toastr (JavaScript usage):
toastr.success('Success message');
toastr.error('Error message');
toastr.warning('Warning message');
Summary
Font-Awesome is a comprehensive icon library offering a wide range of scalable vector icons, while Toastr is a JavaScript library for non-blocking notifications. Font-Awesome provides visual elements for UI design, whereas Toastr focuses on displaying toast notifications.
Font-Awesome excels in providing a vast selection of icons but may be excessive for simple projects. Toastr, on the other hand, is lightweight and easy to implement for quick notifications but lacks the visual versatility of Font-Awesome.
Choose Font-Awesome for projects requiring diverse iconography, and opt for Toastr when you need a straightforward notification system without the overhead of a full icon library.
β¨ A beautiful, responsive, highly customizable and accessible (WAI-ARIA) replacement for JavaScript's popup boxes. Zero dependencies. πΊπ¦
Pros of SweetAlert2
- More feature-rich, offering customizable modals with various input types
- Better support for asynchronous operations and promises
- Responsive design with mobile-friendly layouts
Cons of SweetAlert2
- Larger file size, potentially impacting page load times
- Steeper learning curve due to more complex API
- May be overkill for simple notification needs
Code Comparison
SweetAlert2:
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
}
})
Toastr:
toastr.warning('Are you sure you want to delete this?', 'Warning', {
closeButton: true,
timeOut: 0,
extendedTimeOut: 0,
onclick: function() {
// Deletion logic here
}
});
SweetAlert2 provides a more structured approach for complex interactions, while Toastr offers a simpler API for basic notifications. SweetAlert2 is better suited for modal dialogs and user input, whereas Toastr excels in non-intrusive toast notifications. The choice between the two depends on the specific requirements of your project and the level of user interaction needed.
A beautiful replacement for JavaScript's "alert"
Pros of SweetAlert
- More visually appealing and customizable alerts
- Supports input fields and custom buttons within alerts
- Offers a promise-based API for easier handling of user interactions
Cons of SweetAlert
- Larger file size and potentially slower load times
- May be considered "too fancy" for simple notification needs
- Requires more setup and configuration for basic use cases
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) {
// Perform delete action
}
})
Toastr:
toastr.warning('Are you sure you want to delete this?', 'Warning', {
closeButton: true,
timeOut: 0,
extendedTimeOut: 0,
onclick: function() {
// Perform delete action
}
});
The code comparison shows that SweetAlert offers a more structured approach to creating complex alerts with built-in confirmation handling, while Toastr provides a simpler API for basic notifications. SweetAlert's code is more verbose but offers greater flexibility for user interactions.
β° Day.js 2kB immutable date-time library alternative to Moment.js with the same modern API
Pros of dayjs
- Lightweight and modular date manipulation library
- Immutable and chainable API for easier date operations
- Extensive plugin ecosystem for additional functionality
Cons of dayjs
- Limited to date and time manipulation, not a general-purpose utility library
- May require additional plugins for advanced features, increasing bundle size
Code Comparison
dayjs:
import dayjs from 'dayjs'
const date = dayjs('2023-05-01')
const formattedDate = date.format('MMMM D, YYYY')
console.log(formattedDate) // Output: May 1, 2023
toastr:
toastr.options = {
closeButton: true,
progressBar: true
}
toastr.success('Operation completed successfully', 'Success')
Key Differences
- Purpose: dayjs is a date manipulation library, while toastr is a notification library
- Functionality: dayjs focuses on date operations, toastr on displaying notifications
- Use cases: dayjs is used for working with dates in JavaScript, toastr for showing user-friendly messages in web applications
Conclusion
While both libraries are popular in their respective domains, they serve different purposes. dayjs is ideal for projects requiring extensive date manipulation, while toastr is better suited for applications needing a flexible notification system. The choice between them depends on the specific requirements of your project.
Parse, validate, manipulate, and display dates in javascript.
Pros of moment
- Extensive date and time manipulation capabilities
- Robust internationalization support
- Large community and ecosystem of plugins
Cons of moment
- Larger bundle size due to comprehensive feature set
- More complex API compared to simpler libraries
- No longer recommended for new projects (in maintenance mode)
Code comparison
moment:
import moment from 'moment';
const date = moment('2023-05-15');
console.log(date.format('MMMM Do YYYY'));
console.log(date.add(1, 'week').calendar());
toastr:
import toastr from 'toastr';
toastr.success('Hello World!');
toastr.error('An error occurred', 'Error');
toastr.warning('Warning message');
Summary
moment and toastr serve different purposes. moment is a date and time manipulation library, while toastr is a notification library. moment offers powerful date handling capabilities but has a larger footprint, whereas toastr provides a simple API for displaying notifications. The choice between them depends on the specific needs of your project: date manipulation (moment) or user notifications (toastr).
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
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 customized and extended.
Browser testing provided by BrowserStack.
Current Version
2.1.4
Demo
- Demo can be found at http://codeseven.github.io/toastr/demo.html
- Demo using FontAwesome icons with toastr
CDNs
Toastr is hosted at cdnjs and jsdelivr
Debug
Minified
- //cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/toastr.min.js
- //cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/toastr.min.css
Install
NuGet Gallery
Install-Package toastr
Bower
bower install toastr
npm
npm install --save toastr
yarn
yarn add toastr
Ruby on Rails
# Gemfile
gem 'toastr-rails'
# application.coffee
#= require toastr
// application.scss
@import "toastr";
Wiki and Change Log
Breaking Changes
Animation Changes
The following animations options have been deprecated and should be replaced:
- Replace
options.fadeIn
withoptions.showDuration
- Replace
options.onFadeIn
withoptions.onShown
- Replace
options.fadeOut
withoptions.hideDuration
- Replace
options.onFadeOut
withoptions.onHidden
Quick Start
3 Easy Steps
For other API calls, see the demo.
-
Link to toastr.css
<link href="toastr.css" rel="stylesheet"/>
-
Link to toastr.js
<script src="toastr.js"></script>
-
use toastr to display a toast for info, success, warning or error
// Display an info toast with no title toastr.info('Are you the 6 fingered man?')
Other Options
// Display a warning toast, with no title
toastr.warning('My name is Inigo Montoya. You killed my father, prepare to die!')
// Display a success toast, with a title
toastr.success('Have fun storming the castle!', 'Miracle Max Says')
// Display an error toast, with a title
toastr.error('I do not think that word means what you think it means.', 'Inconceivable!')
// Immediately remove current toasts without using animation
toastr.remove()
// Remove current toasts using animation
toastr.clear()
// Override global options
toastr.success('We do have the Kapua suite available.', 'Turtle Bay Resort', {timeOut: 5000})
Escape HTML characters
In case you want to escape HTML characters in title and message
toastr.options.escapeHtml = true;
Close Button
Optionally enable a close button
toastr.options.closeButton = true;
Optionally override the close button's HTML.
toastr.options.closeHtml = '<button><i class="icon-off"></i></button>';
You can also override the CSS/LESS for #toast-container .toast-close-button
Optionally override the hide animation when the close button is clicked (falls back to hide configuration).
toastr.options.closeMethod = 'fadeOut';
toastr.options.closeDuration = 300;
toastr.options.closeEasing = 'swing';
Display Sequence
Show newest toast at bottom (top is default)
toastr.options.newestOnTop = false;
Callbacks
// Define a callback for when the toast is shown/hidden/clicked
toastr.options.onShown = function() { console.log('hello'); }
toastr.options.onHidden = function() { console.log('goodbye'); }
toastr.options.onclick = function() { console.log('clicked'); }
toastr.options.onCloseClick = function() { console.log('close button clicked'); }
Animation Options
Toastr will supply default animations, so you do not have to provide any of these settings. However you have the option to override the animations if you like.
Easings
Optionally override the animation easing to show or hide the toasts. Default is swing. swing and linear are built into jQuery.
toastr.options.showEasing = 'swing';
toastr.options.hideEasing = 'linear';
toastr.options.closeEasing = 'linear';
Using the jQuery Easing plugin (http://www.gsgd.co.uk/sandbox/jquery/easing/)
toastr.options.showEasing = 'easeOutBounce';
toastr.options.hideEasing = 'easeInBack';
toastr.options.closeEasing = 'easeInBack';
Animation Method
Use the jQuery show/hide method of your choice. These default to fadeIn/fadeOut. The methods fadeIn/fadeOut, slideDown/slideUp, and show/hide are built into jQuery.
toastr.options.showMethod = 'slideDown';
toastr.options.hideMethod = 'slideUp';
toastr.options.closeMethod = 'slideUp';
Prevent Duplicates
Rather than having identical toasts stack, set the preventDuplicates property to true. Duplicates are matched to the previous toast based on their message content.
toastr.options.preventDuplicates = true;
Timeouts
Control how toastr interacts with users by setting timeouts appropriately.
toastr.options.timeOut = 30; // How long the toast will display without user interaction
toastr.options.extendedTimeOut = 60; // How long the toast will display after a user hovers over it
Prevent from Auto Hiding
To prevent toastr from closing based on the timeouts, set the timeOut
and extendedTimeOut
options to 0
. The toastr will persist until selected.
toastr.options.timeOut = 0;
toastr.options.extendedTimeOut = 0;
Progress Bar
Visually indicate how long before a toast expires.
toastr.options.progressBar = true;
rtl
Flip the toastr to be displayed properly for right-to-left languages.
toastr.options.rtl = true;
Building Toastr
To build the minified and css versions of Toastr you will need node installed. (Use Homebrew or Chocolatey.)
npm install -g gulp karma-cli
npm install
At this point the dependencies have been installed and you can build Toastr
- Run the analytics
gulp analyze
- Run the test
gulp test
- Run the build
gulp
Contributing
For a pull request to be considered it must resolve a bug, or add a feature which is beneficial to a large audience.
Pull requests must pass existing unit tests, CI processes, and add additional tests to indicate successful operation of a new feature, or the resolution of an identified bug.
Requests must be made against the develop
branch. Pull requests submitted against the master
branch will not be considered.
All pull requests are subject to approval by the repository owners, who have sole discretion over acceptance or denial.
Authors
John Papa
Tim Ferrell
Hans FjΓΒ€llemark
Credits
Inspired by https://github.com/Srirangan/notifer.js/.
Copyright
Copyright ΓΒ© 2012-2015
License
toastr is under MIT license - http://www.opensource.org/licenses/mit-license.php
Top Related Projects
The iconic SVG, font, and CSS toolkit
β¨ A beautiful, responsive, highly customizable and accessible (WAI-ARIA) replacement for JavaScript's popup boxes. Zero dependencies. πΊπ¦
A beautiful replacement for JavaScript's "alert"
β° Day.js 2kB immutable date-time library alternative to Moment.js with the same modern API
Parse, validate, manipulate, and display dates in javascript.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot