Add support for anchors to active-link.js

Created on 1 August 2024, 6 months ago

Problem/Motivation

On our site we have a menu that contains links whose data-drupal-link-system-path attribute matches. The links point to the same URL but with different anchors. Both are given the is-active class, which results in our CSS putting an "active" indicator on both links.

The is-active class is added by core/misc/active-link.js, which queries the DOM for links with several attributes:

  • data-drupal-link-query (query string)
  • data-drupal-link-system-path (internal path)
  • hreflang (language code)

As you can see, it supports querying the DOM for query strings, language codes, and internal URL codes (e.g., /node/1234). But, it doesn't support anchors. What I found was:

  • Two different menu links pointing to different anchors on the same page will have different href attributes, but the same data-drupal-link-system-path.
  • Because core/misc/active-link.js relies on the data-drupal-link-system-path to determine the current page, any anchors present in the current URL or the link URL are ignored.

Steps to reproduce

  • Create two menu links to the same URL, but add a different anchor to each one
  • Go to the URL you set for the menu links.
  • Make sure the menu is on the page.
  • Inspect the menu links you added in the first step.
  • Both links will have:
    • Different href attributes (because the anchors are different)
    • The same data-drupal-link-system-path attribute

Proposed resolution

I was able to get this working with only small modifications to core/misc/active-link.js. I'll attach a merge request with the changes to this issue.

Remaining tasks

Testing and verification. I'd completely understand if there's a better, or more "Drupal" way to do this. If that's the case, I'm happy to make the changes needed.

✨ Feature request
Status

Active

Version

11.0 πŸ”₯

Component
Menu systemΒ  β†’

Last updated 1 day ago

Created by

πŸ‡ΊπŸ‡ΈUnited States jsutta United States

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Production build 0.71.5 2024