From a0714796fd6d479f643c30ea602ac85810b546fc Mon Sep 17 00:00:00 2001 From: Ernest Tien Date: Mon, 23 Nov 2026 17:53:01 -0800 Subject: [PATCH 1/2] 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. --- apps/storybook/stories/Comments.stories.tsx | 4 ++ packages/lab/blocks/CommentsShowcase.doc.mjs | 2 +- packages/lab/blocks/CommentsShowcase.tsx | 4 ++ packages/lab/src/Comments/CommentRow.tsx | 42 +++++++------------ .../lab/src/Comments/CommentThread.doc.mjs | 39 +++++++++++++++-- packages/lab/src/Comments/CommentThread.tsx | 2 + packages/lab/src/Comments/Comments.doc.mjs | 17 ++++++-- packages/lab/src/Comments/Comments.test.tsx | 41 +++++++++++------- packages/lab/src/Comments/Comments.tsx | 2 + packages/lab/src/Comments/CommentsContext.tsx | 3 ++ packages/lab/src/Comments/index.ts | 1 + packages/lab/src/Comments/types.ts | 15 ++++++- packages/lab/src/index.ts | 1 + 13 files changed, 121 insertions(+), 52 deletions(-) diff --git a/apps/storybook/stories/Comments.stories.tsx b/apps/storybook/stories/Comments.stories.tsx index 3db308b954f2e..5cb39f297ed8b 100644 --- a/apps/storybook/stories/Comments.stories.tsx +++ b/apps/storybook/stories/Comments.stories.tsx @@ -6,6 +6,7 @@ import {VStack} from '@astryxdesign/core/Layout'; import {Popover} from '@astryxdesign/core/Popover'; import {Button} from '@astryxdesign/core/Button'; import { + ChatTypingIndicator, CommentThread, Comments, type CommentThreadData, @@ -167,6 +168,9 @@ export const LiveState: Story = { }} currentUser={VIEWER} isSelected + typingIndicator={({users}) => ( + user.name)} /> + )} onSubmit={() => {}} /> diff --git a/packages/lab/blocks/CommentsShowcase.doc.mjs b/packages/lab/blocks/CommentsShowcase.doc.mjs index fc6217ebf9c05..45885cdae4e2a 100644 --- a/packages/lab/blocks/CommentsShowcase.doc.mjs +++ b/packages/lab/blocks/CommentsShowcase.doc.mjs @@ -11,5 +11,5 @@ export default { isReady: true, isShowcase: true, aspectRatio: 3 / 4, - componentsUsed: ['Comments', 'EmojiPicker', 'VStack'], + componentsUsed: ['Comments', 'EmojiPicker', 'ChatTypingIndicator', 'VStack'], }; diff --git a/packages/lab/blocks/CommentsShowcase.tsx b/packages/lab/blocks/CommentsShowcase.tsx index 91e318e199479..34596bd3e3e2c 100644 --- a/packages/lab/blocks/CommentsShowcase.tsx +++ b/packages/lab/blocks/CommentsShowcase.tsx @@ -14,6 +14,7 @@ import {useRef, useState} from 'react'; import {VStack} from '@astryxdesign/core/Layout'; import { + ChatTypingIndicator, DEFAULT_COMMENT_REACTIONS, Comments, EmojiPicker, @@ -316,6 +317,9 @@ export default function CommentsShowcase() { onSelectedThreadChange={setSelectedThreadId} density="compact" emojiPicker={({onSelect}) => } + typingIndicator={({users}) => ( + user.name)} /> + )} hasOlderComments={hasOlderComments} onLoadOlder={() => { setComments(current => [ diff --git a/packages/lab/src/Comments/CommentRow.tsx b/packages/lab/src/Comments/CommentRow.tsx index dcf0a7e3b5876..bf103a0ab8899 100644 --- a/packages/lab/src/Comments/CommentRow.tsx +++ b/packages/lab/src/Comments/CommentRow.tsx @@ -5,21 +5,15 @@ /** * @file CommentRow.tsx * @input Uses React, StyleX, Astryx Card/Section/Link/Timestamp/Badge, - * useContainerReveal, useClickableContainer, ChatMessageMetadata, - * ChatTypingIndicator, and the Comments family's context and parts + * useContainerReveal, useClickableContainer, ChatMessageMetadata, the + * caller's typing indicator, and the Comments family's context and parts * @output Exports CommentRow: a thread (its first comment, render-derived open - * replies, typing state, hidden seen metadata, and reply composer, on a - * selectable Card or Section) at depth 0, or a reply at depth 1 + * replies, typing state, and reply composer, on a selectable Card or + * Section) at depth 0, or a reply at depth 1 * @position Internal row renderer shared by Comments and CommentThread */ -import { - useCallback, - useRef, - useState, - type CSSProperties, - type ReactNode, -} from 'react'; +import {useCallback, useRef, useState, type ReactNode} from 'react'; import * as stylex from '@stylexjs/stylex'; import {Badge} from '@astryxdesign/core/Badge'; import {Card} from '@astryxdesign/core/Card'; @@ -50,7 +44,6 @@ import { } from './CommentActions'; import {CommentComposerBase} from './CommentComposer'; import {CommentMarkdown} from './CommentMarkdown'; -import {ChatTypingIndicator} from '../Chat/ChatTypingIndicator'; import { AuthorName, AvatarSpacer, @@ -87,10 +80,6 @@ export type ThreadDivider = 'top' | 'bottom'; /** The 28px avatar column (StyleX resolves only same-file constants). */ const AVATAR_SIZE = `calc(${spacingVars['--spacing-6']} + ${spacingVars['--spacing-1']})`; -const TYPING_INDICATOR_STYLE: CSSProperties & - Record<'--astryx-chat-typing-indicator-dots-column-width', string> = { - '--astryx-chat-typing-indicator-dots-column-width': AVATAR_SIZE, -}; export const THREAD_DIVIDERS: readonly ThreadDivider[] = ['top', 'bottom']; const styles = stylex.create({ @@ -130,9 +119,6 @@ const styles = stylex.create({ flexBasis: 0, minWidth: 0, }, - typingIndicator: { - width: '100%', - }, // Row controls carry one spacing step of inline padding. Pulling the row // back by that step puts their glyphs on the body text line, so only their // hover and pressed fills cross it. @@ -350,6 +336,7 @@ export function CommentRow({ replyError, replyTarget, selectedThreadId, + typingIndicator, } = scope; const [isMetadataMenuOpen, setIsMetadataMenuOpen] = useState(false); const {getContainerProps, getContentRevealProps} = useContainerReveal(); @@ -812,15 +799,14 @@ export function CommentRow({ ))} )} - {depth === 0 && isThreadSelected && typingUsers.length > 0 && ( - - user.name)} - xstyle={styles.typingIndicator} - style={TYPING_INDICATOR_STYLE} - /> - - )} + {depth === 0 && + isThreadSelected && + typingIndicator != null && + typingUsers.length > 0 && ( + + {typingIndicator({users: typingUsers})} + + )} {depth === 0 && diff --git a/packages/lab/src/Comments/CommentThread.doc.mjs b/packages/lab/src/Comments/CommentThread.doc.mjs index d71acbb92da6e..37bc739b57eb4 100644 --- a/packages/lab/src/Comments/CommentThread.doc.mjs +++ b/packages/lab/src/Comments/CommentThread.doc.mjs @@ -50,7 +50,7 @@ export const docs = { name: 'Live state', required: false, description: - '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.', + '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.', }, { name: 'Reply composer', @@ -78,7 +78,7 @@ export const docs = { { guidance: true, description: - 'Map your collaboration state to `typingUsers`, then publish local reply and edit activity with `onComposerDraftChange`. The thread never opens a connection or expires participants.', + '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.', }, { guidance: false, @@ -111,6 +111,13 @@ export const docs = { description: '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.', }, + { + name: 'Typing indicator', + category: 'Content', + criterion: '4.1.3 Status Messages', + description: + '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.', + }, { name: 'Reduced motion', criterion: '2.3.3 Animation from Interactions', @@ -276,6 +283,12 @@ export const docs = { description: '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.', }, + { + name: 'typingIndicator', + type: '(props: {users: readonly CommentUser[]}) => ReactNode', + description: + 'Renders the typing indicator at the end of the open thread while `typingUsers` name someone besides the viewer. Without it, typing shows nothing.', + }, { name: 'renderAnchor', type: '(comment: CommentThreadData, defaultAnchor: ReactNode) => ReactNode', @@ -386,7 +399,7 @@ export const docsZh = { { guidance: true, description: - '将协作状态映射到 `typingUsers`,再通过 `onComposerDraftChange` 发布本地的回复和编辑动态。线程从不建立连接,也不会让参与者过期。', + '将协作状态映射到 `typingUsers` 并用 `typingIndicator` 显示,再通过 `onComposerDraftChange` 发布本地的回复和编辑动态。线程从不建立连接,也不会让参与者过期。', }, { guidance: false, @@ -419,6 +432,13 @@ export const docsZh = { description: '当关闭会丢弃未保存的回复或编辑时,线程保持打开,焦点回到草稿,礼貌型实时区域会说明草稿尚未保存,再次关闭将丢弃它。抖动从来不是唯一的提示。', }, + { + name: '输入状态指示', + category: 'Content', + criterion: '4.1.3 Status Messages', + description: + '单独呈现的线程没有日志区域,因此 `typingIndicator` 返回的指示器要在每次变化时以礼貌方式播报一次谁在输入,除非页面已把线程放在会播报它的实时区域中。在减少动态效果设置下,其动画也要保持安全。', + }, { name: '减少动态效果', criterion: '2.3.3 Animation from Interactions', @@ -464,6 +484,8 @@ export const docsZh = { getReplyMention: '回复某条回复时,用提及其作者的内容预填。', emojiPicker: '渲染回复输入框打开的表情选择器,并将 `onSelect` 传给它;省略时没有表情按钮。选择器填满一个固定宽度、无内边距的面板,需自带内边距。', + typingIndicator: + '线程展开且 `typingUsers` 中有查看者以外的人时,在线程末尾渲染输入状态指示;省略时不显示输入状态。', renderAnchor: '替换线程引用的段落。', renderBody: '替换评论的 Markdown 正文。', renderVersionBody: '替换编辑历史中某个版本的正文。', @@ -498,7 +520,7 @@ export const docsDense = { { guidance: true, description: - 'Map collaboration state to `typingUsers`; publish local drafts via `onComposerDraftChange`. No connections, no participant expiry.', + 'Map collaboration state to `typingUsers`, show it via `typingIndicator`; publish local drafts via `onComposerDraftChange`. No connections, no participant expiry.', }, { guidance: false, @@ -531,6 +553,13 @@ export const docsDense = { description: 'A dismissal over an unsaved reply/edit keeps the thread open, refocuses the draft, and announces politely. Shake is never the only signal.', }, + { + name: 'Typing indicator', + category: 'Content', + criterion: '4.1.3 Status Messages', + description: + '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.', + }, { name: 'Reduced motion', criterion: '2.3.3 Animation from Interactions', @@ -576,6 +605,8 @@ export const docsDense = { getReplyMention: 'Seeds a reply-to-reply with an author mention.', emojiPicker: '({onSelect}) => picker for the reply composer (fixed width, no padding: bring your own); none = no emoji button.', + typingIndicator: + '({users}) => typing row at the end of the open thread; none = no typing row.', renderAnchor: 'Replaces the quoted passage.', renderBody: 'Replaces a comment’s Markdown body.', renderVersionBody: 'Replaces a history version’s body.', diff --git a/packages/lab/src/Comments/CommentThread.tsx b/packages/lab/src/Comments/CommentThread.tsx index 33f01de25112d..f4ab6e3bb7f21 100644 --- a/packages/lab/src/Comments/CommentThread.tsx +++ b/packages/lab/src/Comments/CommentThread.tsx @@ -102,6 +102,7 @@ export function CommentThread({ markdownInlinePlugins, getReplyMention, emojiPicker, + typingIndicator, isSubmitting, onComposerDraftChange, onSubmit, @@ -151,6 +152,7 @@ export function CommentThread({ markdownInlinePlugins, getReplyMention, emojiPicker, + typingIndicator, isSubmitting, onComposerDraftChange, onSubmit, diff --git a/packages/lab/src/Comments/Comments.doc.mjs b/packages/lab/src/Comments/Comments.doc.mjs index c79d2e39f6197..9e4e5fd6209ac 100644 --- a/packages/lab/src/Comments/Comments.doc.mjs +++ b/packages/lab/src/Comments/Comments.doc.mjs @@ -74,7 +74,7 @@ export const docs = { name: 'Live state', required: false, description: - '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.', + '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.', }, { name: 'Activity event', @@ -108,7 +108,7 @@ export const docs = { { guidance: true, description: - '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.', + '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.', }, { guidance: false, @@ -133,7 +133,7 @@ export const docs = { category: 'Content', criterion: '4.1.3 Status Messages', description: - 'Typing is a polite status message with reduced-motion-safe dots.', + '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.', }, { name: 'Thread toggle', @@ -445,6 +445,12 @@ export const docs = { description: '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.', }, + { + name: 'typingIndicator', + type: '(props: {users: readonly CommentUser[]}) => ReactNode', + description: + '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.', + }, { name: 'emptyState', type: 'ReactNode', @@ -614,7 +620,7 @@ import {Comments} from '@astryxdesign/lab'; }, { label: 'Live collaboration state', - code: `import {Comments} from '@astryxdesign/lab'; + code: `import {ChatTypingIndicator, Comments} from '@astryxdesign/lab'; ({ @@ -622,6 +628,9 @@ import {Comments} from '@astryxdesign/lab'; typingUsers: typingUsersByThread[thread.id] ?? [], }))} currentUser={viewer} + typingIndicator={({users}) => ( + user.name)} /> + )} onSubmit={post} onComposerDraftChange={(value, context) => publishTyping({context, isTyping: value.trim() !== ''}) diff --git a/packages/lab/src/Comments/Comments.test.tsx b/packages/lab/src/Comments/Comments.test.tsx index 78b70383dc8e1..390945dcb3110 100644 --- a/packages/lab/src/Comments/Comments.test.tsx +++ b/packages/lab/src/Comments/Comments.test.tsx @@ -575,7 +575,14 @@ describe('Comments rendering', () => { }); describe('Comments live collaboration seams', () => { - it('shows typing only after the thread opens', async () => { + it('renders typingIndicator once the thread opens, with each typist once and without the viewer', async () => { + const typingIndicator = vi.fn( + ({users}: {users: readonly CommentUser[]}) => ( + + {users.map(user => user.name).join(', ')} + + ), + ); render( { }, ]} currentUser={VIEWER} + typingIndicator={typingIndicator} /> , ); - expect( - screen.queryByText('Maya Chen and Release Assistant are typing…'), - ).toBeNull(); + expect(screen.queryByTestId('typing')).toBeNull(); await click(screen.getByRole('button', {name: 'Thread by Avery Kim'})); - const typingStatus = screen - .getByText('Maya Chen and Release Assistant are typing…') - .closest('.astryx-chat-typing-indicator'); - expect(typingStatus).not.toBeNull(); - expect(typingStatus).not.toHaveAttribute('role'); - expect( - (typingStatus as HTMLElement).style.getPropertyValue( - '--astryx-chat-typing-indicator-dots-column-width', - ), - ).not.toBe(''); + expect(screen.getByTestId('typing').textContent).toBe( + 'Maya Chen, Release Assistant', + ); + expect(typingIndicator).toHaveBeenLastCalledWith({users: [OTHER, AGENT]}); + }); + + it('shows no typing row without typingIndicator', async () => { + render( + + + , + ); + await click(screen.getByRole('button', {name: 'Thread by Avery Kim'})); + expect(document.querySelector('[data-thread-typing-indicator]')).toBeNull(); }); it('reports a new-comment draft with its anchor and clears it after posting', async () => { diff --git a/packages/lab/src/Comments/Comments.tsx b/packages/lab/src/Comments/Comments.tsx index 4a12e8914ff71..b6d027b227dea 100644 --- a/packages/lab/src/Comments/Comments.tsx +++ b/packages/lab/src/Comments/Comments.tsx @@ -217,6 +217,7 @@ export function Comments({ markdownInlinePlugins, getReplyMention, emojiPicker, + typingIndicator, isSubmitting, onComposerDraftChange, onSubmit, @@ -319,6 +320,7 @@ export function Comments({ markdownInlinePlugins, getReplyMention, emojiPicker, + typingIndicator, isSubmitting, onComposerDraftChange, onSubmit, diff --git a/packages/lab/src/Comments/CommentsContext.tsx b/packages/lab/src/Comments/CommentsContext.tsx index 27fad769654f9..ae08cadc8f625 100644 --- a/packages/lab/src/Comments/CommentsContext.tsx +++ b/packages/lab/src/Comments/CommentsContext.tsx @@ -140,6 +140,7 @@ export interface CommentsContextValue { markdownPlugins?: readonly MarkdownPluginEntry[]; markdownInlinePlugins?: readonly MarkdownInlinePlugin[]; emojiPicker?: CommentThreadSharedProps['emojiPicker']; + typingIndicator?: CommentThreadSharedProps['typingIndicator']; onUploadImages?: CommentImageUploader; renderAnchor?: CommentThreadSharedProps['renderAnchor']; renderBody?: CommentThreadSharedProps['renderBody']; @@ -236,6 +237,7 @@ export function useCommentsController({ markdownInlinePlugins, getReplyMention, emojiPicker, + typingIndicator, isSubmitting = false, onComposerDraftChange, onSubmit, @@ -568,6 +570,7 @@ export function useCommentsController({ markdownPlugins, markdownInlinePlugins, emojiPicker, + typingIndicator, onUploadImages, renderAnchor, renderBody, diff --git a/packages/lab/src/Comments/index.ts b/packages/lab/src/Comments/index.ts index 48dd212ba2b7c..fd64f01f44979 100644 --- a/packages/lab/src/Comments/index.ts +++ b/packages/lab/src/Comments/index.ts @@ -33,6 +33,7 @@ export type { CommentReactionOption, CommentThreadData, CommentThreadSharedProps, + CommentTypingIndicatorRenderProps, CommentUploadedImage, CommentUser, CommentVersion, diff --git a/packages/lab/src/Comments/types.ts b/packages/lab/src/Comments/types.ts index 09b9a0a6742f7..31e9cf321816b 100644 --- a/packages/lab/src/Comments/types.ts +++ b/packages/lab/src/Comments/types.ts @@ -168,7 +168,8 @@ export interface CommentThreadData extends CommentData { /** * Other people currently composing in this thread. Controlled, transient * state: update it from your collaboration layer and expire stale entries - * there. While this thread is open, the current viewer is not shown. + * there. While this thread is open, `typingIndicator` shows them, without + * the current viewer. */ typingUsers?: readonly CommentUser[]; /** One level of replies, oldest first. */ @@ -249,6 +250,12 @@ export interface CommentEmojiPickerRenderProps { onSelect: (emoji: string) => void; } +/** What a `typingIndicator` render function receives. */ +export interface CommentTypingIndicatorRenderProps { + /** The people typing in the open thread, each once, without the viewer. */ + users: readonly CommentUser[]; +} + /** Uploads images for a composer and returns where they were stored. */ export type CommentImageUploader = ( files: readonly File[], @@ -307,6 +314,12 @@ export interface CommentThreadSharedProps { * picker fills a fixed-width panel with no padding, so it brings its own. */ emojiPicker?: (props: CommentEmojiPickerRenderProps) => ReactNode; + /** + * Renders the typing indicator at the end of an open thread while its + * `typingUsers` name someone besides the viewer. Without it, typing shows + * nothing. + */ + typingIndicator?: (props: CommentTypingIndicatorRenderProps) => ReactNode; /** Host-controlled pending state for a post or reply. */ isSubmitting?: boolean; /** diff --git a/packages/lab/src/index.ts b/packages/lab/src/index.ts index 55e107ce4b6d3..945da6dca4a99 100644 --- a/packages/lab/src/index.ts +++ b/packages/lab/src/index.ts @@ -74,6 +74,7 @@ export { type CommentReactionOption, type CommentThreadData, type CommentThreadSharedProps, + type CommentTypingIndicatorRenderProps, type CommentUploadedImage, type CommentUser, type CommentVersion, From f735ad98d12a035dadcd75c7444b7b8a5e3ddedf Mon Sep 17 00:00:00 2001 From: Ernest Tien Date: Wed, 2 Dec 2026 10:21:12 -0800 Subject: [PATCH 2/2] refactor(Comments): the typing slot is renderTypingIndicator, and it renders as its own component A public component prop whose value is a function that returns rendered output is named render (spec:AST-055 DEC-6), so `typingIndicator` becomes `renderTypingIndicator` on Comments and CommentThread, with their docs, the Live state story, the panel showcase, and the tests. The row called the indicator function inside its own render, and only while someone typed. An indicator component that calls hooks then changed the row's hook count when typing started or stopped, and the thread crashed ("Rendered more hooks than during the previous render"). The indicator now renders through a small component, so its hooks run in their own fiber. --- apps/storybook/stories/Comments.stories.tsx | 2 +- packages/lab/blocks/CommentsShowcase.tsx | 2 +- packages/lab/src/Comments/CommentRow.tsx | 21 ++++++++-- .../lab/src/Comments/CommentThread.doc.mjs | 20 ++++----- packages/lab/src/Comments/CommentThread.tsx | 4 +- packages/lab/src/Comments/Comments.doc.mjs | 8 ++-- packages/lab/src/Comments/Comments.test.tsx | 41 ++++++++++++++++--- packages/lab/src/Comments/Comments.tsx | 4 +- packages/lab/src/Comments/CommentsContext.tsx | 6 +-- packages/lab/src/Comments/types.ts | 8 ++-- 10 files changed, 82 insertions(+), 34 deletions(-) diff --git a/apps/storybook/stories/Comments.stories.tsx b/apps/storybook/stories/Comments.stories.tsx index 5cb39f297ed8b..4e9d17648a9d1 100644 --- a/apps/storybook/stories/Comments.stories.tsx +++ b/apps/storybook/stories/Comments.stories.tsx @@ -168,7 +168,7 @@ export const LiveState: Story = { }} currentUser={VIEWER} isSelected - typingIndicator={({users}) => ( + renderTypingIndicator={({users}) => ( user.name)} /> )} onSubmit={() => {}} diff --git a/packages/lab/blocks/CommentsShowcase.tsx b/packages/lab/blocks/CommentsShowcase.tsx index 90a0f53935e93..cff3504443b72 100644 --- a/packages/lab/blocks/CommentsShowcase.tsx +++ b/packages/lab/blocks/CommentsShowcase.tsx @@ -317,7 +317,7 @@ export default function CommentsShowcase() { onSelectedThreadChange={setSelectedThreadId} density="compact" renderEmojiPicker={({onSelect}) => } - typingIndicator={({users}) => ( + renderTypingIndicator={({users}) => ( user.name)} /> )} hasOlderComments={hasOlderComments} diff --git a/packages/lab/src/Comments/CommentRow.tsx b/packages/lab/src/Comments/CommentRow.tsx index f533095252faf..330c5a78e65aa 100644 --- a/packages/lab/src/Comments/CommentRow.tsx +++ b/packages/lab/src/Comments/CommentRow.tsx @@ -73,11 +73,23 @@ import { import type { CommentComposerContext, CommentThreadData, + CommentTypingIndicatorRenderProps, CommentUser, } from './types'; export type ThreadDivider = 'top' | 'bottom'; +/* Its own component, so the hooks of an indicator component run in their own + fiber, not in the row's render. */ +function TypingIndicatorSlot({ + render, + users, +}: CommentTypingIndicatorRenderProps & { + render: (props: CommentTypingIndicatorRenderProps) => ReactNode; +}) { + return render({users}); +} + /** The 28px avatar column (StyleX resolves only same-file constants). */ const AVATAR_SIZE = `calc(${spacingVars['--spacing-6']} + ${spacingVars['--spacing-1']})`; export const THREAD_DIVIDERS: readonly ThreadDivider[] = ['top', 'bottom']; @@ -336,7 +348,7 @@ export function CommentRow({ replyError, replyTarget, selectedThreadId, - typingIndicator, + renderTypingIndicator, } = scope; const [isMetadataMenuOpen, setIsMetadataMenuOpen] = useState(false); const {getContainerProps, getContentRevealProps} = useContainerReveal(); @@ -801,10 +813,13 @@ export function CommentRow({ )} {depth === 0 && isThreadSelected && - typingIndicator != null && + renderTypingIndicator != null && typingUsers.length > 0 && ( - {typingIndicator({users: typingUsers})} + )} diff --git a/packages/lab/src/Comments/CommentThread.doc.mjs b/packages/lab/src/Comments/CommentThread.doc.mjs index 676ab1e8814a0..0f912eb23e61e 100644 --- a/packages/lab/src/Comments/CommentThread.doc.mjs +++ b/packages/lab/src/Comments/CommentThread.doc.mjs @@ -50,7 +50,7 @@ export const docs = { name: 'Live state', required: false, description: - '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.', + 'With `renderTypingIndicator`, controlled typing state follows the conversation while the thread is open: the thread passes the people typing, each once and without the viewer.', }, { name: 'Reply composer', @@ -78,7 +78,7 @@ export const docs = { { guidance: true, description: - '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.', + 'Map your collaboration state to `typingUsers` and show it with `renderTypingIndicator`, then publish local reply and edit activity with `onComposerDraftChange`. The thread never opens a connection or expires participants.', }, { guidance: false, @@ -116,7 +116,7 @@ export const docs = { category: 'Content', criterion: '4.1.3 Status Messages', description: - '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.', + 'A standalone thread has no log, so the indicator `renderTypingIndicator` 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.', }, { name: 'Reduced motion', @@ -284,7 +284,7 @@ export const docs = { '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.', }, { - name: 'typingIndicator', + name: 'renderTypingIndicator', type: '(props: {users: readonly CommentUser[]}) => ReactNode', description: 'Renders the typing indicator at the end of the open thread while `typingUsers` name someone besides the viewer. Without it, typing shows nothing.', @@ -399,7 +399,7 @@ export const docsZh = { { guidance: true, description: - '将协作状态映射到 `typingUsers` 并用 `typingIndicator` 显示,再通过 `onComposerDraftChange` 发布本地的回复和编辑动态。线程从不建立连接,也不会让参与者过期。', + '将协作状态映射到 `typingUsers` 并用 `renderTypingIndicator` 显示,再通过 `onComposerDraftChange` 发布本地的回复和编辑动态。线程从不建立连接,也不会让参与者过期。', }, { guidance: false, @@ -437,7 +437,7 @@ export const docsZh = { category: 'Content', criterion: '4.1.3 Status Messages', description: - '单独呈现的线程没有日志区域,因此 `typingIndicator` 返回的指示器要在每次变化时以礼貌方式播报一次谁在输入,除非页面已把线程放在会播报它的实时区域中。在减少动态效果设置下,其动画也要保持安全。', + '单独呈现的线程没有日志区域,因此 `renderTypingIndicator` 返回的指示器要在每次变化时以礼貌方式播报一次谁在输入,除非页面已把线程放在会播报它的实时区域中。在减少动态效果设置下,其动画也要保持安全。', }, { name: '减少动态效果', @@ -484,7 +484,7 @@ export const docsZh = { getReplyMention: '回复某条回复时,用提及其作者的内容预填。', renderEmojiPicker: '渲染回复输入框打开的表情选择器,并将 `onSelect` 传给它;省略时没有表情按钮。选择器填满一个固定宽度、无内边距的面板,需自带内边距。', - typingIndicator: + renderTypingIndicator: '线程展开且 `typingUsers` 中有查看者以外的人时,在线程末尾渲染输入状态指示;省略时不显示输入状态。', renderAnchor: '替换线程引用的段落。', renderBody: '替换评论的 Markdown 正文。', @@ -520,7 +520,7 @@ export const docsDense = { { guidance: true, description: - 'Map collaboration state to `typingUsers`, show it via `typingIndicator`; publish local drafts via `onComposerDraftChange`. No connections, no participant expiry.', + 'Map collaboration state to `typingUsers`, show it via `renderTypingIndicator`; publish local drafts via `onComposerDraftChange`. No connections, no participant expiry.', }, { guidance: false, @@ -558,7 +558,7 @@ export const docsDense = { category: 'Content', criterion: '4.1.3 Status Messages', description: - '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.', + 'No log in a standalone thread: the `renderTypingIndicator` result announces who is typing once per change, politely, unless a live region already speaks it. Reduced-motion safe.', }, { name: 'Reduced motion', @@ -605,7 +605,7 @@ export const docsDense = { getReplyMention: 'Seeds a reply-to-reply with an author mention.', renderEmojiPicker: '({onSelect}) => picker for the reply composer (fixed width, no padding: bring your own); none = no emoji button.', - typingIndicator: + renderTypingIndicator: '({users}) => typing row at the end of the open thread; none = no typing row.', renderAnchor: 'Replaces the quoted passage.', renderBody: 'Replaces a comment’s Markdown body.', diff --git a/packages/lab/src/Comments/CommentThread.tsx b/packages/lab/src/Comments/CommentThread.tsx index a7657f520d277..c24f2937dce6d 100644 --- a/packages/lab/src/Comments/CommentThread.tsx +++ b/packages/lab/src/Comments/CommentThread.tsx @@ -102,7 +102,7 @@ export function CommentThread({ markdownInlinePlugins, getReplyMention, renderEmojiPicker, - typingIndicator, + renderTypingIndicator, isSubmitting, onComposerDraftChange, onSubmit, @@ -152,7 +152,7 @@ export function CommentThread({ markdownInlinePlugins, getReplyMention, renderEmojiPicker, - typingIndicator, + renderTypingIndicator, isSubmitting, onComposerDraftChange, onSubmit, diff --git a/packages/lab/src/Comments/Comments.doc.mjs b/packages/lab/src/Comments/Comments.doc.mjs index 2239a70ba7608..8854e9fd70978 100644 --- a/packages/lab/src/Comments/Comments.doc.mjs +++ b/packages/lab/src/Comments/Comments.doc.mjs @@ -74,7 +74,7 @@ export const docs = { name: 'Live state', required: false, description: - '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.', + 'With `renderTypingIndicator`, 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.', }, { name: 'Activity event', @@ -108,7 +108,7 @@ export const docs = { { guidance: true, description: - '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.', + 'Keep live state controlled and transport-neutral: map your collaboration layer to `typingUsers`, show it with `renderTypingIndicator`, 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.', }, { guidance: false, @@ -446,7 +446,7 @@ export const docs = { '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.', }, { - name: 'typingIndicator', + name: 'renderTypingIndicator', type: '(props: {users: readonly CommentUser[]}) => ReactNode', description: '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.', @@ -628,7 +628,7 @@ import {Comments} from '@astryxdesign/lab'; typingUsers: typingUsersByThread[thread.id] ?? [], }))} currentUser={viewer} - typingIndicator={({users}) => ( + renderTypingIndicator={({users}) => ( user.name)} /> )} onSubmit={post} diff --git a/packages/lab/src/Comments/Comments.test.tsx b/packages/lab/src/Comments/Comments.test.tsx index 390945dcb3110..f17577d41f7ca 100644 --- a/packages/lab/src/Comments/Comments.test.tsx +++ b/packages/lab/src/Comments/Comments.test.tsx @@ -575,8 +575,8 @@ describe('Comments rendering', () => { }); describe('Comments live collaboration seams', () => { - it('renders typingIndicator once the thread opens, with each typist once and without the viewer', async () => { - const typingIndicator = vi.fn( + it('renders renderTypingIndicator once the thread opens, with each typist once and without the viewer', async () => { + const renderIndicator = vi.fn( ({users}: {users: readonly CommentUser[]}) => ( {users.map(user => user.name).join(', ')} @@ -594,7 +594,7 @@ describe('Comments live collaboration seams', () => { }, ]} currentUser={VIEWER} - typingIndicator={typingIndicator} + renderTypingIndicator={renderIndicator} /> , ); @@ -605,10 +605,41 @@ describe('Comments live collaboration seams', () => { expect(screen.getByTestId('typing').textContent).toBe( 'Maya Chen, Release Assistant', ); - expect(typingIndicator).toHaveBeenLastCalledWith({users: [OTHER, AGENT]}); + expect(renderIndicator).toHaveBeenLastCalledWith({users: [OTHER, AGENT]}); }); - it('shows no typing row without typingIndicator', async () => { + it('renders an indicator component that calls hooks as typing starts and stops', async () => { + function HookIndicator({users}: {users: readonly CommentUser[]}) { + const [suffix] = useState('is typing'); + return ( + + {`${users.map(user => user.name).join(', ')} ${suffix}`} + + ); + } + function Host({typingUsers}: {typingUsers: readonly CommentUser[]}) { + return ( + + + + ); + } + const {rerender} = render(); + await click(screen.getByRole('button', {name: 'Thread by Avery Kim'})); + expect(screen.queryByTestId('typing')).toBeNull(); + rerender(); + expect(screen.getByTestId('typing').textContent).toBe( + 'Maya Chen is typing', + ); + rerender(); + expect(screen.queryByTestId('typing')).toBeNull(); + }); + + it('shows no typing row without renderTypingIndicator', async () => { render( void; } -/** What a `typingIndicator` render function receives. */ +/** What a `renderTypingIndicator` function receives. */ export interface CommentTypingIndicatorRenderProps { /** The people typing in the open thread, each once, without the viewer. */ users: readonly CommentUser[]; @@ -319,7 +319,9 @@ export interface CommentThreadSharedProps { * `typingUsers` name someone besides the viewer. Without it, typing shows * nothing. */ - typingIndicator?: (props: CommentTypingIndicatorRenderProps) => ReactNode; + renderTypingIndicator?: ( + props: CommentTypingIndicatorRenderProps, + ) => ReactNode; /** Host-controlled pending state for a post or reply. */ isSubmitting?: boolean; /**