Skip to content

fix(core): announce CodeBlock copy success to screen readers - #3709

Merged
cixzhang merged 1 commit into
meta:mainfrom
bhamodi:a11y/codeblock-copy-announce
Jul 9, 2026
Merged

cixzhang merged 1 commit into
meta:mainfrom
bhamodi:a11y/codeblock-copy-announce

Conversation

@bhamodi

@bhamodi bhamodi commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

Summary

CodeBlock's copy button signalled success only by swapping its accessible name (aria-label: Copy code → Copied) and a local timeout. Changing a focused element's aria-label is not reliably announced by screen readers, so blind users got no confirmation the copy happened.

This adds a polite useAnnounce("Copied") on successful copy — the same live-region primitive Pagination and others use. Additive; the visual/label behavior is unchanged.

Changes

  • CodeBlock/CodeBlock.tsx: import useAnnounce; announce "Copied" after navigator.clipboard.writeText succeeds; add announce to the handleCopy dependency array.

Test plan

  • pnpm -F @astryxdesign/core exec vitest run src/CodeBlock/CodeBlock.test.tsx — 10 pass, including a new test:
    • announces "Copied" to a polite live region after copying — clicks copy and waitFors the polite live region ([data-astryx-live-region="polite"]) to contain "Copied". Uses __resetLiveRegionsForTest in afterEach.
  • Existing copy/collapse/scroll-region tests still green.
  • tsc --project packages/core/tsconfig.json --noEmit — clean.
  • eslint on changed files — clean (no exhaustive-deps warning).

Notes

Found during a broader a11y audit of core components. One fix per PR.

@vercel

vercel Bot commented Jul 9, 2026

Copy link
Copy Markdown

@bhamodi is attempting to deploy a commit to the Meta Open Source Team on Vercel.

A member of the Team first needs to authorize it.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Jul 9, 2026
Copy success was conveyed only by swapping the copy button's aria-label
(Copy code -> Copied), which assistive tech does not reliably announce.
Add a polite useAnnounce("Copied") on successful copy.

Test plan:
- pnpm -F @astryxdesign/core exec vitest run src/CodeBlock/CodeBlock.test.tsx
  (10 pass, incl. new 'announces "Copied" to a polite live region after copying')
- core typecheck + eslint on changed files: clean

@cixzhang cixzhang left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for this — clean fix. announce('Copied') fires imperatively inside the copy click handler, so it's one announcement per explicit user action, with no effect and no mount-time behavior to worry about. Swapping the button's aria-label alone is indeed unreliable across screen readers, so the polite live region is the right call.

This is the pattern I'd point the other announcement PRs at: announce at the interaction site, not from an effect watching derived state.

Approving.


PR: #3709 — fix(core): announce CodeBlock copy success to screen readers · Author: @bhamodi · What it does: confirms a successful copy via a polite live region · Recommendation: approve

@cixzhang
cixzhang merged commit c43d6e1 into meta:main Jul 9, 2026
14 of 15 checks passed
@bhamodi
bhamodi deleted the a11y/codeblock-copy-announce branch July 15, 2026 01:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants