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

43 lines
956 B
JavaScript
Raw Permalink 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() {
this.toast_event = this._toast_event.bind(this);
2021-01-25 19:46:16 +00:00
window.addEventListener("toast", this.toast_event);
}
disconnect() {
window.removeEventListener("toast", this.toast_event);
}
_toast_event(event) {
this.contentTarget.innerText = event.detail.content;
this.set(true);
if (this.interval) {
clearTimeout(this.interval);
}
this.interval = setTimeout(() => {
this.set(false);
this.interval = null;
}, 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
}