govuk-frontend
Version:
GOV.UK Frontend contains the code you need to start building a user interface for government platforms and services.
284 lines (281 loc) • 8.29 kB
JavaScript
import { getBreakpoint, getFragmentFromUrl } from '../../common/index.mjs';
import { ElementError } from '../../errors/index.mjs';
import { GOVUKFrontendComponent } from '../../govuk-frontend-component.mjs';
/**
* Tabs component
*
* @preserve
*/
class Tabs extends GOVUKFrontendComponent {
/**
* @param {Element | null} $root - HTML element to use for tabs
*/
constructor($root) {
super($root);
this.$tabs = void 0;
this.$tabList = void 0;
this.$tabListItems = void 0;
this.jsHiddenClass = 'govuk-tabs__panel--hidden';
this.changingHash = false;
this.boundTabClick = void 0;
this.boundTabKeydown = void 0;
this.boundOnHashChange = void 0;
this.mql = null;
const $tabs = this.$root.querySelectorAll('a.govuk-tabs__tab');
if (!$tabs.length) {
throw new ElementError({
component: Tabs,
identifier: 'Links (`<a class="govuk-tabs__tab">`)'
});
}
this.$tabs = $tabs;
this.boundTabClick = this.onTabClick.bind(this);
this.boundTabKeydown = this.onTabKeydown.bind(this);
this.boundOnHashChange = this.onHashChange.bind(this);
const $tabList = this.$root.querySelector('.govuk-tabs__list');
const $tabListItems = this.$root.querySelectorAll('li.govuk-tabs__list-item');
if (!$tabList) {
throw new ElementError({
component: Tabs,
identifier: 'List (`<ul class="govuk-tabs__list">`)'
});
}
if (!$tabListItems.length) {
throw new ElementError({
component: Tabs,
identifier: 'List items (`<li class="govuk-tabs__list-item">`)'
});
}
this.$tabList = $tabList;
this.$tabListItems = $tabListItems;
this.setupResponsiveChecks();
}
setupResponsiveChecks() {
const breakpoint = getBreakpoint('tablet');
if (!breakpoint.value) {
throw new ElementError({
component: Tabs,
identifier: `CSS custom property (\`${breakpoint.property}\`) on pseudo-class \`:root\``
});
}
this.mql = window.matchMedia(`(min-width: ${breakpoint.value})`);
if ('addEventListener' in this.mql) {
this.mql.addEventListener('change', () => this.checkMode());
} else {
this.mql.addListener(() => this.checkMode());
}
this.checkMode();
}
checkMode() {
var _this$mql;
if ((_this$mql = this.mql) != null && _this$mql.matches) {
this.setup();
} else {
this.teardown();
}
}
setup() {
var _this$getTab;
this.$tabList.setAttribute('role', 'tablist');
this.$tabListItems.forEach($item => {
$item.setAttribute('role', 'presentation');
});
this.$tabs.forEach($tab => {
this.setAttributes($tab);
$tab.addEventListener('click', this.boundTabClick, true);
$tab.addEventListener('keydown', this.boundTabKeydown, true);
this.hideTab($tab);
});
const $activeTab = (_this$getTab = this.getTab(window.location.hash)) != null ? _this$getTab : this.$tabs[0];
this.showTab($activeTab);
window.addEventListener('hashchange', this.boundOnHashChange, true);
}
teardown() {
this.$tabList.removeAttribute('role');
this.$tabListItems.forEach($item => {
$item.removeAttribute('role');
});
this.$tabs.forEach($tab => {
$tab.removeEventListener('click', this.boundTabClick, true);
$tab.removeEventListener('keydown', this.boundTabKeydown, true);
this.unsetAttributes($tab);
});
window.removeEventListener('hashchange', this.boundOnHashChange, true);
}
onHashChange() {
const hash = window.location.hash;
const $tabWithHash = this.getTab(hash);
if (!$tabWithHash) {
return;
}
if (this.changingHash) {
this.changingHash = false;
return;
}
const $previousTab = this.getCurrentTab();
if (!$previousTab) {
return;
}
this.hideTab($previousTab);
this.showTab($tabWithHash);
$tabWithHash.focus();
}
hideTab($tab) {
this.unhighlightTab($tab);
this.hidePanel($tab);
}
showTab($tab) {
this.highlightTab($tab);
this.showPanel($tab);
}
getTab(hash) {
return this.$root.querySelector(`a.govuk-tabs__tab[href="${hash}"]`);
}
setAttributes($tab) {
const panelId = getFragmentFromUrl($tab.href);
if (!panelId) {
return;
}
$tab.setAttribute('id', `tab_${panelId}`);
$tab.setAttribute('role', 'tab');
$tab.setAttribute('aria-controls', panelId);
$tab.setAttribute('aria-selected', 'false');
$tab.setAttribute('tabindex', '-1');
const $panel = this.getPanel($tab);
if (!$panel) {
return;
}
$panel.setAttribute('role', 'tabpanel');
$panel.setAttribute('aria-labelledby', $tab.id);
$panel.classList.add(this.jsHiddenClass);
}
unsetAttributes($tab) {
$tab.removeAttribute('id');
$tab.removeAttribute('role');
$tab.removeAttribute('aria-controls');
$tab.removeAttribute('aria-selected');
$tab.removeAttribute('tabindex');
const $panel = this.getPanel($tab);
if (!$panel) {
return;
}
$panel.removeAttribute('role');
$panel.removeAttribute('aria-labelledby');
$panel.classList.remove(this.jsHiddenClass);
}
onTabClick(event) {
const $currentTab = this.getCurrentTab();
const $nextTab = event.currentTarget;
if (!$currentTab || !($nextTab instanceof HTMLAnchorElement)) {
return;
}
event.preventDefault();
this.hideTab($currentTab);
this.showTab($nextTab);
this.createHistoryEntry($nextTab);
}
createHistoryEntry($tab) {
const $panel = this.getPanel($tab);
if (!$panel) {
return;
}
const panelId = $panel.id;
$panel.id = '';
this.changingHash = true;
window.location.hash = panelId;
$panel.id = panelId;
}
onTabKeydown(event) {
switch (event.key) {
case 'ArrowLeft':
case 'Left':
this.activatePreviousTab();
event.preventDefault();
break;
case 'ArrowRight':
case 'Right':
this.activateNextTab();
event.preventDefault();
break;
}
}
activateNextTab() {
const $currentTab = this.getCurrentTab();
if (!($currentTab != null && $currentTab.parentElement)) {
return;
}
const $nextTabListItem = $currentTab.parentElement.nextElementSibling;
if (!$nextTabListItem) {
return;
}
const $nextTab = $nextTabListItem.querySelector('a.govuk-tabs__tab');
if (!$nextTab) {
return;
}
this.hideTab($currentTab);
this.showTab($nextTab);
$nextTab.focus();
this.createHistoryEntry($nextTab);
}
activatePreviousTab() {
const $currentTab = this.getCurrentTab();
if (!($currentTab != null && $currentTab.parentElement)) {
return;
}
const $previousTabListItem = $currentTab.parentElement.previousElementSibling;
if (!$previousTabListItem) {
return;
}
const $previousTab = $previousTabListItem.querySelector('a.govuk-tabs__tab');
if (!$previousTab) {
return;
}
this.hideTab($currentTab);
this.showTab($previousTab);
$previousTab.focus();
this.createHistoryEntry($previousTab);
}
getPanel($tab) {
const panelId = getFragmentFromUrl($tab.href);
if (!panelId) {
return null;
}
return this.$root.querySelector(`#${panelId}`);
}
showPanel($tab) {
const $panel = this.getPanel($tab);
if (!$panel) {
return;
}
$panel.classList.remove(this.jsHiddenClass);
}
hidePanel($tab) {
const $panel = this.getPanel($tab);
if (!$panel) {
return;
}
$panel.classList.add(this.jsHiddenClass);
}
unhighlightTab($tab) {
if (!$tab.parentElement) {
return;
}
$tab.setAttribute('aria-selected', 'false');
$tab.parentElement.classList.remove('govuk-tabs__list-item--selected');
$tab.setAttribute('tabindex', '-1');
}
highlightTab($tab) {
if (!$tab.parentElement) {
return;
}
$tab.setAttribute('aria-selected', 'true');
$tab.parentElement.classList.add('govuk-tabs__list-item--selected');
$tab.setAttribute('tabindex', '0');
}
getCurrentTab() {
return this.$root.querySelector('.govuk-tabs__list-item--selected a.govuk-tabs__tab');
}
}
Tabs.moduleName = 'govuk-tabs';
export { Tabs };
//# sourceMappingURL=tabs.mjs.map