Skip to content

Commit

Permalink
feat(Popover.jsx): Add close functionality when clicks on elements ot…
Browse files Browse the repository at this point in the history
…her than the popover
  • Loading branch information
MarcosViniciusPC committed Sep 23, 2024
1 parent d9e14bb commit 37246ce
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 16 deletions.
12 changes: 11 additions & 1 deletion components/Popover/Popover.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const TriggerBlock = styled.div`
const Popover = ({
visible = false,
inverted = false,
closeOnClickOut = false,
onClose = () => {},
skin = 'neutral',
placement = 'top',
Expand All @@ -40,13 +41,21 @@ const Popover = ({
placement={placement}
onPopoverClose={() => handleVisible(false)}
inverted={inverted}
closeOnClickOut={closeOnClickOut}
skin={skin}
{...rest}
>
{children}
</Content>
)}
<TriggerBlock onClick={() => handleVisible(true)}>{trigger}</TriggerBlock>
<TriggerBlock
onClick={(event) => {
event.stopPropagation();
handleVisible(true);
}}
>
{trigger}
</TriggerBlock>
</Wrapper>
);
};
Expand All @@ -64,6 +73,7 @@ Popover.propTypes = {
skin: oneOf(['neutral', 'primary', 'success', 'warning', 'error']),
trigger: PropTypes.node.isRequired,
onClose: PropTypes.func,
closeOnClickOut: PropTypes.bool,
};

export default Popover;
55 changes: 40 additions & 15 deletions components/Popover/sub-components/Content.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import styled, { css } from 'styled-components';
import PropTypes from 'prop-types';

import { useEffect, useRef } from 'react';
import { components, spacing, colors, breakpoints } from '../../shared/theme';
import getArrow from '../arrowProperties';
import Button from '../../Button';
Expand Down Expand Up @@ -131,24 +132,48 @@ const Content = ({
},
skin = 'neutral',
inverted = false,
closeOnClickOut = false,
...rest
}) => (
<PopoverContent
theme={theme}
inverted={inverted}
placement={placement}
skin={skin}
{...rest}
>
<PopoverChildren>{children}</PopoverChildren>
<CloseButton
skin={skin}
}) => {
const popoverRef = useRef(null);

// eslint-disable-next-line consistent-return
useEffect(() => {
if (closeOnClickOut) {
const onClickOutside = (event) => {
const element = event.target;

if (popoverRef.current && !popoverRef.current.contains(element)) {
onPopoverClose();
}
};

window.addEventListener('click', onClickOutside);
return () => {
window.removeEventListener('click', onClickOutside);
};
}
}, []);

return (
<PopoverContent
theme={theme}
inverted={inverted}
onClick={onPopoverClose}
/>
</PopoverContent>
);
placement={placement}
skin={skin}
ref={popoverRef}
{...rest}
>
<PopoverChildren>{children}</PopoverChildren>
<CloseButton
skin={skin}
theme={theme}
inverted={inverted}
onClick={onPopoverClose}
/>
</PopoverContent>
);
};

CloseButton.displayName = 'CloseButton';
PopoverChildren.displayName = 'PopoverChildren';
Expand Down

0 comments on commit 37246ce

Please sign in to comment.