-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtabs.js
273 lines (225 loc) · 7.19 KB
/
tabs.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
const template = document.createElement('template');
template.innerHTML = `
<style>
:host {
--data-color: var(--iobio-data-color, #2d8fc1);
}
.tab-panel-container {
display: flex;
flex-direction: column;
box-sizing: border-box;
width: 100%;
height: 100%;
}
.tabs {
display: flex;
align-items: center;
justify-content: center;
width: 100%;
gap: 10px;
}
.panels {
position: relative;
width: 100%;
height: 100%;
}
::slotted(iobio-tab) {
outline: none;
cursor: pointer;
text-align: center;
color: grey;
}
::slotted(iobio-tab[selected]) {
color: var(--data-color);
}
.panels ::slotted(.hidden-panel) {
position: absolute;
visibility: hidden;
z-index: -1;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
</style>
<div class="tab-panel-container">
<div class="tabs">
<slot name="tab"></slot>
</div>
<div class="panels">
<slot name="panel"></slot>
</div>
</div>
`;
/**
* This code is derived from 'components-howto-tabs'
* The link: https://web.dev/articles/components-howto-tabs
*/
class Tabs extends HTMLElement {
constructor() {
super();
this._onSlotChange = this._onSlotChange.bind(this);
this.attachShadow({mode: 'open'});
this.shadowRoot.appendChild(template.content.cloneNode(true));
this._tabSlot = this.shadowRoot.querySelector('slot[name=tab]');
this._panelSlot = this.shadowRoot.querySelector('slot[name=panel]');
this._tabSlot.addEventListener('slotchange', this._onSlotChange);
this._panelSlot.addEventListener('slotchange', this._onSlotChange);
}
connectedCallback() {
this.addEventListener('click', this._onClick);
if (!this.hasAttribute('role')) {
this.setAttribute('role', 'tablist');
}
this._addSeparators();
}
disconnectedCallback() {
this.removeEventListener('click', this._onClick);
}
/**
* `_onSlotChange()` is called whenever an element is added or removed from
* one of the shadow DOM slots.
*/
_onSlotChange() {
this._linkPanels();
}
/**
* `_linkPanels()` links up tabs with their adjacent panels using
* `aria-controls` and `aria-labelledby`. Additionally, the method makes
* sure only one tab is active.
*/
_linkPanels() {
const tabs = this._allTabs();
// Give each panel a `aria-labelledby` attribute that refers to the tab
// that controls it.
tabs.forEach(tab => {
const tabContent = tab.querySelector('[slot="tab-content"]');
if (!tabContent) {
console.error(`Tab #${tab.id} does not have a [slot="tab-content"] element.`);
return;
}
const panel = tab.nextElementSibling;
if (panel.tagName.toLowerCase() !== 'iobio-tab-panel') {
console.error(`Tab #${tab.id} is not a` +
`sibling of a <iobio-tab-panel>`);
return;
}
tab.setAttribute('aria-controls', panel.id);
panel.setAttribute('aria-labelledby', tab.id);
});
// The element checks if any of the tabs have been marked as selected.
// If not, the first tab is now selected.
const selectedTab =
tabs.find(tab => tab.selected) || tabs[0];
this._selectTab(selectedTab);
}
_allPanels() {
return Array.from(this.querySelectorAll('iobio-tab-panel'));
}
_allTabs() {
return Array.from(this.querySelectorAll('iobio-tab'));
}
_panelForTab(tab) {
const panelId = tab.getAttribute('aria-controls');
return this.querySelector(`#${panelId}`);
}
reset() {
const tabs = this._allTabs();
const panels = this._allPanels();
tabs.forEach(tab => tab.selected = false);
panels.forEach(panel => panel.classList.add('hidden-panel'));
}
_selectTab(newTab) {
// Deselect all tabs and hide all panels.
this.reset();
// Get the panel that the `newTab` is associated with.
const newPanel = this._panelForTab(newTab);
// If that panel doesn’t exist, abort.
if (!newPanel)
throw new Error(`No panel with id ${newPanelId}`);
// Find the tab content element
const newTabContent = newTab.querySelector('[slot="tab-content"]');
if (!newTabContent) {
console.error(`Tab #${newTab.id} does not have a [slot="tab-content"] element.`);
return;
}
newTab.selected = true;
newPanel.classList.remove('hidden-panel');
}
_onClick(event) {
const tabContent = event.target.closest('[slot="tab-content"]');
if (tabContent) {
this._selectTab(tabContent.closest('iobio-tab'));
}
}
_addSeparators() {
const tabs = this.querySelectorAll('iobio-tab');
tabs.forEach((tab, index) => {
// Skip the last tab
if (index < tabs.length - 1) {
const separator = document.createElement('span');
separator.textContent = '|';
separator.style.margin = '0 10px';
separator.style.color = 'grey';
tab.appendChild(separator);
}
});
}
}
customElements.define('iobio-tabs', Tabs);
let TabCounter = 0;
class Tab extends HTMLElement {
static get observedAttributes() {
return ['selected'];
}
constructor() {
super();
}
connectedCallback() {
// If this is executed, JavaScript is working and the element
// changes its role to `tab`.
this.setAttribute('role', 'tab');
if (!this.id)
this.id = `iobio-tab-generated-${TabCounter++}`;
// Set a well-defined initial state.
this.setAttribute('aria-selected', 'false');
this.setAttribute('tabindex', -1);
this._upgradeProperty('selected');
}
_upgradeProperty(prop) {
if (this.hasOwnProperty(prop)) {
let value = this[prop];
delete this[prop];
this[prop] = value;
}
}
attributeChangedCallback() {
const value = this.hasAttribute('selected');
this.setAttribute('aria-selected', value);
this.setAttribute('tabindex', value ? 0 : -1);
}
set selected(value) {
value = Boolean(value);
if (value)
this.setAttribute('selected', '');
else
this.removeAttribute('selected');
}
get selected() {
return this.hasAttribute('selected');
}
}
customElements.define('iobio-tab', Tab);
let PanelCounter = 0;
class TabPanel extends HTMLElement {
constructor() {
super();
}
connectedCallback() {
this.setAttribute('role', 'tabpanel');
if (!this.id)
this.id = `iobio-tab-panel-generated-${PanelCounter++}`;
}
}
customElements.define('iobio-tab-panel', TabPanel);
export {Tabs, Tab, TabPanel}