mirror of
https://github.com/picocss/pico.git
synced 2025-04-20 16:46:14 -04:00
RTL Dropdown Nav corrected thanks to @64bitint
Added popovers to possibly replace the notifications. Started adding popover toasts
This commit is contained in:
parent
8a7b25a44f
commit
64373c2d44
244 changed files with 3461 additions and 724 deletions
80
docs/js/popover-toast.js
Normal file
80
docs/js/popover-toast.js
Normal file
|
@ -0,0 +1,80 @@
|
|||
/**
|
||||
* Popover Toast
|
||||
*
|
||||
* Modified from https://mdn.github.io/dom-examples/popover-api/toast-popovers/
|
||||
* Modified by Yohn
|
||||
*/
|
||||
|
||||
const successToastBtn = document.getElementById("success-toast-btn");
|
||||
const failureToastBtn = document.getElementById("failure-toast-btn");
|
||||
const counter = document.querySelector("p");
|
||||
|
||||
function makeToast(result) {
|
||||
// Create an element and make it into a popover
|
||||
const popover = document.createElement("article");
|
||||
popover.popover = "manual";
|
||||
popover.classList.add("toast");
|
||||
popover.classList.add("newest");
|
||||
|
||||
let msg;
|
||||
|
||||
// Give the toast an appropriate text content, class for styling, and update
|
||||
// the relevant count, depending on whether it was a success or a failure
|
||||
if (result === "success") {
|
||||
msg = "Action was successful!";
|
||||
popover.classList.add("success");
|
||||
successCount++;
|
||||
} else if (result === "failure") {
|
||||
msg = "Action failed!";
|
||||
popover.classList.add("failure");
|
||||
failCount++;
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
|
||||
// Give the toast its text content, and add it to the DOM
|
||||
popover.textContent = msg;
|
||||
document.body.appendChild(popover);
|
||||
|
||||
// Show the popover
|
||||
popover.showPopover();
|
||||
|
||||
// Remove the toast again after 4 seconds
|
||||
setTimeout(() => {
|
||||
popover.hidePopover();
|
||||
popover.remove();
|
||||
}, 4000);
|
||||
|
||||
// When a new toast appears, run the movetoastsUp() function
|
||||
popover.addEventListener("toggle", (event) => {
|
||||
if (event.newState === "open") {
|
||||
moveToastsUp();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function moveToastsUp() {
|
||||
const toasts = document.querySelectorAll(".toast");
|
||||
|
||||
toasts.forEach((toast) => {
|
||||
// If the toast is the one that has just appeared, we don't want it to move up.
|
||||
if (toast.classList.contains("newest")) {
|
||||
toast.style.bottom = `5px`;
|
||||
toast.classList.remove("newest");
|
||||
} else {
|
||||
// Move up all the other toasts by 50px to make way for the new one
|
||||
const prevValue = toast.style.bottom.replace("px", "");
|
||||
const newValue = parseInt(prevValue) + 50;
|
||||
toast.style.bottom = `${newValue}px`;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Handlers to wire up the buttons to the makeToast() function
|
||||
successToastBtn.addEventListener("click", () => {
|
||||
makeToast("success");
|
||||
});
|
||||
|
||||
failureToastBtn.addEventListener("click", () => {
|
||||
makeToast("failure");
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue