Skip to content

Commit

Permalink
fix(popover): do not stopPropagation on body click
Browse files Browse the repository at this point in the history
  • Loading branch information
xballoy committed Jul 6, 2020
1 parent b04cf0c commit 421d083
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions packages/popover/src/Popover.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useEffect, useState } from 'react';
import React, { useEffect, useRef, useState } from 'react';
import PropTypes from 'prop-types';
import { Constants } from '@axa-fr/react-toolkit-core';
import PopoverBase from './PopoverBase';
Expand Down Expand Up @@ -28,21 +28,25 @@ const defaultProps = {
export const PopoverClick = props => {
const { children, placement, className, classModifier } = props;

const wrapperRef = useRef(null);
const [isOpen, setOpen] = useState(false);
const [isHover, setHover] = useState(false);

useEffect(() => {
const body = window.document.getElementsByTagName('body')[0];
body.addEventListener('click', click);
const handleClickOutside = event => {
if (wrapperRef.current && !wrapperRef.current.contains(event.target)) {
setOpen(false);
}
};

document.addEventListener('click', handleClickOutside);
return () => {
body.removeEventListener('click', click);
document.removeEventListener('click', handleClickOutside);
};
});
}, [wrapperRef]);

const click = event => {
const click = () => {
setOpen(!isOpen && isHover);
event.stopPropagation();
};

const enter = () => {
Expand All @@ -55,6 +59,7 @@ export const PopoverClick = props => {

return (
<button
ref={wrapperRef}
className="af-popover__wrapper"
type="button"
onMouseEnter={enter}
Expand Down

0 comments on commit 421d083

Please sign in to comment.