Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

docs(CodeBlock): restore headerless example (#6892)

Cindy Zhang373c2d7499f2
diff --git a/apps/storybook/stories/CodeBlock.stories.tsx b/apps/storybook/stories/CodeBlock.stories.tsx
index 24a41fbbae..eb0cf68eb7 100644
--- a/apps/storybook/stories/CodeBlock.stories.tsx
+++ b/apps/storybook/stories/CodeBlock.stories.tsx
@@ -275,6 +275,7 @@ export const NoHeader: Story = {
code: `const greeting = "Hello, world!";
console.log(greeting);`,
language: 'typescript',
+ hasLanguageLabel: false,
hasCopyButton: true,
},
};
diff --git a/packages/core/src/CodeBlock/CodeBlock.doc.mjs b/packages/core/src/CodeBlock/CodeBlock.doc.mjs
index a1f10a0318..cafa98f011 100644
--- a/packages/core/src/CodeBlock/CodeBlock.doc.mjs
+++ b/packages/core/src/CodeBlock/CodeBlock.doc.mjs
@@ -172,7 +172,7 @@ export const docs = {
{guidance: false, description: 'Nest a code block inside a scrollable container. Use the maxHeight prop instead, which handles overflow natively.'},
],
anatomy: [
- {name: 'Header Bar', required: false, description: 'Shows the title, language label, and copy button. Appears when any of these props are set.'},
+ {name: 'Header Bar', required: false, description: 'Shows the title, visible language label, and copy button when a header is present. A title or visible language label creates the header; the copy button alone floats at the top-end of a headerless block.'},
{name: 'Line Numbers', required: false, description: 'Numbered gutter along the left edge. Enable with hasLineNumbers.'},
{name: 'Code Body', required: true, description: 'The syntax-highlighted code content.'},
{name: 'Highlighted Lines', required: false, description: 'Background accent on specific lines to draw attention.'},
diff --git a/packages/core/src/CodeBlock/CodeBlock.test.tsx b/packages/core/src/CodeBlock/CodeBlock.test.tsx
index 8d30083cd5..0550203b18 100644
--- a/packages/core/src/CodeBlock/CodeBlock.test.tsx
+++ b/packages/core/src/CodeBlock/CodeBlock.test.tsx
@@ -374,6 +374,29 @@ describe('CodeBlock', () => {
expect(container.querySelector('.astryx-codeblock-title')).toBeNull();
});
+ it('keeps copy available when a known language label is hidden', async () => {
+ const {container} = render(
+ <CodeBlock
+ code="const x = 1;"
+ language="javascript"
+ hasLanguageLabel={false}
+ />,
+ );
+
+ expect(container.querySelector('.astryx-codeblock-header')).toBeNull();
+ expect(container.querySelector('.astryx-codeblock-title')).toBeNull();
+ expect(screen.getByRole('group')).toHaveAttribute('aria-label', 'Code');
+
+ const copyButton = screen.getByRole('button', {name: 'Copy code'});
+ expect(copyButton.parentElement).toBe(container.querySelector('pre'));
+ await act(async () => {
+ fireEvent.click(copyButton);
+ });
+ expect(navigator.clipboard.writeText).toHaveBeenCalledWith(
+ 'const x = 1;',
+ );
+ });
+
it('exposes the header and title as themeable defineTheme targets', () => {
// jsdom can't resolve the @layer cascade, so this asserts the targets are
// reachable by a theme via the sanctioned defineTheme channel — replacing