openfoodfoundation/openfoodnetwork

View on GitHub
app/components/vertical_ellipsis_menu/component_controller.js

Summary

Maintainability
A
0 mins
Test Coverage
import { Controller } from "stimulus";

export default class extends Controller {
  static targets = ["content"];

  connect() {
    super.connect();
    window.addEventListener("click", this.#hideIfClickedOutside);
  }

  toggle() {
    this.contentTarget.classList.toggle("show");
  }

  #hideIfClickedOutside = (event) => {
    if (this.element.contains(event.target)) {
      return;
    }
    this.#hide();
  };

  #hide() {
    this.contentTarget.classList.remove("show");
  }
}