Skip to content

refactor(react): use declarative HTML for known Text variants#1529

Open
chenman9226 wants to merge 1 commit into
a2ui-project:mainfrom
acoder-ai-infra:fix/react-text-variant-declarative-html
Open

refactor(react): use declarative HTML for known Text variants#1529
chenman9226 wants to merge 1 commit into
a2ui-project:mainfrom
acoder-ai-infra:fix/react-text-variant-declarative-html

Conversation

@chenman9226
Copy link
Copy Markdown

@chenman9226 chenman9226 commented Jun 3, 2026

Description

Refactors React v0.9 Text component to render known variants (h1–h5, caption) using declarative HTML elements instead of prepending Markdown syntax (# , ## , *...*) and relying on the injected MarkdownRenderer. This aligns the React renderer with the Angular fix in #1502.

Markdown rendering is still used for unknown/default variants (like 'body').

Pre-launch Checklist

If you need help, consider asking for advice on the discussion board.

Known variants (h1–h5, caption) now render using native HTML elements
directly instead of prepending Markdown syntax and relying on the
Markdown renderer. This aligns the React renderer with the Angular
fix in a2ui-project#1502.

Markdown rendering is still used for unknown/default variants (e.g.
'body') to preserve rich text support.

Fixes: a2ui-project#1516
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request refactors the Text component to render known typography variants (such as h1h5 and caption) directly as HTML tags instead of wrapping them in Markdown syntax and parsing them. Feedback suggests extracting the Markdown rendering logic into a separate helper component (MarkdownText) to avoid calling the useMarkdown hook unconditionally, which prevents unnecessary overhead and potential console warnings when rendering known variants.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment thread renderers/react/src/v0_9/catalog/basic/components/Text.tsx
Copy link
Copy Markdown
Collaborator

@ditman ditman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the contribution, this is great! Can you please also update the CHANGELOG.md, so users of the @a2ui/react package are aware of this change? Thanks!

return text;
}
};
const NON_MARKDOWN_VARIANTS = new Set(['h1', 'h2', 'h3', 'h4', 'h5', 'caption']);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, add a JSDoc comment above this constant to explain what it's used for, something like: 'These variants are directly rendered with HTML markup, instead of being handled by the markdown processor' or similar!

const markdownText = handleVariant(text, props.variant);
const renderedHtml = useMarkdown(markdownText);
const variant = props.variant;
const isKnownVariant = variant !== undefined && NON_MARKDOWN_VARIANTS.has(variant);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're not saving a lot by checking variant !== undefined before asking the set if it has it IMO. This could be simplified to just the has check.

const variant = props.variant;
const isKnownVariant = variant !== undefined && NON_MARKDOWN_VARIANTS.has(variant);

const renderedHtml = useMarkdown(text);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do not renderHtml(text) until it's needed, move this to under the if(isKnownVariant) (right when it's needed)

Comment on lines +39 to +40
const isCaption = variant === 'caption';
const classes = [styles.a2uiText, isCaption ? styles.a2uiCaption : variant || 'body'];
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These seem to only be used inside if(isKnownVariant), can you move these two consts to inside the if block?

Comment on lines -69 to -73
: {children: markdownText};
: {children: text};

if (isCaption) {
return <span className={classes.join(' ')} style={style} {...contentProps} />;
}
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great, I need to backport this to Lit as well!

const h1 = view.container.querySelector('div.h1');
expect(h1).not.toBeNull();
expect(h1?.textContent).toBe('# Title');
expect(h1?.textContent).toBe('Title');
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Chef's kiss!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Markdown rendering in Text component corrupts plain-text content in structured UI (buttons, labels, prices)

2 participants