2023-04-18 17:20:02 -04:00
|
|
|
import {Component} from './component';
|
2022-11-16 08:04:22 -05:00
|
|
|
|
2020-06-30 17:12:45 -04:00
|
|
|
/**
|
|
|
|
* Tabs
|
2023-01-28 07:50:51 -05:00
|
|
|
* Uses accessible attributes to drive its functionality.
|
|
|
|
* On tab wrapping element:
|
|
|
|
* - role=tablist
|
|
|
|
* On tabs (Should be a button):
|
|
|
|
* - id
|
|
|
|
* - role=tab
|
|
|
|
* - aria-selected=true/false
|
|
|
|
* - aria-controls=<id-of-panel-section>
|
|
|
|
* On panels:
|
|
|
|
* - id
|
|
|
|
* - tabindex=0
|
|
|
|
* - role=tabpanel
|
|
|
|
* - aria-labelledby=<id-of-tab-for-panel>
|
|
|
|
* - hidden (If not shown by default).
|
2020-06-30 17:12:45 -04:00
|
|
|
*/
|
2022-11-16 08:04:22 -05:00
|
|
|
export class Tabs extends Component {
|
2020-06-30 17:12:45 -04:00
|
|
|
|
|
|
|
setup() {
|
2023-01-28 07:50:51 -05:00
|
|
|
this.container = this.$el;
|
2023-05-27 11:58:10 -04:00
|
|
|
this.tabList = this.container.querySelector('[role="tablist"]');
|
|
|
|
this.tabs = Array.from(this.tabList.querySelectorAll('[role="tab"]'));
|
|
|
|
this.panels = Array.from(this.container.querySelectorAll(':scope > [role="tabpanel"], :scope > * > [role="tabpanel"]'));
|
|
|
|
this.activeUnder = this.$opts.activeUnder ? Number(this.$opts.activeUnder) : 10000;
|
|
|
|
this.active = null;
|
2020-06-30 17:12:45 -04:00
|
|
|
|
2023-01-28 07:50:51 -05:00
|
|
|
this.container.addEventListener('click', event => {
|
2023-05-27 11:58:10 -04:00
|
|
|
const tab = event.target.closest('[role="tab"]');
|
|
|
|
if (tab && this.tabs.includes(tab)) {
|
|
|
|
this.show(tab.getAttribute('aria-controls'));
|
2020-06-30 17:12:45 -04:00
|
|
|
}
|
2023-01-28 07:50:51 -05:00
|
|
|
});
|
2023-05-27 11:58:10 -04:00
|
|
|
|
|
|
|
window.addEventListener('resize', this.updateActiveState.bind(this), {
|
|
|
|
passive: true,
|
|
|
|
});
|
|
|
|
this.updateActiveState();
|
2020-06-30 17:12:45 -04:00
|
|
|
}
|
|
|
|
|
2023-01-28 07:50:51 -05:00
|
|
|
show(sectionId) {
|
|
|
|
for (const panel of this.panels) {
|
|
|
|
panel.toggleAttribute('hidden', panel.id !== sectionId);
|
|
|
|
}
|
2020-06-30 17:12:45 -04:00
|
|
|
|
2023-01-28 07:50:51 -05:00
|
|
|
for (const tab of this.tabs) {
|
|
|
|
const tabSection = tab.getAttribute('aria-controls');
|
|
|
|
const selected = tabSection === sectionId;
|
|
|
|
tab.setAttribute('aria-selected', selected ? 'true' : 'false');
|
2020-06-30 17:12:45 -04:00
|
|
|
}
|
2023-01-28 11:06:11 -05:00
|
|
|
|
2023-01-28 12:11:15 -05:00
|
|
|
this.$emit('change', {showing: sectionId});
|
2020-06-30 17:12:45 -04:00
|
|
|
}
|
|
|
|
|
2023-05-27 11:58:10 -04:00
|
|
|
updateActiveState() {
|
|
|
|
const active = window.innerWidth < this.activeUnder;
|
|
|
|
if (active === this.active) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (active) {
|
|
|
|
this.activate();
|
|
|
|
} else {
|
|
|
|
this.deactivate();
|
|
|
|
}
|
|
|
|
|
|
|
|
this.active = active;
|
|
|
|
}
|
|
|
|
|
|
|
|
activate() {
|
2023-05-29 10:50:36 -04:00
|
|
|
const panelToShow = this.panels.find(p => !p.hasAttribute('hidden')) || this.panels[0];
|
|
|
|
this.show(panelToShow.id);
|
2023-05-27 11:58:10 -04:00
|
|
|
this.tabList.toggleAttribute('hidden', false);
|
|
|
|
}
|
|
|
|
|
|
|
|
deactivate() {
|
|
|
|
for (const panel of this.panels) {
|
|
|
|
panel.removeAttribute('hidden');
|
|
|
|
}
|
|
|
|
for (const tab of this.tabs) {
|
|
|
|
tab.setAttribute('aria-selected', 'false');
|
|
|
|
}
|
|
|
|
this.tabList.toggleAttribute('hidden', true);
|
|
|
|
}
|
|
|
|
|
2023-04-18 17:20:02 -04:00
|
|
|
}
|