Skip to content

Commit 4e4e565

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 2513fb7 commit 4e4e565

13 files changed

Lines changed: 100 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: 14 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,
@@ -276,6 +276,12 @@ export const docs = {
276276
description:
277277
'Renders the emoji picker the reply composer opens; pass `onSelect` to it. Without it, there is no emoji button.',
278278
},
279+
{
280+
name: 'typingIndicator',
281+
type: '(props: {users: readonly CommentUser[]}) => ReactNode',
282+
description:
283+
'Renders the typing indicator at the end of the open thread while `typingUsers` name someone besides the viewer. Without it, typing shows nothing.',
284+
},
279285
{
280286
name: 'renderAnchor',
281287
type: '(comment: CommentThreadData, defaultAnchor: ReactNode) => ReactNode',
@@ -386,7 +392,7 @@ export const docsZh = {
386392
{
387393
guidance: true,
388394
description:
389-
'将协作状态映射到 `typingUsers`,再通过 `onComposerDraftChange` 发布本地的回复和编辑动态。线程从不建立连接,也不会让参与者过期。',
395+
'将协作状态映射到 `typingUsers` 并用 `typingIndicator` 显示,再通过 `onComposerDraftChange` 发布本地的回复和编辑动态。线程从不建立连接,也不会让参与者过期。',
390396
},
391397
{
392398
guidance: false,
@@ -464,6 +470,8 @@ export const docsZh = {
464470
getReplyMention: '回复某条回复时,用提及其作者的内容预填。',
465471
emojiPicker:
466472
'渲染回复输入框打开的表情选择器,并将 `onSelect` 传给它;省略时没有表情按钮。',
473+
typingIndicator:
474+
'线程展开且 `typingUsers` 中有查看者以外的人时,在线程末尾渲染输入状态指示;省略时不显示输入状态。',
467475
renderAnchor: '替换线程引用的段落。',
468476
renderBody: '替换评论的 Markdown 正文。',
469477
renderVersionBody: '替换编辑历史中某个版本的正文。',
@@ -498,7 +506,7 @@ export const docsDense = {
498506
{
499507
guidance: true,
500508
description:
501-
'Map collaboration state to `typingUsers`; publish local drafts via `onComposerDraftChange`. No connections, no participant expiry.',
509+
'Map collaboration state to `typingUsers`, show it via `typingIndicator`; publish local drafts via `onComposerDraftChange`. No connections, no participant expiry.',
502510
},
503511
{
504512
guidance: false,
@@ -576,6 +584,8 @@ export const docsDense = {
576584
getReplyMention: 'Seeds a reply-to-reply with an author mention.',
577585
emojiPicker:
578586
'({onSelect}) => picker for the reply composer; none = no emoji button.',
587+
typingIndicator:
588+
'({users}) => typing row at the end of the open thread; none = no typing row.',
579589
renderAnchor: 'Replaces the quoted passage.',
580590
renderBody: 'Replaces a comment’s Markdown body.',
581591
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.',
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
@@ -576,7 +576,14 @@ describe('Comments rendering', () => {
576576
});
577577

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

595-
expect(
596-
screen.queryByText('Maya Chen and Release Assistant are typing…'),
597-
).toBeNull();
603+
expect(screen.queryByTestId('typing')).toBeNull();
598604

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

612625
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,

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ export interface CommentsContextValue {
140140
markdownPlugins?: readonly MarkdownPluginEntry[];
141141
markdownInlinePlugins?: readonly MarkdownInlinePlugin[];
142142
emojiPicker?: CommentThreadSharedProps['emojiPicker'];
143+
typingIndicator?: CommentThreadSharedProps['typingIndicator'];
143144
onUploadImages?: CommentImageUploader;
144145
renderAnchor?: CommentThreadSharedProps['renderAnchor'];
145146
renderBody?: CommentThreadSharedProps['renderBody'];
@@ -236,6 +237,7 @@ export function useCommentsController({
236237
markdownInlinePlugins,
237238
getReplyMention,
238239
emojiPicker,
240+
typingIndicator,
239241
isSubmitting = false,
240242
onComposerDraftChange,
241243
onSubmit,
@@ -568,6 +570,7 @@ export function useCommentsController({
568570
markdownPlugins,
569571
markdownInlinePlugins,
570572
emojiPicker,
573+
typingIndicator,
571574
onUploadImages,
572575
renderAnchor,
573576
renderBody,

0 commit comments

Comments
 (0)