An open source design system that's fully customizable and agent ready
TypeScript
fix(core): announce CodeBlock copy success to screen readers (#3709)
Baraa Hamodic43d6e14d0ef
diff --git a/.changeset/codeblock-copy-announce.md b/.changeset/codeblock-copy-announce.mdnew file mode 100644index 0000000000..5dfe29f72d--- /dev/null+++ b/.changeset/codeblock-copy-announce.md@@ -0,0 +1,6 @@+---+'@astryxdesign/core': patch+---++[fix] CodeBlock: announce "Copied" via a polite live region when the copy button is used. Previously success was signalled only by swapping the button's `aria-label`, which screen readers don't reliably announce. (#3709)+@bhamodidiff --git a/packages/core/src/CodeBlock/CodeBlock.test.tsx b/packages/core/src/CodeBlock/CodeBlock.test.tsxindex 6a0db662a7..025cf0487e 100644--- a/packages/core/src/CodeBlock/CodeBlock.test.tsx+++ b/packages/core/src/CodeBlock/CodeBlock.test.tsx@@ -9,11 +9,16 @@* SYNC: When CodeBlock.tsx changes, update tests to match new behavior*/-import {describe, it, expect, vi, beforeEach} from 'vitest';-import {render, screen, fireEvent} from '@testing-library/react';+import {describe, it, expect, vi, beforeEach, afterEach} from 'vitest';+import {render, screen, fireEvent, waitFor} from '@testing-library/react';import {CodeBlock} from './CodeBlock';+import {__resetLiveRegionsForTest} from '../hooks/useAnnounce';import {dracula} from '../theme/syntax';+function politeRegion(): HTMLElement | null {+ return document.querySelector('[data-astryx-live-region="polite"]');+}+// A code sample long enough to exceed the default collapsible threshold (10).const LONG_CODE = Array.from({length: 15},@@ -28,6 +33,10 @@ describe('CodeBlock', () => {});});+ afterEach(() => {+ __resetLiveRegionsForTest();+ });+it('renders the code', () => {render(<CodeBlock code="const x = 1;" language="javascript" />);expect(screen.getByText(/const/)).toBeInTheDocument();@@ -54,6 +63,15 @@ describe('CodeBlock', () => {expect(navigator.clipboard.writeText).toHaveBeenCalledWith('const x = 1;');});+ it('announces "Copied" to a polite live region after copying', async () => {+ render(<CodeBlock code="const x = 1;" language="javascript" />);+ const copyButton = screen.getByRole('button', {name: 'Copy code'});+ fireEvent.click(copyButton);+ await waitFor(() => {+ expect(politeRegion()).toHaveTextContent('Copied');+ });+ });+it('does NOT collapse the block when the copy button is clicked', () => {render(<CodeBlockdiff --git a/packages/core/src/CodeBlock/CodeBlock.tsx b/packages/core/src/CodeBlock/CodeBlock.tsxindex 0a3cde4945..7e55b16d23 100644--- a/packages/core/src/CodeBlock/CodeBlock.tsx+++ b/packages/core/src/CodeBlock/CodeBlock.tsx@@ -32,6 +32,7 @@ import {easeVars,} from '../theme/tokens.stylex';import {mergeProps} from '../utils';+import {useAnnounce} from '../hooks/useAnnounce';import {Icon} from '../Icon';import {tokenize,@@ -650,6 +651,7 @@ export function CodeBlock({...props}: CodeBlockProps) {const [copied, setCopied] = useState(false);+ const announce = useAnnounce();const useSpans =highlightMode === 'spans' ||@@ -675,12 +677,15 @@ export function CodeBlock({try {await navigator.clipboard.writeText(code);setCopied(true);+ // Swapping the button's aria-label alone isn't reliably announced by+ // screen readers, so confirm the copy via a polite live region.+ announce('Copied');onCopy?.();setTimeout(() => setCopied(false), 2000);} catch {// Clipboard failures leave the copied state unchanged.}- }, [code, onCopy]);+ }, [code, onCopy, announce]);const sizeStyle = size === 'sm' ? styles.sizeSm : styles.sizeMd;const gutterSizeStyle = size === 'sm' ? styles.gutterSm : styles.gutterMd;