diff --git a/core/static/bundled/core/accordion-index.ts b/core/static/bundled/core/accordion-index.ts new file mode 100644 index 00000000..39e37564 --- /dev/null +++ b/core/static/bundled/core/accordion-index.ts @@ -0,0 +1,25 @@ +const setMaxHeight = (element: HTMLDetailsElement) => { + element.setAttribute("style", `max-height: ${element.scrollHeight}px`); +}; + +// Initialize max-height at load +window.addEventListener("DOMContentLoaded", () => { + for (const el of document.querySelectorAll("details.accordion")) { + setMaxHeight(el as HTMLDetailsElement); + } +}); + +// Accordion opened +new MutationObserver((mutations: MutationRecord[]) => { + for (const mutation of mutations) { + const target = mutation.target as HTMLDetailsElement; + if (target.tagName !== "DETAILS" || !target.classList.contains("accordion")) { + continue; + } + setMaxHeight(target); + } +}).observe(document.body, { + attributes: true, + attributeFilter: ["open"], + subtree: true, +}); diff --git a/core/static/core/accordion.scss b/core/static/core/accordion.scss index 69362ccd..07793a7a 100644 --- a/core/static/core/accordion.scss +++ b/core/static/core/accordion.scss @@ -50,31 +50,6 @@ details.accordion>.accordion-content { border-bottom-left-radius: 3px; } -@keyframes open { - 0% { - opacity: 0 - } - - 100% { - opacity: 1 - } -} - -/* closing animation */ -@keyframes close { - 0% { - opacity: 1 - } - - 100% { - opacity: 0 - } -} - -details[open].accordion>summary~* { - animation: open 700ms; -} - -details::not([open]).accordion>summary~* { - animation: open 700ms; +details.accordion { + transition: max-height 300ms ease-in-out; } \ No newline at end of file diff --git a/core/templates/core/base.jinja b/core/templates/core/base.jinja index e132699a..e7b2b3ca 100644 --- a/core/templates/core/base.jinja +++ b/core/templates/core/base.jinja @@ -27,6 +27,7 @@ <script type="module" src="{{ static('bundled/htmx-index.js') }}"></script> <script type="module" src="{{ static('bundled/country-flags-index.ts') }}"></script> <script type="module" src="{{ static('bundled/core/tooltips-index.ts') }}"></script> + <script type="module" src="{{ static('bundled/core/accordion-index.ts') }}"></script> <!-- Jquery declared here to be accessible in every django widgets --> <script src="{{ static('bundled/vendored/jquery.min.js') }}"></script>