Skip to content

Commit 0026f37

Browse files
committed
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.
1 parent ee9ee12 commit 0026f37

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
@@ -145,6 +145,28 @@ describe('CommentThread', () => {
145145
).toBeTruthy();
146146
});
147147

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

0 commit comments

Comments
 (0)