Files
openfoodnetwork/app/webpacker/controllers/toggle_controller.js
Cillian O'Ruanaidh 67ae2e72f3 Replace inline JS for toggling order cycle advanced settings with StimulusJs controller
Partially addresses #8699.

This adjusts the Stimulus toggle controller so you can toggle content in both directions via a single element. This is in addition to the previous behaviour for toggling via multiple elements like radio buttons when each element always toggles in one direction only.

If a toggle element contains a chevron icon this will automatically toggle the direction of that icon too.

Note, in order to not have to re-implement the animation provided by the slideToggle() function in standard JavaScript, this just switches the style :display between 'none' and 'block' so it is not as smooth. Perhaps it could be made more smooth later with a CSS transition.
2022-01-28 16:19:36 +00:00

26 lines
749 B
JavaScript

import { Controller } from "stimulus";
export default class extends Controller {
static targets = ["content"];
toggle(event) {
event.stopImmediatePropagation()
const input = event.currentTarget;
const chevron = input.querySelector(".icon-chevron-down, .icon-chevron-up")
const toggleViaSingleElement = !input.dataset.toggleShow;
if(chevron) {
chevron.classList.toggle("icon-chevron-down");
chevron.classList.toggle("icon-chevron-up");
}
this.contentTargets.forEach((t) => {
if(toggleViaSingleElement) {
t.style.display = t.style.display === "none" ? "block" : "none";
} else {
t.style.display = input.dataset.toggleShow === "true" ? "block" : "none";
}
});
}
}