Skip to content

Commit

Permalink
fix: overlay mask onClick been called multiple times (#257)
Browse files Browse the repository at this point in the history
The cause:
`click` event listener is not properly unsubscribed

Signed-off-by: Yulong Ruan <[email protected]>
Co-authored-by: Anan Zhuang <[email protected]>
  • Loading branch information
ruanyl and ananzh authored Feb 28, 2023
1 parent e8fcf75 commit b945c86
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/components/overlay_mask/overlay_mask.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -120,16 +120,18 @@ export const OuiOverlayMask: FunctionComponent<OuiOverlayMaskProps> = ({

useEffect(() => {
if (!overlayMaskNode.current || !onClick) return;

const portalTarget = overlayMaskNode.current;
overlayMaskNode.current.addEventListener('click', (e) => {
const handleClick = (e: MouseEvent) => {
if (e.target === overlayMaskNode.current) {
onClick();
}
});
};
portalTarget.addEventListener('click', handleClick);

return () => {
if (portalTarget && onClick) {
portalTarget.removeEventListener('click', onClick);
if (portalTarget) {
portalTarget.removeEventListener('click', handleClick);
}
};
}, [onClick]);
Expand Down

0 comments on commit b945c86

Please sign in to comment.