Skip to content

Commit a8ce645

Browse files
edisdevkefranabg
authored andcommittedSep 15, 2019
fix($theme-default): Make navbar dropdown links accessible (#1837)
1 parent 3a22fd1 commit a8ce645

File tree

12 files changed

+61
-10
lines changed

12 files changed

+61
-10
lines changed
 

‎packages/@vuepress/theme-default/__tests__/components/DropdownLink.spec.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import { createLocalVue } from '@vuepress/test-utils/client'
55
describe('DropdownLink', () => {
66
test('renders dropdown link.', () => {
77
const item = {
8-
text: 'VuePress',
8+
text: 'Learn More',
9+
ariaLabel: 'Learn More Select',
910
items: [
1011
{
1112
text: 'Guide',

‎packages/@vuepress/theme-default/__tests__/components/__snapshots__/DropdownLink.spec.js.snap

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

33
exports[`DropdownLink renders dropdown link. 1`] = `
4-
<div class="dropdown-wrapper"><a class="dropdown-title"><span class="title">VuePress</span> <span class="arrow right"></span></a>
4+
<div class="dropdown-wrapper"><button type="button" aria-label="Learn More Select" class="dropdown-title"><span class="title">Learn More</span> <span class="arrow right"></span></button>
55
<ul class="nav-dropdown" style="display: none;" name="dropdown">
66
<li class="dropdown-item">
77
<!----> <a class="nav-link">Guide</a></li>

‎packages/@vuepress/theme-default/components/DropdownLink.vue

+39-4
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,18 @@
33
class="dropdown-wrapper"
44
:class="{ open }"
55
>
6-
<a
6+
<button
77
class="dropdown-title"
8+
type="button"
9+
:aria-label="dropdownAriaLabel"
810
@click="toggle"
911
>
1012
<span class="title">{{ item.text }}</span>
1113
<span
1214
class="arrow"
1315
:class="open ? 'down' : 'right'"
1416
></span>
15-
</a>
17+
</button>
1618

1719
<DropdownTransition>
1820
<ul
@@ -35,12 +37,19 @@
3537
:key="childSubItem.link"
3638
v-for="childSubItem in subItem.items"
3739
>
38-
<NavLink :item="childSubItem"/>
40+
<NavLink
41+
@focusout="
42+
isLastItemOfArray(childSubItem, subItem.items) &&
43+
isLastItemOfArray(subItem, item.items) &&
44+
toggle()
45+
"
46+
:item="childSubItem"/>
3947
</li>
4048
</ul>
4149

4250
<NavLink
4351
v-else
52+
@focusout="isLastItemOfArray(subItem, item.items) && toggle()"
4453
:item="subItem"
4554
/>
4655
</li>
@@ -52,6 +61,7 @@
5261
<script>
5362
import NavLink from '@theme/components/NavLink.vue'
5463
import DropdownTransition from '@theme/components/DropdownTransition.vue'
64+
import last from 'lodash/last'
Has a conversation. Original line has a conversation.
5565
5666
export default {
5767
components: { NavLink, DropdownTransition },
@@ -68,9 +78,26 @@ export default {
6878
}
6979
},
7080
81+
computed: {
82+
83+
dropdownAriaLabel () {
84+
return this.item.ariaLabel || this.item.text
85+
}
86+
},
87+
7188
methods: {
7289
toggle () {
7390
this.open = !this.open
91+
},
92+
93+
isLastItemOfArray (item, array) {
94+
return last(array) === item
95+
}
96+
},
97+
98+
watch: {
99+
$route () {
100+
this.open = false
74101
}
75102
}
76103
}
@@ -81,6 +108,11 @@ export default {
81108
cursor pointer
82109
.dropdown-title
83110
display block
111+
font-size 0.9rem
112+
background transparent
113+
border none
114+
font-weight 500
115+
color $textColor
84116
&:hover
85117
border-color transparent
86118
.arrow
@@ -149,9 +181,12 @@ export default {
149181
@media (min-width: $MQMobile)
150182
.dropdown-wrapper
151183
height 1.8rem
152-
&:hover .nav-dropdown
184+
&:hover .nav-dropdown,
185+
&.open .nav-dropdown
153186
// override the inline style.
154187
display block !important
188+
&.open:blur
189+
display none
155190
.dropdown-title .arrow
156191
// make the arrow always down at desktop
157192
border-left 4px solid transparent

‎packages/@vuepress/theme-default/components/NavLink.vue

+6-1
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22
<router-link
33
class="nav-link"
44
:to="link"
5+
@focusout.native="focusoutAction"
56
v-if="!isExternal(link)"
67
:exact="exact"
78
>{{ item.text }}</router-link>
89
<a
910
v-else
1011
:href="link"
12+
@focusout="focusoutAction"
1113
class="nav-link external"
1214
:target="isMailto(link) || isTel(link) ? null : '_blank'"
1315
:rel="isMailto(link) || isTel(link) ? null : 'noopener noreferrer'"
@@ -43,7 +45,10 @@ export default {
4345
methods: {
4446
isExternal,
4547
isMailto,
46-
isTel
48+
isTel,
49+
focusoutAction () {
50+
this.$emit('focusout')
51+
}
4752
}
4853
}
4954
</script>

‎packages/@vuepress/theme-default/components/NavLinks.vue

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ export default {
5454
const themeLocales = this.$site.themeConfig.locales || {}
5555
const languageDropdown = {
5656
text: this.$themeLocaleConfig.selectText || 'Languages',
57+
ariaLabel: this.$themeLocaleConfig.ariaLabel || 'Select language',
5758
items: Object.keys(locales).map(path => {
5859
const locale = locales[path]
5960
const text = themeLocales[path] && themeLocales[path].label || locale.lang

‎packages/docs/docs/.vuepress/config.js

+2
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ module.exports = ctx => ({
3939
'/': {
4040
label: 'English',
4141
selectText: 'Languages',
42+
ariaLabel: 'Select language',
4243
editLinkText: 'Edit this page on GitHub',
4344
lastUpdated: 'Last Updated',
4445
nav: require('./nav/en'),
@@ -52,6 +53,7 @@ module.exports = ctx => ({
5253
'/zh/': {
5354
label: '简体中文',
5455
selectText: '选择语言',
56+
ariaLabel: "选择语言",
5557
editLinkText: '在 GitHub 上编辑此页',
5658
lastUpdated: '上次更新',
5759
nav: require('./nav/zh'),

‎packages/docs/docs/.vuepress/nav/en.js

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ module.exports = [
1717
},
1818
{
1919
text: "Learn More",
20+
ariaLabel: "Learn More",
2021
items: [
2122
{
2223
text: "API",

‎packages/docs/docs/.vuepress/nav/zh.js

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ module.exports = [
1717
},
1818
{
1919
text: "了解更多",
20+
ariaLabel: "了解更多",
2021
items: [
2122
{
2223
text: "API",

‎packages/docs/docs/guide/i18n.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ module.exports = {
5454
selectText: 'Languages',
5555
// label for this locale in the language dropdown
5656
label: 'English',
57+
// Aria Label for locale in the dropdown
58+
ariaLabel: 'Languages'
5759
// text for the edit-on-github link
5860
editLinkText: 'Edit this page on GitHub',
5961
// config for Service Worker
@@ -66,7 +68,7 @@ module.exports = {
6668
// algolia docsearch options for current locale
6769
algolia: {},
6870
nav: [
69-
{ text: 'Nested', link: '/nested/' }
71+
{ text: 'Nested', link: '/nested/' , ariaLabel: 'Nested' }
7072
],
7173
sidebar: {
7274
'/': [/* ... */],

‎packages/docs/docs/theme/default-theme-config.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ module.exports = {
4848
nav: [
4949
{ text: 'Home', link: '/' },
5050
{ text: 'Guide', link: '/guide/' },
51-
{ text: 'External', link: 'https://google.com' },
51+
{ text: 'External', link: 'https://google.com' }
5252
]
5353
}
5454
}
@@ -62,6 +62,7 @@ module.exports = {
6262
nav: [
6363
{
6464
text: 'Languages',
65+
ariaLabel: 'Language Menu',
6566
items: [
6667
{ text: 'Chinese', link: '/language/chinese/' },
6768
{ text: 'Japanese', link: '/language/japanese/' }

‎packages/docs/docs/zh/guide/i18n.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ module.exports = {
5252
'/': {
5353
selectText: 'Languages',
5454
label: 'English',
55+
ariaLabel: 'Languages'
5556
editLinkText: 'Edit this page on GitHub',
5657
serviceWorker: {
5758
updatePopup: {
@@ -61,7 +62,7 @@ module.exports = {
6162
},
6263
algolia: {},
6364
nav: [
64-
{ text: 'Nested', link: '/nested/' }
65+
{ text: 'Nested', link: '/nested/', ariaLabel: 'Nested' }
6566
],
6667
sidebar: {
6768
'/': [/* ... */],

‎packages/docs/docs/zh/theme/default-theme-config.md

+1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ module.exports = {
5858
nav: [
5959
{
6060
text: 'Languages',
61+
ariaLabel: 'Language Menu',
6162
items: [
6263
{ text: 'Chinese', link: '/language/chinese/' },
6364
{ text: 'Japanese', link: '/language/japanese/' }

0 commit comments

Comments
 (0)
Please sign in to comment.