Skip to content

Commit

Permalink
fix(overlay): Allow for custom onClick behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
mlaursen committed Jan 30, 2022
1 parent 6eff8a8 commit 60dce54
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
10 changes: 9 additions & 1 deletion packages/overlay/src/Overlay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ export const Overlay = forwardRef<HTMLDivElement, OverlayProps>(
portalInto,
portalIntoId,
tabIndex = -1,
onClick,
...props
},
nodeRef
Expand Down Expand Up @@ -112,7 +113,14 @@ export const Overlay = forwardRef<HTMLDivElement, OverlayProps>(
}),
className
)}
onClick={onRequestClose}
onClick={(event) => {
onClick?.(event);
if (event.isPropagationStopped()) {
return;
}

onRequestClose();
}}
tabIndex={tabIndex}
>
{children}
Expand Down
16 changes: 16 additions & 0 deletions packages/overlay/src/__tests__/Overlay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,20 @@ describe("Overlay", () => {
fireEvent.click(overlay);
expect(onRequestClose).toBeCalledTimes(1);
});

it("should not call onRequestClose if the onClick handler calls event.stopPropagation()", () => {
const onRequestClose = jest.fn();
const { getByTestId } = render(
<Overlay
data-testid="overlay"
visible
onRequestClose={onRequestClose}
onClick={(event) => event.stopPropagation()}
/>
);

const overlay = getByTestId("overlay");
fireEvent.click(overlay);
expect(onRequestClose).not.toBeCalled();
});
});

0 comments on commit 60dce54

Please sign in to comment.