Skip to content

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

TypeScript

feat(Markdown): contentAlign prop, blockIndent for lists/tables, blockquote border (#1290)

Cindy Zhang4a565f12ab62
diff --git a/apps/storybook/stories/Markdown.stories.tsx b/apps/storybook/stories/Markdown.stories.tsx
index 368d718e4d..f99627e6af 100644
--- a/apps/storybook/stories/Markdown.stories.tsx
+++ b/apps/storybook/stories/Markdown.stories.tsx
@@ -254,3 +254,77 @@ export const Streaming: Story = {
);
},
};
+
+export const WithImages: Story = {
+ name: 'With Images',
+ render: () => (
+ <div style={{maxWidth: 800}}>
+ <XDSMarkdown>{`
+Here is some text before the image.
+
+![A landscape photo](https://images.unsplash.com/photo-1506744038136-46273834b3fb?w=680&h=400&fit=crop&auto=format)
+
+Text between two images.
+
+![A tall portrait photo](https://images.unsplash.com/photo-1470071459604-3b5ec3a7fe05?w=400&h=600&fit=crop&auto=format)
+
+And here's a really wide one:
+
+![Wide panoramic shot](https://images.unsplash.com/photo-1472214103451-9374bd1c798e?w=1200&h=300&fit=crop&auto=format)
+
+Final paragraph after all images.
+`}</XDSMarkdown>
+ </div>
+ ),
+};
+
+const CONTENT_ALIGN_TEXT = `
+# Content Alignment
+
+This paragraph is constrained by \`contentWidth\`. Notice how it's narrower than the code block and table below. The alignment prop controls where this narrow prose sits within the wider container.
+
+Here's a bullet list that also respects prose width:
+- First item with some explanation text
+- Second item that wraps to show the width constraint
+- Third item for good measure
+
+\`\`\`typescript
+// Code blocks break out to full container width regardless of contentAlign
+export function calculateLayout(items: Item[], containerWidth: number): Layout {
+ const columns = Math.floor(containerWidth / COLUMN_MIN_WIDTH);
+ return { columns, gap: GRID_GAP, items: distributeItems(items, columns) };
+}
+\`\`\`
+
+Back to prose — this paragraph is aligned according to the \`contentAlign\` prop while the code block above spans the full width.
+
+| Component | Status | Notes |
+|-----------|--------|-------|
+| Button | Stable | Full API |
+| CodeBlock | Stable | With collapsible |
+| Markdown | In progress | Adding alignment |
+
+Final paragraph after the table.
+`;
+
+export const ContentAlignStart: Story = {
+ name: 'Content Align: Start',
+ render: () => (
+ <div style={{maxWidth: 900, border: '1px dashed #ccc', padding: 16}}>
+ <XDSMarkdown contentWidth={580} contentAlign="start">
+ {CONTENT_ALIGN_TEXT}
+ </XDSMarkdown>
+ </div>
+ ),
+};
+
+export const ContentAlignCenter: Story = {
+ name: 'Content Align: Center',
+ render: () => (
+ <div style={{maxWidth: 900, border: '1px dashed #ccc', padding: 16}}>
+ <XDSMarkdown contentWidth={580} contentAlign="center">
+ {CONTENT_ALIGN_TEXT}
+ </XDSMarkdown>
+ </div>
+ ),
+};
diff --git a/packages/core/src/Markdown/XDSMarkdown.tsx b/packages/core/src/Markdown/XDSMarkdown.tsx
index bb9f9c2171..2a2a8cbfb6 100644
--- a/packages/core/src/Markdown/XDSMarkdown.tsx
+++ b/packages/core/src/Markdown/XDSMarkdown.tsx
@@ -95,6 +95,14 @@ export interface XDSMarkdownProps {
* ```
*/
contentWidth?: number | string;
+ /**
+ * Alignment of prose content within the container when `contentWidth`
+ * is narrower than the available space.
+ * - 'start': left-aligned (default)
+ * - 'center': centered
+ * @default 'start'
+ */
+ contentAlign?: 'start' | 'center';
xstyle?: StyleXStyles;
className?: string;
style?: React.CSSProperties;
@@ -105,13 +113,29 @@ export interface XDSMarkdownProps {
// Styles
// ---------------------------------------------------------------------------
+const ALIGN_MARGIN: Record<string, string> = {
+ start: '0',
+ center: 'auto',
+};
+
+const BLOCK_ALIGN_MARGIN: Record<string, string | null> = {
+ start: null,
+ center: 'auto',
+};
+
const dynamicStyles = stylex.create({
proseWidth: (maxWidth: string) => ({
maxWidth,
}),
+ proseAlign: (marginInline: string) => ({
+ marginInline,
+ }),
tableMinWidth: (minWidth: string) => ({
minWidth,
}),
+ blockAlign: (marginInline: string) => ({
+ marginInline,
+ }),
});
const styles = stylex.create({
@@ -242,7 +266,7 @@ const styles = stylex.create({
blockquote: {
borderInlineStartWidth: spacingVars['--spacing-0-5'],
borderInlineStartStyle: 'solid',
- borderInlineStartColor: colorVars['--color-accent'],
+ borderInlineStartColor: colorVars['--color-border-emphasized'],
paddingInlineStart: spacingVars['--spacing-4'],
color: colorVars['--color-text-secondary'],
marginInlineStart: 0,
@@ -253,6 +277,9 @@ const styles = stylex.create({
overflowX: 'auto',
minWidth: 0,
},
+ blockIndent: {
+ marginInline: `calc(-1 * ${spacingVars['--spacing-2']})`,
+ },
table: {
borderCollapse: 'collapse',
width: 'auto',
@@ -802,6 +829,7 @@ function renderBlock(
cursor: StreamingCursor,
citationCtx: CitationContext | null,
contentWidthValue: string | null,
+ contentAlign: 'start' | 'center',
): React.ReactNode {
const spacing = getElementSpacing(node, density);
const isFirst = index === 0;
@@ -827,6 +855,9 @@ function renderBlock(
contentWidthValue != null
? dynamicStyles.proseWidth(contentWidthValue)
: null,
+ contentAlign !== 'start'
+ ? dynamicStyles.proseAlign(ALIGN_MARGIN[contentAlign])
+ : null,
isFirst && styles.noMarginBlockStart,
isLast && styles.noMarginBlockEnd,
)}>
@@ -845,6 +876,9 @@ function renderBlock(
contentWidthValue != null
? dynamicStyles.proseWidth(contentWidthValue)
: null,
+ contentAlign !== 'start'
+ ? dynamicStyles.proseAlign(ALIGN_MARGIN[contentAlign])
+ : null,
isFirst && styles.noMarginBlockStart,
isLast && styles.noMarginBlockEnd,
)}>
@@ -882,6 +916,9 @@ function renderBlock(
contentWidthValue != null
? dynamicStyles.proseWidth(contentWidthValue)
: null,
+ contentAlign !== 'start'
+ ? dynamicStyles.proseAlign(ALIGN_MARGIN[contentAlign])
+ : null,
isFirst && styles.noMarginBlockStart,
isLast && styles.noMarginBlockEnd,
)}>
@@ -896,6 +933,7 @@ function renderBlock(
cursor,
citationCtx,
contentWidthValue,
+ contentAlign,
),
)}
</blockquote>
@@ -924,6 +962,7 @@ function renderBlock(
label="Task list"
isLabelHidden
value={checkedValues}
+ xstyle={styles.blockIndent}
isReadOnly
density="compact">
{node.items.map((item, i) => {
@@ -954,6 +993,7 @@ function renderBlock(
cursor,
citationCtx,
contentWidthValue,
+ contentAlign,
),
)}
</>
@@ -992,12 +1032,16 @@ function renderBlock(
contentWidthValue != null
? dynamicStyles.proseWidth(contentWidthValue)
: null,
+ contentAlign !== 'start'
+ ? dynamicStyles.proseAlign(ALIGN_MARGIN[contentAlign])
+ : null,
isFirst && styles.noMarginBlockStart,
isLast && styles.noMarginBlockEnd,
)}>
<XDSList
listStyle={node.ordered ? 'decimal' : 'disc'}
- density="compact">
+ density="compact"
+ xstyle={styles.blockIndent}>
{node.items.map((item, i) => {
const firstChild = item.children[0];
const isInline =
@@ -1028,6 +1072,7 @@ function renderBlock(
cursor,
citationCtx,
contentWidthValue,
+ contentAlign,
),
)}
</>
@@ -1068,9 +1113,13 @@ function renderBlock(
<table
{...stylex.props(
styles.table,
+ styles.blockIndent,
contentWidthValue != null
? dynamicStyles.tableMinWidth(contentWidthValue)
: null,
+ BLOCK_ALIGN_MARGIN[contentAlign] != null
+ ? dynamicStyles.blockAlign(BLOCK_ALIGN_MARGIN[contentAlign]!)
+ : null,
)}>
<thead>
<tr>
@@ -1184,6 +1233,7 @@ export function XDSMarkdown({
sources,
citationStyle = 'label',
contentWidth = 680,
+ contentAlign = 'start',
xstyle,
className,
style,
@@ -1261,6 +1311,7 @@ export function XDSMarkdown({
? `${contentWidth}px`
: contentWidth
: null,
+ contentAlign,
),
)}
</div>