Toastify is a lightweight, vanilla JS toast notification library.
- Multiple stacked notifications
- No blocking of execution thread
- Notification Text
- Duration
- Close icon display
- Display position
- Offset position
- 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"
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
New toasts are created by calling the core Toastify
function, and then shown to the user by calling it's showToast
function.
Toastify({
text: "This is a simple toast using default values.."
}).showToast();
Toast are configured by means of specific keys in the configuration object passed in at the time of creation.
Toast messages will be centered on devices with screen width less than 360px.
- See the changelog
Message to be displayed in the toast
- default:
undefined
- notes:
- toast will fail to render if a message is not provided.
Provide a node to be mounted inside the toast. node
takes higher precedence over text
- default:
undefined
Duration for which the toast should be displayed.
- default:
3000
- options:
- any positive integer in milliseconds
-1
for a persistent toast that must be explicitly dismissed
- notes:
- this implicitly enables
close
unlessonClick
is defined
- this implicitly enables
CSS Selector or Element Node on which the toast should be added
- default:
body
URL to which the browser should be navigated on click of the toast
- default:
undefined
Decides whether the destination
should be opened in a new window or not
- default:
false
To show the close icon or not
- default:
false
To show the toast from top or bottom
- default:
"top"
- options:
"top"
to start lining up toasts from the top viewport edge"bottom"
to start lining up toasts from the bottom viewport edge
To show the toast on left or right
- default:
"right"
- options:
"left"
to position toast along the left viewport edge"right"
to position toast along the right viewport edge
Image/icon to be shown before text
- default:
undefined
Ability to provide custom class name for further customization
- default:
undefined
To stop timer when hovered over the toast (Only if duration is set)
- default:
true
Invoked when the toast is dismissed
- default:
undefined
Invoked when the toast is clicked
- default:
undefined
Ability to add some offset to axis
- default:
undefined
- options:
{ x: number }
to set the horizontal offset{ y: number }
to set the vertical offset{ x: number, y: number }
to set the both offsets
- notes:
- if
position
orgravity
are used, this offset will push the toast inwards from the relevant edge.
- if
Toggle the default behavior of escaping HTML markup
- default:
true
Announce the toast to screen readers, see https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/ARIA_Live_Regions for options
- default:
"polite"
- options:
"off"
will prevent screen readers from reading the notification"polite"
will cause screen readers to read the notification when the user is next idle"assertive"
will cause screen readers to read the notification as soon as it is shown
Set the order in which toasts are stacked in page
- default:
true
IE / Edge |
Firefox |
Chrome |
Safari |
Opera |
---|---|---|---|---|
IE10, IE11, Edge | last 10 versions | last 10 versions | last 10 versions | last 10 versions |
MIT © Varun A P