sutty-base-jekyll-theme/_packs/controllers/floating_alert_controller.js
Nulo 3a0c41b736 Limpiar eventos cuando el elemento o controlador que los escucha desaparece
Squashed commit of the following:

commit 482eea28821868f03ace33562e7bd34ab9a4478f
Merge: 5f48528 1c128f2
Author: f <f@sutty.nl>
Date:   Thu Nov 25 18:31:35 2021 -0300

    Merge branch 'master' into limpiar-eventos

commit 5f48528c28b0709bd859a4dc52a830f60bfedc6e
Author: f <f@sutty.nl>
Date:   Thu Nov 25 18:23:23 2021 -0300

    pretty

commit 70d05bc90a6cb64d1c4bfc39f48388af3fbc3c18
Merge: c4f33c0 ff1bc21
Author: Nulo <nulo@sutty.nl>
Date:   Thu Oct 28 16:46:31 2021 -0300

    Merge branch 'master' into limpiar-eventos

commit c4f33c084058002a10fc0ec2137ffe045826cfd2
Author: f <f@sutty.nl>
Date:   Thu Oct 28 14:52:41 2021 -0300

    limpiar eventos
2021-11-25 21:40:44 +00:00

42 lines
956 B
JavaScript

import { Controller } from "stimulus";
// Ejemplo de uso:
// window.dispatchEvent(
// new CustomEvent("toast", { detail: { content: "¡Hola, usuarix!" } })
// );
export default class extends Controller {
static targets = ["content"];
connect() {
this.toast_event = this._toast_event.bind(this);
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);
}
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");
}
}
}