An open source design system that's fully customizable and agent ready
TypeScript
fix(Chat): align message-bubble tokens the way the composer does (#5402)
Cindy Zhang98ea5baeccbf
diff --git a/.changeset/fix-chat-bubble-token-alignment.md b/.changeset/fix-chat-bubble-token-alignment.mdnew file mode 100644index 0000000000..f3fce05847--- /dev/null+++ b/.changeset/fix-chat-bubble-token-alignment.md@@ -0,0 +1,7 @@+---+'@astryxdesign/core': patch+---++[fix] Chat: a token in a message bubble now sits on the line the way it does in the composer. `ChatTokenizedText` wraps each token in the same `inline-flex` / `vertical-align: middle` box `ChatComposerInput` uses, so a chip stops lifting off the text the moment the message is sent. Follows #5324, which fixed the composer half.++@cixzhangdiff --git a/apps/storybook/stories/ChatTokenizedText.stories.tsx b/apps/storybook/stories/ChatTokenizedText.stories.tsxindex 04796b1a9c..aae6387e9c 100644--- a/apps/storybook/stories/ChatTokenizedText.stories.tsx+++ b/apps/storybook/stories/ChatTokenizedText.stories.tsx@@ -3,6 +3,8 @@import type {Meta, StoryObj} from '@storybook/react';import {ChatTokenizedText} from '@astryxdesign/core/Chat';import {ChatMessage, ChatMessageBubble} from '@astryxdesign/core/Chat';+import {Icon} from '@astryxdesign/core/Icon';+import {UserCircleIcon} from '@heroicons/react/24/solid';const meta: Meta<typeof ChatTokenizedText> = {title: 'Core/ChatTokenizedText',@@ -96,3 +98,43 @@ export const TokensAtEdges: Story = {</ChatMessage>),};++/** Tokens carrying an icon, and a fully custom token — both sit on the line */+export const IconAndCustomTokens: Story = {+ render: () => (+ <ChatMessage sender="user">+ <ChatMessageBubble>+ <ChatTokenizedText+ tokens={[+ {+ value: '@cindy',+ label: '@Cindy Zhang',+ variant: 'blue',+ icon: <Icon icon={UserCircleIcon} size="sm" />,+ },+ {+ value: '@navi',+ render: () => (+ <span+ style={{+ display: 'inline-flex',+ alignItems: 'center',+ gap: 4,+ height: 20,+ padding: '0 8px',+ borderRadius: 999,+ background: '#e8def8',+ fontSize: 12,+ }}>+ <span aria-hidden>★</span>+ @Navi+ </span>+ ),+ },+ ]}>+ Hey @cindy and @navi can you take a look?+ </ChatTokenizedText>+ </ChatMessageBubble>+ </ChatMessage>+ ),+};diff --git a/packages/core/src/Chat/ChatTokenizedText.tsx b/packages/core/src/Chat/ChatTokenizedText.tsxindex e696cc217a..7b6bf87df5 100644--- a/packages/core/src/Chat/ChatTokenizedText.tsx+++ b/packages/core/src/Chat/ChatTokenizedText.tsx@@ -14,6 +14,8 @@** SYNC: When modified, update:* - /packages/core/src/Chat/index.ts+ * - /packages/core/src/Chat/ChatComposerInput.tsx (token alignment must match,+ * or a token moves when the message is sent)* - /packages/cli/assets/templates/blocks/components/ChatTokenizedText/ (block examples)*/@@ -33,6 +35,10 @@ const styles = stylex.create({root: {display: 'inline',},+ token: {+ display: 'inline-flex',+ verticalAlign: 'middle',+ },});// =============================================================================@@ -156,16 +162,17 @@ function renderTokens(text: string, tokens: ChatComposerToken[]): ReactNode[] {const token = tokenMap.get(matched);if (token) {parts.push(- isCustomToken(token) ? (- <span key={`${matched}-${match.index}`}>{token.render()}</span>- ) : (- <Badge- key={`${matched}-${match.index}`}- label={token.label}- variant={token.variant}- icon={token.icon}- />- ),+ <span key={`${matched}-${match.index}`} {...stylex.props(styles.token)}>+ {isCustomToken(token) ? (+ token.render()+ ) : (+ <Badge+ label={token.label}+ variant={token.variant}+ icon={token.icon}+ />+ )}+ </span>,);}