Problem/Motivation

In a set of links such as a menu, the currently-active link has the is-active class added so it can be styled to make it visually apparent that this link goes to the current page. This information should also be made available programmatically. In order to do this, the aria-current attribute can be set to "page."

The attribute should be added to the link, not a list item or other element. If the currently-active item is not linked to (for example, if the link is removed when the target is the current page, such as in breadcrumbs where the page title is displayed), then the aria-current attribute is not required.

References:

Steps to reproduce

  1. Install the latest version of Drupal using the standard profile.
  2. Create three basic pages and add them to the main menu. Note they appear as links in the main menu region.
  3. Follow one of the links in the main menu.
  4. Inspect the code of the link to the current page. Note that it is still a link and it has the is-active class. Note that it does not have an aria-current attribute.

Proposed resolution

Add aria-current="page" to all links where the is-active class is being added. Do not add the attribute to non-links; do not add it when the current item is not a page (for example, tab panels, steps in a form, etc).

Remaining tasks

Manual testing of other places where the is-active class is added. Test pagination links in Views etc.

User interface changes

None.

API changes

None.

Data model changes

None.

Release notes snippet

TBD.

Issue fork drupal-3038523

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

Chi created an issue. See original summary.

mgifford’s picture

Issue summary: View changes
Issue tags: +aria
Related issues: +#2952488: Use aria-current=page in pagination links.

Tagging & adding related links. This isn't a well used pattern. Could be useful, but not sure that it is. Combination of browser support & user patterns.

That said, it's increased semantics and could be useful for many things in the future.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

alphex’s picture

This is a w3c recommendation from December 2017 - and my clients are starting to get this flagged by their accessibility compliance audits.

hoping to see

aria-current="true"

+1 hoping this gets rolled in to core.

idebr’s picture

This contrib module adds the aria-current attribute to active items in <nav> elements: https://www.drupal.org/project/aria_current

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

luke.leber’s picture

Might this be something to add into \Drupal\Core\EventSubscriber\ActiveLinkResponseFilter to catch more instances?

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

jayhuskins made their first commit to this issue’s fork.

mgifford’s picture

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

gauravvvv’s picture

gauravvvv’s picture

Status: Active » Needs work
StatusFileSize
new4.66 KB

Added aria-current attribute on pager and breadcrumb. Still needs to be added on menu items.

gauravvvv’s picture

cboyden’s picture

Rather than each theme needing to implement this, would it be possible to do as Luke.Leber suggests and have the attribute added more globally? It should probably be done anywhere the is-active class is applied to a menu link. For example, in the JS file /core/misc/active-link.es6.js and in the ActiveLinkResponseFilter.php class.

cboyden’s picture

Status: Needs work » Needs review
StatusFileSize
new2.96 KB
new2.25 KB

I've created a new MR with the approach of adding the aria-current="page" attribute everywhere (that I can find) that the is-active class is being added to a menu link. I've also created two patches, one for 10.1.x/10.0.x, and one for 9.5.x, in case anyone wants to use them.

According to the ARIA spec for the aria-current attribute, it should be added to a set of navigation links to programmatically indicate the current page among that set of links. It needs to be added to the link, not the list item or other element. And if the current item is not a link, the aria-current attribute is not needed. For reference see https://www.w3.org/TR/wai-aria-1.2/#aria-current and https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Attribut....

irene_dobbs’s picture

Status: Needs review » Needs work

On the latest patch for 3038523-aria-current-link-core-9.5.x-22.patch and 3038523-aria-current-link-core-10.x-22.patch,
I added menu items to the main navigation and looked at the is-active class and I do not see the aria-current attribute added.

dsnopek’s picture

I tested the Drupal 9.5 patch from #22, and it worked for me locally! I'm not sure why it didn't work for @irene_dobbs?

However, it looks like this also needs to update web/core/tests/Drupal/Tests/Core/EventSubscriber/ActiveLinkResponseFilterTest.php because there's a test failure in CI.

cboyden’s picture

Status: Needs work » Needs review
StatusFileSize
new13.68 KB
new13 KB

I've updated the MR to include a fix for ActiveLinkResponseFilterTest. I'm also attaching new patches for 9.5.x and 10.1.x/10.0.x.

smustgrave’s picture

Issue summary: View changes
Status: Needs review » Needs work
Issue tags: +Needs issue summary update

Before testing the issue summary seem incomplete. Added the template for reference.

cboyden’s picture

