diff --git a/public/app/core/components/Tooltip/Popover.tsx b/public/app/core/components/Tooltip/Popover.tsx index ee86d07fb53..136ff98ff9d 100644 --- a/public/app/core/components/Tooltip/Popover.tsx +++ b/public/app/core/components/Tooltip/Popover.tsx @@ -1,34 +1,19 @@ -import React from 'react'; -import withTooltip from './withTooltip'; -import { Target } from 'react-popper'; - -interface PopoverProps { - tooltipSetState: (prevState: object) => void; -} - -class Popover extends React.Component { - constructor(props) { - super(props); - this.toggleTooltip = this.toggleTooltip.bind(this); - } - - toggleTooltip() { - const { tooltipSetState } = this.props; - tooltipSetState(prevState => { - return { - ...prevState, - show: !prevState.show, - }; - }); - } +import React, { PureComponent } from 'react'; +import Popper from './Popper'; +import withPopper, { UsingPopperProps } from './withPopper'; +class Popover extends PureComponent { render() { + const { children, hidePopper, showPopper, className, ...restProps } = this.props; + + const togglePopper = restProps.show === true ? hidePopper : showPopper; + return ( - - {this.props.children} - +
+ {children} +
); } } -export default withTooltip(Popover); +export default withPopper(Popover); diff --git a/public/app/core/components/Tooltip/Popper.tsx b/public/app/core/components/Tooltip/Popper.tsx new file mode 100644 index 00000000000..164584ea746 --- /dev/null +++ b/public/app/core/components/Tooltip/Popper.tsx @@ -0,0 +1,42 @@ +import React, { PureComponent } from 'react'; +import { Manager, Popper as ReactPopper, Reference } from 'react-popper'; + +interface Props { + renderContent: (content: any) => any; + show: boolean; + placement?: any; + content: string | ((props: any) => JSX.Element); +} + +class Popper extends PureComponent { + render() { + const { children, renderContent, show, placement } = this.props; + const { content } = this.props; + const modifiers = { + flip: { enabled: false }, + preventOverflow: { enabled: false }, + hide: { enabled: false }, + }; + return ( + + {({ ref }) =>
{children}
}
+ {show && ( + + {({ ref, style, placement, arrowProps }) => { + return ( +
+
+ {renderContent(content)} +
+
+
+ ); + }} + + )} + + ); + } +} + +export default Popper; diff --git a/public/app/core/components/Tooltip/Tooltip.tsx b/public/app/core/components/Tooltip/Tooltip.tsx index 62be658f7cf..795da94a03c 100644 --- a/public/app/core/components/Tooltip/Tooltip.tsx +++ b/public/app/core/components/Tooltip/Tooltip.tsx @@ -1,36 +1,17 @@ import React, { PureComponent } from 'react'; -import withTooltip from './withTooltip'; -import { Target } from 'react-popper'; - -interface Props { - tooltipSetState: (prevState: object) => void; -} - -class Tooltip extends PureComponent { - showTooltip = () => { - const { tooltipSetState } = this.props; - - tooltipSetState(prevState => ({ - ...prevState, - show: true, - })); - }; - - hideTooltip = () => { - const { tooltipSetState } = this.props; - tooltipSetState(prevState => ({ - ...prevState, - show: false, - })); - }; +import Popper from './Popper'; +import withPopper, { UsingPopperProps } from './withPopper'; +class Tooltip extends PureComponent { render() { + const { children, hidePopper, showPopper, className, ...restProps } = this.props; + return ( - - {this.props.children} - +
+ {children} +
); } } -export default withTooltip(Tooltip); +export default withPopper(Tooltip); diff --git a/public/app/core/components/Tooltip/__snapshots__/Popover.test.tsx.snap b/public/app/core/components/Tooltip/__snapshots__/Popover.test.tsx.snap index 12ec960a017..5cc3881a607 100644 --- a/public/app/core/components/Tooltip/__snapshots__/Popover.test.tsx.snap +++ b/public/app/core/components/Tooltip/__snapshots__/Popover.test.tsx.snap @@ -3,11 +3,9 @@ exports[`Popover renders correctly 1`] = `
-
+
diff --git a/public/app/core/components/Tooltip/__snapshots__/Tooltip.test.tsx.snap b/public/app/core/components/Tooltip/__snapshots__/Tooltip.test.tsx.snap index 0ec9bdd86fe..d4c0c051435 100644 --- a/public/app/core/components/Tooltip/__snapshots__/Tooltip.test.tsx.snap +++ b/public/app/core/components/Tooltip/__snapshots__/Tooltip.test.tsx.snap @@ -3,12 +3,10 @@ exports[`Tooltip renders correctly 1`] = `
-
+
diff --git a/public/app/core/components/Tooltip/withPopper.tsx b/public/app/core/components/Tooltip/withPopper.tsx new file mode 100644 index 00000000000..247ad104186 --- /dev/null +++ b/public/app/core/components/Tooltip/withPopper.tsx @@ -0,0 +1,83 @@ +import React from 'react'; + +export interface UsingPopperProps { + showPopper: (prevState: object) => void; + hidePopper: (prevState: object) => void; + renderContent: (content: any) => any; + show: boolean; + placement?: string; + content: string | ((props: any) => JSX.Element); + className?: string; +} + +interface Props { + placement?: string; + className?: string; + content: string | ((props: any) => JSX.Element); +} + +interface State { + placement: string; + show: boolean; +} + +export default function withPopper(WrappedComponent) { + return class extends React.Component { + constructor(props) { + super(props); + this.setState = this.setState.bind(this); + this.state = { + placement: this.props.placement || 'auto', + show: false, + }; + } + + componentWillReceiveProps(nextProps) { + if (nextProps.placement && nextProps.placement !== this.state.placement) { + this.setState(prevState => { + return { + ...prevState, + placement: nextProps.placement, + }; + }); + } + } + + showPopper = () => { + this.setState(prevState => ({ + ...prevState, + show: true, + })); + }; + + hidePopper = () => { + this.setState(prevState => ({ + ...prevState, + show: false, + })); + }; + + renderContent(content) { + if (typeof content === 'function') { + // If it's a function we assume it's a React component + const ReactComponent = content; + return ; + } + return content; + } + + render() { + const { show, placement } = this.state; + return ( + + ); + } + }; +} diff --git a/public/app/core/components/Tooltip/withTooltip.tsx b/public/app/core/components/Tooltip/withTooltip.tsx deleted file mode 100644 index a09de67622e..00000000000 --- a/public/app/core/components/Tooltip/withTooltip.tsx +++ /dev/null @@ -1,58 +0,0 @@ -import React from 'react'; -import { Manager, Popper, Arrow } from 'react-popper'; - -interface IwithTooltipProps { - placement?: string; - content: string | ((props: any) => JSX.Element); - className?: string; -} - -export default function withTooltip(WrappedComponent) { - return class extends React.Component { - constructor(props) { - super(props); - - this.setState = this.setState.bind(this); - this.state = { - placement: this.props.placement || 'auto', - show: false, - }; - } - - componentWillReceiveProps(nextProps) { - if (nextProps.placement && nextProps.placement !== this.state.placement) { - this.setState(prevState => { - return { - ...prevState, - placement: nextProps.placement, - }; - }); - } - } - - renderContent(content) { - if (typeof content === 'function') { - // If it's a function we assume it's a React component - const ReactComponent = content; - return ; - } - return content; - } - - render() { - const { content, className } = this.props; - - return ( - - - {this.state.show ? ( - - {this.renderContent(content)} - - - ) : null} - - ); - } - }; -} diff --git a/public/sass/components/_popper.scss b/public/sass/components/_popper.scss index 0b28d5b21f4..ae7cc46435e 100644 --- a/public/sass/components/_popper.scss +++ b/public/sass/components/_popper.scss @@ -1,12 +1,8 @@ .popper { position: absolute; z-index: $zindex-tooltip; - background: $tooltipBackground; color: $tooltipColor; max-width: 400px; - border-radius: 3px; - box-shadow: 0 0 2px rgba(0, 0, 0, 0.5); - padding: 10px; text-align: center; } @@ -35,10 +31,18 @@ left: calc(50% - 5px); margin-top: 0; margin-bottom: 0; + padding-top: 5px; } .popper[data-placement^='bottom'] { - margin-top: 5px; + padding-top: 5px; +} + +.popper__background { + background: $tooltipBackground; + border-radius: 3px; + box-shadow: 0 0 2px rgba(0, 0, 0, 0.5); + padding: 10px; } .popper[data-placement^='bottom'] .popper__arrow { @@ -46,21 +50,21 @@ border-left-color: transparent; border-right-color: transparent; border-top-color: transparent; - top: -5px; - left: calc(50% - 5px); + top: 0; + left: calc(50% - 8px); margin-top: 0; margin-bottom: 0; } .popper[data-placement^='right'] { - margin-left: 5px; + padding-left: 5px; } .popper[data-placement^='right'] .popper__arrow { border-width: 5px 5px 5px 0; border-left-color: transparent; border-top-color: transparent; border-bottom-color: transparent; - left: -5px; - top: calc(50% - 5px); + left: 0; + top: calc(50% - 8px); margin-left: 0; margin-right: 0; }