Updates to Popper to be positions correctly within window

This commit is contained in:
Dominik Prokop 2019-01-18 09:43:47 +01:00
parent 4384eb2f0b
commit c74b39696c
3 changed files with 17 additions and 6 deletions

View File

@ -4,13 +4,12 @@ import { Manager, Popper as ReactPopper } from 'react-popper';
import { Portal } from '@grafana/ui';
import Transition from 'react-transition-group/Transition';
const defaultTransitionStyles = {
transition: 'opacity 200ms linear',
opacity: 0,
};
const transitionStyles: {[key: string]: object} = {
const transitionStyles: { [key: string]: object } = {
exited: { opacity: 0 },
entering: { opacity: 0 },
entered: { opacity: 1 },
@ -23,12 +22,12 @@ interface Props extends React.HTMLAttributes<HTMLDivElement> {
placement?: PopperJS.Placement;
content: string | ((props: any) => JSX.Element);
referenceElement: PopperJS.ReferenceObject;
arrowClassName?: string;
}
class Popper extends PureComponent<Props> {
render() {
const { renderContent, show, placement, onMouseEnter, onMouseLeave, className } = this.props;
const { renderContent, show, placement, onMouseEnter, onMouseLeave, className, arrowClassName } = this.props;
const { content } = this.props;
return (
@ -36,7 +35,12 @@ class Popper extends PureComponent<Props> {
<Transition in={show} timeout={100} mountOnEnter={true} unmountOnExit={true}>
{transitionState => (
<Portal>
<ReactPopper placement={placement} referenceElement={this.props.referenceElement}>
<ReactPopper
placement={placement}
referenceElement={this.props.referenceElement}
// TODO: move modifiers config to popper controller
modifiers={{ preventOverflow: { enabled: true, boundariesElement: 'window' } }}
>
{({ ref, style, placement, arrowProps }) => {
return (
<div
@ -53,7 +57,12 @@ class Popper extends PureComponent<Props> {
>
<div className={className}>
{renderContent(content)}
<div ref={arrowProps.ref} data-placement={placement} className="popper__arrow" />
<div
ref={arrowProps.ref}
style={{ ...arrowProps.style }}
data-placement={placement}
className={arrowClassName}
/>
</div>
</div>
);

View File

@ -70,6 +70,7 @@ class PopperController extends React.Component<Props, State> {
};
renderContent(content: PopperContent) {
if (typeof content === 'function') {
// If it's a function we assume it's a React component
const ReactComponent = content;

View File

@ -28,6 +28,7 @@ export const Tooltip = ({ children, renderContent, theme, ...controllerProps }:
onMouseLeave={hidePopper}
referenceElement={tooltipTriggerRef.current}
className={popperBackgroundClassName}
arrowClassName={'popper__arrow'}
/>
)}
{React.cloneElement(children, {