-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtabs.html
More file actions
273 lines (235 loc) · 7.81 KB
/
tabs.html
File metadata and controls
273 lines (235 loc) · 7.81 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
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
<!DOCTYPE html>
<html lang="en">
<head>
<title>Page Tabs</title>
<style type="text/css">
body, html {
height: 100%;
margin: 0; /*Needed if there's a heading at the top due to the margin with headings*/
color: #FFF;
background-color: #000;
}
h1 {
text-align: center;
}
.tabs {
font-family: "lucida grande", sans-serif;
}
[role="tablist"] {
min-width: 100%;
}
[role="tab"],
[role="tab"]:focus,
[role="tab"]:hover {
display: inline-block;
position: relative;
z-index: 2;
top: 2px;
margin: 0;
margin-top: 4px;
padding: 3px 3px 4px;
border: 1px solid black;
border-bottom: 2px solid hsl(219deg 1% 72%);
border-radius: 5px 5px 0 0;
background-color: #303841;
outline: none;
font-weight: bold;
max-width: 22%;
overflow: hidden;
text-align: left;
cursor: pointer;
color: #FFF;
}
[role="tab"][aria-selected="true"] {
padding: 2px 2px 4px;
margin-top: 0;
border-width: 2px;
border-top-width: 6px;
border-top-color: rgb(36 116 214);
border-bottom-color: black;
background-color: #303841;
}
[role="tab"][aria-selected="false"] {
border-bottom: 1px solid black;
}
[role="tab"] span.focus {
display: inline-block;
margin: 2px;
padding: 4px 6px;
}
[role="tab"]:hover span.focus,
[role="tab"]:focus span.focus,
[role="tab"]:active span.focus {
padding: 2px 4px;
border: 2px solid rgb(36 116 214);
border-radius: 3px;
}
[role="tabpanel"] {
padding: 5px;
border: 2px solid black;
border-radius: 0 5px 5px;
color: #FFF;
background-color: #303841;
min-height: 10em;
width: 60%;
overflow: auto;
}
[role="tabpanel"].is-hidden {
display: none;
}
[role="tabpanel"] p {
margin: 0;
}
</style>
</head>
<body>
<h1>Page Tabs</h1>
<div class="tabs">
<p>This example was taken from the ARIA Authoring Practices. It is the <a href="https://www.w3.org/WAI/ARIA/apg/patterns/tabs/examples/tabs-automatic/">Example of Tabs with Automatic Activation</a>. I gave it a "dark mode" appearance but, other than that, it's their code. They've documented the keyboard support on their page. I didn't want to reinvent the wheel, I just wanted an example I could easily style and understand. For more information about the roles and structures used and expected behavior of this widget, follow the aforementioned link.</p>
<h2 id="tablist-1">
Famous Quotes
</h2>
<div role="tablist" aria-labelledby="tablist-1" class="automatic">
<button id="tab-1" type="button" role="tab" aria-selected="true" aria-controls="tabpanel-1">
<span class="focus">Alan Watts</span>
</button>
<button id="tab-2" type="button" role="tab" aria-selected="false" aria-controls="tabpanel-2" tabindex="-1">
<span class="focus">Ram Dass</span>
</button>
<button id="tab-3" type="button" role="tab" aria-selected="false" aria-controls="tabpanel-3" tabindex="-1">
<span class="focus">Maya Angelou</span>
</button>
<button id="tab-4" type="button" role="tab" aria-selected="false" aria-controls="tabpanel-4" tabindex="-1">
<span class="focus">Neil Armstrong</span>
</button>
</div>
<div id="tabpanel-1" role="tabpanel" tabindex="0" aria-labelledby="tab-1">
<p>"Trying to define yourself is like trying to bite your own teeth."</p>
</div>
<div id="tabpanel-2" role="tabpanel" tabindex="0" aria-labelledby="tab-2" class="is-hidden">
<p>"I help people as a work on myself and I work on myself to help people."</p>
</div>
<div id="tabpanel-3" role="tabpanel" tabindex="0" aria-labelledby="tab-3" class="is-hidden">
<p>"You teach people how to treat you."</p>
</div>
<div id="tabpanel-4" role="tabpanel" tabindex="0" aria-labelledby="tab-4" class="is-hidden">
<p>"It suddenly struck me that that tiny pea, pretty and blue, was the Earth."</p>
</div>
</div>
<footer>
<a href="http://robertjmccaffery.com/examples.html">Back to Examples</a> | <a href="http://robertjmccaffery.com/">Home</a>
</footer>
<script type="text/javascript">
/*
* This content is licensed according to the W3C Software License at
* https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document
*
* File: tabs-automatic.js
*
* Desc: Tablist widget that implements ARIA Authoring Practices
*/
'use strict';
class TabsAutomatic {
constructor(groupNode) {
this.tablistNode = groupNode;
this.tabs = [];
this.firstTab = null;
this.lastTab = null;
this.tabs = Array.from(this.tablistNode.querySelectorAll('[role=tab]'));
this.tabpanels = [];
for (var i = 0; i < this.tabs.length; i += 1) {
var tab = this.tabs[i];
var tabpanel = document.getElementById(tab.getAttribute('aria-controls'));
tab.tabIndex = -1;
tab.setAttribute('aria-selected', 'false');
this.tabpanels.push(tabpanel);
tab.addEventListener('keydown', this.onKeydown.bind(this));
tab.addEventListener('click', this.onClick.bind(this));
if (!this.firstTab) {
this.firstTab = tab;
}
this.lastTab = tab;
}
this.setSelectedTab(this.firstTab, false);
}
setSelectedTab(currentTab, setFocus) {
if (typeof setFocus !== 'boolean') {
setFocus = true;
}
for (var i = 0; i < this.tabs.length; i += 1) {
var tab = this.tabs[i];
if (currentTab === tab) {
tab.setAttribute('aria-selected', 'true');
tab.removeAttribute('tabindex');
this.tabpanels[i].classList.remove('is-hidden');
if (setFocus) {
tab.focus();
}
} else {
tab.setAttribute('aria-selected', 'false');
tab.tabIndex = -1;
this.tabpanels[i].classList.add('is-hidden');
}
}
}
setSelectedToPreviousTab(currentTab) {
var index;
if (currentTab === this.firstTab) {
this.setSelectedTab(this.lastTab);
} else {
index = this.tabs.indexOf(currentTab);
this.setSelectedTab(this.tabs[index - 1]);
}
}
setSelectedToNextTab(currentTab) {
var index;
if (currentTab === this.lastTab) {
this.setSelectedTab(this.firstTab);
} else {
index = this.tabs.indexOf(currentTab);
this.setSelectedTab(this.tabs[index + 1]);
}
}
/* EVENT HANDLERS */
onKeydown(event) {
var tgt = event.currentTarget,
flag = false;
switch (event.key) {
case 'ArrowLeft':
this.setSelectedToPreviousTab(tgt);
flag = true;
break;
case 'ArrowRight':
this.setSelectedToNextTab(tgt);
flag = true;
break;
case 'Home':
this.setSelectedTab(this.firstTab);
flag = true;
break;
case 'End':
this.setSelectedTab(this.lastTab);
flag = true;
break;
default:
break;
}
if (flag) {
event.stopPropagation();
event.preventDefault();
}
}
onClick(event) {
this.setSelectedTab(event.currentTarget);
}
}
// Initialize tablist
window.addEventListener('load', function () {
var tablists = document.querySelectorAll('[role=tablist].automatic');
for (var i = 0; i < tablists.length; i++) {
new TabsAutomatic(tablists[i]);
}
});
</script>
</body>
</html>