-
Notifications
You must be signed in to change notification settings - Fork 10
Getting Started
Note: This page has not yet been updated to reflect changes in version 5.1.0. It will be updated following the release of 4.1.0. Until then, to see examples of new method signatures, refer to the demo page.
Setup is extremely straightforward. Simply include the CSS in your document's <head>
and the JavaScript at the bottom of the <body>
tag.
Install from npm, or via CDN. Currently jsDelivr is supported.
npm i bootstrap-toaster
<head>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/css/bootstrap-toaster.min.css" />
</head>
<body>
...
<script src="https://cdn.jsdelivr.net/npm/[email protected]/js/bootstrap-toaster.min.js"></script>
</body>
- Bootstrap 5 (>= 5.0.0-beta2), for the toasts themselves
- Bootstrap Icons (>= 1.3.0), for the toast status icons
- jQuery (1.9.1 - 3.x), for Bootstrap's own functions to create a toast
- Bootstrap 4 (>= 4.2.1), for the toasts themselves
- Bootstrap Icons (>= 1.3.0), for the toast status icons
Bootstrap Toaster will take care of its own setup work. When the script loads, it will insert a fixed-position container into the DOM that will house all of your toasts when they appear, so you can get to generating toasts in a snap!
All it takes to generate one is a call to Toast.create()
, like so:
Toast.create("Wow, that was easy!", "Just like that, this toast will appear on the page",
TOAST_STATUS.SUCCESS, 5000);
The Toast.create()
function supports the following 4 parameters:
-
title
: The text of the toast's header. -
message
: The text of the toast's body. -
status
: The status/urgency of the toast. Affects status icon and ARIA accessibility features. Defaults to 0, which renders no icon. Default -> no status icon, same ARIA attributes as success and info toasts -
timeout
: Time in ms until toast disappears automatically. Default -> 0, in which case the toast must be manually dismissed.
Since the invocation is so simple, you can generate a toast from anywhere or for anything! For example, here's how you might use it after using jQuery AJAX to post data to an API:
$.ajax({
type: "POST",
url: "https://some-web-api/endpoint",
data: {
...
},
success: function (response) {
response = JSON.parse(response);
Toast.create("Success", response.message, TOAST_STATUS.SUCCESS, 10000);
}
error: function (response) {
console.error(response);
Toast.create("Error", "Something went wrong.", TOAST_STATUS.DANGER);
}
});
There are 4 built-in options for toast status in the call to Toast.create()
, named after Bootstrap's color convention. They are as follows:
TOAST_STATUS.SUCCESS
TOAST_STATUS.DANGER
TOAST_STATUS.WARNING
TOAST_STATUS.INFO
As mentioned in the accessibility section, the status is important for correctly setting up ARIA attributes for the toast, but it also determines the toast's status icon.