Files
openfoodnetwork/app/webpacker/controllers/flash_controller.js
David Cook 0f46da07b2 Render flashes along with table
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.
2023-12-06 15:13:15 +11:00

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();
}
}