-
Notifications
You must be signed in to change notification settings - Fork 0
/
tabs.js
164 lines (145 loc) · 5.5 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
(function () {
'use strict';
let selected_ = null;
customElements.define('custom-tabs', class extends HTMLElement {
constructor() {
super(); // always call super() first in the ctor.
// Create shadow DOM for the component.
let shadowRoot = this.attachShadow({ mode: 'open' });
shadowRoot.innerHTML = `
<style>
:host {
display: inline-block;
contain: content;
border: 1px solid var(--quote-border);
border-radius: 8px;
width: 100%;
}
#panels {
padding: 10px;
}
#tabs {
border-bottom: 1px solid var(--quote-border);
background-color: var(--sidebar-bg);
overflow-x: auto;
}
#tabs slot {
display: inline-flex; /* Safari bug. Treats <slot> as a parent */
}
#tabs ::slotted(*) {
color: var(--sidebar-fg);
padding: 16px 8px;
margin: 0;
text-align: center;
text-overflow: ellipsis;
white-space: nowrap;
overflow: hidden;
cursor: pointer;
border-top-left-radius: 8px;
border-top-right-radius: 3px;
border: none; /* if the user users a <button> */
}
#tabs ::slotted([tabindex="0"]), #tabs ::slotted(*:hover) {
color: var(--sidebar-active);
}
#panels ::slotted([aria-hidden="true"]) {
display: none;
}
pre {
margin: 0;
}
</style>
<div id="tabs">
<slot id="tabsSlot" name="title"></slot>
</div>
<div id="panels">
<slot id="panelsSlot"></slot>
</div>
`;
}
get selected() {
return selected_;
}
set selected(idx) {
selected_ = idx;
this._selectTab(idx);
this.setAttribute('selected', idx);
}
connectedCallback() {
this.setAttribute('role', 'tablist');
const tabsSlot = this.shadowRoot.querySelector('#tabsSlot');
const panelsSlot = this.shadowRoot.querySelector('#panelsSlot');
this.tabs = tabsSlot.assignedNodes({ flatten: true });
this.panels = panelsSlot.assignedNodes({ flatten: true }).filter(el => {
return el.nodeType === Node.ELEMENT_NODE;
});
// Save refer to we can remove listeners later.
this._boundOnTitleClick = this._onTitleClick.bind(this);
tabsSlot.addEventListener('click', this._boundOnTitleClick);
document.addEventListener('mdbook-category-changed', this._onSiblingCategoryChanged.bind(this));
this.selected = this._findFirstSelectedTab() || this._findStoredSelectedTab() || 0;
}
disconnectedCallback() {
const tabsSlot = this.shadowRoot.querySelector('#tabsSlot');
tabsSlot.removeEventListener('click', this._boundOnTitleClick);
document.removeEventListener('mdbook-category-changed', this._onSiblingCategoryChanged.bind(this));
}
_onTitleClick(e) {
if (e.target.slot === 'title') {
this.selected = this.tabs.indexOf(e.target);
e.target.focus();
}
}
_findFirstSelectedTab() {
let selectedIdx;
for (let [i, tab] of this.tabs.entries()) {
tab.setAttribute('role', 'tab');
if (tab.hasAttribute('selected')) {
selectedIdx = i;
}
}
return selectedIdx;
}
_findStoredSelectedTab() {
let selectedIdx;
if (this.getAttribute("category")) {
let selectedText;
try { selectedText = localStorage.getItem('mdbook-tabs-' + this.getAttribute("category")); } catch (e) { }
if (selectedText) {
for (let [i, tab] of this.tabs.entries()) {
if (tab.textContent === selectedText) {
selectedIdx = i;
break;
}
}
}
}
return selectedIdx;
}
_selectTab(idx = null, propagate = true) {
let category = this.getAttribute("category");
for (let i = 0, tab; tab = this.tabs[i]; ++i) {
let select = i === idx;
tab.setAttribute('tabindex', select ? 0 : -1);
tab.setAttribute('aria-selected', select);
this.panels[i].setAttribute('aria-hidden', !select);
if (select && category && tab.textContent) {
try { localStorage.setItem('mdbook-tabs-' + category, tab.textContent); } catch (e) { }
}
}
if (propagate) {
document.dispatchEvent(new CustomEvent(
'mdbook-category-changed',
{ detail: { category: category, idx: idx }}
));
}
}
_onSiblingCategoryChanged(e) {
let category = this.getAttribute("category")
if (category === e.detail.category) {
this._selectTab(e.detail.idx, false);
this.setAttribute('selected', e.detail.idx);
}
}
});
})();