-
Notifications
You must be signed in to change notification settings - Fork 90
/
NcAppNavigation.vue
291 lines (257 loc) · 7.1 KB
/
NcAppNavigation.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
<!--
- @copyright Copyright (c) 2019 Christoph Wurst <christoph@winzerhof-wurst.at>
-
- @author Christoph Wurst <christoph@winzerhof-wurst.at>
- @author John Molakvoæ <skjnldsv@protonmail.com>
- @author Marco Ambrosini <marcoambrosini@icloud.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/>.
-
-->
<docs>
The navigation bar can be open and closed from anywhere in the app using the
nextcloud event bus.
### Install the event bus package
```bash
npm i -S @nextcloud/event-bus
```
### Usage
#### Open the navigation
```js static
import { emit } from '@nextcloud/event-bus'
emit('toggle-navigation', {
open: true,
})
```
#### Close the navigation
```js static
import { emit } from '@nextcloud/event-bus'
emit('toggle-navigation', {
open: false,
})
```
</docs>
<template>
<div ref="appNavigationContainer"
class="app-navigation"
:class="{'app-navigation--close':!open }">
<nav id="app-navigation-vue"
:aria-hidden="open ? 'false' : 'true'"
:aria-label="ariaLabel || undefined"
:aria-labelledby="ariaLabelledby || undefined"
class="app-navigation__content"
:inert="!open || undefined"
@keydown.esc="handleEsc">
<slot />
<!-- List for Navigation li-items -->
<ul v-if="$scopedSlots.list" class="app-navigation__list">
<slot name="list" />
</ul>
<!-- Footer for e.g. AppNavigationSettings -->
<slot name="footer" />
</nav>
<NcAppNavigationToggle :open="open" @update:open="toggleNavigation" />
</div>
</template>
<script>
import { useIsMobile } from '../../composables/useIsMobile/index.js'
import { getTrapStack } from '../../utils/focusTrap.js'
import { emit, subscribe, unsubscribe } from '@nextcloud/event-bus'
import { createFocusTrap } from 'focus-trap'
import NcAppNavigationToggle from '../NcAppNavigationToggle/index.js'
import Vue from 'vue'
export default {
name: 'NcAppNavigation',
components: {
NcAppNavigationToggle,
},
// Injected from NcContent
inject: {
setHasAppNavigation: {
default: () => () => Vue.util.warn('NcAppNavigation is not mounted inside NcContent, this is probably an error.'),
from: 'NcContent:setHasAppNavigation',
},
},
props: {
/**
* The aria label to describe the navigation
*/
ariaLabel: {
type: String,
default: '',
},
/**
* aria-labelledby attribute to describe the navigation
*/
ariaLabelledby: {
type: String,
default: '',
},
},
setup() {
return {
isMobile: useIsMobile(),
}
},
data() {
return {
open: !this.isMobile,
focusTrap: null,
}
},
watch: {
isMobile() {
this.open = !this.isMobile
this.toggleFocusTrap()
},
open() {
this.toggleFocusTrap()
},
},
mounted() {
this.setHasAppNavigation(true)
subscribe('toggle-navigation', this.toggleNavigationByEventBus)
// Emit an event with the initial state of the navigation
emit('navigation-toggled', {
open: this.open,
})
this.focusTrap = createFocusTrap(this.$refs.appNavigationContainer, {
allowOutsideClick: true,
fallbackFocus: this.$refs.appNavigationContainer,
trapStack: getTrapStack(),
escapeDeactivates: false,
})
this.toggleFocusTrap()
},
unmounted() {
this.setHasAppNavigation(false)
unsubscribe('toggle-navigation', this.toggleNavigationByEventBus)
this.focusTrap.deactivate()
},
methods: {
/**
* Toggle the navigation
*
* @param {boolean} [state] set the state instead of inverting the current one
*/
toggleNavigation(state) {
// Early return if alreay in that state
if (this.open === state) {
emit('navigation-toggled', {
open: this.open,
})
return
}
this.open = (typeof state === 'undefined') ? !this.open : state
const bodyStyles = getComputedStyle(document.body)
const animationLength = parseInt(bodyStyles.getPropertyValue('--animation-quick')) || 100
setTimeout(() => {
emit('navigation-toggled', {
open: this.open,
})
// We wait for 1.5 times the animation length to give the animation time to really finish.
}, 1.5 * animationLength)
},
toggleNavigationByEventBus({ open }) {
this.toggleNavigation(open)
},
/**
* Activate focus trap if it is currently needed, otherwise deactivate
*/
toggleFocusTrap() {
if (this.isMobile && this.open) {
this.focusTrap.activate()
} else {
this.focusTrap.deactivate()
}
},
handleEsc() {
if (this.isMobile) {
this.toggleNavigation(false)
}
},
},
}
</script>
<style lang="scss">
.app-navigation,
.app-content {
/** Distance of the app naviation toggle and the first navigation item to the top edge of the app content container */
--app-navigation-padding: #{$app-navigation-padding};
}
</style>
<style lang="scss" scoped>
.app-navigation {
// Set scoped variable override
// Using --color-text-maxcontrast as a fallback evaluates to an invalid value as it references itself in this scope instead of the variable defined higher up
--color-text-maxcontrast: var(--color-text-maxcontrast-background-blur, var(--color-text-maxcontrast-default));
transition: transform var(--animation-quick), margin var(--animation-quick);
width: $navigation-width;
// Left toggle button padding + toggle button + right padding from NcAppContent
max-width: calc(100vw - (var(--app-navigation-padding) + var(--default-clickable-area) + var(--default-grid-baseline)));
position: relative;
top: 0;
left: 0;
padding: 0px;
// Above appcontent
z-index: 1800;
height: 100%;
box-sizing: border-box;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
flex-grow: 0;
flex-shrink: 0;
background-color: var(--color-main-background-blur, var(--color-main-background));
-webkit-backdrop-filter: var(--filter-background-blur, none);
backdrop-filter: var(--filter-background-blur, none);
&--close {
transform: translateX(-100%);
position: absolute;
}
&__content > ul,
&__list {
position: relative;
height: 100%;
width: 100%;
overflow-x: hidden;
overflow-y: auto;
box-sizing: border-box;
display: flex;
flex-direction: column;
gap: var(--default-grid-baseline, 4px);
padding: var(--app-navigation-padding);
}
&__content {
height: 100%;
display: flex;
flex-direction: column;
}
}
// add extra border for high contrast mode
[data-themes*='highcontrast'] {
.app-navigation {
border-right: 1px solid var(--color-border);
}
}
// When on mobile, we make the navigation slide over the appcontent
@media only screen and (max-width: $breakpoint-mobile) {
.app-navigation:not(.app-navigation--close) {
position: absolute;
}
}
</style>