(function () {
"use strict";
function initCollapsibleExamples() {
const examples = document.querySelectorAll(".example");
examples.forEach((example, index) => {
const content = Array.from(example.childNodes);
const contentWrapper = document.createElement("div");
contentWrapper.className = "example-content";
contentWrapper.style.maxHeight = "0";
contentWrapper.style.overflow = "hidden";
contentWrapper.style.transition = "max-height 0.3s ease";
const title = example.querySelector(".example-title");
content.forEach((node) => {
if (node !== title) {
contentWrapper.appendChild(node);
}
});
example.appendChild(contentWrapper);
const toggleBtn = document.createElement("button");
toggleBtn.className = "example-toggle";
toggleBtn.innerHTML = "▶";
toggleBtn.setAttribute("aria-label", "Toggle example");
toggleBtn.setAttribute("aria-expanded", "false");
toggleBtn.style.cssText = `
position: absolute;
top: -0.6em;
right: 1em;
background: #ef4444;
color: white;
border: none;
border-radius: 3px;
width: 1.8em;
height: 1.8em;
cursor: pointer;
font-size: 0.9em;
display: flex;
align-items: center;
justify-content: center;
transition: transform 0.3s ease;
z-index: 10;
`;
if (title) {
title.parentNode.insertBefore(toggleBtn, title.nextSibling);
} else {
example.insertBefore(toggleBtn, example.firstChild);
}
function toggleExample() {
const isExpanded = toggleBtn.getAttribute("aria-expanded") === "true";
if (isExpanded) {
contentWrapper.style.maxHeight = "0";
toggleBtn.innerHTML = "▶";
toggleBtn.setAttribute("aria-expanded", "false");
toggleBtn.style.transform = "rotate(0deg)";
} else {
contentWrapper.style.maxHeight = contentWrapper.scrollHeight + "px";
toggleBtn.innerHTML = "▼";
toggleBtn.setAttribute("aria-expanded", "true");
toggleBtn.style.transform = "rotate(0deg)";
}
}
toggleBtn.addEventListener("click", toggleExample);
toggleBtn.addEventListener("keydown", (e) => {
if (e.key === "Enter" || e.key === " ") {
e.preventDefault();
toggleExample();
}
});
let resizeTimer;
window.addEventListener("resize", () => {
clearTimeout(resizeTimer);
resizeTimer = setTimeout(() => {
if (toggleBtn.getAttribute("aria-expanded") === "true") {
contentWrapper.style.maxHeight = contentWrapper.scrollHeight + "px";
}
}, 250);
});
});
}
if (document.readyState === "loading") {
document.addEventListener("DOMContentLoaded", initCollapsibleExamples);
} else {
initCollapsibleExamples();
}
})();