Skip to content

Commit 65947ee

Browse files
cixzhangernestt
andcommitted
fix(CommentComposer): the caller's onClick runs, and a click bubbles outside a thread
CommentComposer set its own root onClick after the caller's props, so a caller's `onClick` never ran, and it stopped every click from bubbling. The caller's handler now runs first. Only a reply or edit composer inside a thread stops the click, so a press on its empty space does not collapse the thread. Cherry-picked from #13738 (by @ernestt). Co-authored-by: Ernest Tien <ernesttien@meta.com>
1 parent 4700add commit 65947ee

3 files changed

Lines changed: 55 additions & 4 deletions

File tree

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,25 @@ async function settle() {
106106
}
107107

108108
describe('CommentComposer', () => {
109+
it("calls the caller's onClick and lets the click bubble", () => {
110+
const onClick = vi.fn();
111+
const onAncestorClick = vi.fn();
112+
render(
113+
<div onClick={onAncestorClick}>
114+
<CommentComposer
115+
value=""
116+
onChange={vi.fn()}
117+
onSubmit={vi.fn()}
118+
onClick={onClick}
119+
data-testid="composer"
120+
/>
121+
</div>,
122+
);
123+
fireEvent.click(screen.getByTestId('composer'));
124+
expect(onClick).toHaveBeenCalledTimes(1);
125+
expect(onAncestorClick).toHaveBeenCalledTimes(1);
126+
});
127+
109128
it('posts the draft from Post, and never an empty one', async () => {
110129
const onSubmit = vi.fn();
111130
const {rerender} = render(

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

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -568,7 +568,14 @@ export function CommentComposerBase({
568568
</VStack>
569569
);
570570

571-
const {xstyle, className, style, ...restRootProps} = rootProps ?? {};
571+
const {
572+
xstyle,
573+
className,
574+
style,
575+
onClick: onRootClick,
576+
...restRootProps
577+
} = rootProps ?? {};
578+
const isInThread = context.kind !== 'comment';
572579
return (
573580
<VStack
574581
ref={rootRef}
@@ -583,9 +590,12 @@ export function CommentComposerBase({
583590
style,
584591
)}
585592
onClick={event => {
586-
// The composer keeps its clicks: in a thread, a press on its empty
587-
// space would otherwise reach the thread and collapse it.
588-
event.stopPropagation();
593+
onRootClick?.(event);
594+
// In a thread, a press on the composer's empty space would otherwise
595+
// reach the thread and collapse it.
596+
if (isInThread) {
597+
event.stopPropagation();
598+
}
589599
const target = event.target as Element;
590600
if (
591601
target.closest(COMPOSER_CONTROL_SELECTOR) == null &&

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,28 @@ describe('CommentThread', () => {
143143
).toBeTruthy();
144144
});
145145

146+
it('stays open on a press in the empty space of its reply composer', async () => {
147+
const onSelectedChange = vi.fn();
148+
render(
149+
<CommentThread
150+
comment={THREAD}
151+
currentUser={VIEWER}
152+
onSubmit={vi.fn()}
153+
onSelectedChange={onSelectedChange}
154+
/>,
155+
);
156+
fireEvent.click(screen.getByRole('button', {name: 'Thread by Maya Chen'}));
157+
await settle();
158+
const composer = document.querySelector('.astryx-comment-composer');
159+
expect(composer).not.toBeNull();
160+
fireEvent.click(composer!);
161+
await settle();
162+
expect(onSelectedChange).not.toHaveBeenCalledWith(false);
163+
expect(
164+
screen.getByRole('textbox', {name: 'Reply to Maya Chen…'}),
165+
).toBeTruthy();
166+
});
167+
146168
it('opens when the host selects it', async () => {
147169
const {rerender} = render(
148170
<CommentThread

0 commit comments

Comments
 (0)