diff --git a/src/components/overlay_mask/overlay_mask.tsx b/src/components/overlay_mask/overlay_mask.tsx index 9e820eea0c..863beb1b08 100644 --- a/src/components/overlay_mask/overlay_mask.tsx +++ b/src/components/overlay_mask/overlay_mask.tsx @@ -120,16 +120,18 @@ export const OuiOverlayMask: FunctionComponent = ({ 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]);