sutty-base-jekyll-theme/_packs/controllers/floating_alert_controller.js

35 lines
812 B
JavaScript
Raw Normal View History

2021-11-22 17:51:50 +00:00
import { Controller } from "stimulus";
2021-01-25 19:46:16 +00:00
2021-11-25 17:54:28 +00:00
// Ejemplo de uso:
// window.dispatchEvent(
// new CustomEvent("toast", { detail: { content: "¡Hola, usuarix!" } })
// );
2021-01-25 19:46:16 +00:00
export default class extends Controller {
2021-11-22 17:51:50 +00:00
static targets = ["content"];
2021-01-25 19:46:16 +00:00
2021-11-22 17:51:50 +00:00
connect() {
window.addEventListener("toast", (event) => {
this.contentTarget.innerText = event.detail.content;
2021-11-25 17:53:32 +00:00
this.set(true);
2021-01-25 19:46:16 +00:00
2021-11-25 17:53:32 +00:00
if (this.interval) {
clearTimeout(this.interval);
}
this.interval = setTimeout(() => {
this.set(false);
this.interval = null;
2021-11-22 17:51:50 +00:00
}, 3000);
});
2021-01-25 19:46:16 +00:00
}
2021-11-25 17:53:32 +00:00
set(show) {
if (show) {
this.element.classList.remove("hide");
this.element.classList.add("show");
} else {
this.element.classList.add("hide");
this.element.classList.remove("show");
}
}
2021-01-25 19:46:16 +00:00
}