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(a11y): add aria-current="page" attribute to links #20413

Merged
merged 3 commits into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -55,9 +55,8 @@ export const VBreadcrumbsItem = genericComponent()({
{ !link.isLink.value ? slots.default?.() ?? props.title : (
<a
class="v-breadcrumbs-item--link"
href={ link.href.value }
aria-current={ link['aria-current']?.value }
onClick={ link.navigate }
{ ...link.linkProps }
>
{ slots.default?.() ?? props.title }
</a>
Expand Down
3 changes: 1 addition & 2 deletions packages/vuetify/src/components/VBtn/VBtn.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -212,11 +212,10 @@ export const VBtn = genericComponent<VBtnSlots>()({
]}
aria-busy={ props.loading ? true : undefined }
disabled={ isDisabled.value || undefined }
href={ link.href.value }
aria-current={ link['aria-current']?.value }
tabindex={ props.loading || props.readonly ? -1 : undefined }
onClick={ onClick }
value={ valueAttr.value }
{ ...link.linkProps }
>
{ genOverlays(true, 'v-btn') }

Expand Down
3 changes: 1 addition & 2 deletions packages/vuetify/src/components/VCard/VCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,10 @@ export const VCard = genericComponent<VCardSlots>()({
locationStyles.value,
props.style,
]}
href={ link.href.value }
aria-current={ link['aria-current']?.value }
onClick={ isClickable.value && link.navigate }
v-ripple={ isClickable.value && props.ripple }
tabindex={ props.disabled ? -1 : undefined }
{ ...link.linkProps }
>
{ hasImage && (
<div key="image" class="v-card__image">
Expand Down
3 changes: 1 addition & 2 deletions packages/vuetify/src/components/VChip/VChip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -204,12 +204,11 @@ export const VChip = genericComponent<VChipSlots>()({
]}
disabled={ props.disabled || undefined }
draggable={ props.draggable }
href={ link.href.value }
aria-current={ link['aria-current']?.value }
tabindex={ isClickable.value ? 0 : undefined }
onClick={ onClick }
onKeydown={ isClickable.value && !isLink.value && onKeyDown }
v-ripple={[isClickable.value && props.ripple, null]}
{ ...link.linkProps }
>
{ genOverlays(isClickable.value, 'v-chip') }

Expand Down
3 changes: 1 addition & 2 deletions packages/vuetify/src/components/VList/VListItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -243,12 +243,11 @@ export const VListItem = genericComponent<VListItemSlots>()({
dimensionStyles.value,
props.style,
]}
href={ link.href.value }
aria-current={ link['aria-current']?.value }
tabindex={ isClickable.value ? (list ? -2 : 0) : undefined }
onClick={ onClick }
onKeydown={ isClickable.value && !isLink.value && onKeyDown }
v-ripple={ isClickable.value && props.ripple }
{ ...link.linkProps }
>
{ genOverlays(isClickable.value || isActive.value, 'v-list-item') }

Expand Down
16 changes: 11 additions & 5 deletions packages/vuetify/src/composables/router.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import {
computed,
nextTick,
onScopeDispose,
onScopeDispose, reactive,
resolveDynamicComponent,
toRef,
} from 'vue'
Expand Down Expand Up @@ -47,7 +47,7 @@ export interface UseLink extends Omit<Partial<ReturnType<typeof _useLink>>, 'hre
isLink: ComputedRef<boolean>
isClickable: ComputedRef<boolean>
href: Ref<string | undefined>
'aria-current'?: ComputedRef<'page' | undefined>
linkProps: Record<string, string | undefined>
}

export function useLink (props: LinkProps & LinkListeners, attrs: SetupContext['attrs']): UseLink {
Expand All @@ -59,10 +59,12 @@ export function useLink (props: LinkProps & LinkListeners, attrs: SetupContext['
})

if (typeof RouterLink === 'string' || !('useLink' in RouterLink)) {
const href = toRef(props, 'href')
return {
isLink,
isClickable,
href: toRef(props, 'href'),
href,
linkProps: reactive({ href }),
}
}
// vue-router useLink `to` prop needs to be reactive and useLink will crash if undefined
Expand All @@ -82,15 +84,19 @@ export function useLink (props: LinkProps & LinkListeners, attrs: SetupContext['

return link.value.isExactActive?.value && deepEqual(link.value.route.value.query, route.value.query)
})
const href = computed(() => props.to ? link.value?.route.value.href : props.href)

return {
isLink,
isClickable,
isActive,
route: link.value?.route,
navigate: link.value?.navigate,
href: computed(() => props.to ? link.value?.route.value.href : props.href),
'aria-current': computed(() => isActive.value ? 'page' : undefined),
href,
linkProps: reactive({
href,
ariaCurrent: computed(() => isActive.value ? 'page' : undefined),
Copy link
Contributor Author

Choose a reason for hiding this comment

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

aria-current should use kebab case name, because vue 3 won't concert it automatically.

Copy link
Member

Choose a reason for hiding this comment

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

Screenshot_20240903_171939

Copy link
Member

Choose a reason for hiding this comment

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

Looks like it depends on the attribute, HTMLElement.ariaCurrent exists so it works but others might not: vuejs/core#5477

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Chrome automatically converts attributes, but Firefox doesn't. There is an open issue about it on vuetify and vue repos.

}),
}
}

Expand Down