-
Notifications
You must be signed in to change notification settings - Fork 96
Expand file tree
/
Copy pathNcAppNavigation.vue
More file actions
427 lines (376 loc) Ā· 10.2 KB
/
NcAppNavigation.vue
File metadata and controls
427 lines (376 loc) Ā· 10.2 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
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
<!--
- SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
- SPDX-License-Identifier: AGPL-3.0-or-later
-->
<docs>
```vue
<template>
<div class="styleguide-nc-content">
<NcAppNavigation>
<template #search>
<div class="navigation__header">
<NcAppNavigationSearch v-model="searchValue" label="SearchĀ ā¦" />
<NcActions>
<NcActionButton close-after-click @click="showModal = true">
<template #icon>
<IconCogOutline />
</template>
App settings (close after click)
</NcActionButton>
<NcActionButton @click="showModal = true">
<template #icon>
<IconCogOutline />
</template>
App settings (handle only click)
</NcActionButton>
</NcActions>
</div>
</template>
<template #list>
<NcAppNavigationItem v-for="item in items" :key="item" :name="item">
<template #icon>
<IconCheck :size="20" />
</template>
</NcAppNavigationItem>
</template>
<template #footer>
<div class="navigation__footer">
<NcButton wide @click="showModal = true">
<template #icon>
<IconCogOutline />
</template>
App settings
</NcButton>
<NcModal v-if="showModal" name="Modal for focus-trap check" @close="showModal = false">
<div class="modal-content">
<h4>Focus-trap should be locked inside the modal</h4>
<NcTextField v-model="modalValue" label="Focus me" />
</div>
</NcModal>
</div>
</template>
</NcAppNavigation>
</div>
</template>
<script>
import IconCheck from 'vue-material-design-icons/Check'
import IconCogOutline from 'vue-material-design-icons/CogOutline'
export default {
components: {
IconCheck,
IconCogOutline,
},
provide() {
return {
'NcContent:setHasAppNavigation': () => {},
}
},
data() {
return {
items: Array.from({ length: 5 }, (v, i) => `Item ${i+1}`),
searchValue: '',
modalValue: '',
showModal: false,
}
},
}
</script>
<style scoped>
/* Mock NcContent */
.styleguide-nc-content {
position: relative;
height: 300px;
background-color: var(--color-background-plain);
overflow: hidden;
}
.navigation__header,
.navigation__footer {
display: flex;
align-items: center;
gap: 4px;
padding: 4px;
}
.modal-content {
height: 120px;
padding: 10px;
}
</style>
```
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>
<script setup lang="ts">
import type { FocusTrap } from 'focus-trap'
import type { Slot } from 'vue'
import { emit, subscribe, unsubscribe } from '@nextcloud/event-bus'
import { createFocusTrap } from 'focus-trap'
import { inject, onMounted, onUnmounted, ref, useTemplateRef, warn, watch, watchEffect } from 'vue'
import NcAppNavigationList from '../NcAppNavigationList/NcAppNavigationList.vue'
import NcAppNavigationToggle from './NcAppNavigationToggle.vue'
import { useIsMobile } from '../../composables/useIsMobile/index.ts'
import { getTrapStack } from '../../utils/focusTrap.ts'
import { HAS_APP_NAVIGATION_KEY } from '../NcContent/constants.ts'
const props = defineProps<{
/**
* The aria label to describe the navigation
*/
ariaLabel?: string
/**
* aria-labelledby attribute to describe the navigation
*/
ariaLabelledby?: string
}>()
defineSlots<{
/**
* The main content of the navigation.
* If no list is passed to the `#list` slot, stretched vertically.
*/
default?: Slot
/**
* Footer for e.g. `NcAppNavigationSettings`
*/
footer?: Slot
/**
* List for Navigation list items.
* Stretched between the main content and the footer
*/
list?: Slot
/**
* For in-app search you can pass a `NcAppNavigationSearch` component as the slot content.
*/
search?: Slot
}>()
let focusTrap: FocusTrap
const setHasAppNavigation = inject(
HAS_APP_NAVIGATION_KEY,
() => warn('NcAppNavigation is not mounted inside NcContent, this is probably an error.'),
false,
)
const appNavigationContainerElement = useTemplateRef('appNavigationContainer')
const isMobile = useIsMobile()
const open = ref(!isMobile.value)
watchEffect(() => {
if (!props.ariaLabel && !props.ariaLabelledby) {
warn('NcAppNavigation requires either `ariaLabel` or `ariaLabelledby` to be set for accessibility.')
}
})
watch(isMobile, () => {
open.value = !isMobile.value
})
watch(open, () => {
toggleFocusTrap()
})
onMounted(() => {
setHasAppNavigation(true)
subscribe('toggle-navigation', toggleNavigationByEventBus)
// Emit an event with the initial state of the navigation
emit('navigation-toggled', {
open: open.value,
})
focusTrap = createFocusTrap(appNavigationContainerElement.value!, {
allowOutsideClick: true,
clickOutsideDeactivates: () => {
if (isMobile.value) {
focusTrap.deactivate({ returnFocus: false })
toggleNavigation(false)
}
return false
},
fallbackFocus: appNavigationContainerElement.value!,
trapStack: getTrapStack(),
escapeDeactivates: false,
})
toggleFocusTrap()
})
onUnmounted(() => {
setHasAppNavigation(false)
unsubscribe('toggle-navigation', toggleNavigationByEventBus)
focusTrap.deactivate()
})
/**
* Toggle the navigation
*
* @param state set the state instead of inverting the current one
*/
function toggleNavigation(state?: boolean): void {
// Early return if already in that state
if (open.value === state) {
emit('navigation-toggled', {
open: open.value,
})
return
}
open.value = state === undefined ? !open.value : state
const bodyStyles = getComputedStyle(document.body)
const animationLength = parseInt(bodyStyles.getPropertyValue('--animation-quick')) || 100
setTimeout(() => {
emit('navigation-toggled', {
open: open.value,
})
// We wait for 1.5 times the animation length to give the animation time to really finish.
}, 1.5 * animationLength)
}
/**
* Handler for the event-bus navigation event.
*
* @param context - The event bus context
* @param context.open - The new navigation open state
*/
function toggleNavigationByEventBus({ open }: { open: boolean }): void {
return toggleNavigation(open)
}
/**
* Activate focus trap if it is currently needed, otherwise deactivate
*/
function toggleFocusTrap(): void {
if (isMobile.value && open.value) {
focusTrap.activate()
} else {
focusTrap.deactivate()
}
}
/**
* Handle hotkey for closing the navigation.
*/
function handleEsc(): void {
if (isMobile.value) {
toggleNavigation(false)
}
}
</script>
<template>
<div
ref="appNavigationContainer"
class="app-navigation"
:class="{ 'app-navigation--closed': !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">
<div class="app-navigation__search">
<slot name="search" />
</div>
<div class="app-navigation__body" :class="{ 'app-navigation__body--no-list': !$slots.list }">
<slot />
</div>
<NcAppNavigationList v-if="$slots.list" class="app-navigation__list">
<slot name="list" />
</NcAppNavigationList>
<slot name="footer" />
</nav>
<NcAppNavigationToggle :open @update:open="toggleNavigation" />
</div>
</template>
<style lang="scss">
.app-navigation,
.app-content {
/** Distance of the app navigation 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
--app-navigation-max-width: calc(100vw - (var(--app-navigation-padding) + var(--default-clickable-area) + var(--default-grid-baseline)));
max-width: var(--app-navigation-max-width);
position: relative;
top: 0;
inset-inline-start: 0;
padding: 0px;
// Above NcAppContent
z-index: 1800;
height: 100%;
-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);
&--closed {
margin-inline-start: calc(-1 * min($navigation-width, var(--app-navigation-max-width)));
}
&__search {
width: 100%;
}
&__body {
overflow-y: scroll;
}
// For legacy purposes support passing a bare list to the content in #default slot and including #footer slot
// Same styles as NcAppNavigationList
&__content > ul {
position: relative;
width: 100%;
overflow-x: hidden;
overflow-y: auto;
display: flex;
flex-direction: column;
gap: var(--default-grid-baseline, 4px);
padding: var(--app-navigation-padding);
}
// Always stretch the navigation list
& &__list {
height: 100%;
}
// Stretch the main content if there is no stretched list
&__body--no-list {
flex: 1 1 auto;
overflow: auto;
height: 100%;
}
&__content {
height: 100%;
display: flex;
flex-direction: column;
}
}
// Add extra border for high contrast mode
[data-themes*="highcontrast"] {
.app-navigation {
border-inline-end: 1px solid var(--color-border);
}
}
// When on mobile, we make the navigation slide over the NcAppContent
@media only screen and (max-width: $breakpoint-mobile) {
.app-navigation {
position: absolute;
border-inline-end: 1px solid var(--color-border);
}
}
// Put the toggle behind NcAppSidebar on small screens
@media only screen and (max-width: $breakpoint-small-mobile) {
.app-navigation {
z-index: 1400;
}
}
</style>