Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

enh(a11y): Add nav to header menu #4487

Merged
merged 2 commits into from Sep 14, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
47 changes: 41 additions & 6 deletions src/components/NcHeaderMenu/NcHeaderMenu.vue
Expand Up @@ -64,14 +64,18 @@ export default {
</docs>

<template>
<div :id="id"
<component :is="wrapperTag"
:id="id"
v-click-outside="clickOutsideConfig"
:aria-labelledby="isNav ? triggerId : null"
:class="{ 'header-menu--opened': opened }"
class="header-menu">
<!-- Trigger -->
<button ref="trigger"
<button :id="triggerId"
ref="trigger"
class="header-menu__trigger button-vue"
:aria-label="ariaLabel"
:aria-describedby="description ? descriptionId : null"
:aria-controls="`header-menu-${id}`"
:aria-expanded="opened.toString()"
@click.prevent="toggleMenu">
Expand All @@ -80,26 +84,32 @@ export default {
<slot name="trigger" />
</button>

<span v-if="description"
:id="descriptionId"
class="header-menu__description hidden-visually">
{{ description }}
</span>

<!-- Visual triangle -->
<div v-show="opened" class="header-menu__carret" />

<!-- Menu opened content -->
<div v-show="opened"
:id="`header-menu-${id}`"
class="header-menu__wrapper"
role="menu">
class="header-menu__wrapper">
<div ref="content" class="header-menu__content">
<!-- @slot Main content -->
<slot />
</div>
</div>
</div>
</component>
</template>

<script>
import { vOnClickOutside as ClickOutside } from '@vueuse/components'
import { createFocusTrap } from 'focus-trap'

import GenRandomId from '../../utils/GenRandomId.js'
import { clickOutsideOptions } from '../../mixins/index.js'
import { getTrapStack } from '../../utils/focusTrap.js'

Expand Down Expand Up @@ -138,6 +148,26 @@ export default {
type: Boolean,
default: false,
},

/**
* Pass `true` if the header menu is used for website navigation
*
* The wrapper tag will be set to `nav` and its `aria-labelledby`
* will be associated with the menu open button
*/
isNav: {
type: Boolean,
default: false,
},

/**
* Additional visually hidden description text for the menu
* open button
*/
description: {
type: String,
default: null,
},
},

emits: [
Expand All @@ -154,10 +184,16 @@ export default {
focusTrap: null,
opened: this.open,
shortcutsDisabled: window.OCP?.Accessibility?.disableKeyboardShortcuts?.(),
triggerId: GenRandomId(),
descriptionId: GenRandomId(),
}
},

computed: {
wrapperTag() {
return this.isNav ? 'nav' : 'div'
},

clickOutsideConfig() {
return [
this.closeMenu,
Expand Down Expand Up @@ -360,5 +396,4 @@ $externalMargin: 8px;
}
}
}

</style>