function tabbed(view, options = {}) {
const {
defaultLabel = "Settings",
selected = 0,
scope = DOM.uid("tabbed").id
} = options;
const tabs = html`<ul class="${scope}-tabs">`;
const body = html`<div class="${scope}-body">`;
const style = html`<style>${theme(scope)}`;
const registerTab = (title, node, active = false) => {
const tab = tabs.appendChild(html`<li>${title}`);
const content = html`<div>`;
content.append(...node.children);
const cActive = `${scope}-active`;
if (active) tab.classList.add(cActive);
tab.classList.add(`${scope}-tab`);
tab.onclick = () => {
for (const n of tabs.children) n.classList.remove(cActive);
for (const n of body.children) n.remove();
tab.classList.add(cActive);
body.append(content);
};
return tab;
};
view.querySelectorAll("summary").forEach((summary) => {
const details = summary.parentElement;
let parent = details.parent;
details.remove();
summary.remove();
registerTab(summary.textContent, details);
while (parent && parent !== view && !parent.children.length) {
const n = parent;
parent = parent.parentElement;
n.remove();
}
});
if (view.children.length && defaultLabel != null) {
const common = html`<div>`;
common.append(...view.children);
registerTab(defaultLabel, common);
tabs.prepend(tabs.lastElementChild);
}
(tabs.children[selected] || tabs.firstElementChild).click();
view.append(html`<div class="${scope}">${tabs}${body}${style}`);
return view;
}