Skip to content

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

TypeScript

feat(core): add CodeBlock header and title theme targets (#4943)

freddymeta64df3b6c7fec
diff --git a/.changeset/codeblock-header-title-targets.md b/.changeset/codeblock-header-title-targets.md
new file mode 100644
index 0000000000..f5bb41af89
--- /dev/null
+++ b/.changeset/codeblock-header-title-targets.md
@@ -0,0 +1,7 @@
+---
+'@astryxdesign/core': patch
+---
+
+[feat] CodeBlock: add `codeblock-header` and `codeblock-title` theme targets on the header row and the title/language-label element. A theme can now restyle the header (e.g. padding) and the title (e.g. font size) directly, instead of reaching them through structural `> div:first-child > div > span` selectors that reverse-engineer the header layout. Both reflect the `size`/`language`(`/container`) visual props like the root.
+
+@freddymeta
diff --git a/packages/core/src/CodeBlock/CodeBlock.doc.mjs b/packages/core/src/CodeBlock/CodeBlock.doc.mjs
index 08ddef425a..43c9378e6a 100644
--- a/packages/core/src/CodeBlock/CodeBlock.doc.mjs
+++ b/packages/core/src/CodeBlock/CodeBlock.doc.mjs
@@ -142,6 +142,8 @@ export const docs = {
targets: [
{className: 'astryx-code', visualProps: ['color']},
{className: 'astryx-codeblock', visualProps: ['size', 'language', 'container']},
+ {className: 'astryx-codeblock-header', visualProps: ['size', 'language', 'container']},
+ {className: 'astryx-codeblock-title', visualProps: ['size', 'language']},
{className: 'astryx-codeblock-copy-button'},
],
},
diff --git a/packages/core/src/CodeBlock/CodeBlock.test.tsx b/packages/core/src/CodeBlock/CodeBlock.test.tsx
index 1e1e5ce045..a263ad464b 100644
--- a/packages/core/src/CodeBlock/CodeBlock.test.tsx
+++ b/packages/core/src/CodeBlock/CodeBlock.test.tsx
@@ -15,6 +15,13 @@ import {CodeBlock} from './CodeBlock';
import {__resetLiveRegionsForTest} from '../hooks/useAnnounce';
import {dracula} from '../theme/syntax';
import {InternationalizationProvider} from '../i18n';
+import {defineTheme} from '../theme/defineTheme';
+import {generateThemeCSS} from '../theme/generateThemeRules';
+
+function generateThemeTestCSS(theme: Parameters<typeof generateThemeCSS>[0]) {
+ const {prose, component} = generateThemeCSS(theme);
+ return [prose, component].filter(Boolean).join('\n\n');
+}
function politeRegion(): HTMLElement | null {
return document.querySelector('[data-astryx-live-region="polite"]');
@@ -328,4 +335,64 @@ describe('CodeBlock', () => {
expect(container.querySelector('[data-astryx-syntax-theme]')).toBeNull();
expect(container.firstElementChild?.tagName).toBe('PRE');
});
+
+ describe('header theming targets', () => {
+ it('puts astryx-codeblock-header on the header row when a header shows', () => {
+ const {container} = render(
+ <CodeBlock
+ code="const x = 1;"
+ language="javascript"
+ title="example.js"
+ />,
+ );
+ expect(
+ container.querySelector('.astryx-codeblock-header'),
+ ).not.toBeNull();
+ });
+
+ it('puts astryx-codeblock-title on the header title element', () => {
+ const {container} = render(
+ <CodeBlock
+ code="const x = 1;"
+ language="javascript"
+ title="example.js"
+ />,
+ );
+ const titleEl = container.querySelector('.astryx-codeblock-title');
+ expect(titleEl).not.toBeNull();
+ // The language label + title text live in this element.
+ expect(titleEl).toHaveTextContent('example.js');
+ });
+
+ it('renders no header targets when there is no header', () => {
+ // plaintext hides the language label and no title is given, so the
+ // header row is not rendered at all.
+ const {container} = render(
+ <CodeBlock code="const x = 1;" language="plaintext" />,
+ );
+ expect(container.querySelector('.astryx-codeblock-header')).toBeNull();
+ expect(container.querySelector('.astryx-codeblock-title')).toBeNull();
+ });
+
+ 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
+ // the structural `> div:first-child > div > span` header/title selectors a
+ // consumer would otherwise need to restyle the header padding and title.
+ const theme = defineTheme({
+ name: 'codeblock-header-target-test',
+ components: {
+ 'codeblock-header': {
+ base: {paddingBlock: 'var(--spacing-1)'},
+ },
+ 'codeblock-title': {
+ base: {fontSize: 'var(--text-body-size)'},
+ },
+ },
+ });
+ const css = generateThemeTestCSS(theme);
+ expect(css).toContain('.astryx-codeblock-header');
+ expect(css).toContain('.astryx-codeblock-title');
+ });
+ });
});
diff --git a/packages/core/src/CodeBlock/CodeBlock.tsx b/packages/core/src/CodeBlock/CodeBlock.tsx
index 28e36fe861..4c58858f26 100644
--- a/packages/core/src/CodeBlock/CodeBlock.tsx
+++ b/packages/core/src/CodeBlock/CodeBlock.tsx
@@ -813,9 +813,12 @@ export function CodeBlock({
const headerEl = showHeader ? (
<div
- {...stylex.props(
- styles.headerRow,
- hasLineNumbers ? styles.headerWithDivider : styles.headerCompact,
+ {...mergeProps(
+ themeProps('codeblock-header', {size, language, container}),
+ stylex.props(
+ styles.headerRow,
+ hasLineNumbers ? styles.headerWithDivider : styles.headerCompact,
+ ),
)}>
<div
role={canCollapse ? 'button' : undefined}
@@ -837,7 +840,11 @@ export function CodeBlock({
styles.header,
canCollapse && styles.headerCollapsible,
)}>
- <span {...stylex.props(styles.headerTitle)}>
+ <span
+ {...mergeProps(
+ themeProps('codeblock-title', {size, language}),
+ stylex.props(styles.headerTitle),
+ )}>
{canCollapse && (
<span {...stylex.props(styles.collapseChevron)}>
<Icon