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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix($theme-default): Make navbar dropdown links accessible #1837

Merged
merged 18 commits into from
Sep 15, 2019
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe('DropdownLink', () => {
stubs: {
'router-link': RouterLinkStub
},
propsData: { item }
propsData: { item, dropdownName: 'Languages' }
})
expect(wrapper.html()).toMatchSnapshot()
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`DropdownLink renders dropdown link. 1`] = `
<div class="dropdown-wrapper"><a class="dropdown-title"><span class="title">VuePress</span> <span class="arrow right"></span></a>
<div class="dropdown-wrapper"><button type="button" aria-label="Languages" class="dropdown-title"><span class="title">VuePress</span> <span class="arrow right"></span></button>
<ul class="nav-dropdown" style="display: none;" name="dropdown">
<li class="dropdown-item">
<!----> <a class="nav-link">Guide</a></li>
Expand Down
45 changes: 40 additions & 5 deletions packages/@vuepress/theme-default/components/DropdownLink.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@
class="dropdown-wrapper"
:class="{ open }"
>
<a
<button
class="dropdown-title"
type="button"
:aria-label="dropdownName"
@click="toggle"
>
<span class="title">{{ item.text }}</span>
<span
class="arrow"
:class="open ? 'down' : 'right'"
></span>
</a>
</button>

<DropdownTransition>
<ul
Expand All @@ -33,14 +35,17 @@
<li
class="dropdown-subitem"
:key="childSubItem.link"
v-for="childSubItem in subItem.items"
v-for="(childSubItem, subIndex) in subItem.items"
>
<NavLink :item="childSubItem"/>
<NavLink
@keypressTab="focusoutDropdown((index + 2) * (subIndex + 1))"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand the + 2? Could you explain? Why not just + 1?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm sorry, There was a bug here. I resolved this. 👍

:item="childSubItem"/>
</li>
</ul>

<NavLink
v-else
@keypressTab="focusoutDropdown(index + 1)"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that focusoutDropdown is a correct name for what the function is doing, right?

:item="subItem"
/>
</li>
Expand All @@ -65,12 +70,34 @@ export default {
props: {
item: {
required: true
},
dropdownName: {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why using a props for the dropdown name? Why don't we just keep it as a local variable in the component? Btw I think we should consider a way to make this label customizable. The user might want to display different labels depending on the current selected language. This should be a default theme local. Could you add this feature and update the documentation?

default: 'Dropdown',
type: String
}
},

computed: {
AllItemsCount () {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be named with a lower-case a

return this.item.items.reduce((totalCount, item) => {
return item.items ? totalCount + item.items.length : totalCount + 1
}, 0)
}
},

methods: {
toggle () {
this.open = !this.open
},

focusoutDropdown (index) {
if (this.AllItemsCount === index) this.open = false
}
},

watch: {
$route () {
this.open = false
}
}
}
Expand All @@ -81,6 +108,11 @@ export default {
cursor pointer
.dropdown-title
display block
font-size 0.9rem
background transparent
border none
font-weight 500
color $textColor
&:hover
border-color transparent
.arrow
Expand Down Expand Up @@ -149,9 +181,12 @@ export default {
@media (min-width: $MQMobile)
.dropdown-wrapper
height 1.8rem
&:hover .nav-dropdown
&:hover .nav-dropdown,
&.open .nav-dropdown
// override the inline style.
display block !important
&.open:blur
display none
.dropdown-title .arrow
// make the arrow always down at desktop
border-left 4px solid transparent
Expand Down
7 changes: 6 additions & 1 deletion packages/@vuepress/theme-default/components/NavLink.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
<router-link
class="nav-link"
:to="link"
@keyup.tab.native="keypressTab"
v-if="!isExternal(link)"
:exact="exact"
>{{ item.text }}</router-link>
<a
v-else
:href="link"
@keyup.tab.prevent="keypressTab"
class="nav-link external"
:target="isMailto(link) || isTel(link) ? null : '_blank'"
:rel="isMailto(link) || isTel(link) ? null : 'noopener noreferrer'"
Expand Down Expand Up @@ -43,7 +45,10 @@ export default {
methods: {
isExternal,
isMailto,
isTel
isTel,
keypressTab () {
this.$emit('keypressTab')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you rename this event keypress-tab. Other events names in the vuepress code uses - syntax.

}
}
}
</script>
1 change: 1 addition & 0 deletions packages/@vuepress/theme-default/components/NavLinks.vue
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<DropdownLink
v-if="item.type === 'links'"
:item="item"
dropdownName="Select Dropdown"
/>
<NavLink
v-else
Expand Down
2 changes: 1 addition & 1 deletion packages/docs/docs/theme/default-theme-config.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ module.exports = {
nav: [
{ text: 'Home', link: '/' },
{ text: 'Guide', link: '/guide/' },
{ text: 'External', link: 'https://google.com' },
{ text: 'External', link: 'https://google.com' }
]
}
}
Expand Down