Skip to content

Commit 3464ce0

Browse files
author
vikasrohit
committed
Merge pull request #85 from appirio-tech/feature/tooltip-persistence-enhancement
React Tooltip - Persistence enhancement.
2 parents 0a27a49 + 9ed3fef commit 3464ce0

File tree

3 files changed

+65
-4
lines changed

3 files changed

+65
-4
lines changed

components/Tooltip/Tooltip.jsx

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,14 @@ import ReactDOM from 'react-dom'
77
class Tooltip extends Component {
88
constructor(props) {
99
super(props)
10+
this.tooltipHideTimeout = props.tooltipHideTimeout || 250
1011
this.state = { isActive: false }
1112
this.showTooltip = this.showTooltip.bind(this)
1213
this.hideTooltip = this.hideTooltip.bind(this)
14+
this.onMouseEnterTarget = this.onMouseEnterTarget.bind(this)
15+
this.onMouseLeaveTarget = this.onMouseLeaveTarget.bind(this)
16+
this.onMouseEnterTooltip = this.onMouseEnterTooltip.bind(this)
17+
this.onMouseLeaveTooltip = this.onMouseLeaveTooltip.bind(this)
1318
this.onClick = this.onClick.bind(this)
1419
}
1520

@@ -102,11 +107,38 @@ class Tooltip extends Component {
102107
this.setState({ isActive: !this.state.isActive })
103108
}
104109

110+
onMouseEnterTarget(evt) {
111+
clearTimeout(this.state.hideTooltipTimeout)
112+
this.showTooltip(evt)
113+
}
114+
115+
onMouseLeaveTarget() {
116+
this.startHideTooltipTimeout()
117+
}
118+
119+
onMouseEnterTooltip() {
120+
clearTimeout(this.state.hideTooltipTimeout)
121+
}
122+
123+
onMouseLeaveTooltip() {
124+
this.startHideTooltipTimeout()
125+
}
126+
127+
startHideTooltipTimeout() {
128+
const timeout = setTimeout(() => {
129+
this.hideTooltip()
130+
}, this.tooltipHideTimeout)
131+
this.setState({hideTooltipTimeout: timeout})
132+
}
133+
105134
componentDidMount() {
106135
const target = ReactDOM.findDOMNode(this).querySelector('.tooltip-target')
107136
if (this.props.popMethod === 'hover') {
108-
target.addEventListener('mouseenter', this.showTooltip)
109-
target.addEventListener('mouseleave', this.hideTooltip)
137+
target.addEventListener('mouseenter', this.onMouseEnterTarget)
138+
target.addEventListener('mouseleave', this.onMouseLeaveTarget)
139+
const tooltip = ReactDOM.findDOMNode(this).querySelector('.tooltip-container')
140+
tooltip.addEventListener('mouseenter', this.onMouseEnterTooltip)
141+
tooltip.addEventListener('mouseleave', this.onMouseLeaveTooltip)
110142
} else if (this.props.popMethod === 'click') {
111143
target.classList.add('click-pointer')
112144
target.addEventListener('click', this.onClick)
@@ -115,11 +147,14 @@ class Tooltip extends Component {
115147

116148
componentWillUnmount() {
117149
const target = ReactDOM.findDOMNode(this).querySelector('.tooltip-target')
118-
target.removeEventListener('mouseenter', this.showTooltip)
119-
target.removeEventListener('mouseleave', this.hideTooltip)
150+
target.removeEventListener('mouseenter', this.onMouseEnterTarget)
151+
target.removeEventListener('mouseleave', this.onMouseLeaveTarget)
120152

121153
target.removeEventListener('click', this.onClick)
122154

155+
const tooltip = ReactDOM.findDOMNode(this).querySelector('.tooltip-container')
156+
tooltip.removeEventListener('mouseenter', this.onMouseEnterTooltip)
157+
tooltip.removeEventListener('mouseleave', this.onMouseLeaveTooltip)
123158
}
124159

125160
render() {

components/Tooltip/TooltipExamples.jsx

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,26 @@ const TooltipExamples = () => (
5151
<p>More text.</p>
5252
</div>
5353
</Tooltip>
54+
55+
<Tooltip tooltipHideTimeout={250}>
56+
<div className="tooltip-target" id="tooltip-7">
57+
Tooltip Containing Link #1.
58+
This tooltip will stick around for 250ms after being hovered out of.
59+
</div>
60+
<div className="tooltip-body">
61+
<a href="https://www.example.com">http://www.example.com</a>
62+
</div>
63+
</Tooltip>
64+
65+
<Tooltip tooltipHideTimeout={1000}>
66+
<div className="tooltip-target" id="tooltip-8">
67+
Tooltip Containing Link #2.
68+
This tooltip will stick around for 1000ms after being hovered out of.
69+
</div>
70+
<div className="tooltip-body">
71+
<a href="https://www.example.com">http://www.example.com</a>
72+
</div>
73+
</Tooltip>
5474
</div>
5575
)
5676

components/Tooltip/TooltipExamples.scss

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@
4646
height: 200px;
4747
width: 200px;
4848
}
49+
50+
&#tooltip-7, &#tooltip-8 {
51+
background-color: steelblue;
52+
height: 200px;
53+
width: 200px;
54+
}
4955
}
5056
//custom theme
5157
&.blue-round {

0 commit comments

Comments
 (0)