Skip to content

Commit

Permalink
fix menu overlay by putting it in popper
Browse files Browse the repository at this point in the history
  • Loading branch information
amrelbialy committed Mar 30, 2021
1 parent 774bcc1 commit c138548
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 6 deletions.
12 changes: 10 additions & 2 deletions packages/ui/src/core/menu/menu.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const Menu = intrinsicComponent<MenuProps, HTMLDivElement>(
maxHeight,
position,
popperOptions,
enableOverlay,
...rest
},
ref
Expand Down Expand Up @@ -94,8 +95,14 @@ const Menu = intrinsicComponent<MenuProps, HTMLDivElement>(

return (
<>
<Styled.Overlay onClick={handleClose} />
<Popper ref={menuRef} position={position || 'bottom'} open={Boolean(anchorEl)} anchorEl={anchorEl}>
<Popper
ref={menuRef}
position={position || 'bottom'}
open={Boolean(anchorEl)}
anchorEl={anchorEl}
overlay={Boolean(enableOverlay)}
onClick={handleClose}
>
<Styled.Menu
{...containerProps}
alignCenter={Boolean(alignCenter)}
Expand Down Expand Up @@ -140,6 +147,7 @@ export const propTypes = {
maxHeight: PT.oneOfType([PT.string, PT.number]),
popperOptions: popperPropTypes.popperOptions,
position: PT.oneOf(objectValues(Position)),
enableOverlay: PT.bool,
};

Menu.propTypes = propTypes;
Expand Down
1 change: 1 addition & 0 deletions packages/ui/src/core/menu/menu.props.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ export interface MenuProps extends PropsWithChildren, React.HTMLAttributes<HTMLD
maxHeight?: string | number;
position?: PopperPositionType;
popperOptions?: PopperOptions;
enableOverlay?: boolean;
}
23 changes: 21 additions & 2 deletions packages/ui/src/core/popper/popper.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,20 @@ import Styled from './popper.styles';

const Popper = intrinsicComponent<PopperProps, HTMLDivElement>(
(
{ anchorEl, children, open, position: initialPlacement = 'bottom', popperOptions }: PopperProps,
{
anchorEl,
children,
open,
position: initialPlacement = 'bottom',
popperOptions,
onClick,
overlay = false,
}: PopperProps,
ref
): JSX.Element => {
const target = usePortal(generateClassNames('Popper'));
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const overlayTarget = document.querySelector('body')!;
const Ref = useRef(null);
const ownRef = useForkRef(Ref, ref);

Expand Down Expand Up @@ -49,7 +59,15 @@ const Popper = intrinsicComponent<PopperProps, HTMLDivElement>(
</Styled.Popper>
);

return createPortal(render(), target);
const renderOverlay = (): JSX.Element => <Styled.Overlay onClick={onClick} />;

return (
<>
{createPortal(render(), target)}

{overlay && createPortal(renderOverlay(), overlayTarget)}
</>
);
}
);

Expand Down Expand Up @@ -86,6 +104,7 @@ export const propTypes = {
placement: PT.oneOf(objectValues(Position)),
strategy: PT.oneOf(objectValues(Strategy)),
}) as Validator<PopperOptions>,
overlay: PT.bool,
};

Popper.propTypes = propTypes;
Expand Down
1 change: 1 addition & 0 deletions packages/ui/src/core/popper/popper.props.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,5 @@ export interface PopperProps extends React.HTMLAttributes<HTMLDivElement> {
position: TooltipPositionType;
popperOptions?: PopperOptions;
open: boolean;
overlay?: boolean;
}
15 changes: 14 additions & 1 deletion packages/ui/src/core/popper/popper.styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,19 @@ const Popper = styled.div.attrs({
`
);

const Styled = applyDisplayNames({ Popper });
const Overlay = styled.div.attrs({
className: generateClassNames(baseClassName, 'Overlay'),
})(
() => css`
position: fixed;
right: 0px;
bottom: 0px;
top: 0px;
left: 0px;
background-color: transparent;
z-index: -1;
`
);
const Styled = applyDisplayNames({ Popper, Overlay });

export default Styled;
2 changes: 1 addition & 1 deletion packages/ui/stories/core/menu.story.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ const BasicTemplate: Story<MenuProps> = ({ ...args }) => {
Open menu
</Button>

<Menu {...args} anchorEl={anchorEl} open={Boolean(anchorEl)} onClose={handleClose}>
<Menu {...args} anchorEl={anchorEl} open={Boolean(anchorEl)} onClose={handleClose} enableOverlay>
<MenuItem list={defaultArgs.list} />
<MenuItem onClick={handleClose}>Item 3</MenuItem>
<MenuItem onClick={handleClose}>Item 4</MenuItem>
Expand Down

0 comments on commit c138548

Please sign in to comment.