Skip to content

Commit 361a5d9

Browse files
committed
fix click/touch trigger for hybrid devices
1 parent cfeb43f commit 361a5d9

File tree

7 files changed

+1094
-1278
lines changed

7 files changed

+1094
-1278
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
npm-debug.log
77
yarn-error.log
88
.idea/
9+
.vscode/
910
node_modules/
1011
dist/
1112
.docz/

.size-snapshot.json

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
{
22
"dist/cjs/react-popper-tooltip.js": {
3-
"bundled": 14683,
4-
"minified": 7887,
5-
"gzipped": 2209
3+
"bundled": 14562,
4+
"minified": 7842,
5+
"gzipped": 2194
66
},
77
"dist/esm/react-popper-tooltip.js": {
8-
"bundled": 14631,
9-
"minified": 7854,
10-
"gzipped": 2204,
8+
"bundled": 14510,
9+
"minified": 7809,
10+
"gzipped": 2190,
1111
"treeshaked": {
1212
"rollup": {
13-
"code": 7279,
13+
"code": 7284,
1414
"import_statements": 331
1515
},
1616
"webpack": {
17-
"code": 8513
17+
"code": 8518
1818
}
1919
}
2020
}

package.json

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -43,44 +43,44 @@
4343
"author": "Mohsin Ul Haq <[email protected]>",
4444
"license": "MIT",
4545
"peerDependencies": {
46-
"react": "^16.7.0",
47-
"react-dom": "^16.7.0"
46+
"react": "^16.6.0",
47+
"react-dom": "^16.6.0"
4848
},
4949
"dependencies": {
5050
"@babel/runtime": "^7.3.1",
51-
"react-popper": "^1.3.2"
51+
"react-popper": "^1.3.3"
5252
},
5353
"devDependencies": {
5454
"@babel/cli": "^7.2.3",
55-
"@babel/core": "^7.2.2",
56-
"@babel/plugin-proposal-class-properties": "^7.3.0",
55+
"@babel/core": "^7.3.3",
56+
"@babel/plugin-proposal-class-properties": "^7.3.3",
5757
"@babel/plugin-transform-runtime": "^7.2.0",
5858
"@babel/preset-env": "^7.3.1",
5959
"@babel/preset-react": "^7.0.0",
60-
"@babel/preset-typescript": "^7.1.0",
61-
"@types/jest": "^23.3.13",
62-
"@types/react": "^16.7.20",
63-
"@types/react-dom": "^16.0.11",
60+
"@babel/preset-typescript": "^7.3.3",
61+
"@types/jest": "^24.0.6",
62+
"@types/react": "^16.8.4",
63+
"@types/react-dom": "^16.8.2",
6464
"docz": "^0.13.7",
6565
"docz-plugin-css": "^0.11.0",
6666
"docz-theme-default": "^0.13.7",
6767
"gh-pages": "^2.0.1",
68-
"jest": "^24.0.0",
68+
"jest": "^24.1.0",
6969
"pre-commit": "^1.2.2",
70-
"prettier": "^1.16.1",
71-
"react": "^16.7.0",
72-
"react-dom": "^16.7.0",
73-
"react-testing-library": "^5.4.4",
70+
"prettier": "^1.16.4",
71+
"react": "^16.8.3",
72+
"react-dom": "^16.8.3",
73+
"react-testing-library": "^5.9.0",
7474
"rimraf": "^2.6.3",
75-
"rollup": "^1.1.2",
75+
"rollup": "^1.2.2",
7676
"rollup-plugin-babel": "^4.3.2",
77-
"rollup-plugin-node-resolve": "^4.0.0",
77+
"rollup-plugin-node-resolve": "^4.0.1",
7878
"rollup-plugin-size-snapshot": "^0.8.0",
7979
"tslint": "^5.12.1",
80-
"tslint-config-prettier": "^1.17.0",
80+
"tslint-config-prettier": "^1.18.0",
8181
"tslint-eslint-rules": "^5.4.0",
8282
"tslint-plugin-prettier": "^2.0.1",
8383
"tslint-react": "^3.6.0",
84-
"typescript": "^3.2.4"
84+
"typescript": "^3.3.3333"
8585
}
8686
}

src/Tooltip.tsx

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ class Tooltip extends Component<ITooltipProps> {
9191
}
9292

9393
private handleOutsideClick?: EventListener = event => {
94+
event.stopPropagation();
95+
event.preventDefault();
9496
if (!findDOMNode(this)!.contains(event.target as Node)) {
9597
const {
9698
hideTooltip,
@@ -123,11 +125,8 @@ class Tooltip extends Component<ITooltipProps> {
123125
};
124126

125127
private addOutsideClickHandler = () => {
126-
if ('ontouchend' in window) {
127-
document.addEventListener('touchend', this.handleOutsideClick!);
128-
} else {
129-
document.addEventListener('click', this.handleOutsideClick!);
130-
}
128+
document.addEventListener('touchend', this.handleOutsideClick!);
129+
document.addEventListener('click', this.handleOutsideClick!);
131130
};
132131

133132
private removeOutsideClickHandler = () => {

src/TooltipTrigger.tsx

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,10 @@ class TooltipTrigger extends Component<
184184
this[action]({pageX, pageY});
185185
};
186186

187-
private clickToggle: React.MouseEventHandler = ({pageX, pageY}) => {
187+
private clickToggle: React.MouseEventHandler = (event: React.MouseEvent) => {
188+
event.stopPropagation();
189+
event.preventDefault();
190+
const {pageX, pageY} = event;
188191
const action = this.props.followCursor ? 'showTooltip' : 'toggleTooltip';
189192
this[action]({pageX, pageY});
190193
};
@@ -201,18 +204,13 @@ class TooltipTrigger extends Component<
201204
const isClickTriggered = trigger === 'click';
202205
const isHoverTriggered = trigger === 'hover';
203206
const isRightClickTriggered = trigger === 'right-click';
204-
const isTouchEnabled = canUseDOM() && 'ontouchend' in window;
205207

206208
return {
207209
...props,
208-
...(isClickTriggered &&
209-
isTouchEnabled && {
210-
onTouchEnd: callAll(this.clickToggle, props.onTouchEnd)
211-
}),
212-
...(isClickTriggered &&
213-
!isTouchEnabled && {
214-
onClick: callAll(this.clickToggle, props.onClick)
215-
}),
210+
...(isClickTriggered && {
211+
onClick: callAll(this.clickToggle, props.onClick),
212+
onTouchEnd: callAll(this.clickToggle, props.onTouchEnd)
213+
}),
216214
...(isRightClickTriggered && {
217215
onContextMenu: callAll(this.contextMenuToggle, props.onContextMenu)
218216
}),

tsconfig.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
{
22
"compilerOptions": {
33
/* Basic Options */
4-
"target": "esnext", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */
4+
"target": "es5", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */
55
"module": "esnext", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
6-
// "lib": [], /* Specify library files to be included in the compilation. */
6+
"lib": [
7+
"dom",
8+
"dom.iterable",
9+
"esnext"
10+
], /* Specify library files to be included in the compilation. */
711
// "allowJs": true, /* Allow javascript files to be compiled. */
812
// "checkJs": true, /* Report errors in .js files. */
913
"jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */

0 commit comments

Comments
 (0)