Files
hextra_mirror/assets/js/tabs.js
2025-06-04 00:52:20 +01:00

51 lines
1.6 KiB
JavaScript

(function () {
function updateGroup(container, index) {
const tabs = Array.from(container.querySelectorAll('.hextra-tabs-toggle'));
tabs.forEach((tab, i) => {
tab.dataset.state = i === index ? 'selected' : '';
if (i === index) {
tab.setAttribute('aria-selected', 'true');
tab.tabIndex = 0;
} else {
tab.removeAttribute('aria-selected');
tab.removeAttribute('tabindex');
}
});
const panelsContainer = container.parentElement.nextElementSibling;
Array.from(panelsContainer.children).forEach((panel, i) => {
panel.dataset.state = i === index ? 'selected' : '';
if (i === index) {
panel.tabIndex = 0;
} else {
panel.removeAttribute('tabindex');
}
});
}
const groups = document.querySelectorAll('[data-tab-group]');
groups.forEach((group) => {
const key = group.dataset.tabGroup;
const saved = localStorage.getItem('hextra-tab-' + key);
if (saved !== null) {
updateGroup(group, parseInt(saved, 10));
}
});
document.querySelectorAll('.hextra-tabs-toggle').forEach((button) => {
button.addEventListener('click', function (e) {
const container = e.target.parentElement;
const index = Array.from(container.querySelectorAll('.hextra-tabs-toggle')).indexOf(
e.target
);
const key = container.dataset.tabGroup;
document
.querySelectorAll('[data-tab-group="' + key + '"]')
.forEach((grp) => updateGroup(grp, index));
if (key) {
localStorage.setItem('hextra-tab-' + key, index.toString());
}
});
});
})();