Skip to content

fix(SidebarItem): manage keyboard focus on TOC anchor navigation (#8458) - #8475

Open
AnushkaShendge wants to merge 4 commits into
webpack:mainfrom
AnushkaShendge:fix/toc-anchor-focus
Open

AnushkaShendge wants to merge 4 commits into
webpack:mainfrom
AnushkaShendge:fix/toc-anchor-focus

Conversation

@AnushkaShendge

@AnushkaShendge AnushkaShendge commented Oct 31, 2026 •

Copy link
Copy Markdown

Description

Fixes #8458.

When keyboard or screen-reader users clicked Table of Contents (TOC) anchor links in the sidebar, keyboard focus remained stuck on the sidebar link or was lost.

This PR:

  • Adds an onClick handler to TOC NavLink items in SidebarItem.jsx that finds the target heading (accounting for inner anchor spans), sets tabindex="-1" if necessary, and programmatically shifts focus to the heading.
  • Adds visible focus outline styling for headings in Markdown.css.
  • Adds unit tests in SidebarItem.test.jsx covering focus behavior, inner span targets, modifier keys, non-existent targets, and same-hash clicks.

Testing

  • Verified with Jest unit tests (SidebarItem.test.jsx - 12 passed)
  • Code formatted and verified with Prettier and ESLint

Summary by CodeRabbit

  • Accessibility
    • Markdown headings display a visible focus outline, with colors adapted for dark mode.
    • Following a sidebar link to a section moves keyboard focus to its heading, or to the target when no heading is available.
    • When a section appears after content loads, it scrolls into view and receives focus unless you have moved focus elsewhere.
    • Selecting a link for the section already in view scrolls to that section and focuses it.
    • Modified clicks, links to other pages, and links without a matching target retain their existing behavior.

@vercel

vercel Bot commented Oct 31, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
webpack-js-org Ready Ready Preview Nov 21, 2026 10:01am UTC

Request Review

@linux-foundation-easycla

linux-foundation-easycla Bot commented Oct 31, 2026 •

Copy link
Copy Markdown

CLA Signed
The committers listed above are authorized under a signed CLA.

  • ✅ login: AnushkaShendge / name: AnushkaShendge (69a63a4)

@coderabbitai

coderabbitai Bot commented Oct 31, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Page hash navigation and sidebar TOC anchor clicks now focus the target heading after 100 ms. The code adds tabindex="-1" when needed. Page navigation cancels pending focus during cleanup and skips focus if focus moves elsewhere. Sidebar focus handling ignores modified clicks, cross-page links, and missing targets. Same-hash clicks scroll the target into view. Markdown headings receive visible focus outlines, with a separate color in dark mode. Tests cover these behaviors.

Severity of issue fixed: Medium

Merge Risk: Minimal · up to 55cb5

This change moves keyboard focus to the target heading after sidebar table-of-contents navigation and adds visible focus outlines. No concrete merge-blocking risk remains in the reviewed changes. One minor edge case is left: a focus change made within 100 ms of a same-page link click can be overridden.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the SidebarItem component and the keyboard focus management on TOC anchor navigation. The changeset adds an anchor click handler in SidebarItem.jsx that focuses the target heading, upd…
Linked Issues check Passed Directly linked issue #8458 has three coding requirements: (1) make heading anchor targets focusable, (2) add an onClick handler to TOC NavLinks that finds the target by ID and calls focus() after a s…
Out of Scope Changes check Passed All changed files serve the #8458 focus objective. SidebarItem.jsx adds the TOC click handler. Markdown.css adds visible heading focus feedback. Page.jsx adds focus after hash navigation, which covers…
Full details: Title check

Explanation

The title names the SidebarItem component and the keyboard focus management on TOC anchor navigation. The changeset adds an anchor click handler in SidebarItem.jsx that focuses the target heading, updates Page.jsx to focus hash targets, and adds focus outline styles in Markdown.css. The title matches the main change. It omits the Page and CSS changes, which is acceptable. The title follows the conventional commit style and is specific. The title's PR number (#8458) differs from the PR objectives' number (#8475), but this does not affect the title's relevance to the changeset.

Full details: Linked Issues check

Explanation

Directly linked issue #8458 has three coding requirements: (1) make heading anchor targets focusable, (2) add an onClick handler to TOC NavLinks that finds the target by ID and calls focus() after a short timeout, and (3) keep a visible focus outline on headings. Verified in the whole-PR diff (mergeBase to head): SidebarItem.jsx adds handleAnchorClick, passed as onClick to the Anchors NavLink. It resolves the target with document.getElementById, uses closest("h1, ... h6") to handle inner anchor spans, sets tabindex="-1" when absent, and calls heading.focus() after 100 ms. Modified clicks, missing IDs, and cross-page links return early. Markdown.css adds a :focus outline (2px, 2px offset) on h1 to h6, with a dark-mode color override. Requirement (1) and (2) are met. Requirement (3) is met by the CSS. Page.jsx adds the same focus logic for hash navigation on load and for dynamic content. I read the SidebarItem.jsx and Markdown.css diffs directly. I did not read the test files or Page.jsx in this pass. The test coverage claims rely on the change summary, not on direct test reads. No non-coding requirement applies.

Full details: Out of Scope Changes check

Explanation

All changed files serve the #8458 focus objective. SidebarItem.jsx adds the TOC click handler. Markdown.css adds visible heading focus feedback. Page.jsx adds focus after hash navigation, which covers cross-page TOC links and dynamically loaded content. It applies to any hash load, which is wider than sidebar clicks, but it addresses the same focus loss. The PR description does not mention Page.jsx. This is a documentation gap, not a scope violation. The test files support the listed behaviors. The Page.jsx and test-file conclusions rely on the change summary, not on direct reads in this pass. Minor note: the 100 ms timeout in handleAnchorClick is not cleared on unmount, which does not affect scope.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 295a7540-7a6b-48ee-9b2a-47acd6423692
Commits

Reviewing files that changed from the base of the PR and between c6fd65f and 55cb59d.

Files selected for processing: 2

  • src/components/Markdown/Markdown.css
  • src/components/Page/Page.jsx
Files skipped from review as they are similar to previous changes: 1

  • src/components/Markdown/Markdown.css

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 2


Review info
Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 50140316-0eb0-4fb7-ad6d-7cd4cca66486
Commits

Reviewing files that changed from the base of the PR and between a1b4b8d and 69a63a4.

Files selected for processing: 3
  • src/components/Markdown/Markdown.css
  • src/components/SidebarItem/SidebarItem.jsx
  • src/components/SidebarItem/SidebarItem.test.jsx

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.

return;
}

const target = document.getElementById(anchorId);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Major · Functional Correctness · Heavy lift

Supported by static analysis

Script executed:

sed -n 1,140p src/components/SidebarItem/SidebarItem.jsx
sed -n 1,200p src/components/Sidebar/Sidebar.jsx

Repository: webpack/webpack.js.org

Length of output: 7964


Script executed:

nl -ba src/components/SidebarItem/SidebarItem.jsx | sed -n '1,220p'
nl -ba src/components/Sidebar/Sidebar.jsx | sed -n '1,150p'

Repository: webpack/webpack.js.org

Length of output: 10737


Focus cross-page anchors after the destination renders.

Sidebar renders SidebarItem for every page, and SidebarItem renders each page’s anchor NavLink. For an anchor on another page, handleAnchorClick calls document.getElementById(anchorId) before navigation. The destination heading is therefore unavailable unless the current page contains the same ID. Move cross-page focus handling to the destination page after its content renders.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1


Review info
Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: c9a4b685-c744-47de-8931-bc78408a40cb
Commits

Reviewing files that changed from the base of the PR and between 69a63a4 and 5809009.

Files selected for processing: 4
  • src/components/Page/Page.jsx
  • src/components/Page/__tests__/Page.test.jsx
  • src/components/SidebarItem/SidebarItem.jsx
  • src/components/SidebarItem/SidebarItem.test.jsx
Files skipped from review as they are similar to previous changes: 2
  • src/components/SidebarItem/SidebarItem.jsx
  • src/components/SidebarItem/SidebarItem.test.jsx

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 6 remain after this review.

Comment thread src/components/Page/Page.jsx Resolved

This branch was successfully deployed

1 active deployment
Preview — 55cb59d6 Deployed Nov 21, 2026 by vercel[bot]
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.

a11y: keyboard focus is lost when clicking Table of Contents (TOC) anchor links

1 participant