Skip to content

Instantly share code, notes, and snippets.

@imsamuka
Last active September 29, 2023 18:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save imsamuka/d4cc757ce3f2cefaf0b966b00f86be65 to your computer and use it in GitHub Desktop.
Save imsamuka/d4cc757ce3f2cefaf0b966b00f86be65 to your computer and use it in GitHub Desktop.
Spawn a updatable notification that includes a optional progress bar, styled with Bulma
function spawnNotif(box, category, message, progressMax = 0) {
const categories = {
"error": "is-danger",
"info": "is-info",
"success": "is-success",
"warning": "is-warning",
"warn": "is-warning",
"primary": "is-primary",
}
const notif = document.createElement("div");
notif.classList.add("notification", "is-light");
notif.innerHTML = '<button class="delete" onclick="this.parentElement.remove()"></button>';
const progress = document.createElement('progress');
progress.classList.add('progress', "is-light");
progress.textContent = 'Loading';
const msgBlock = document.createElement('div');
msgBlock.classList.add('block', 'content');
const progBlock = document.createElement('div');
progBlock.classList.add('block');
progBlock.appendChild(progress)
notif.append(msgBlock);
if (progressMax) {
notif.appendChild(progBlock);
}
notif.msgBlock = msgBlock
notif.progBlock = progBlock
notif.progress = progress
Object.defineProperties(notif, {
category: {
get() {
return notif._category;
},
set(cat) {
notif._category = cat
const newClass = categories[cat] || "is-primary"
notif.classList.remove(...Object.values(categories));
progress.classList.remove(...Object.values(categories));
notif.classList.add(newClass);
progress.classList.add(newClass);
},
},
message: {
get() {
return msgBlock.innerHTML;
},
set(msg) {
msgBlock.innerHTML = msg;
},
},
barValue: {
get() {
return progress.value;
},
set(val) {
progress.value = val;
progress.textContent = `${Math.round(100 * val / progress.max)}%`;
},
},
barMax: {
get() {
return progress.max;
},
set(val) {
progress.max = val;
if (val && !notif.contains(progBlock)) {
notif.appendChild(progBlock);
} else if (!val && notif.contains(progBlock)) {
notif.removeChild(progBlock);
}
},
}
});
notif.category = category
notif.message = message
notif.barMax = progressMax
box.appendChild(notif);
return notif
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment