Skip to content

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

TypeScript

feat(CodeBlock): add container prop for width and border control (#2132)

Ernestf0bbb4a07010
diff --git a/apps/storybook/stories/CodeBlock.stories.tsx b/apps/storybook/stories/CodeBlock.stories.tsx
index a44c6211f2..bf6ae85fd3 100644
--- a/apps/storybook/stories/CodeBlock.stories.tsx
+++ b/apps/storybook/stories/CodeBlock.stories.tsx
@@ -29,6 +29,15 @@ const meta: Meta<typeof XDSCodeBlock> = {
options: ['sm', 'md'],
description: 'Text size',
},
+ width: {
+ control: 'text',
+ description: 'Width of the code block (any CSS width value)',
+ },
+ container: {
+ control: 'select',
+ options: ['card', 'section'],
+ description: 'Container presentation style',
+ },
hasLineNumbers: {control: 'boolean'},
hasCopyButton: {control: 'boolean'},
isWrapped: {control: 'boolean'},
@@ -278,3 +287,22 @@ It preserves whitespace and line breaks.
hasLineNumbers: true,
},
};
+
+export const FullWidth: Story = {
+ args: {
+ code: tsExample,
+ language: 'typescript',
+ title: 'useUser.ts',
+ width: '100%',
+ },
+};
+
+export const ContainerSection: Story = {
+ args: {
+ code: tsExample,
+ language: 'typescript',
+ title: 'useUser.ts',
+ width: '100%',
+ container: 'section',
+ },
+};
diff --git a/packages/core/src/CodeBlock/XDSCodeBlock.doc.mjs b/packages/core/src/CodeBlock/XDSCodeBlock.doc.mjs
index 03175fccd5..15b21a9bae 100644
--- a/packages/core/src/CodeBlock/XDSCodeBlock.doc.mjs
+++ b/packages/core/src/CodeBlock/XDSCodeBlock.doc.mjs
@@ -21,6 +21,8 @@ export const docs = {
{name: 'isWrapped', type: 'boolean', description: 'Wrap long lines instead of enabling horizontal scroll.', default: 'false'},
{name: 'maxHeight', type: 'number | string', description: 'Max height before the block scrolls vertically.'},
{name: 'size', type: "'sm' | 'md'", description: 'Text size variant.', default: "'md'"},
+ {name: 'width', type: "string", description: "Width of the code block. Any CSS width value. 'fit-content' (default) shrinks to longest line. '100%' fills parent width.", default: "'fit-content'"},
+ {name: 'container', type: "'card' | 'section'", description: "Container presentation style. 'card' (default): border and radius for standalone card look. 'section': no border or radius — for embedding inside cards or panels.", default: "'card'"},
{name: 'tokenizer', type: '(code: string, language: string) => Array<{type: string; start: number; end: number}>', description: 'Custom tokenizer override for unsupported languages.'},
{name: 'isCollapsible', type: 'boolean', description: 'Allow collapsing the code body into just the header bar. Starts expanded; the header becomes clickable to toggle. Only shows the toggle when the code exceeds collapsibleThreshold lines.', default: 'false'},
{name: 'collapsibleThreshold', type: 'number', description: 'Minimum number of lines before the collapse toggle appears. Below this threshold the code block renders normally even when isCollapsible is true.', default: '10'},
@@ -101,6 +103,8 @@ export const docsZh = {
isWrapped: '换行显示长行,而非启用水平滚动。',
maxHeight: '代码块垂直滚动前的最大高度。',
size: '文字大小变体。',
+ width: "代码块宽度。接受任何CSS宽度值。'fit-content'(默认)适应最长行宽度;'100%' 填满父容器宽度。",
+ container: "容器展示样式。'card'(默认)=带边框和圆角;'section'=无边框和圆角——适用于嵌入卡片或面板。",
tokenizer: '用于不支持语言的自定义分词器。',
isCollapsible: '允许将代码折叠为仅标题栏。当代码超过 collapsibleThreshold 行时显示切换按钮。',
collapsibleThreshold: '折叠切换出现前的最小行数。低于此阈值时即使 isCollapsible 为 true 也正常渲染。',
@@ -153,6 +157,8 @@ export const docsDense = {
isWrapped: 'wrap long lines vs h-scroll',
maxHeight: 'max height before vertical scroll',
size: 'text size variant',
+ width: "any CSS width value; fit-content (default) or 100% to fill parent",
+ container: "card=border+radius (default); section=no border/radius (embed in cards)",
tokenizer: 'custom tokenizer for unsupported languages',
isCollapsible: 'Collapse code to header bar. Toggle appears when lines > collapsibleThreshold. Default: false.',
collapsibleThreshold: 'Min lines before collapse toggle shows. Default: 10.',
diff --git a/packages/core/src/CodeBlock/XDSCodeBlock.tsx b/packages/core/src/CodeBlock/XDSCodeBlock.tsx
index 9aafb830d3..013f5ff1b6 100644
--- a/packages/core/src/CodeBlock/XDSCodeBlock.tsx
+++ b/packages/core/src/CodeBlock/XDSCodeBlock.tsx
@@ -46,6 +46,29 @@ import {applyHighlightRangesChunked} from './highlightRanges';
// Styles
// ---------------------------------------------------------------------------
+const containerStyles = stylex.create({
+ card: {
+ borderRadius: radiusVars['--radius-element'],
+ borderWidth: borderVars['--border-width'],
+ borderStyle: 'solid',
+ borderColor: colorVars['--color-border'],
+ },
+ section: {
+ borderRadius: 0,
+ borderWidth: 0,
+ borderStyle: 'none',
+ borderColor: 'transparent',
+ },
+});
+
+const dynamicStyles = stylex.create({
+ width: (value: string) => ({
+ width: value,
+ minWidth: value === 'fit-content' ? 'min(100%, 400px)' : null,
+ maxWidth: value === 'fit-content' ? '100%' : null,
+ }),
+});
+
const styles = stylex.create({
root: {
position: 'relative',
@@ -53,14 +76,7 @@ const styles = stylex.create({
display: 'flex',
flexDirection: 'column',
margin: 0,
- width: 'fit-content',
- minWidth: 'min(100%, 400px)',
- maxWidth: '100%',
- borderRadius: radiusVars['--radius-element'],
backgroundColor: 'var(--color-syntax-background)',
- borderWidth: borderVars['--border-width'],
- borderStyle: 'solid',
- borderColor: colorVars['--color-border'],
overflow: 'hidden',
},
header: {
@@ -322,6 +338,21 @@ export interface XDSCodeBlockProps extends XDSBaseProps<HTMLPreElement> {
isCollapsible?: boolean;
collapsibleThreshold?: number;
size?: 'sm' | 'md';
+ /**
+ * Width of the code block. Accepts any CSS width value.
+ * - `'fit-content'` (default): shrinks to the width of the longest line (with a min-width floor).
+ * - `'100%'`: stretches to fill the parent container width.
+ * - Any valid CSS width string (e.g. `'600px'`, `'50vw'`).
+ * @default 'fit-content'
+ */
+ width?: string;
+ /**
+ * Container presentation style.
+ * - `'card'` (default): border-radius and border — standalone card look.
+ * - `'section'`: no border-radius, no border — for embedding inside cards or panels.
+ * @default 'card'
+ */
+ container?: 'card' | 'section';
tokenizer?: (
code: string,
language: string,
@@ -542,6 +573,8 @@ export function XDSCodeBlock({
isCollapsible = false,
collapsibleThreshold = 10,
size = 'md',
+ width: widthProp = 'fit-content',
+ container = 'card',
tokenizer: customTokenizer,
highlightMode = 'auto',
xstyle,
@@ -720,8 +753,13 @@ export function XDSCodeBlock({
<pre
ref={ref}
{...mergeProps(
- xdsClassName('codeblock', {size, language}),
- stylex.props(styles.root, xstyle),
+ xdsClassName('codeblock', {size, language, container}),
+ stylex.props(
+ styles.root,
+ dynamicStyles.width(widthProp),
+ containerStyles[container],
+ xstyle,
+ ),
className,
style,
)}