mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-24 20:36:49 +00:00
It doesn't matter where the flash messages appear in the HTML (thanks to fixed positioning), so why not keep it simple and send them with the main response. preventDefault in case we are inside a form, so the button doesn't submit it.
30 lines
644 B
JavaScript
30 lines
644 B
JavaScript
import { Controller } from "stimulus";
|
|
|
|
document.addEventListener("turbolinks:before-cache", () =>
|
|
document.getElementById("flash").remove()
|
|
);
|
|
|
|
export default class extends Controller {
|
|
static values = {
|
|
autoClose: Boolean,
|
|
};
|
|
|
|
connect() {
|
|
if (this.autoCloseValue) {
|
|
setTimeout(this.close.bind(this), 5000);
|
|
}
|
|
}
|
|
|
|
close(e) {
|
|
// Fade out
|
|
this.element.classList.remove("animate-show");
|
|
this.element.classList.add("animate-hide-500");
|
|
setTimeout(this.remove.bind(this), 500);
|
|
e && e.preventDefault(); // Prevent submitting if we're inside a form
|
|
}
|
|
|
|
remove() {
|
|
this.element.remove();
|
|
}
|
|
}
|