An open source design system that's fully customizable and agent ready
TypeScript
fix(core): visible focus ring and aria-controls on CodeBlock collapse header (#3723)
Baraa Hamodid7bde219a222
diff --git a/.changeset/codeblock-header-focus-controls.md b/.changeset/codeblock-header-focus-controls.mdnew file mode 100644index 0000000000..74d7528b44--- /dev/null+++ b/.changeset/codeblock-header-focus-controls.md@@ -0,0 +1,6 @@+---+'@astryxdesign/core': patch+---++[fix] CodeBlock: complete the collapsible header's disclosure pattern. It now shows the standard accent focus ring on `:focus-visible` — previously it defined no focus style and fell back to the browser's default outline, unlike the system's other disclosure controls (Collapsible, TabMenu) — and links to the code region it shows/hides via `aria-controls` (the header already exposed `aria-expanded`). The region stays mounted when collapsed (CSS grid animation), so `aria-controls` is an always-resolvable reference. (#3723)+@bhamodidiff --git a/packages/core/src/CodeBlock/CodeBlock.test.tsx b/packages/core/src/CodeBlock/CodeBlock.test.tsxindex 025cf0487e..7c797e0836 100644--- a/packages/core/src/CodeBlock/CodeBlock.test.tsx+++ b/packages/core/src/CodeBlock/CodeBlock.test.tsx@@ -132,6 +132,49 @@ describe('CodeBlock', () => {expect(header).toHaveAttribute('aria-expanded', 'false');});+ it('links the collapsible header to its code region via aria-controls', () => {+ render(+ <CodeBlock+ code={LONG_CODE}+ language="javascript"+ title="example"+ isCollapsible+ />,+ );+ const header = screen+ .getAllByRole('button')+ .find(el => el.hasAttribute('aria-expanded'))!;+ const controlsId = header.getAttribute('aria-controls');+ // aria-controls must be present and point at the real code region.+ expect(controlsId).toBeTruthy();+ const region = document.getElementById(controlsId as string);+ expect(region).not.toBeNull();+ // The region contains the scrollable code body (role="group").+ expect(region).toContainElement(screen.getByRole('group'));+ });++ it('keeps aria-controls resolvable when collapsed (region stays mounted)', () => {+ render(+ <CodeBlock+ code={LONG_CODE}+ language="javascript"+ title="example"+ isCollapsible+ />,+ );+ const header = screen+ .getAllByRole('button')+ .find(el => el.hasAttribute('aria-expanded'))!;+ fireEvent.click(header);+ expect(header).toHaveAttribute('aria-expanded', 'false');+ // The code region uses a CSS grid animation to collapse, so it stays in+ // the DOM — aria-controls stays a valid, resolvable reference (unlike a+ // conditionally-mounted region, which would need a conditional attribute).+ const controlsId = header.getAttribute('aria-controls');+ expect(controlsId).toBeTruthy();+ expect(document.getElementById(controlsId as string)).not.toBeNull();+ });+it('applies a per-instance syntax theme via the syntaxTheme prop', () => {const {container} = render(<CodeBlockdiff --git a/packages/core/src/CodeBlock/CodeBlock.tsx b/packages/core/src/CodeBlock/CodeBlock.tsxindex 7e55b16d23..20548f9160 100644--- a/packages/core/src/CodeBlock/CodeBlock.tsx+++ b/packages/core/src/CodeBlock/CodeBlock.tsx@@ -11,6 +11,7 @@import {useInsertionEffect,useEffect,+ useId,useRef,useState,useCallback,@@ -175,6 +176,17 @@ const styles = stylex.create({headerCollapsible: {cursor: 'pointer',userSelect: 'none',+ // Restore a keyboard-only focus ring with the standard token/offset so this+ // disclosure control matches the rest of the system (Collapsible, TabMenu);+ // otherwise it falls back to the inconsistent UA default outline.+ outline: {+ default: null,+ ':focus-visible': `2px solid ${colorVars['--color-accent']}`,+ },+ outlineOffset: {+ default: '0',+ ':focus-visible': '2px',+ },},gutter: {flexShrink: 0,@@ -695,6 +707,11 @@ export function CodeBlock({const canCollapse = isCollapsible && lines.length >= collapsibleThreshold;const [isCollapsed, setIsCollapsed] = useState(false);+ // Links the collapsible header to the code region it shows/hides so assistive+ // tech can move from the button to its controlled content (disclosure+ // pattern). The region stays mounted when collapsed (CSS grid animation), so+ // this is always a resolvable reference — aria-controls can be unconditional.+ const regionId = useId();const scrollContainerRef = useRef<HTMLDivElement>(null);const scrollStyle: CSSProperties | undefined = maxHeight@@ -732,6 +749,7 @@ export function CodeBlock({role={canCollapse ? 'button' : undefined}tabIndex={canCollapse ? 0 : undefined}aria-expanded={canCollapse ? !isCollapsed : undefined}+ aria-controls={canCollapse ? regionId : undefined}onClick={canCollapse ? () => setIsCollapsed(prev => !prev) : undefined}onKeyDown={canCollapse@@ -835,6 +853,7 @@ export function CodeBlock({{headerEl}{canCollapse ? (<div+ id={regionId}{...stylex.props(styles.collapseGrid,isCollapsed && styles.collapseGridCollapsed,