Skip to content

Commit

Permalink
[update] resolves #80, using try/catch for adding event listeners, ch…
Browse files Browse the repository at this point in the history
…ange the timeout for showing the tooltip and don't render tooltip when using mobile devices
  • Loading branch information
ZhijieZhang committed Dec 15, 2018
1 parent e8685b7 commit bdcfa17
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
22 changes: 11 additions & 11 deletions src/components/ToolTip/ToolTip.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import ReactDOM from 'react-dom';
import PropTypes from 'prop-types';
import { translate } from 'react-i18next';

import { isMac } from 'helpers/device';
import { isMac, isIOS, isAndroid } from 'helpers/device';

import './ToolTip.scss';

Expand Down Expand Up @@ -68,16 +68,15 @@ class ToolTip extends React.PureComponent {
}

addEventListeners = DOMElement => {
if (!DOMElement) {
// we have this check just to make sure UI doesn't blow up when DOMElement is null
// although we haven't met this situation
try {
DOMElement.addEventListener('mouseenter', this.show);
DOMElement.addEventListener('mouseleave', this.hide);
DOMElement.addEventListener('click', this.hide);
} catch (e) {
// we have this catch block here just to make sure UI doesn't blow up when DOMElement is null
// although we haven't met this situation yet
console.warn(`${this.props.children} is rendering null`);
return;
}

DOMElement.addEventListener('mouseenter', this.show);
DOMElement.addEventListener('mouseleave', this.hide);
DOMElement.addEventListener('click', this.hide);
}

setTopAndLeft = DOMElement => {
Expand Down Expand Up @@ -117,7 +116,7 @@ class ToolTip extends React.PureComponent {
show = () => {
this.showTimer = setTimeout(() => {
this.setState({ show: true });
}, this.props.delayShow);
}, this.props.delayShow - this.opacityTimeout);
}

hide = () => {
Expand Down Expand Up @@ -174,12 +173,13 @@ class ToolTip extends React.PureComponent {

render() {
const { location, content } = this.props;
const isUsingMobileDevices = isIOS || isAndroid;

return (
<React.Fragment>
{this.renderChildren()}
{
this.state.show && content &&
this.state.show && content && !isUsingMobileDevices &&
ReactDOM.createPortal(
<div className={`tooltip--${location}`} style={this.state.style}>
<div className={`tooltip__content`}>{this.renderContent()}</div>
Expand Down
1 change: 1 addition & 0 deletions src/helpers/device.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ export const isIEEdge = navigator.userAgent.indexOf('Edge') > -1;
export const isIE11 = navigator.userAgent.indexOf('Trident/7.0') > -1;
export const isIE = navigator.userAgent.indexOf('Edge') > -1 || navigator.userAgent.indexOf('Trident/7.0') > -1;
export const isIOS = window.navigator.userAgent.match(/(iPad|iPhone|iPod)/i);
export const isAndroid = window.navigator.userAgent.match(/Android/i);
export const isMac = navigator.appVersion.indexOf('Mac') > -1;

0 comments on commit bdcfa17

Please # to comment.