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

feat(core): add aria-current to active links (close #2116) #3073

Merged
merged 5 commits into from
Mar 20, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
7 changes: 7 additions & 0 deletions docs/api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,13 @@ If you add a `target="_blank"` to your `a` element, you must omit the `@click="n

Configure the active CSS class applied when the link is active with exact match. Note the default value can also be configured globally via the `linkExactActiveClass` router constructor option.

### aria-current-value

- type: `'page' | 'step' | 'location' | 'date' | 'time'`
- default: `"page"`

Configure the value of `aria-current` when the link is active with exact match. It must be one of the [allowed values for aria-current](https://www.w3.org/TR/wai-aria-1.2/#aria-current) in the ARIA spec. In most cases, the default of `page` should be the best fit.

## `<router-view>`

The `<router-view>` component is a functional component that renders the matched component for the given path. Components rendered in `<router-view>` can also contain their own `<router-view>`, which will render components for nested paths.
Expand Down
9 changes: 8 additions & 1 deletion src/components/link.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ export default {
replace: Boolean,
activeClass: String,
exactActiveClass: String,
ariaCurrentValue: {
type: String,
default: 'page'
},
event: {
type: eventTypes,
default: 'click'
Expand Down Expand Up @@ -67,6 +71,8 @@ export default {
? classes[exactActiveClass]
: isIncludedRoute(current, compareTarget)

const ariaCurrentValue = classes[exactActiveClass] ? this.ariaCurrentValue : null

const handler = e => {
if (guardEvent(e)) {
if (this.replace) {
Expand Down Expand Up @@ -117,7 +123,7 @@ export default {

if (this.tag === 'a') {
data.on = on
data.attrs = { href }
data.attrs = { href, 'aria-current': ariaCurrentValue }
} else {
// find the first <a> child and apply listener and href
const a = findAnchor(this.$slots.default)
Expand Down Expand Up @@ -145,6 +151,7 @@ export default {

const aAttrs = (a.data.attrs = extend({}, a.data.attrs))
aAttrs.href = href
aAttrs['aria-current'] = ariaCurrentValue
} else {
// doesn't have <a> child, apply listener to self
data.on = on
Expand Down
3 changes: 3 additions & 0 deletions test/e2e/specs/active-links.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ module.exports = {
.assert.attributeContains('li:nth-child(18) a', 'href', '/active-links/redirect-image')
.assert.attributeContains('li:nth-child(19) a', 'href', '/active-links/redirect-image')
.assert.containsText('.view', 'Home')
.assert.not.attributeEquals(`li:nth-child(3) a`, 'aria-current', 'page')

assertActiveLinks(1, [1, 2], null, [1, 2])
assertActiveLinks(2, [1, 2], null, [1, 2])
Expand Down Expand Up @@ -70,12 +71,14 @@ module.exports = {
browser.assert
.cssClassPresent(`li:nth-child(${i}) a`, 'router-link-exact-active')
.assert.cssClassPresent(`li:nth-child(${i}) a`, 'router-link-active')
.assert.attributeEquals(`li:nth-child(${i}) a`, 'aria-current', 'page')
posva marked this conversation as resolved.
Show resolved Hide resolved
})
exactActiveLI &&
exactActiveLI.forEach(i => {
browser.assert
.cssClassPresent(`li:nth-child(${i})`, 'router-link-exact-active')
.assert.cssClassPresent(`li:nth-child(${i})`, 'router-link-active')
.assert.attributeEquals(`li:nth-child(${i}) a`, 'aria-current', 'page')
})
}
}
Expand Down