Skip to content

Commit 60dce54

Browse files
committed
fix(overlay): Allow for custom onClick behavior
1 parent 6eff8a8 commit 60dce54

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

packages/overlay/src/Overlay.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ export const Overlay = forwardRef<HTMLDivElement, OverlayProps>(
7979
portalInto,
8080
portalIntoId,
8181
tabIndex = -1,
82+
onClick,
8283
...props
8384
},
8485
nodeRef
@@ -112,7 +113,14 @@ export const Overlay = forwardRef<HTMLDivElement, OverlayProps>(
112113
}),
113114
className
114115
)}
115-
onClick={onRequestClose}
116+
onClick={(event) => {
117+
onClick?.(event);
118+
if (event.isPropagationStopped()) {
119+
return;
120+
}
121+
122+
onRequestClose();
123+
}}
116124
tabIndex={tabIndex}
117125
>
118126
{children}

packages/overlay/src/__tests__/Overlay.tsx

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,20 @@ describe("Overlay", () => {
5656
fireEvent.click(overlay);
5757
expect(onRequestClose).toBeCalledTimes(1);
5858
});
59+
60+
it("should not call onRequestClose if the onClick handler calls event.stopPropagation()", () => {
61+
const onRequestClose = jest.fn();
62+
const { getByTestId } = render(
63+
<Overlay
64+
data-testid="overlay"
65+
visible
66+
onRequestClose={onRequestClose}
67+
onClick={(event) => event.stopPropagation()}
68+
/>
69+
);
70+
71+
const overlay = getByTestId("overlay");
72+
fireEvent.click(overlay);
73+
expect(onRequestClose).not.toBeCalled();
74+
});
5975
});

0 commit comments

Comments
 (0)