forked from bblanchon/django-htmx-messages-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
toasts.js
34 lines (26 loc) · 974 Bytes
/
toasts.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
;(function () {
const toastOptions = { delay: 2000 }
function createToast(message) {
// Clone the template
const element = htmx.find("[data-toast-template]").cloneNode(true)
// Remove the data-toast-template attribute
delete element.dataset.toastTemplate
// Set the CSS class
element.className += " " + message.tags
// Set the text
htmx.find(element, "[data-toast-body]").innerText = message.message
// Add the new element to the container
htmx.find("[data-toast-container]").appendChild(element)
// Show the toast using Bootstrap's API
const toast = new bootstrap.Toast(element, toastOptions)
toast.show()
}
htmx.on("messages", (event) => {
event.detail.value.forEach(createToast)
})
// Show all existsing toasts, except the template
htmx.findAll(".toast:not([data-toast-template])").forEach((element) => {
const toast = new bootstrap.Toast(element, toastOptions)
toast.show()
})
})()