openfoodfoundation/openfoodnetwork

View on GitHub
app/webpacker/controllers/flash_controller.js

Summary

Maintainability
A
0 mins
Test Coverage
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();
  }
}