Skip to content

Commit a071479

Browse files
committed
feat(Comments): a typingIndicator render prop shows who is typing
Comments drew typing state with Lab's ChatTypingIndicator, so Comments and CommentThread imported a component that stays in Lab. `typingIndicator` now renders it: while an open thread's `typingUsers` name someone besides the viewer, Comments passes those people, each once, and places what the function returns at the end of the thread. It follows `emojiPicker`'s shape. Without it, typing shows nothing. The new `CommentTypingIndicatorRenderProps` type names what the function receives. Docs, the Live state story, and the panel showcase pass ChatTypingIndicator.
1 parent af30358 commit a071479

13 files changed

Lines changed: 121 additions & 52 deletions

‎apps/storybook/stories/Comments.stories.tsx‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {VStack} from '@astryxdesign/core/Layout';
66
import {Popover} from '@astryxdesign/core/Popover';
77
import {Button} from '@astryxdesign/core/Button';
88
import {
9+
ChatTypingIndicator,
910
CommentThread,
1011
Comments,
1112
type CommentThreadData,
@@ -167,6 +168,9 @@ export const LiveState: Story = {
167168
}}
168169
currentUser={VIEWER}
169170
isSelected
171+
typingIndicator={({users}) => (
172+
<ChatTypingIndicator names={users.map(user => user.name)} />
173+
)}
170174
onSubmit={() => {}}
171175
/>
172176
</VStack>

‎packages/lab/blocks/CommentsShowcase.doc.mjs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ export default {
1111
isReady: true,
1212
isShowcase: true,
1313
aspectRatio: 3 / 4,
14-
componentsUsed: ['Comments', 'EmojiPicker', 'VStack'],
14+
componentsUsed: ['Comments', 'EmojiPicker', 'ChatTypingIndicator', 'VStack'],
1515
};

