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
19 changes: 19 additions & 0 deletions packages/lab/src/Comments/CommentComposer.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,25 @@ async function settle() {
}

describe('CommentComposer', () => {
it("calls the caller's onClick and lets the click bubble", () => {
const onClick = vi.fn();
const onAncestorClick = vi.fn();
render(
<div onClick={onAncestorClick}>
<CommentComposer
value=""
onChange={vi.fn()}
onSubmit={vi.fn()}
onClick={onClick}
data-testid="composer"
/>
</div>,
);
fireEvent.click(screen.getByTestId('composer'));
expect(onClick).toHaveBeenCalledTimes(1);
expect(onAncestorClick).toHaveBeenCalledTimes(1);
});

it('posts the draft from Post, and never an empty one', async () => {
const onSubmit = vi.fn();
const {rerender} = render(
Expand Down
18 changes: 14 additions & 4 deletions packages/lab/src/Comments/CommentComposer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,14 @@ export function CommentComposerBase({
</VStack>
);

const {xstyle, className, style, ...restRootProps} = rootProps ?? {};
const {
xstyle,
className,
style,
onClick: onRootClick,
...restRootProps
} = rootProps ?? {};
const isInThread = context.kind !== 'comment';
return (
<VStack
ref={rootRef}
Expand All @@ -583,9 +590,12 @@ export function CommentComposerBase({
style,
)}
onClick={event => {
// The composer keeps its clicks: in a thread, a press on its empty
// space would otherwise reach the thread and collapse it.
event.stopPropagation();
onRootClick?.(event);

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.

Optional: the caller's handler runs before the composer's own work, and it cannot cancel that work. So a caller that moves focus synchronously here, for example into a panel it opens, loses that focus: on a press in the composer's empty space, the composer then focuses its input. Consider calling onRootClick?.(event) after the focus step, or documenting the order on onClick and pinning it in the new test.

// In a thread, a press on the composer's empty space would otherwise
// reach the thread and collapse it.
if (isInThread) {
event.stopPropagation();
}
const target = event.target as Element;
if (
target.closest(COMPOSER_CONTROL_SELECTOR) == null &&
Expand Down
22 changes: 22 additions & 0 deletions packages/lab/src/Comments/CommentThread.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,28 @@ describe('CommentThread', () => {
).toBeTruthy();
});

it('stays open on a press in the empty space of its reply composer', async () => {
const onSelectedChange = vi.fn();
render(
<CommentThread
comment={THREAD}
currentUser={VIEWER}
onSubmit={vi.fn()}
onSelectedChange={onSelectedChange}
/>,
);
fireEvent.click(screen.getByRole('button', {name: 'Thread by Maya Chen'}));
await settle();
const composer = document.querySelector('.astryx-comment-composer');
expect(composer).not.toBeNull();
fireEvent.click(composer!);
await settle();
expect(onSelectedChange).not.toHaveBeenCalledWith(false);
expect(
screen.getByRole('textbox', {name: 'Reply to Maya Chen…'}),
).toBeTruthy();
});

it('opens when the host selects it', async () => {
const {rerender} = render(
<CommentThread
Expand Down