Skip to content

A11y: Fix MDX heading anchors not keyboard accessible#34368

Merged
Sidnioulz merged 3 commits into
storybookjs:nextfrom
TheSeydiCharyyev:fix/issue-33240-mdx-heading-anchors-a11y
May 21, 2026
Merged

A11y: Fix MDX heading anchors not keyboard accessible#34368
Sidnioulz merged 3 commits into
storybookjs:nextfrom
TheSeydiCharyyev:fix/issue-33240-mdx-heading-anchors-a11y

Conversation

@TheSeydiCharyyev
Copy link
Copy Markdown
Contributor

@TheSeydiCharyyev TheSeydiCharyyev commented Mar 27, 2026

Description

Makes MDX heading anchors keyboard accessible by replacing the inaccessible styled anchor with a Button component.

Related Issue

Fixes #33240

Changes Made

  • Replaced styled.a (OcticonAnchor) with Button asChild wrapping an <a> element for consistent styling and built-in accessibility
  • Removed aria-hidden="true" and tabIndex={-1} so the anchor is reachable by keyboard and screen readers
  • Added ariaLabel="Copy heading URL to address bar" which also provides an automatic tooltip on hover/focus
  • Added &:focus-within svg style to show the link icon on keyboard focus (alongside existing :hover)
  • Anchor remains an <a> with href so tocbot Table of Contents continues to work

How it works

  • Button asChild uses Radix Slot to render an <a> element with button styles — visually a button, semantically a link
  • ariaLabel on Button automatically creates a tooltip via InteractiveTooltipWrapper
  • :focus-within on the heading container makes the SVG icon visible when the anchor inside receives keyboard focus

Manual testing

  1. Open any MDX docs page with headings
  2. Press Tab — anchor icons should become visible on focus
  3. Tooltip "Copy heading URL to address bar" should appear on focus/hover
  4. Enter navigates to the heading anchor
  5. Hover still works as before

Checklist

  • Linter passes (0 errors)
  • Tested manually in sandbox

Summary by CodeRabbit

  • New Features

    • Header icons now appear on both hover and keyboard focus, improving discoverability and keyboard navigation.
  • Bug Fixes

    • Copy-heading control updated for more reliable activation and target validation; accessibility labels and visual behavior improved for keyboard and assistive‑tech users.

Review Change Stack

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Mar 27, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: f7087380-9cd0-4150-82c8-c770052925a9

📥 Commits

Reviewing files that changed from the base of the PR and between 837b786 and 97cb78f.

📒 Files selected for processing (1)
  • code/addons/docs/src/blocks/blocks/mdx.tsx

📝 Walkthrough

Walkthrough

Refactors the MDX heading copy-link to a Button-wrapped anchor, makes the octicon visible on both :hover and :focus-within, and updates the click handler to call preventDefault(), verify the target element exists, then invoke navigate(context, hash).

Changes

MDX Heading Anchor

Layer / File(s) Summary
Import, styling, wrapper, and click behavior
code/addons/docs/src/blocks/blocks/mdx.tsx
Adds Button import; makes octicon SVG visible on :hover and :focus-within; introduces OcticonAnchorWrapper to adjust float/spacing and force nested a to color: inherit and no text-decoration; replaces the previous styled anchor with a Button (asChild) wrapping an a whose onClick preventDefault()s, checks document.getElementById(id), and calls navigate(context, hash) if found.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs


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

Comment @coderabbitai help to get the list of available commands and usage tips.

@TheSeydiCharyyev
Copy link
Copy Markdown
Contributor Author

Synced the branch with the latest next — no conflicts, mdx.tsx wasn't touched upstream.

@Sidnioulz Sidnioulz self-assigned this May 21, 2026
@Sidnioulz Sidnioulz force-pushed the fix/issue-33240-mdx-heading-anchors-a11y branch from 308b110 to 7173d68 Compare May 21, 2026 08:41
@Sidnioulz Sidnioulz changed the title Fix MDX heading anchors not keyboard accessible A11y: Fix MDX heading anchors not keyboard accessible May 21, 2026
@Sidnioulz Sidnioulz added accessibility ci:normal a11y: keyboard Accessibility issues related to keyboard navigation or shortcuts and removed Stale labels May 21, 2026
Comment thread code/addons/docs/src/blocks/blocks/mdx.tsx Outdated
Copy link
Copy Markdown
Member

@Sidnioulz Sidnioulz left a comment

Choose a reason for hiding this comment

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

LGTM overall. Waiting for the Chromatic UI Test to finish before I approve :)

@TheSeydiCharyyev
Copy link
Copy Markdown
Contributor Author

Pulled the commit and ran yarn task compile -s compile — compiles clean, no issues.

Copy link
Copy Markdown
Member

@Sidnioulz Sidnioulz left a comment

Choose a reason for hiding this comment

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

@TheSeydiCharyyev if you look at the UI Tests, you'll see there's a layout shift for titles. They have all moved to the right.

Could you please find a way to make sure there is no visual change for titles? Thanks.

You can ignore the failed Linux test, it's a flake. It should go away the next time CI runs.

@TheSeydiCharyyev
Copy link
Copy Markdown
Contributor Author

Fixed — the anchor was floated, and the Button's padding widened it, which pushed the heading text to the right. It's now positioned absolutely in the left gutter, so the Button's dimensions no longer affect heading layout. Verified locally on an MDX page with headings — titles are flush-left with body text again.

@Sidnioulz Sidnioulz merged commit 7dcd46b into storybookjs:next May 21, 2026
134 checks passed
@github-project-automation github-project-automation Bot moved this from Empathy Queue (prioritized) to Done in Core Team Projects May 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

a11y: keyboard Accessibility issues related to keyboard navigation or shortcuts accessibility addon: docs bug ci:normal

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

[A11y]: anchors in MDX headings are not keyboard accessible

3 participants