Fix for render issue for tabs

This commit is contained in:
A McIlwraith 2025-04-22 15:25:41 -04:00
parent 89870edaaf
commit df95905fe0

View File

@ -31,78 +31,76 @@ const waitForElement = (selector) => {
} }
module.exports = { export function init(container = document, spacer = true, args = {}) {
init: (container = document, spacer = true, args = {}) => {
container.querySelectorAll(".tab-group, tabset").forEach(tabGroup => { container.querySelectorAll(".tab-group, tabset").forEach(tabGroup => {
if (tabGroup.querySelector("[role=tablist]") === null) { if (tabGroup.querySelector("[role=tablist]") === null) {
const tabgroup = tabGroup.getAttribute("id"); const tabgroup = tabGroup.getAttribute("id");
let tablist = ""; let tablist = "";
Array.from(tabGroup.children).forEach(child => { Array.from(tabGroup.children).forEach(child => {
const tab = child.getAttribute("tab") || child.getAttribute("data-tab"); const tab = child.getAttribute("tab") || child.getAttribute("data-tab");
if (tab !== null) { if (tab !== null) {
const tabID = tab.replace(/\W+/g, "-").toLowerCase(); const tabID = tab.replace(/\W+/g, "-").toLowerCase();
const tabPanel = document.createElement('div'); const tabPanel = document.createElement('div');
tabPanel.id = `tab-panel-${tabgroup}-${tabID}`; tabPanel.id = `tab-panel-${tabgroup}-${tabID}`;
tabPanel.className = tablist === "" ? "open" : ""; tabPanel.className = tablist === "" ? "open" : "";
tabPanel.setAttribute("role", "tabpanel"); tabPanel.setAttribute("role", "tabpanel");
tabPanel.setAttribute("tabindex", "0"); tabPanel.setAttribute("tabindex", "0");
tabPanel.setAttribute("aria-labelledby", `tab-${tabgroup}-${tabID}`); tabPanel.setAttribute("aria-labelledby", `tab-${tabgroup}-${tabID}`);
tabPanel.appendChild(child.cloneNode(true)); tabPanel.appendChild(child.cloneNode(true));
child.parentNode.replaceChild(tabPanel, child); child.parentNode.replaceChild(tabPanel, child);
tablist += `<li tabindex="0" role="tab" ${tablist === "" ? "class='selected'" : ""} id="tab-${tabgroup}-${tabID}"><span>${tab}</span></li>`; let cls = tablist === "" ? "class='selected'" : "";
tablist += `<li tabindex="0" role="tab" ${cls}="" id="tab-${tabgroup}-${tabID}"><span>${tab}</span></li>`;
} else {
child.classList.add("tab-hidden");
}
});
const ul = document.createElement('ul');
ul.setAttribute("role", "tablist");
ul.innerHTML = spacer != true ? `${tablist}` : `${tablist}<li role="separator" class="separator"></li>`;
tabGroup.insertBefore(ul, tabGroup.firstChild);
tabGroup.querySelectorAll('[role="tab"]').forEach(tab => {
tab.addEventListener("click", (evt) => {
if (evt.altKey && typeof args.altModifier == "function") {
args.altModifier(tab, evt);
} else if (evt.shiftKey && typeof args.shiftModifier == "function") {
args.shiftModifier(tab, evt);
} else if (evt.metaKey && typeof args.metaModifier == "function") {
args.metaModifier(tab, evt);
} else { } else {
child.classList.add("tab-hidden"); const siblings = Array.from(tab.parentNode.children);
siblings.forEach(sibling => sibling.classList.remove("selected"));
tab.classList.add("selected");
const tabPanels = Array.from(tab.parentNode.parentNode.children)
.filter(child => child.getAttribute("role") === "tabpanel");
tabPanels.forEach(panel => panel.classList.remove("open"));
const tabPanelId = tab.getAttribute("id").replace("tab", "tab-panel");
document.getElementById(tabPanelId).classList.add("open");
} }
}); });
const ul = document.createElement('ul');
ul.setAttribute("role", "tablist");
ul.innerHTML = spacer != true ? `${tablist}` : `${tablist}<li role="separator" class="separator"></li>`;
tabGroup.insertBefore(ul, tabGroup.firstChild);
tabGroup.querySelectorAll('[role="tab"]').forEach(tab => { tab.addEventListener("keypress", (e) => {
tab.addEventListener("click", (e) => { e.preventDefault();
if (e.altKey && typeof args.altModifier == "function") { if( e.which == 32 || e.which == 13 ) {
args.altModifier(tab); e.currentTarget.dispatchEvent(click);
} else if (e.shiftKey && typeof args.shiftModifier == "function") { }
args.shiftModifier(tab); })
} else if (e.metaKey && typeof args.metaModifier == "function") { });
args.metaModifier(tab); }
} else {
const siblings = Array.from(tab.parentNode.children);
siblings.forEach(sibling => sibling.classList.remove("selected"));
tab.classList.add("selected");
const tabPanels = Array.from(tab.parentNode.parentNode.children) if (document.location.hash != "" && document.location.hash.substring(0,5) == "#tab-") {
.filter(child => child.getAttribute("role") === "tabpanel"); waitForElement(document.location.hash).then((el) => {
tabPanels.forEach(panel => panel.classList.remove("open")); el.scrollIntoView();
el.focus();
const tabPanelId = tab.getAttribute("id").replace("tab", "tab-panel"); el.dispatchEvent(click);
document.getElementById(tabPanelId).classList.add("open"); });
} }
}); });
tab.addEventListener("keypress", (e) => {
e.preventDefault();
if( e.which == 32 || e.which == 13 ) {
e.currentTarget.dispatchEvent(click);
}
})
});
}
if (document.location.hash != "" && document.location.hash.substring(0,5) == "#tab-") {
waitForElement(document.location.hash).then((el) => {
el.scrollIntoView();
el.focus();
el.dispatchEvent(click);
});
}
});
}
} }