Last active
May 21, 2022 19:32
-
-
Save blairanderson/c7128976b08bbc600331e80dcd2c5c2f to your computer and use it in GitHub Desktop.
Bootstrap 5 : jQuery Toast Builder
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var toastIDCounter = 0; | |
(function ($) { | |
$.fn.bsToast = function (options) { | |
if (typeof options === "string") { | |
options = { | |
body: options | |
} | |
} | |
var settings = $.extend({ | |
// These are the defaults. | |
body: "MISSING body: <br/>$(...).bsToast({body: 'toast body text here'})<br/><strong><em>html is OK!</em></strong>", | |
animation: true, // Apply a CSS fade transition to the toast | |
autohide: true, // Auto hide the toast | |
delay: 3000, // Delay hiding the toast (ms) | |
dispose: true | |
}, options); | |
var $toastContainer = $("#toast-container"); | |
if ($toastContainer.length === 0) { | |
// re-create toastPosition and toastContainer | |
var $toastPosition = $("<div>", { | |
"id": "toast-position", | |
"aria-live": "polite", | |
"aria-atomic": "true", | |
"style": "position: fixed; min-height: 200px;top: 20px;right: 40px;min-width: 100%;max-width: 500px;" | |
}); | |
$toastContainer = $("<div>", { | |
"id": "toast-container", | |
"style": "position: absolute; top: 0; right: 0;" | |
}); | |
$(document.body).append($toastPosition); | |
$toastPosition.append($toastContainer) | |
} | |
var toastid = "toast-id-" + toastIDCounter; | |
toastIDCounter++ | |
var $toast = $("<div>", { | |
"id": toastid, | |
"class": "toast", | |
"style": "min-width: 300px;", | |
"role": "alert", | |
"aria-live": "assertive", | |
"aria-atomic": true | |
}); | |
if (settings.header && settings.header.text) { | |
var $header = $("<div>", {"class": "toast-header"}); | |
if (settings.header.logo) { | |
$header.append(`<img src="${settings.header.logo}" class="rounded me-2" height="25" width="25" alt="logo">`) | |
} | |
$header.append(`<strong class="me-auto">${settings.header.text}</strong>`) | |
// $header.append(`<small class="text-muted">just now</small>`) | |
$header.append(`<button type="button" class="ms-2 mb-1 close" data-dismiss="toast" aria-label="Close"><span aria-hidden="true">×</span></button>`) | |
$toast.append($header) | |
} | |
var $toastBody = $("<div>", {"class": "toast-body"}); | |
$toastBody.html(settings.body) | |
$toast.append($toastBody) | |
$toastContainer.append($toast) | |
var toastEl = $toast[0] | |
toastEl.addEventListener('hidden.bs.toast', toastEl.remove) | |
var t = new bootstrap.Toast(toastEl, {delay: settings.delay}); | |
t.show() | |
}; | |
}(jQuery)); | |
Hi! Thank you for this snippet!
You should change mr-*
and ml-*
to me-*
and ms-*
respectively. According to this
Hi! Thank you for this snippet!
You should changemr-*
andml-*
tome-*
andms-*
respectively. According to this
Updated to the me/ms versions 👍
line 24 should be "Class" not "ID"
line 24 should be "Class" not "ID"
i don't think bootstrap has a toast-position
class in their styles. I added that as ID so that the element can be referenced later.
There should be only 1 toast-position and 1 toast-container.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
which lets you do stuff like:
or more simply: