/
NcAppSidebarTabs.vue
345 lines (317 loc) · 7.81 KB
/
NcAppSidebarTabs.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
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
<!--
- @copyright Copyright (c) 2019 John Molakvoæ <skjnldsv@protonmail.com>
- @copyright Copyright (c) 2020 Simon Belbeoch <simon.belbeoch@gmail.com>
-
- @author John Molakvoæ <skjnldsv@protonmail.com>
-
- @license GNU AGPL version 3 or any later version
-
- This program is free software: you can redistribute it and/or modify
- it under the terms of the GNU Affero General Public License as
- published by the Free Software Foundation, either version 3 of the
- License, or (at your option) any later version.
-
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU Affero General Public License for more details.
-
- You should have received a copy of the GNU Affero General Public License
- along with this program. If not, see <http://www.gnu.org/licenses/>.
-
-->
<!-- Follows the tab aria guidelines
https://www.w3.org/TR/wai-aria-practices/examples/tabs/tabs-1/tabs.html -->
<template>
<div class="app-sidebar-tabs">
<!-- tabs navigation -->
<!-- 33 and 34 code is for page up and page down -->
<nav v-if="hasMultipleTabs"
role="tablist"
class="app-sidebar-tabs__nav"
@keydown.left.exact.prevent="focusPreviousTab"
@keydown.right.exact.prevent="focusNextTab"
@keydown.tab.exact.prevent="focusActiveTabContent"
@keydown.home.exact.prevent="focusFirstTab"
@keydown.end.exact.prevent="focusLastTab"
@keydown.33.exact.prevent="focusFirstTab"
@keydown.34.exact.prevent="focusLastTab">
<ul>
<li v-for="tab in tabs" :key="tab.id" class="app-sidebar-tabs__tab">
<a :id="tab.id"
:aria-controls="`tab-${tab.id}`"
:aria-selected="activeTab === tab.id"
:class="{ active: activeTab === tab.id }"
:data-id="tab.id"
:href="`#tab-${tab.id}`"
:tabindex="activeTab === tab.id ? 0 : -1"
role="tab"
@click.prevent="setActive(tab.id)">
<span class="app-sidebar-tabs__tab-icon">
<NcVNodes :vnodes="tab.renderIcon()" />
</span>
{{ tab.name }}
</a>
</li>
</ul>
</nav>
<!-- tabs content -->
<div :class="{'app-sidebar-tabs__content--multiple': hasMultipleTabs}"
class="app-sidebar-tabs__content">
<!-- @slot Tabs content - NcAppSidebarTab components or any content if there is no tabs -->
<slot />
</div>
</div>
</template>
<script>
import NcVNodes from '../NcVNodes/index.js'
export default {
name: 'NcAppSidebarTabs',
components: {
NcVNodes,
},
provide() {
return {
registerTab: this.registerTab,
unregisterTab: this.unregisterTab,
// Getter as an alternative to Vue 2.7 computed(() => this.activeTab)
getActiveTab: () => this.activeTab,
}
},
props: {
/**
* Id of the tab to activate
*/
active: {
type: String,
default: '',
},
},
emits: ['update:active'],
data() {
return {
/**
* Tab descriptions from the passed NcSidebarTab components' props to build the tab navbar from.
*/
tabs: [],
/**
* Local active (open) tab's ID. It allows to use component without active.sync
*/
activeTab: '',
}
},
computed: {
/**
* Has multiple tabs. If only one tab - its content is shown without navigation
*
* @return {boolean}
*/
hasMultipleTabs() {
return this.tabs.length > 1
},
currentTabIndex() {
return this.tabs.findIndex((tab) => tab.id === this.activeTab)
},
},
watch: {
active(active) {
// Prevent running it twice
if (active !== this.activeTab) {
this.updateActive()
}
},
},
methods: {
/**
* Set the current active tab
*
* @param {string} id the id of the tab
*/
setActive(id) {
this.activeTab = id
/**
* @property {string} active - active tab's id
*/
this.$emit('update:active', this.activeTab)
},
/**
* Focus the previous tab
* and emit to the parent component
*/
focusPreviousTab() {
if (this.currentTabIndex > 0) {
this.setActive(this.tabs[this.currentTabIndex - 1].id)
}
this.focusActiveTab()
},
/**
* Focus the next tab
* and emit to the parent component
*/
focusNextTab() {
if (this.currentTabIndex < this.tabs.length - 1) {
this.setActive(this.tabs[this.currentTabIndex + 1].id)
}
this.focusActiveTab()
},
/**
* Focus the first tab
* and emit to the parent component
*/
focusFirstTab() {
this.setActive(this.tabs[0].id)
this.focusActiveTab()
},
/**
* Focus the last tab
* and emit to the parent component
*/
focusLastTab() {
this.setActive(this.tabs[this.tabs.length - 1].id)
this.focusActiveTab()
},
/**
* Focus the current active tab
*/
focusActiveTab() {
this.$el.querySelector('#' + this.activeTab).focus()
},
/**
* Focus the content on tab
* see aria accessibility guidelines
*/
focusActiveTabContent() {
this.$el.querySelector('#tab-' + this.activeTab).focus()
},
/**
* Update the current active tab
*/
updateActive() {
this.activeTab = this.active
&& this.tabs.some(tab => tab.id === this.active)
? this.active
: this.tabs.length > 0
? this.tabs[0].id
: ''
},
/**
* Register child tab in the tabs
*
* @param {object} tab child tab passed to slot
*/
registerTab(tab) {
this.tabs.push(tab)
this.tabs.sort((a, b) => {
if (a.order === b.order) {
return OC.Util.naturalSortCompare(a.name, b.name)
}
return a.order - b.order
})
if (!this.activeTab) {
this.updateActive()
}
},
/**
* Unregister child tab from the tabs
*
* @param {string} id tab's id
*/
unregisterTab(id) {
const tabIndex = this.tabs.findIndex((tab) => tab.id === id)
if (tabIndex !== -1) {
this.tabs.splice(tabIndex, 1)
}
if (this.activeTab === id) {
this.updateActive()
}
},
},
}
</script>
<style lang="scss" scoped>
.app-sidebar-tabs {
display: flex;
flex-direction: column;
min-height: 0;
flex: 1 1 100%;
&__nav {
margin-top: 10px;
ul {
display: flex;
justify-content: stretch;
}
}
&__tab {
display: block;
flex: 1 1;
min-width: 0;
text-align: center;
a {
position: relative;
display: block;
overflow: hidden;
padding: 25px 5px 5px 5px;
transition: color var(--animation-quick), opacity var(--animation-quick), border-color var(--animation-quick);
text-align: center;
white-space: nowrap;
text-overflow: ellipsis;
opacity: $opacity_normal;
color: var(--color-main-text);
border-bottom: 1px solid var(--color-border);
&:hover,
&:focus,
&:active,
&.active {
opacity: $opacity_full;
.app-sidebar-tabs__tab-icon {
opacity: $opacity_full;
}
}
&:not(.active):hover,
&:not(.active):focus {
border-bottom-color: var(--color-background-darker);
box-shadow: inset 0 -1px 0 var(--color-background-darker);
}
&.active {
color: var(--color-main-text);
border-bottom-color: var(--color-main-text);
box-shadow: inset 0 -1px 0 var(--color-main-text);
font-weight: bold;
}
// differentiate the two for accessibility purpose
// make sure the user knows she's focusing the navigation
// and can use arrows/home/pageup...
&:focus {
border-bottom-color: var(--color-primary-element);
box-shadow: inset 0 -1px 0 var(--color-primary-element);
}
}
}
&__tab-icon {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 25px;
transition: opacity var(--animation-quick);
opacity: $opacity_normal;
& > span {
display: flex;
align-items: center;
justify-content: center;
background-size: 16px;
}
}
&__content {
position: relative;
// take full available height
min-height: 0;
height: 100%;
// force the use of the tab component if more than one tab
// you can just put raw content if you don't use tabs
&--multiple > :not(section) {
display: none;
}
}
}
</style>