Keeping utility for accordion and observer for the future.

This commit is contained in:
Bradley Shellnut 2022-12-31 16:18:08 -08:00
parent eca732ae1d
commit 4ca9e4a4fd

27
src/lib/util/accordion.ts Normal file
View file

@ -0,0 +1,27 @@
export default function (node: HTMLElement) {
const setHeight = (detail: HTMLDetailsElement, open = false) => {
detail.open = open;
const rect = detail.getBoundingClientRect();
detail.dataset.width = `${rect.width}`;
detail.style.setProperty(open ? `--expanded` : `--collapsed`, `${rect.height}px`);
};
const RO = new ResizeObserver((entries) => {
return entries.forEach((entry) => {
const detail: HTMLDetailsElement = entry.target;
const width: number = parseInt(detail.dataset.width, 10);
if (width !== parseInt(entry.contentRect.width, 10)) {
detail.removeAttribute('style');
setHeight(detail);
setHeight(detail, true);
detail.open = false;
}
});
});
RO.observe(node);
return {
destroy() {
RO.unobserve(node);
}
};
}