An open source design system that's fully customizable and agent ready
TypeScript
fix(core): make collapsed CodeBlock content inert (#3990)
Baraa Hamodie84039d5370b
diff --git a/.changeset/codeblock-collapsed-inert.md b/.changeset/codeblock-collapsed-inert.mdnew file mode 100644index 0000000000..9fb291b3e7--- /dev/null+++ b/.changeset/codeblock-collapsed-inert.md@@ -0,0 +1,7 @@+---+'@astryxdesign/core': patch+---++[fix] CodeBlock: collapsed code regions are now inert, so keyboard focus can no longer land on the invisible scroll container while collapsed.++@bhamodidiff --git a/packages/core/src/CodeBlock/CodeBlock.test.tsx b/packages/core/src/CodeBlock/CodeBlock.test.tsxindex b689fa9460..9b14562f44 100644--- a/packages/core/src/CodeBlock/CodeBlock.test.tsx+++ b/packages/core/src/CodeBlock/CodeBlock.test.tsx@@ -210,6 +210,62 @@ describe('CodeBlock', () => {expect(document.getElementById(controlsId as string)).not.toBeNull();});+ it('makes the collapsed region inert so the scroll container is unreachable', () => {+ render(+ <CodeBlock+ code={LONG_CODE}+ language="javascript"+ title="example"+ isCollapsible+ />,+ );+ const header = screen+ .getAllByRole('button')+ .find(el => el.hasAttribute('aria-expanded'))!;+ const region = document.getElementById(+ header.getAttribute('aria-controls') as string,+ )!;+ // Expanded: the region is not inert and the scroll container is reachable.+ expect(region).not.toHaveAttribute('inert');++ fireEvent.click(header);+ expect(header).toHaveAttribute('aria-expanded', 'false');+ // Collapsed: the wrapper is inert, so the keyboard-focusable scroll+ // container (tabIndex=0) inside it drops out of the tab order and the+ // accessibility tree instead of remaining an invisible tab stop.+ expect(region).toHaveAttribute('inert');+ const scrollContainer = screen.getByRole('group');+ expect(scrollContainer.closest('[inert]')).toBe(region);+ });++ it('restores focusability of the scroll container after expanding again', () => {+ render(+ <CodeBlock+ code={LONG_CODE}+ language="javascript"+ title="example"+ isCollapsible+ />,+ );+ const header = screen+ .getAllByRole('button')+ .find(el => el.hasAttribute('aria-expanded'))!;+ const region = document.getElementById(+ header.getAttribute('aria-controls') as string,+ )!;+ // Collapse, then expand again.+ fireEvent.click(header);+ expect(region).toHaveAttribute('inert');+ fireEvent.click(header);+ expect(header).toHaveAttribute('aria-expanded', 'true');+ // Expanded again: inert is removed and the scroll container is a+ // keyboard-focusable group once more.+ expect(region).not.toHaveAttribute('inert');+ const scrollContainer = screen.getByRole('group');+ expect(scrollContainer.closest('[inert]')).toBeNull();+ expect(scrollContainer).toHaveAttribute('tabindex', '0');+ });+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 bdde40fc25..71d24bd80a 100644--- a/packages/core/src/CodeBlock/CodeBlock.tsx+++ b/packages/core/src/CodeBlock/CodeBlock.tsx@@ -925,6 +925,12 @@ export function CodeBlock({{canCollapse ? (<divid={regionId}+ // While collapsed, the region is only hidden visually (0fr grid+ // row); inert also removes it from the tab order and accessibility+ // tree so keyboard users cannot Tab into the invisible scroll+ // container (tabIndex=0). aria-controls pointing at an inert+ // element remains a valid, resolvable reference.+ inert={isCollapsed ? true : undefined}{...stylex.props(styles.collapseGrid,isCollapsed && styles.collapseGridCollapsed,