‎packages/lab/blocks/CommentsShowcase.tsx‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import {useRef, useState} from 'react';
1515
import {VStack} from '@astryxdesign/core/Layout';
1616
import {
17+
ChatTypingIndicator,
1718
DEFAULT_COMMENT_REACTIONS,
1819
Comments,
1920
EmojiPicker,
@@ -316,6 +317,9 @@ export default function CommentsShowcase() {
316317
onSelectedThreadChange={setSelectedThreadId}
317318
density="compact"
318319
emojiPicker={({onSelect}) => <EmojiPicker onSelect={onSelect} />}
320+
typingIndicator={({users}) => (
321+
<ChatTypingIndicator names={users.map(user => user.name)} />
322+
)}
319323
hasOlderComments={hasOlderComments}
320324
onLoadOlder={() => {
321325
setComments(current => [

‎packages/lab/src/Comments/CommentRow.tsx‎

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,15 @@
55
/**
66
* @file CommentRow.tsx
77
* @input Uses React, StyleX, Astryx Card/Section/Link/Timestamp/Badge,
8-
* useContainerReveal, useClickableContainer, ChatMessageMetadata,
9-
* ChatTypingIndicator, and the Comments family's context and parts
8+
* useContainerReveal, useClickableContainer, ChatMessageMetadata, the
9+
* caller's typing indicator, and the Comments family's context and parts
1010
* @output Exports CommentRow: a thread (its first comment, render-derived open
11-
* replies, typing state, hidden seen metadata, and reply composer, on a
12-
* selectable Card or Section) at depth 0, or a reply at depth 1
11+
* replies, typing state, and reply composer, on a selectable Card or
12+
* Section) at depth 0, or a reply at depth 1
1313
* @position Internal row renderer shared by Comments and CommentThread
1414
*/
1515

16-
import {
17-
useCallback,
18-
useRef,
19-
useState,
20-
type CSSProperties,
21-
type ReactNode,
22-
} from 'react';
16+
import {useCallback, useRef, useState, type ReactNode} from 'react';
2317
import * as stylex from '@stylexjs/stylex';
2418
import {Badge} from '@astryxdesign/core/Badge';
2519
import {Card} from '@astryxdesign/core/Card';
@@ -50,7 +44,6 @@ import {
5044
} from './CommentActions';
5145
import {CommentComposerBase} from './CommentComposer';
5246
import {CommentMarkdown} from './CommentMarkdown';
53-
import {ChatTypingIndicator} from '../Chat/ChatTypingIndicator';
5447
import {
5548
AuthorName,
5649
AvatarSpacer,
@@ -87,10 +80,6 @@ export type ThreadDivider = 'top' | 'bottom';
8780

8881
/** The 28px avatar column (StyleX resolves only same-file constants). */
8982
const AVATAR_SIZE = `calc(${spacingVars['--spacing-6']} + ${spacingVars['--spacing-1']})`;
90-
const TYPING_INDICATOR_STYLE: CSSProperties &
91-
Record<'--astryx-chat-typing-indicator-dots-column-width', string> = {
92-
'--astryx-chat-typing-indicator-dots-column-width': AVATAR_SIZE,
93-
};
9483
export const THREAD_DIVIDERS: readonly ThreadDivider[] = ['top', 'bottom'];
9584

9685
const styles = stylex.create({
@@ -130,9 +119,6 @@ const styles = stylex.create({
130119
flexBasis: 0,
131120
minWidth: 0,
132121
},
133-
typingIndicator: {
134-
width: '100%',
135-
},
136122
// Row controls carry one spacing step of inline padding. Pulling the row
137123
// back by that step puts their glyphs on the body text line, so only their
138124
// hover and pressed fills cross it.
@@ -350,6 +336,7 @@ export function CommentRow({
350336
replyError,
351337
replyTarget,
352338
selectedThreadId,
339+
typingIndicator,
353340
} = scope;
354341
const [isMetadataMenuOpen, setIsMetadataMenuOpen] = useState(false);
355342
const {getContainerProps, getContentRevealProps} = useContainerReveal();
@@ -812,15 +799,14 @@ export function CommentRow({
812799
))}
813800
</VStack>
814801
)}
815-
{depth === 0 && isThreadSelected && typingUsers.length > 0 && (
816-
<HStack width="100%" data-thread-typing-indicator="true">
817-
<ChatTypingIndicator
818-
names={typingUsers.map(user => user.name)}
819-
xstyle={styles.typingIndicator}
820-
style={TYPING_INDICATOR_STYLE}
821-
/>
822-
</HStack>
823-
)}
802+
{depth === 0 &&
803+
isThreadSelected &&
804+
typingIndicator != null &&
805+
typingUsers.length > 0 && (
806+
<HStack width="100%" data-thread-typing-indicator="true">
807+
{typingIndicator({users: typingUsers})}
808+
</HStack>
809+
)}
824810
</VStack>
825811

826812
{depth === 0 &&

‎packages/lab/src/Comments/CommentThread.doc.mjs‎

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export const docs = {
5050
name: 'Live state',
5151
required: false,
5252
description:
53-
'Controlled typing state follows the conversation while the thread is open, with its pulsing dots in the avatar column and its label on the comment-text line.',
53+
'With `typingIndicator`, controlled typing state follows the conversation while the thread is open: the thread passes the people typing, each once and without the viewer.',
5454
},
5555
{
5656
name: 'Reply composer',
@@ -78,7 +78,7 @@ export const docs = {
7878
{
7979
guidance: true,
8080
description:
81-
'Map your collaboration state to `typingUsers`, then publish local reply and edit activity with `onComposerDraftChange`. The thread never opens a connection or expires participants.',
81+
'Map your collaboration state to `typingUsers` and show it with `typingIndicator`, then publish local reply and edit activity with `onComposerDraftChange`. The thread never opens a connection or expires participants.',
8282
},
8383
{
8484
guidance: false,
@@ -111,6 +111,13 @@ export const docs = {
111111
description:
112112
'When a dismissal would discard an unsaved reply or edit, the thread stays open, focus returns to the draft, and a polite live region says the draft is unsaved and that dismissing again discards it. The shake is never the only signal.',
113113
},
114+
{
115+
name: 'Typing indicator',
116+
category: 'Content',
117+
criterion: '4.1.3 Status Messages',
118+
description:
119+
'A standalone thread has no log, so the indicator `typingIndicator` returns says who is typing once per change, politely, unless your page puts the thread inside a live region that already speaks it. It keeps its motion safe under reduced motion.',
120+
},
114121
{
115122
name: 'Reduced motion',
116123
criterion: '2.3.3 Animation from Interactions',
@@ -276,6 +283,12 @@ export const docs = {
276283
description:
277284
'Renders the emoji picker the reply composer opens; pass `onSelect` to it. Without it, there is no emoji button. The picker fills a fixed-width panel with no padding, so it brings its own.',
278285
},
286+
{
287+
name: 'typingIndicator',
288+
type: '(props: {users: readonly CommentUser[]}) => ReactNode',
289+
description:
290+
'Renders the typing indicator at the end of the open thread while `typingUsers` name someone besides the viewer. Without it, typing shows nothing.',
291+
},
279292
{
280293
name: 'renderAnchor',
281294
type: '(comment: CommentThreadData, defaultAnchor: ReactNode) => ReactNode',
@@ -386,7 +399,7 @@ export const docsZh = {
386399
{
387400
guidance: true,
388401
description:
389-
'将协作状态映射到 `typingUsers`,再通过 `onComposerDraftChange` 发布本地的回复和编辑动态。线程从不建立连接,也不会让参与者过期。',
402+
'将协作状态映射到 `typingUsers` 并用 `typingIndicator` 显示,再通过 `onComposerDraftChange` 发布本地的回复和编辑动态。线程从不建立连接,也不会让参与者过期。',
390403
},
391404
{
392405
guidance: false,
@@ -419,6 +432,13 @@ export const docsZh = {
419432
description:
420433
'当关闭会丢弃未保存的回复或编辑时,线程保持打开,焦点回到草稿,礼貌型实时区域会说明草稿尚未保存,再次关闭将丢弃它。抖动从来不是唯一的提示。',
421434
},
435+
{
436+
name: '输入状态指示',
437+
category: 'Content',
438+
criterion: '4.1.3 Status Messages',
439+
description:
440+
'单独呈现的线程没有日志区域,因此 `typingIndicator` 返回的指示器要在每次变化时以礼貌方式播报一次谁在输入,除非页面已把线程放在会播报它的实时区域中。在减少动态效果设置下,其动画也要保持安全。',
441+
},
422442
{
423443
name: '减少动态效果',
424444
criterion: '2.3.3 Animation from Interactions',
@@ -464,6 +484,8 @@ export const docsZh = {
464484
getReplyMention: '回复某条回复时,用提及其作者的内容预填。',
465485
emojiPicker:
466486
'渲染回复输入框打开的表情选择器,并将 `onSelect` 传给它;省略时没有表情按钮。选择器填满一个固定宽度、无内边距的面板,需自带内边距。',
487+
typingIndicator:
488+
'线程展开且 `typingUsers` 中有查看者以外的人时,在线程末尾渲染输入状态指示;省略时不显示输入状态。',
467489
renderAnchor: '替换线程引用的段落。',
468490
renderBody: '替换评论的 Markdown 正文。',
469491
renderVersionBody: '替换编辑历史中某个版本的正文。',
@@ -498,7 +520,7 @@ export const docsDense = {
498520
{
499521
guidance: true,
500522
description:
501-
'Map collaboration state to `typingUsers`; publish local drafts via `onComposerDraftChange`. No connections, no participant expiry.',
523+
'Map collaboration state to `typingUsers`, show it via `typingIndicator`; publish local drafts via `onComposerDraftChange`. No connections, no participant expiry.',
502524
},
503525
{
504526
guidance: false,
@@ -531,6 +553,13 @@ export const docsDense = {
531553
description:
532554
'A dismissal over an unsaved reply/edit keeps the thread open, refocuses the draft, and announces politely. Shake is never the only signal.',
533555
},
556+
{
557+
name: 'Typing indicator',
558+
category: 'Content',
559+
criterion: '4.1.3 Status Messages',
560+
description:
561+
'No log in a standalone thread: the `typingIndicator` result announces who is typing once per change, politely, unless a live region already speaks it. Reduced-motion safe.',
562+
},
534563
{
535564
name: 'Reduced motion',
536565
criterion: '2.3.3 Animation from Interactions',
@@ -576,6 +605,8 @@ export const docsDense = {
576605
getReplyMention: 'Seeds a reply-to-reply with an author mention.',
577606
emojiPicker:
578607
'({onSelect}) => picker for the reply composer (fixed width, no padding: bring your own); none = no emoji button.',
608+
typingIndicator:
609+
'({users}) => typing row at the end of the open thread; none = no typing row.',
579610
renderAnchor: 'Replaces the quoted passage.',
580611
renderBody: 'Replaces a comment’s Markdown body.',
581612
renderVersionBody: 'Replaces a history version’s body.',

‎packages/lab/src/Comments/CommentThread.tsx‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ export function CommentThread({
102102
markdownInlinePlugins,
103103
getReplyMention,
104104
emojiPicker,
105+
typingIndicator,
105106
isSubmitting,
106107
onComposerDraftChange,
107108
onSubmit,
@@ -151,6 +152,7 @@ export function CommentThread({
151152
markdownInlinePlugins,
152153
getReplyMention,
153154
emojiPicker,
155+
typingIndicator,
154156
isSubmitting,
155157
onComposerDraftChange,
156158
onSubmit,

‎packages/lab/src/Comments/Comments.doc.mjs‎

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ export const docs = {
7474
name: 'Live state',
7575
required: false,
7676
description:
77-
'Controlled typing state appears at the end of an open thread, with its pulsing dots in the avatar column and its label on the comment-text line.',
77+
'With `typingIndicator`, controlled typing state appears at the end of an open thread: Comments passes the people typing, each once and without the viewer, and places what the function returns.',
7878
},
7979
{
8080
name: 'Activity event',
@@ -108,7 +108,7 @@ export const docs = {
108108
{
109109
guidance: true,
110110
description:
111-
'Keep live state controlled and transport-neutral: map your collaboration layer to `typingUsers`, and use `onComposerDraftChange` to publish local activity. Keep other per-comment metadata, such as who has seen a comment, beside your own records and look it up by comment id in a render callback. Debounce and expire transient activity in your app.',
111+
'Keep live state controlled and transport-neutral: map your collaboration layer to `typingUsers`, show it with `typingIndicator`, and use `onComposerDraftChange` to publish local activity. Keep other per-comment metadata, such as who has seen a comment, beside your own records and look it up by comment id in a render callback. Debounce and expire transient activity in your app.',
112112
},
113113
{
114114
guidance: false,
@@ -133,7 +133,7 @@ export const docs = {
133133
category: 'Content',
134134
criterion: '4.1.3 Status Messages',
135135
description:
136-
'Typing is a polite status message with reduced-motion-safe dots.',
136+
'Inside the threads log (the default layout), the log announces the indicator\'s text, so the indicator must not announce it again. With `layout="embedded"`, which has no log, the indicator says who is typing once per change, politely, and keeps its motion safe under reduced motion.',
137137
},
138138
{
139139
name: 'Thread toggle',
@@ -445,6 +445,12 @@ export const docs = {
445445
description:
446446
'Renders the emoji picker that composers open from their Add emoji button; pass `onSelect` to it. Without it, composers show no emoji button. The picker fills a fixed-width panel with no padding, so it brings its own.',
447447
},
448+
{
449+
name: 'typingIndicator',
450+
type: '(props: {users: readonly CommentUser[]}) => ReactNode',
451+
description:
452+
'Renders the typing indicator at the end of an open thread while its `typingUsers` name someone besides the viewer. `users` lists each person once, without the viewer. Without it, typing shows nothing.',
453+
},
448454
{
449455
name: 'emptyState',
450456
type: 'ReactNode',
@@ -614,14 +620,17 @@ import {Comments} from '@astryxdesign/lab';
614620
},
615621
{
616622
label: 'Live collaboration state',
617-
code: `import {Comments} from '@astryxdesign/lab';
623+
code: `import {ChatTypingIndicator, Comments} from '@astryxdesign/lab';
618624
619625
<Comments
620626
comments={comments.map(thread => ({
621627
...thread,
622628
typingUsers: typingUsersByThread[thread.id] ?? [],
623629
}))}
624630
currentUser={viewer}
631+
typingIndicator={({users}) => (
632+
<ChatTypingIndicator names={users.map(user => user.name)} />
633+
)}
625634
onSubmit={post}
626635
onComposerDraftChange={(value, context) =>
627636
publishTyping({context, isTyping: value.trim() !== ''})

‎packages/lab/src/Comments/Comments.test.tsx‎

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -575,7 +575,14 @@ describe('Comments rendering', () => {
575575
});
576576

577577
describe('Comments live collaboration seams', () => {
578-
it('shows typing only after the thread opens', async () => {
578+
it('renders typingIndicator once the thread opens, with each typist once and without the viewer', async () => {
579+
const typingIndicator = vi.fn(
580+
({users}: {users: readonly CommentUser[]}) => (
581+
<span data-testid="typing">
582+
{users.map(user => user.name).join(', ')}
583+
</span>
584+
),
585+
);
579586
render(
580587
<Sized>
581588
<Comments
@@ -587,25 +594,31 @@ describe('Comments live collaboration seams', () => {
587594
},
588595
]}
589596
currentUser={VIEWER}
597+
typingIndicator={typingIndicator}
590598
/>
591599
</Sized>,
592600
);
593601

594-
expect(
595-
screen.queryByText('Maya Chen and Release Assistant are typing…'),
596-
).toBeNull();
602+
expect(screen.queryByTestId('typing')).toBeNull();
597603

598604
await click(screen.getByRole('button', {name: 'Thread by Avery Kim'}));
599-
const typingStatus = screen
600-
.getByText('Maya Chen and Release Assistant are typing…')
601-
.closest('.astryx-chat-typing-indicator');
602-
expect(typingStatus).not.toBeNull();
603-
expect(typingStatus).not.toHaveAttribute('role');
604-
expect(
605-
(typingStatus as HTMLElement).style.getPropertyValue(
606-
'--astryx-chat-typing-indicator-dots-column-width',
607-
),
608-
).not.toBe('');
605+
expect(screen.getByTestId('typing').textContent).toBe(
606+
'Maya Chen, Release Assistant',
607+
);
608+
expect(typingIndicator).toHaveBeenLastCalledWith({users: [OTHER, AGENT]});
609+
});
610+
611+
it('shows no typing row without typingIndicator', async () => {
612+
render(
613+
<Sized>
614+
<Comments
615+
comments={[{...COMMENTS[0], replies: [], typingUsers: [OTHER]}]}
616+
currentUser={VIEWER}
617+
/>
618+
</Sized>,
619+
);
620+
await click(screen.getByRole('button', {name: 'Thread by Avery Kim'}));
621+
expect(document.querySelector('[data-thread-typing-indicator]')).toBeNull();
609622
});
610623

611624
it('reports a new-comment draft with its anchor and clears it after posting', async () => {

‎packages/lab/src/Comments/Comments.tsx‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ export function Comments({
217217
markdownInlinePlugins,
218218
getReplyMention,
219219
emojiPicker,
220+
typingIndicator,
220221
isSubmitting,
221222
onComposerDraftChange,
222223
onSubmit,
@@ -319,6 +320,7 @@ export function Comments({
319320
markdownInlinePlugins,
320321
getReplyMention,
321322
emojiPicker,
323+
typingIndicator,
322324
isSubmitting,
323325
onComposerDraftChange,
324326
onSubmit,

0 commit comments

Comments
 (0)