-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpanel-set-two.mjs
More file actions
228 lines (205 loc) · 6.7 KB
/
Copy pathpanel-set-two.mjs
File metadata and controls
228 lines (205 loc) · 6.7 KB
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
/* Originally written by Brian Kardell */
(function() {
class PanelSetTwo extends HTMLElement {
static get observedAttributes() { return [ "display" ]; }
attributeChangedCallback(name, oldValue, newValue) {
requestAnimationFrame(() => {
if (name === "display") {
if (newValue === "tabs") {
this.tabSources
.forEach(tabSource => tabSource.slot = tabSource.id);
} else {
this.tabSources
.forEach(tabSource => tabSource.setAttribute("slot", ""));
}
}
});
}
get tabSources() {
return Array.from(this.querySelectorAll(":scope>[x-title]"));
}
get contentSources() {
return Array.from(this.querySelectorAll(":scope>[x-content]"));
}
set activeTab(tabSource) {
this.tabSources.forEach((source, i) => {
let relatedContent = this.querySelector(`#${source.getAttribute("aria-controls")}`);
if (source === tabSource) {
relatedContent.style.display = "block";
this.activeTabIndex = i;
source.tabIndex = 0;
if (this.matches(':focus-within')) {
requestAnimationFrame(() => source.focus());
}
} else {
source.tabIndex = -1;
relatedContent.style.display = "none";
}
});
this.shadowRoot.querySelectorAll('x-tab').forEach((el, i) => {
if (i===this.activeTabIndex) {
el.setAttribute('active','');
} else {
el.removeAttribute('active');
}
});
}
selectNextTab() {
let tabSources = this.tabSources;
this.activeTab =
this.activeTabIndex == tabSources.length - 1
? tabSources[0]
: tabSources[this.activeTabIndex + 1];
}
selectPreviousTab() {
let tabSources = this.tabSources;
this.activeTab =
this.activeTabIndex == 0
? tabSources[tabSources.length - 1]
: tabSources[this.activeTabIndex - 1];
}
constructor() {
super();
this.attachShadow({ mode: "open" });
this.shadowRoot.innerHTML = `
<style>
:host([hidden]),
::slotted([hidden]) {
display: none;
}
:host([theme=default]) [active]{
border-bottom: 1px solid white;
position:relative;
top: 1px;
z-index: 2;
}
:host([theme=default]) x-tab {
background-color: white;
margin: 0;
color: black;
padding: 0.2rem;
border: 1px solid lightgray;
}
:host([theme=default]) x-tab > ::slotted(*) {
font-size: 1rem;
font-weight: normal;
color: black;
margin: 0.2rem;
}
:host([theme=default]) x-tabs {
background-color: transparent;
border-bottom: 1px solid lightgray;
margin: 0;
}
:host([theme=default]) [default] {
display: block;
margin-top: -1rem;
padding: 0.5rem;
background-color: transparent;
border: 1px solid lightgray;
border-top: none;
}
x-tabs {
display: flex;
background-color: gray;
}
x-tab {
margin: 0 0.2rem;
padding: 0 .75rem;
background-color: #e3e0e0;
}
</style>
<x-tabs></x-tabs>
<x-content></x-content>
<slot default></slot>
`;
this.tabsetContainer = this.shadowRoot.querySelector("x-tabs");
let defaultSlot = this.shadowRoot.querySelector("[default]");
this.role = "tablist";
// it might be good to do this on a timeout to let normal parsing happen usually
// and only fire once?
defaultSlot.addEventListener("slotchange", e => {
let assignedEls = e.target.assignedElements();
let unassignedEls = assignedEls.filter(el => !el._init);
let titleEls = unassignedEls.filter(el => el.matches("[x-title]"));
let contentEls = unassignedEls.filter(el => el.matches("[x-content]"));
let size = Math.min(titleEls.length, contentEls.length);
if (titleEls.length !== size) {
console.warn("mismatch in panel-set title/content pairs...");
}
titleEls.forEach(tabSource => {
if (!tabSource.matches('h1,h2,h3,h4,h5,h6,[role="heading"]')) {
console.warn("element marked with x-title should be a heading");
}
});
for (let i = 0; i < size; i++) {
let tabUId = PanelSetTwo.prototype.nextUid();
let contentUId = PanelSetTwo.prototype.nextUid();
let tab = titleEls[i];
let content = contentEls[i];
// tabs are -1, they need to use roving focus :(
tab.tabIndex = -1;
tab.role = "tab";
tab.id = tabUId;
tab.setAttribute("aria-controls", contentUId);
content.role = "tabpanel";
content.tabIndex = 0;
content.id = contentUId;
tab._init = true;
content._init = true;
tab.addEventListener("click", evt => {
this.activeTab = evt.target;
});
// build a stub... we have to make sure we don't do this repeatedly
let range = document.createRange();
this.tabsetContainer.appendChild(
range.createContextualFragment(
`<x-tab><slot name="${tabUId}"></slot></x-tab>`,
"text/html"
)
);
// setting these would project them, but in theory leave them in place...
//titleEls[i].slot = tabUId
}
// todo: figure this part out dynamically...
this.activeTabIndex = 0;
this.activeTab = this.tabSources[0];
let mql = window.matchMedia("(max-width: 720px)");
let mqh = evt => {
this.setAttribute("display", mql.matches ? "accordion" : "tabs");
};
mql.addListener(mqh);
mqh();
});
this.addEventListener(
"keydown",
evt => {
switch (evt.keyCode) {
case 37:
this.selectPreviousTab();
break;
case 38:
this.selectPreviousTab();
evt.preventDefault();
break;
case 39:
this.selectNextTab();
break;
case 40:
evt.preventDefault();
this.selectNextTab();
break;
}
},
false
);
// let tabEls = [...this.querySelectorAll(":scope>.title")];
// contentEls = [...this.querySelectorAll(":scope>.content")];
}
}
PanelSetTwo.prototype.lastUid = 0;
PanelSetTwo.prototype.nextUid = function() {
return `cp${++PanelSetTwo.prototype.lastUid}`;
};
customElements.define("panel-set-two", PanelSetTwo);
})();