blob: c0b557788c48eca0a728cc343608aba966232bd6 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
const expandedClass = "e-content--expanded";
export function toggleContentExpanded(id) {
const content = document.querySelector(`[data-content-id=${id}`);
if (!content) {
throw new Error(`No content found with ID: ${id}`);
}
const classes = content.getAttribute("class").split(" ");
if (classes.includes(expandedClass)) {
const newClasses = classes.filter((c) => c != expandedClass);
content.setAttribute("class", newClasses.join(" "));
} else {
content.setAttribute("class", [...classes, expandedClass].join(" "));
}
}
export function addExpandItemButtons() {
for (const content of document.querySelectorAll(".e-content")) {
const contentId = crypto.randomUUID();
content.setAttribute("data-content-id", contentId);
const button = new HTMLButtonElement();
button.onclick = `toggleContentExpanded(${contentId})`;
content.insertAdjacentElement("afterend", button);
}
}
|