Skip to content

Commit

Permalink
fix(popover): render Portal (#1464)
Browse files Browse the repository at this point in the history
某些时候无法正常显示popover, 比如说:overfilow:hidden
  • Loading branch information
berber1016 committed Nov 16, 2021
1 parent 4681bdc commit 1e8458a
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 16 deletions.
38 changes: 22 additions & 16 deletions src/popover/Popover.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React, { useState, useMemo, useCallback, useEffect, useRef, useLayoutEffe
import classNames from 'classnames';
import { debounce, isUndefined } from 'lodash';
import { usePopper } from 'react-popper';
import ReactDOM from 'react-dom';
import { PopoverProps, placements } from './interface';
import usePrefixCls from '../utils/hooks/use-prefix-cls';

Expand All @@ -21,6 +22,8 @@ const Popover = (props: PopoverProps) => {
overlayInnerClassName,
overlayStyle,
children,
strategy = 'absolute',
getContainer,
} = props;

const prefixCls = usePrefixCls('popover-new', customPrefixCls);
Expand Down Expand Up @@ -52,6 +55,7 @@ const Popover = (props: PopoverProps) => {
const { styles, attributes } = usePopper(referenceElement.current, popperElement.current, {
placement: placements[placement],
modifiers: [{ name: 'arrow', options: { element: arrowElement.current } }],
strategy,
});

const updateVisible = useCallback(
Expand Down Expand Up @@ -144,27 +148,29 @@ const Popover = (props: PopoverProps) => {
}
return roles;
}, [isClickToShow, isFocusToShow, isHoverToShow, onClick, onFocus, onBlur, onMouseEnter, onMouseLeave]);

const contentRender = content && (
<div
{...attributes.popper}
className={contentCls}
ref={popperElement}
style={{ ...(overlayStyle || {}), ...styles.popper }}
onMouseEnter={onContentMouseEnter}
onMouseLeave={onContentMouseLeave}
>
{allowArrow && <div className={`${prefixCls}__arrow`} ref={arrowElement} style={{ ...styles.arrow }} />}
<div className={contentInnerCls} style={overlayInnerStyle}>
{content}
</div>
</div>
);
return (
<>
<div className={`${prefixCls}__popcorn`} ref={referenceElement} {...divRoles}>
{children}
</div>
{content && (
<div
{...attributes.popper}
className={contentCls}
ref={popperElement}
style={{ ...(overlayStyle || {}), ...styles.popper }}
onMouseEnter={onContentMouseEnter}
onMouseLeave={onContentMouseLeave}
>
{allowArrow && <div className={`${prefixCls}__arrow`} ref={arrowElement} style={{ ...styles.arrow }} />}
<div className={contentInnerCls} style={overlayInnerStyle}>
{content}
</div>
</div>
)}
{typeof getContainer === 'function'
? ReactDOM.createPortal(contentRender, getContainer(referenceElement.current as HTMLDivElement))
: contentRender}
</>
);
};
Expand Down
19 changes: 19 additions & 0 deletions src/popover/demos/Popover.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -224,3 +224,22 @@ export const Enterable = EnterableTemplate.bind({});
Enterable.args = {
content,
};

const PortalTemplate: Story<PopoverProps> = (args) => (
<>
<div style={{ overflow: 'hidden' }}>
<Popover {...args} strategy="fixed">
<Input value="Popover supports mouse enter!" />
</Popover>
</div>
<span>|</span>
<Popover {...args} getContainer={(node) => node?.parentElement || document.body}>
<Input value="Popover does't support mouse enter!" />
</Popover>
</>
);

export const Portal = PortalTemplate.bind({});
Portal.args = {
content,
};
6 changes: 6 additions & 0 deletions src/popover/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ export interface PopoverProps {
* 卡片内容区域的样式对象
*/
overlayInnerStyle?: React.CSSProperties;
/**
* position 布局方式
* This can be useful if you want to position a tooltip inside an overflow: hidden container that you want to make overflow.
* Please note that you can also try strategy: 'fixed' to obtain a similar effect with less hassle.
*/
strategy?: 'fixed' | 'absolute';
/**
* 浮动显示的层
*/
Expand Down

1 comment on commit 1e8458a

@vercel
Copy link

@vercel vercel bot commented on 1e8458a Nov 16, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.