Skip to content

Commit

Permalink
feat(menu): use portal
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaly-shaposhnik committed Jan 13, 2021
1 parent 6566efd commit ba12b8e
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions packages/core/src/menu/menu.component.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import React, { useState, useEffect, useCallback } from 'react';
import { createPortal } from 'react-dom';
import PT, { Validator } from 'prop-types';
import { intrinsicComponent } from '@sfx-ui/utils/functions';
import usePortal from '@sfx-ui/hooks/use-portal';
import { intrinsicComponent, generateClassNames } from '@sfx-ui/utils/functions';
import type { MenuProps } from './menu.props';
import Styled from './menu.styles';

Expand All @@ -12,6 +14,7 @@ const Menu = intrinsicComponent<MenuProps, HTMLDivElement>((
): JSX.Element => {
const [timeout, setTimeoutState] = useState<ReturnType<typeof setTimeout> | null>(null);
const [rect, setRect] = useState(new DOMRect());
const target = usePortal(generateClassNames('Menu'));

const updateRect = useCallback(() => {
const defaultPosition = {
Expand Down Expand Up @@ -75,7 +78,7 @@ const Menu = intrinsicComponent<MenuProps, HTMLDivElement>((
}
};

return (
const render = (): JSX.Element => (
<Styled.Wrapper open={Boolean(open)} id={id} ref={ref}>
<Styled.Overlay onClick={handleClose} />

Expand All @@ -90,6 +93,11 @@ const Menu = intrinsicComponent<MenuProps, HTMLDivElement>((
</Styled.Container>
</Styled.Wrapper>
);

return createPortal(
render(),
target,
);
});

export const defaultProps = {
Expand Down

0 comments on commit ba12b8e

Please sign in to comment.