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;
|
|
|
|
this.tabs = Array.from(this.container.querySelectorAll('[role="tab"]'));
|
|
|
|
this.panels = Array.from(this.container.querySelectorAll('[role="tabpanel"]'));
|
2020-06-30 17:12:45 -04:00
|
|
|
|
2023-01-28 07:50:51 -05:00
|
|
|
this.container.addEventListener('click', event => {
|
|
|
|
const button = event.target.closest('[role="tab"]');
|
|
|
|
if (button) {
|
|
|
|
this.show(button.getAttribute('aria-controls'));
|
2020-06-30 17:12:45 -04:00
|
|
|
}
|
2023-01-28 07:50:51 -05:00
|
|
|
});
|
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-04-18 17:20:02 -04:00
|
|
|
}
|