Files
openfoodnetwork/app/webpacker/controllers/selector_controller.js
Jean-Baptiste Bellet 224daf2591 Factorize js controller between Selectore and SuperSelector components
+ add a computeItemsHeight on afterReflex callback
2022-12-07 15:08:52 +01:00

29 lines
767 B
JavaScript

import ApplicationController from "./application_controller";
export default class extends ApplicationController {
reflex = "SelectorComponent";
connect() {
super.connect();
window.addEventListener("click", this.closeOnClickOutside);
this.computeItemsHeight();
}
disconnect() {
super.disconnect();
window.removeEventListener("click", this.closeOnClickOutside);
}
closeOnClickOutside = (event) => {
if (!this.element.contains(event.target)) {
this.stimulate(`${this.reflex}#close`, this.element);
}
};
computeItemsHeight = () => {
const items = this.element.querySelector(".selector-items");
const rect = items.getBoundingClientRect();
items.style.maxHeight = `calc(100vh - ${rect.height}px)`;
};
}