Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions apps/storybook/stories/Comments.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -167,6 +168,9 @@ export const LiveState: Story = {
}}
currentUser={VIEWER}
isSelected
renderTypingIndicator={({users}) => (
<ChatTypingIndicator names={users.map(user => user.name)} />
)}
onSubmit={() => {}}
/>
</VStack>
Expand Down
2 changes: 1 addition & 1 deletion packages/lab/blocks/CommentsShowcase.doc.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ export default {
isReady: true,
isShowcase: true,
aspectRatio: 3 / 4,
componentsUsed: ['Comments', 'EmojiPicker', 'VStack'],
componentsUsed: ['Comments', 'EmojiPicker', 'ChatTypingIndicator', 'VStack'],
};
4 changes: 4 additions & 0 deletions packages/lab/blocks/CommentsShowcase.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import {useRef, useState} from 'react';
import {VStack} from '@astryxdesign/core/Layout';
import {
ChatTypingIndicator,
DEFAULT_COMMENT_REACTIONS,
Comments,
EmojiPicker,
Expand Down Expand Up @@ -316,6 +317,9 @@ export default function CommentsShowcase() {
onSelectedThreadChange={setSelectedThreadId}
density="compact"
renderEmojiPicker={({onSelect}) => <EmojiPicker onSelect={onSelect} />}
renderTypingIndicator={({users}) => (
<ChatTypingIndicator names={users.map(user => user.name)} />
)}
hasOlderComments={hasOlderComments}
onLoadOlder={() => {
setComments(current => [
Expand Down
57 changes: 29 additions & 28 deletions packages/lab/src/Comments/CommentRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -50,7 +44,6 @@ import {
} from './CommentActions';
import {CommentComposerBase} from './CommentComposer';
import {CommentMarkdown} from './CommentMarkdown';
import {ChatTypingIndicator} from '../Chat/ChatTypingIndicator';
import {
AuthorName,
AvatarSpacer,
Expand Down Expand Up @@ -80,17 +73,25 @@ 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});
}
Comment on lines +82 to +91

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.

This repeats EmojiPickerSlot in CommentComposer.tsx:89-98, comment included, with other props. A later fix to how the family renders a slot, such as a caller that switches between two indicator components, would reach only one of them. Make one shared helper and use it in both places, for example:

function RenderSlot<P>({
  render,
  props,
}: {
  render: (props: P) => ReactNode;
  props: P;
}) {
  return render(props);
}
// …
<RenderSlot render={renderTypingIndicator} props={{users: typingUsers}} />

then delete TypingIndicatorSlot and EmojiPickerSlot.


/** 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({
Expand Down Expand Up @@ -130,9 +131,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.
Expand Down Expand Up @@ -350,6 +348,7 @@ export function CommentRow({
replyError,
replyTarget,
selectedThreadId,
renderTypingIndicator,
} = scope;
const [isMetadataMenuOpen, setIsMetadataMenuOpen] = useState(false);
const {getContainerProps, getContentRevealProps} = useContainerReveal();
Expand Down Expand Up @@ -812,15 +811,17 @@ export function CommentRow({
))}
</VStack>
)}
{depth === 0 && isThreadSelected && typingUsers.length > 0 && (
<HStack width="100%" data-thread-typing-indicator="true">
<ChatTypingIndicator
names={typingUsers.map(user => user.name)}
xstyle={styles.typingIndicator}
style={TYPING_INDICATOR_STYLE}
/>
</HStack>
)}
{depth === 0 &&
isThreadSelected &&
renderTypingIndicator != null &&
typingUsers.length > 0 && (
<HStack width="100%" data-thread-typing-indicator="true">
<TypingIndicatorSlot
render={renderTypingIndicator}
users={typingUsers}
/>
</HStack>
)}
Comment on lines +814 to +824

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.

Desktop behavior change needs an owner decision: with default props, an open thread whose typingUsers name someone besides the viewer no longer shows the animated typing row for mouse and trackpad users, since the row now renders only when typingIndicator is passed. Gate the change to touch input, or cite the owner's decision for removing the default row.

</VStack>

{depth === 0 &&
Expand Down
39 changes: 35 additions & 4 deletions packages/lab/src/Comments/CommentThread.doc.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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 `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',
Expand Down Expand Up @@ -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 `renderTypingIndicator`, then publish local reply and edit activity with `onComposerDraftChange`. The thread never opens a connection or expires participants.',
},
{
guidance: false,
Expand Down Expand Up @@ -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 `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',
criterion: '2.3.3 Animation from Interactions',
Expand Down Expand Up @@ -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: '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.',
Comment thread
astracat-bot[bot] marked this conversation as resolved.
Resolved
},
{
name: 'renderAnchor',
type: '(comment: CommentThreadData, defaultAnchor: ReactNode) => ReactNode',
Expand Down Expand Up @@ -386,7 +399,7 @@ export const docsZh = {
{
guidance: true,
description:
'将协作状态映射到 `typingUsers`,再通过 `onComposerDraftChange` 发布本地的回复和编辑动态。线程从不建立连接,也不会让参与者过期。',
'将协作状态映射到 `typingUsers` 并用 `renderTypingIndicator` 显示,再通过 `onComposerDraftChange` 发布本地的回复和编辑动态。线程从不建立连接,也不会让参与者过期。',
},
{
guidance: false,
Expand Down Expand Up @@ -419,6 +432,13 @@ export const docsZh = {
description:
'当关闭会丢弃未保存的回复或编辑时,线程保持打开,焦点回到草稿,礼貌型实时区域会说明草稿尚未保存,再次关闭将丢弃它。抖动从来不是唯一的提示。',
},
{
name: '输入状态指示',
category: 'Content',
criterion: '4.1.3 Status Messages',
description:
'单独呈现的线程没有日志区域,因此 `renderTypingIndicator` 返回的指示器要在每次变化时以礼貌方式播报一次谁在输入,除非页面已把线程放在会播报它的实时区域中。在减少动态效果设置下,其动画也要保持安全。',
},
{
name: '减少动态效果',
criterion: '2.3.3 Animation from Interactions',
Expand Down Expand Up @@ -464,6 +484,8 @@ export const docsZh = {
getReplyMention: '回复某条回复时,用提及其作者的内容预填。',
renderEmojiPicker:
'渲染回复输入框打开的表情选择器,并将 `onSelect` 传给它;省略时没有表情按钮。选择器填满一个固定宽度、无内边距的面板,需自带内边距。',
renderTypingIndicator:
'线程展开且 `typingUsers` 中有查看者以外的人时,在线程末尾渲染输入状态指示;省略时不显示输入状态。',
renderAnchor: '替换线程引用的段落。',
renderBody: '替换评论的 Markdown 正文。',
renderVersionBody: '替换编辑历史中某个版本的正文。',
Expand Down Expand Up @@ -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 `renderTypingIndicator`; publish local drafts via `onComposerDraftChange`. No connections, no participant expiry.',
},
{
guidance: false,
Expand Down Expand Up @@ -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 `renderTypingIndicator` 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',
Expand Down Expand Up @@ -576,6 +605,8 @@ 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.',
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.',
renderVersionBody: 'Replaces a history version’s body.',
Expand Down
2 changes: 2 additions & 0 deletions packages/lab/src/Comments/CommentThread.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ export function CommentThread({
markdownInlinePlugins,
getReplyMention,
renderEmojiPicker,
renderTypingIndicator,
isSubmitting,
onComposerDraftChange,
onSubmit,
Expand Down Expand Up @@ -151,6 +152,7 @@ export function CommentThread({
markdownInlinePlugins,
getReplyMention,
renderEmojiPicker,
renderTypingIndicator,
isSubmitting,
onComposerDraftChange,
onSubmit,
Expand Down
17 changes: 13 additions & 4 deletions packages/lab/src/Comments/Comments.doc.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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 `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',
Expand Down Expand Up @@ -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 `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,
Expand All @@ -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',
Expand Down Expand Up @@ -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: '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.',
},
{
name: 'emptyState',
type: 'ReactNode',
Expand Down Expand Up @@ -614,14 +620,17 @@ import {Comments} from '@astryxdesign/lab';
},
{
label: 'Live collaboration state',
code: `import {Comments} from '@astryxdesign/lab';
code: `import {ChatTypingIndicator, Comments} from '@astryxdesign/lab';

<Comments
comments={comments.map(thread => ({
...thread,
typingUsers: typingUsersByThread[thread.id] ?? [],
}))}
currentUser={viewer}
renderTypingIndicator={({users}) => (
<ChatTypingIndicator names={users.map(user => user.name)} />
)}
onSubmit={post}
onComposerDraftChange={(value, context) =>
publishTyping({context, isTyping: value.trim() !== ''})
Expand Down
Loading