From 9ed3fefadd3d1f2c772a8a1b48e8a821e6792122 Mon Sep 17 00:00:00 2001 From: Utkarsh Kukreti Date: Fri, 27 May 2016 22:14:46 +0530 Subject: [PATCH] React Tooltip - Persistence enhancement. Signed-off-by: vikasrohit --- components/Tooltip/Tooltip.jsx | 43 ++++++++++++++++++++++--- components/Tooltip/TooltipExamples.jsx | 20 ++++++++++++ components/Tooltip/TooltipExamples.scss | 6 ++++ 3 files changed, 65 insertions(+), 4 deletions(-) diff --git a/components/Tooltip/Tooltip.jsx b/components/Tooltip/Tooltip.jsx index dcfc47e14..21f900515 100644 --- a/components/Tooltip/Tooltip.jsx +++ b/components/Tooltip/Tooltip.jsx @@ -7,9 +7,14 @@ import ReactDOM from 'react-dom' class Tooltip extends Component { constructor(props) { super(props) + this.tooltipHideTimeout = props.tooltipHideTimeout || 250 this.state = { isActive: false } this.showTooltip = this.showTooltip.bind(this) this.hideTooltip = this.hideTooltip.bind(this) + this.onMouseEnterTarget = this.onMouseEnterTarget.bind(this) + this.onMouseLeaveTarget = this.onMouseLeaveTarget.bind(this) + this.onMouseEnterTooltip = this.onMouseEnterTooltip.bind(this) + this.onMouseLeaveTooltip = this.onMouseLeaveTooltip.bind(this) this.onClick = this.onClick.bind(this) } @@ -102,11 +107,38 @@ class Tooltip extends Component { this.setState({ isActive: !this.state.isActive }) } + onMouseEnterTarget(evt) { + clearTimeout(this.state.hideTooltipTimeout) + this.showTooltip(evt) + } + + onMouseLeaveTarget() { + this.startHideTooltipTimeout() + } + + onMouseEnterTooltip() { + clearTimeout(this.state.hideTooltipTimeout) + } + + onMouseLeaveTooltip() { + this.startHideTooltipTimeout() + } + + startHideTooltipTimeout() { + const timeout = setTimeout(() => { + this.hideTooltip() + }, this.tooltipHideTimeout) + this.setState({hideTooltipTimeout: timeout}) + } + componentDidMount() { const target = ReactDOM.findDOMNode(this).querySelector('.tooltip-target') if (this.props.popMethod === 'hover') { - target.addEventListener('mouseenter', this.showTooltip) - target.addEventListener('mouseleave', this.hideTooltip) + target.addEventListener('mouseenter', this.onMouseEnterTarget) + target.addEventListener('mouseleave', this.onMouseLeaveTarget) + const tooltip = ReactDOM.findDOMNode(this).querySelector('.tooltip-container') + tooltip.addEventListener('mouseenter', this.onMouseEnterTooltip) + tooltip.addEventListener('mouseleave', this.onMouseLeaveTooltip) } else if (this.props.popMethod === 'click') { target.classList.add('click-pointer') target.addEventListener('click', this.onClick) @@ -115,11 +147,14 @@ class Tooltip extends Component { componentWillUnmount() { const target = ReactDOM.findDOMNode(this).querySelector('.tooltip-target') - target.removeEventListener('mouseenter', this.showTooltip) - target.removeEventListener('mouseleave', this.hideTooltip) + target.removeEventListener('mouseenter', this.onMouseEnterTarget) + target.removeEventListener('mouseleave', this.onMouseLeaveTarget) target.removeEventListener('click', this.onClick) + const tooltip = ReactDOM.findDOMNode(this).querySelector('.tooltip-container') + tooltip.removeEventListener('mouseenter', this.onMouseEnterTooltip) + tooltip.removeEventListener('mouseleave', this.onMouseLeaveTooltip) } render() { diff --git a/components/Tooltip/TooltipExamples.jsx b/components/Tooltip/TooltipExamples.jsx index 5a2590255..3c278bb9f 100644 --- a/components/Tooltip/TooltipExamples.jsx +++ b/components/Tooltip/TooltipExamples.jsx @@ -51,6 +51,26 @@ const TooltipExamples = () => (

More text.

+ + +
+ Tooltip Containing Link #1. + This tooltip will stick around for 250ms after being hovered out of. +
+
+ http://www.example.com +
+
+ + +
+ Tooltip Containing Link #2. + This tooltip will stick around for 1000ms after being hovered out of. +
+
+ http://www.example.com +
+
) diff --git a/components/Tooltip/TooltipExamples.scss b/components/Tooltip/TooltipExamples.scss index 4f560cb05..27a9204e5 100644 --- a/components/Tooltip/TooltipExamples.scss +++ b/components/Tooltip/TooltipExamples.scss @@ -46,6 +46,12 @@ height: 200px; width: 200px; } + + &#tooltip-7, &#tooltip-8 { + background-color: steelblue; + height: 200px; + width: 200px; + } } //custom theme &.blue-round {