Use active-tab width switching within mobile min/max bounds

This commit is contained in:
2026-05-21 18:15:29 +02:00
parent e2eb0fd4aa
commit 28b94ec8a0
+15 -1
View File
@@ -124,6 +124,8 @@
tab.style.removeProperty('flex');
tab.style.removeProperty('min-width');
tab.style.removeProperty('max-width');
tab.removeAttribute('data-tab-min-width');
tab.removeAttribute('data-tab-max-width');
});
if (!mobileBreakpoint.matches) {
@@ -133,16 +135,24 @@
tabs.forEach((tab) => {
const currentWidth = Math.ceil(tab.getBoundingClientRect().width);
const maxWidth = Math.ceil(currentWidth * 1.4);
tab.style.width = `clamp(${currentWidth}px, 33vw, ${maxWidth}px)`;
tab.style.flex = '0 1 auto';
tab.style.minWidth = `${currentWidth}px`;
tab.style.maxWidth = `${maxWidth}px`;
tab.setAttribute('data-tab-min-width', `${currentWidth}px`);
tab.setAttribute('data-tab-max-width', `${maxWidth}px`);
});
};
const activateTab = (targetTab) => {
tabs.forEach((tab) => {
tab.setAttribute('aria-selected', String(tab === targetTab));
if (mobileBreakpoint.matches) {
const minWidth = tab.getAttribute('data-tab-min-width');
const maxWidth = tab.getAttribute('data-tab-max-width');
tab.style.width = tab === targetTab ? maxWidth : minWidth;
} else {
tab.style.removeProperty('width');
}
});
panels.forEach((panel) => {
@@ -170,6 +180,10 @@
});
applyMobileTabWidthBounds();
const activeTab = tabs.find((tab) => tab.getAttribute('aria-selected') === 'true') || tabs[0];
if (activeTab) {
activateTab(activeTab);
}
mobileBreakpoint.addEventListener('change', applyMobileTabWidthBounds);
window.addEventListener('resize', applyMobileTabWidthBounds);
}