Skip to content

Conversation

@protitude
Copy link
Collaborator

@protitude protitude commented Jan 22, 2026

Describe context / purpose for this PR

Add JS for Anchor Link menu Accessibility

Issue link

https://cyberteamportal.atlassian.net/browse/D8-2641

Any other related PRs?

Link to MultiDev instance

http://md-2641-accessmatch.pantheonsite.io

Checklist for PR author

  • I have checked that the PR is ready to be merged
  • I have reviewed the DIFF and checked that the changes are as expected
  • I have assigned myself or someone else to review the PR

Adding accessibility screenreader js to global.js
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Adds JavaScript to improve accessibility for in-page anchor link navigation by moving focus to the target element after clicking an internal #... link.

Changes:

  • Adds a global DOMContentLoaded handler that finds internal hash links and adds click listeners.
  • On click, focuses the target element (temporarily applying tabindex="-1" and then removing it).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 24 to 37
if (target) {
// Temporarily make the element focusable
target.setAttribute('tabindex', '-1');

// Move focus to the target without scrolling the page
target.focus({ preventScroll: true });

// Optional: remove tabindex after a short delay
setTimeout(() => {
target.removeAttribute('tabindex');
}, 1000); // 1 second delay is enough for screen reader to announce
}
});

Copy link

Copilot AI Jan 22, 2026

Choose a reason for hiding this comment

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

The click handler always calls target.setAttribute('tabindex', '-1') and later unconditionally removes the tabindex attribute. If the target element already had a tabindex (or an intentionally empty one), this will remove it and change keyboard navigation semantics. Please preserve and restore the previous tabindex value (or only remove it if it was added by this handler).

Suggested change
if (target) {
// Temporarily make the element focusable
target.setAttribute('tabindex', '-1');
// Move focus to the target without scrolling the page
target.focus({ preventScroll: true });
// Optional: remove tabindex after a short delay
setTimeout(() => {
target.removeAttribute('tabindex');
}, 1000); // 1 second delay is enough for screen reader to announce
}
});
if (target) {
// Store existing tabindex state, if any.
const hadTabIndex = target.hasAttribute('tabindex');
const previousTabIndex = target.getAttribute('tabindex');
// Temporarily make the element focusable if needed.
if (!hadTabIndex) {
target.setAttribute('tabindex', '-1');
}
// Move focus to the target without scrolling the page
target.focus({ preventScroll: true });
// Optional: restore or remove tabindex after a short delay
setTimeout(() => {
if (!hadTabIndex) {
// We added tabindex, so remove it again.
target.removeAttribute('tabindex');
} else if (previousTabIndex !== null) {
// Element had a tabindex; restore its original value (including empty).
target.setAttribute('tabindex', previousTabIndex);
}
}, 1000); // 1 second delay is enough for screen reader to announce
}
});

Copilot uses AI. Check for mistakes.
protitude and others added 2 commits January 23, 2026 09:06
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants