Skip to content

feat(core): add a width prop to ChatMessageBubble so ghost bubbles align custom in-message content with the text column - #3879

Merged
cixzhang merged 3 commits into
meta:mainfrom
jiunshinn:feat/2574-chatmessage-content-alignment
Aug 15, 2026
Merged

cixzhang merged 3 commits into
meta:mainfrom
jiunshinn:feat/2574-chatmessage-content-alignment

Conversation

@jiunshinn

@jiunshinn jiunshinn commented Jul 12, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Custom content placed inside a ChatMessage (an artifact card, attachment chips, inline citations, a standalone ChatMessageMetadata) cannot align to the bubble's text column. Within one assistant message the left edges differ (issue repro, ai-chat template's artifact composition, 1280px viewport):

message text (bubble):       left = 76px   (message edge 60 + bubble paddingInline 16)
standalone timestamp:        left = 60px   (message edge)
custom artifact card:        left = 60–72px (message edge)

The only workaround is hardcoding marginInlineStart: --spacing-4 on the custom content, which mirrors the bubble's private padding and silently breaks if that padding changes or when density changes. A second facet from the issue thread: custom children also can't opt into the full message-column width — the children wrapper aligns items to the sender side, so custom content shrinks to fit.

Fixes #2574

Chosen API (updated after review): width prop on ChatMessageBubble

Earlier revisions of this PR added a ChatMessageContent subcomponent. Per review feedback, variant="ghost" already owns the text-column inset contract — same density mapping, derived from the same styles — so the new component duplicated it (my own SYNC: comment was the tell). The one genuine gap was the width cap, which is now a prop on the component that owns the contract.

<ChatMessage sender="assistant">
  <ChatMessageBubble>Here is the report you asked for.</ChatMessageBubble>
  <ChatMessageBubble variant="ghost" width="100%">
    <ClickableCard label="Open report" onClick={openReport}>…</ClickableCard>
  </ChatMessageBubble>
</ChatMessage>
  • width?: SizeValue follows the existing sizing convention (Card, Center, Button, AlertDialog, …): numbers are pixels, strings pass through, implemented as a dynamic StyleX style like Card's dynamicStyles.sizing.
  • When set, it replaces the default maxWidth: max(80%, 280px) cap on styles.content; when unset, nothing changes for existing bubbles.
  • It isn't limited to full-column: width={420} for a fixed-width artifact bubble is equally expressible — the reason a SizeValue beats an isFullWidth boolean.
  • On the theme-target question from the review: I looked for a concrete breaking example and couldn't produce one — no shipped theme targets chat-message-bubble, and since variant is reflected in the theme props a theme can always scope rules to [data-variant="filled"]. So I'm not claiming that contract violation; the separate component isn't justified.

Trying it at the ai-chat template call sites

The template's artifact message previously rendered a raw ArtifactCard plus a standalone ChatMessageMetadata, both flush at the message edge next to inset ghost text. Reworked:

<ChatMessageBubble
  variant="ghost"
  width="100%"
  metadata={<ChatMessageMetadata timestamp={…} />}>
  <ArtifactCard onOpen={openArtifact} />
</ChatMessageBubble>

It feels right at the call site: the card's left edge lands on the text column, the standalone timestamp collapses into the existing metadata slot idiom ("metadata on the last bubble"), and the card's hardcoded marginBlockStart workaround style was deleted outright. The card keeps its own maxWidth={360}; the bubble just stops imposing the message-level cap. No bubble typography or --radius-chat leakage mattered in practice — the wrapper is transparent and non-clipping, and the card sets its own type.

Change

  • packages/core/src/Chat/ChatMessageBubble.tsx — width?: SizeValue prop + dynamicStyles.sizing; replaces the width cap only when set.
  • packages/core/src/Chat/ChatMessage.tsx — children JSDoc points at the ghost + width idiom.
  • ChatMessageBubble.doc.mjs / Chat.doc.mjs / ChatMessage.doc.mjs — docs, Zh, and dense variants updated to the ghost + width guidance.
  • apps/storybook/stories/Chat.stories.tsx — Custom Content story now uses a ghost width="100%" bubble with the metadata slot.
  • packages/cli/templates/blocks/components/ChatMessageBubble/ChatMessageBubbleCustomContent.* — block example (replaces the ChatMessageContent block).
  • packages/cli/templates/pages/ai-chat/page.tsx — artifact message adopts the new idiom; hardcoded margin workaround removed.
  • ChatMessageContent (component, tests, doc, block, exports) — removed.
  • @astryxdesign/core patch changeset ([feat]) rewritten for the width prop.

Test plan

Coverage moved into the existing colocated ChatMessageBubble.test.tsx (+4 tests). StyleX emits one atomic class per declaration, so class-identity pins the alignment contract:

  • Repro + fix pin: a raw child shares no class with the bubble's name slot (the issue's misalignment), while a ghost bubble sibling shares the slot's paddingInline class.
  • Density-aware: balanced and spacious ghost insets each match their own bubble's slot and differ from each other.
  • Width: default bubble keeps max(80%, 280px); width="100%" resolves maxWidth: none and passes the string through; width={420} renders 420px.

Results: Chat suite 10 files / 131 tests green; CLI template + component doc tests 85 green; pnpm -F @astryxdesign/core typecheck clean; core build green; CLI typecheck:template-docs clean; sync:exports --check clean; check:changesets valid; astryx component ChatMessageBubble --dense renders the width prop and the new block. Storybook typecheck has only the pre-existing unbuilt-theme-package module errors; none in Chat.stories.tsx.

@vercel

vercel Bot commented Jul 12, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
astryx Ready Ready Preview Aug 15, 2026 7:49am

Request Review

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Jul 12, 2026
@github-actions github-actions Bot added community Authored by a community contributor (not on the eng/design team) needs:code-review High-risk change (new package/component/API) — needs human code review before merge needs:design-review Affects visuals — Design should review labels Jul 12, 2026
@jiunshinn
jiunshinn marked this pull request as ready for review July 12, 2026 10:37
@github-actions

github-actions Bot commented Jul 12, 2026 •

Copy link
Copy Markdown
Contributor

PR Analysis Report

📚 Storybook Preview

View Storybook for this PR
GitHub Pages may take up to a minute to hydrate after deploy.

🧪 Sandbox Preview

View Sandbox for this PR
GitHub Pages may take up to a minute to hydrate after deploy.

Modified Components

Chat (@astryxdesign/core) · View in Storybook
Metric Before After Delta
Bundle Size (ESM) N/A N/A N/A
Lines of Code N/A 5383 -
Complexity N/A Very High (439) -

Bundle Size Summary

Package Size (ESM) Size (CJS) Gzipped
@astryxdesign/core N/A 4.7KB 1.2KB

Accessibility Audit

Status: 1 accessibility violation(s) found — 1 serious.

Chat - 1 issue(s)
  • 🟠 serious: Ensure the contrast between foreground and background colors meets WCAG 2 AA minimum contrast ratio thresholds
    • Rule: color-contrast · Affects 2/9 stories · Learn more
    • WCAG: 1.4.3 (Level AA)

Generated by PR Enrichment workflow | Storybook | Sandbox | View full report

@cixzhang

Copy link
Copy Markdown
Contributor

Thanks for this, and apologies for the slow response — the diagnosis here is solid and the misalignment you documented (76px vs 60px within one message) is a real bug worth fixing.

Before adding a new exported subcomponent, though, I'd like to try the existing surface. ChatMessageBubble already has variant="ghost", documented as "use ghost when content needs alignment without a visible boundary" — which is close to word-for-word the problem this PR opens with. Comparing it against ChatMessageContent:

<ChatMessageBubble variant="ghost"> ChatMessageContent
Transparent background ✅ ✅
Inline inset (4 / 4 / 5 per density) ✅ ✅ identical
Block padding zeroed ✅ paddingBlockNone ✅
Full message-column width ❌ maxWidth: max(80%, 280px) ✅ alignSelf: stretch

So of the three things the new component does, ghost already does two — with the same density mapping, derived from the same place. The one genuine gap is width: styles.content caps every bubble at max(80%, 280px), which is correct for text and wrong for an artifact card that should fill the column.

That points at a prop on the bubble rather than a new component. Something like isFullWidth (matching our is* boolean convention — and worth aligning with the isFullBleed naming you proposed in #3938, whichever we settle on) that drops the max-width cap. That fixes the actual gap in the component that already owns the padding contract, and avoids a second place encoding it — note your new file already carries a SYNC: comment saying its inset must be kept in lockstep with ChatMessageBubble.tsx. Two sources for one contract is the kind of thing that quietly drifts.

One counter-argument I want to put on the record, because it's the strongest case for a separate element and the PR doesn't currently make it: a ghost bubble still emits themeProps('chat-message-bubble', {sender, variant, density}), so theme rules authored for bubbles would also style an artifact card wrapped in one. If you think that's a real contract violation rather than a theoretical one, say so and make that the argument — it's a much better justification than alignment, which ghost already handles. I'd want a concrete example of a theme rule that breaks.

Could you try the bubble-prop version and see how it feels at the call sites in the ai-chat-artifact template? If it turns out ghost genuinely can't carry this — bubble typography or --radius-chat leaking into cards in a way that matters, say — that's useful information and I'll reconsider the separate component.

@cixzhang

Copy link
Copy Markdown
Contributor

Correction to my comment above — ignore the isFullWidth suggestion. We already have a convention for this and it's better than a boolean.

Layout and container components take a width prop typed SizeValue (number | string — numbers are pixels, strings pass through, so width="100%"). Card, Center, Button, AlertDialog, CheckboxInput, and CheckboxList all do this today, implemented as a dynamic StyleX style (see Card.tsx's dynamicStyles.sizing). Card also carries maxWidth alongside it.

So the shape I'd like on ChatMessageBubble is:

<ChatMessageBubble variant="ghost" width="100%">
  <ClickableCard label="Open report" onClick={openReport}>…</ClickableCard>
</ChatMessageBubble>

width should override the default maxWidth: max(80%, 280px) cap on styles.content when set, and leave it untouched when it isn't — so nothing changes for existing bubbles.

Two reasons to prefer this over a boolean: it matches what a consumer already knows from every other sizing-capable component, and it isn't limited to the one case — width="600px" on an artifact bubble is just as reasonable as full-column, and a boolean can't express it.

Everything else in my comment stands: I'd rather extend the existing bubble than add ChatMessageContent, and I'm still interested in the theme-target argument if you think it's the real justification.

…geBubble (meta#2574)

Per review: ghost bubbles already own the text-column inset contract, so
extend the bubble instead of adding a second component that mirrors its
padding. width follows the Card/Center SizeValue convention (number = px,
string = as-is) and replaces the max(80%, 280px) cap only when set.
@vercel

vercel Bot commented Aug 11, 2026

Copy link
Copy Markdown

@jiunshinn is attempting to deploy a commit to the Meta Open Source Team on Vercel.

A member of the Team first needs to authorize it.

@github-actions github-actions Bot removed the needs:design-review Affects visuals — Design should review label Aug 11, 2026
@jiunshinn jiunshinn changed the title feat(core): add ChatMessageContent to align custom in-message content with the bubble text column feat(core): add a width prop to ChatMessageBubble so ghost bubbles align custom in-message content with the text column Aug 11, 2026
@jiunshinn

Copy link
Copy Markdown
Contributor Author

Thanks for the thorough review — you were right on the merits, and the PR is reworked to the bubble-prop approach (58fa96d).

  • width?: SizeValue on ChatMessageBubble, per your follow-up: numbers are pixels, strings pass through, implemented as a dynamic StyleX style following Card's dynamicStyles.sizing. When set it replaces the max(80%, 280px) cap; unset bubbles are byte-for-byte unchanged.
  • ChatMessageContent is gone — component, tests, docs, block example, exports. The SYNC: duplication you flagged goes with it; the inset contract now has exactly one owner.
  • On the theme-target argument you invited: I tried to make it concrete and couldn't. No shipped theme targets chat-message-bubble today, and variant is reflected in the emitted theme props, so a theme that cares can always scope its rules to [data-variant="filled"]. I don't think the argument holds, so I'm not claiming it.
  • Tried it at the ai-chat artifact call sites as asked: the raw ArtifactCard + standalone timestamp became a ghost width="100%" bubble with the timestamp on the bubble's metadata slot — which also let me delete the card's hardcoded marginBlockStart workaround style. It honestly reads better than the ChatMessageContent version did: the existing "metadata on the last bubble" idiom applies instead of needing a second wrapper element. Nothing leaked in practice — the ghost wrapper is transparent and non-clipping, and the card sets its own typography, so --radius-chat and bubble type never show.
  • Naming note from your first comment: with width there's no boolean left to align with feat(tablist): add isFullBleed to bleed a tab bar to container edges #3938's isFullBleed, so that question dissolves here.

One honest residual of ghost-as-wrapper, for the record only: wordBreak: break-word and the bubble's body font inherit into wrapped content. Neither mattered at any call site I touched (cards set their own type), but it's the one place a future consumer could be surprised.

@rubyycheung heads-up — the PR changed substantially since your approval (the ChatMessageContent subcomponent is gone in favor of a width prop on ChatMessageBubble), so please re-review when you get a chance.

@cixzhang cixzhang left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for the update! This looks good!

@github-actions github-actions Bot removed the needs:code-review High-risk change (new package/component/API) — needs human code review before merge label Aug 15, 2026
Resolves the only conflict: main moved the CLI templates tree to
packages/cli/assets/templates/, so this branch's new ChatMessageBubble
custom-content block lands there. Its doc.mjs type import now points at
@astryxdesign/cli/authoring — packages/core/src/docs-types was deleted in
the same move.
@cixzhang
cixzhang force-pushed the feat/2574-chatmessage-content-alignment branch from ac64959 to a0ea9cb Compare August 15, 2026 07:45
@cixzhang
cixzhang enabled auto-merge (squash) August 15, 2026 07:46
@cixzhang

Copy link
Copy Markdown
Contributor

Merged main in and armed auto-merge (squash).

Heads up @jiunshinn — I force-pushed over your merge commit ac64959, sorry for the collision (we were merging at the same minute). Nothing of yours was lost: that commit had no content beyond the merge itself, but the pre-commit hook ran prettier --write over every file the merge staged, so it reformatted ~14 files unrelated to this PR. Since we squash-merge, that churn would have landed in main.

Same resolution otherwise, plus one fix: main moved the CLI templates tree to packages/cli/assets/templates/, and packages/core/src/docs-types was deleted in that move — so ChatMessageBubbleCustomContent.doc.mjs now imports its type from @astryxdesign/cli/authoring, like the other 620 block docs.

Verified locally on the merged tree: packages/core/src/Chat tests pass (178), core typecheck clean, check:repo clean.

@cixzhang
cixzhang merged commit 0ad35c3 into meta:main Aug 15, 2026
19 checks passed
github-actions Bot added a commit that referenced this pull request Aug 15, 2026
@github-actions
github-actions Bot deleted the feat/2574-chatmessage-content-alignment branch August 16, 2026 06:40

This branch was successfully deployed

1 active deployment
Preview — a0ea9cb0 Deployed Aug 15, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Meta Open Source bot. community Authored by a community contributor (not on the eng/design team)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

XDSChatMessage: no way to align custom in-message content (cards, attachments) to the bubble's text column

3 participants