Issue summary: View changes
Status: Needs work » Needs review
Issue tags: -Needs issue summary update

Updated issue summary.

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: +Needs Review Queue Initiative

For clarity I'm hiding the patches as MR 4959 is what would be committed.

Following the steps in the IS I can confirm the links with is-active don't have an aria attribute
Applying the MR
Can confirm the links do have aria-attribute now

Worth noting that the is-active class being set by the toolbar is unaffected with this MR. *Edit* Which is expected

cboyden’s picture

Thanks for the review @smustgrave. I took a look at the admin toolbar on the standard install and noted that aria-current is being set to "page" for the active link in the toolbar. To reproduce:

  1. Install the latest version of Drupal from this issue MR using the standard profile.
  2. Log in as an administrator.
  3. In the Manage menu, click Appearance.
  4. While on the Appearance page, inspect the code for the Appearance link.
  5. Note the is-active class is set and the aria-current attribute is set.
  6. Click the Settings tab/link.
  7. While on the Settings page, inspect the code for the Settings tab/link.
  8. Note the is-active class is set and the aria-current attribute is set.
  9. While still on the Settings page, inspect the code for the Appearance tab/link.
  10. Note the is-active class is set, but the aria-current attribute is NOT set.
  11. Click the Olivero tab/link.
  12. While on the Olivero page, inspect the code for the Olivero tab/link.
  13. Note the is-active class is set and the aria-current attribute is set.
  14. While still on the Olivero page, inspect the code for the Settings tab/link.
  15. Note the is-active class is set, but the aria-current attribute is NOT set.

This seems like the appropriate behavior to me - the links in the hierarchy above where you are have the visual styling, but only the actual current page has the aria-current attribute.

lauriii’s picture

Issue summary: View changes
Status: Reviewed & tested by the community » Needs review
StatusFileSize
new418.7 KB

Should the aria-current attribute be used only on menus or should this also work on other kinds of links too?

For example, if I click "Find out more", should that have aria-current?

If that's the case, should we do it in a follow-up or should it be done here?

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

From reading https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Attribut...

When you have a group of related elements, such as several links in a breadcrumb or steps in a multi-step flow, with one element in the group styled differently from the others to indicate to the sighted user that this is the current element within its group, the aria-current should be used to inform the assistive technology user what has been indicated via styling.

I don't think the Find out more link mentioned would count.

luke.leber’s picture

I've always believed that ad-hoc self-referencing links can be avoided by a solid content strategy.

...however I did want to raise one other question: what about Single Page Application-type use cases? Is this an outlier, or should we confirm that same page links don't have a fragment?

  <!-- None of these should probably have the attribute...? -->
  <menu>
    <a href="#">Top</a>
    <a href="#second">Second section</a>
    <a href="#third">Third section</a>
    <a href="#fourth">Fourth section</a>
  </menu>
cboyden’s picture

@Luke.Leber because all we're doing is adding the aria-current attribute in places where core is already adding the is-active class, I would hope that the ActiveLinkResponseFilter class would do the right thing with menus that contain anchor links. If core is not already adding the is-active class where it needs adding, or if it's adding the class to multiple anchor links in the same menu, it might be best to fix that in a separate issue.

larowlan changed the visibility of the branch 3038523-add-aria-current-attribute to hidden.

larowlan’s picture

Issue credits

  • larowlan committed a060dbd0 on 11.x
    Issue #3038523 by cboyden, Gauravvvv, mgifford, Luke.Leber, Chi: Add...

larowlan’s picture

Status: Reviewed & tested by the community » Fixed

Took this for a manual test, tested menu links, breadcrumbs, local tasks, pagers. All looked good.

Read the MDN spec on it and it looks like we're doing things the correct way.

Committed to 11.x

Added and published a change-record

frankdesign’s picture

Thanks for fixing this for D11. Is there any chance that the patch will be updated for D10.2 (the patch at #25 fails to apply)

cboyden’s picture

The 10.0/10.1 patch still applies manually to 10.2.x with a bunch of offsets. I've uploaded a new version that should apply to 10.2.x cleanly.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

aaronpinero’s picture

Thank you @cboyden for the 10.2.x patch. I have applied this to my v10.2.5 Drupal site using composer. The patch applied cleanly and appears to provide the expected a11y enhancement.

chri5tia’s picture

I checked out the patch #40 for 10.2.x to reroll it for 10.3 and all the changes seem to be in core, though I don't see it noted. In case this is helpful to anyone else.