-
-
Notifications
You must be signed in to change notification settings - Fork 39
/
VTabs.vue
230 lines (209 loc) · 6.2 KB
/
VTabs.vue
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
<template>
<div :class="['vts-tabs', classes.root]">
<div
role="tablist"
:aria-label="label"
:aria-orientation="orientation"
:class="['vts-tabs__tablist', classes.tablist]"
>
<button
v-for="(tab, index) in tabList"
:id="`${id}-${tab}`"
:key="tab"
:ref="tab"
:aria-selected="index === activeIndex"
:tabindex="index === activeIndex ? null : -1"
:aria-controls="`${id}-${tab.replace('tab', 'panel')}`"
:class="[
`vts-tabs__tab vts-tabs__tab--${tab} vts-tabs__tab--${index}`,
{
'vts-tabs__tab--active': index === activeIndex,
[classes.tabActive]: index === activeIndex,
},
classes.tab,
]"
role="tab"
type="button"
@keydown="onKeydown"
@click="onClick($event, tab, index)"
>
<slot :name="tab" />
</button>
</div>
<div
v-for="(tab, index) in panelList"
:id="`${id}-panel-${index}`"
:key="tab"
:aria-labelledby="`${id}-tab-${index}`"
:hidden="index !== activeIndex"
:class="[
`vts-tabs__panel vts-tabs__panel--${index}`,
{
'vts-tabs__panel--active': index === activeIndex,
[classes.panelActive]: index === activeIndex,
},
classes.panel,
]"
tabindex="0"
role="tabpanel"
>
<slot :name="tab" />
</div>
</div>
</template>
<script>
import { randomString } from '../../utils.js';
import keycodes from '../../data/keycodes.js';
// const NAME = "vts-tabs"
// https://codesandbox.io/embed/vue-tabs-pt5lm?codemirror=1
/**
* Show and hide content based on which tabs are selected.
*
* Implements best practices for accessible tab components based on W3C. Includes HTML5 role attributes (tablist, tab, tabpanel), aria attributes (aria-label, aria-selected, aria-controls, aria-labelledby), and ideal keyboard navigation.
*
* Keyboard navigation to the tabs only targets active tab. `right` key activates next tab (horizontal orientation) or loops around to start. `left` key activates previous tab (horizontal orientation) or loops around to end. `down` key activates next tab (vertical orientation) or loops around to start. `down` key activates previous tab (vertical orientation) or loops around to end. (in horizontal orientation), `home` key activates first tab. `end` key activates last tab.
*/
export default {
name: 'VTabs',
model: {
prop: 'active',
event: 'change',
},
props: {
active: {
type: Number,
default: 0,
},
label: {
type: String,
default: undefined,
},
orientation: {
type: /** @type {import('vue').PropType<'horizontal'|'vertical'>} */ (
String
),
default: 'horizontal',
},
classes: {
type: Object,
default: () => ({}),
},
},
data() {
return {
activeIndex: this.active,
activeTab: this.active,
};
},
computed: {
/** @returns {Array} */
tabList() {
return Object.keys(this.$slots).filter((name) => name.startsWith('tab-'));
},
/** @returns {Array} */
panelList() {
return Object.keys(this.$slots).filter((name) =>
name.startsWith('panel-')
);
},
},
watch: {
active(next) {
this.activeIndex = Math.max(0, Math.min(this.tabList.length - 1, next));
},
activeIndex(next) {
this.$emit('change', next);
},
},
created() {
this.id = this.$attrs.id || `vts-${randomString(4)}`;
},
methods: {
onClick(event, tab, index) {
if (this.activeIndex !== index) {
this.$nextTick(() => this.$emit('tabChange', { event, tab, index }));
}
this.activeIndex = index;
},
onKeydown(event) {
const { keyCode } = event;
const oldIndex = this.activeIndex;
switch (keyCode) {
case keycodes.END:
event.preventDefault();
this.activeIndex = this.tabList.length - 1;
this.setFocus();
break;
case keycodes.HOME:
event.preventDefault();
this.activeIndex = 0;
this.setFocus();
break;
// Up and down are in keydown because we need to prevent page scroll >:)
case keycodes.LEFT:
case keycodes.RIGHT:
case keycodes.UP:
case keycodes.DOWN:
this.determineOrientation(event);
break;
}
if (oldIndex !== this.activeIndex) {
this.$nextTick(() =>
this.$emit('tabChange', {
event,
tab: this.tablist[this.activeIndex],
index: this.activeIndex,
})
);
}
},
// When a tabList's aria-orientation is set to vertical, only up and down arrow should function. In all other cases only left and right arrow function.
determineOrientation(event) {
const keyCode = event.keyCode;
let proceed = false;
if (this.orientation === 'vertical') {
if (keyCode === keycodes.UP || keyCode === keycodes.DOWN) {
event.preventDefault();
proceed = true;
}
} else {
if (keyCode === keycodes.LEFT || keyCode === keycodes.RIGHT) {
event.preventDefault();
proceed = true;
}
}
if (proceed) {
this.switchTabOnArrowPress(event);
this.setFocus();
}
},
// Either focus the next, previous, first, or last tab depening on key pressed
switchTabOnArrowPress(event) {
const keyCode = event.keyCode;
const directions = {
[keycodes.LEFT]: -1,
[keycodes.UP]: -1,
[keycodes.RIGHT]: 1,
[keycodes.DOWN]: 1,
};
/* istanbul ignore next */
if (!directions[keyCode]) return;
const activeIndex = this.activeIndex;
const tabLength = this.tabList.length;
const nextIndex = activeIndex + directions[keyCode];
if (nextIndex < 0) {
this.activeIndex = tabLength - 1;
} else if (nextIndex >= tabLength) {
this.activeIndex = 0;
} else {
this.activeIndex = nextIndex;
}
},
setFocus() {
const { $refs, tabList, activeIndex } = this;
const activeTab = tabList[activeIndex];
return $refs[activeTab][0].focus();
},
},
};
</script>