Skip to content

Commit 9b81044

Browse files
authored
revert: should use touchstart and mousedown (#52)
1 parent fe33630 commit 9b81044

File tree

1 file changed

+26
-4
lines changed

1 file changed

+26
-4
lines changed

src/Trigger.jsx

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ function returnDocument() {
1919
return window.document;
2020
}
2121

22-
// use fastclick for mobile touch
23-
const ALL_HANDLERS = ['onClick', 'onMouseDown', 'onMouseEnter',
22+
const ALL_HANDLERS = ['onClick', 'onMouseDown', 'onTouchStart', 'onMouseEnter',
2423
'onMouseLeave', 'onFocus', 'onBlur'];
2524

2625
const Trigger = createReactClass({
@@ -166,7 +165,13 @@ const Trigger = createReactClass({
166165
if (!this.clickOutsideHandler && this.isClickToHide()) {
167166
currentDocument = props.getDocument();
168167
this.clickOutsideHandler = addEventListener(currentDocument,
169-
'click', this.onDocumentClick);
168+
'mousedown', this.onDocumentClick);
169+
}
170+
// always hide on mobile
171+
if (!this.touchOutsideHandler) {
172+
currentDocument = currentDocument || props.getDocument();
173+
this.touchOutsideHandler = addEventListener(currentDocument,
174+
'touchstart', this.onDocumentClick);
170175
}
171176
return;
172177
}
@@ -219,6 +224,11 @@ const Trigger = createReactClass({
219224
this.preClickTime = Date.now();
220225
},
221226

227+
onTouchStart(e) {
228+
this.fireEvents('onTouchStart', e);
229+
this.preTouchTime = Date.now();
230+
},
231+
222232
onBlur(e) {
223233
this.fireEvents('onBlur', e);
224234
this.clearDelayTimer();
@@ -232,15 +242,20 @@ const Trigger = createReactClass({
232242
// focus will trigger click
233243
if (this.focusTime) {
234244
let preTime;
235-
if (this.preClickTime) {
245+
if (this.preClickTime && this.preTouchTime) {
246+
preTime = Math.min(this.preClickTime, this.preTouchTime);
247+
} else if (this.preClickTime) {
236248
preTime = this.preClickTime;
249+
} else if (this.preTouchTime) {
250+
preTime = this.preTouchTime;
237251
}
238252
if (Math.abs(preTime - this.focusTime) < 20) {
239253
return;
240254
}
241255
this.focusTime = 0;
242256
}
243257
this.preClickTime = 0;
258+
this.preTouchTime = 0;
244259
event.preventDefault();
245260
const nextVisible = !this.state.popupVisible;
246261
if (this.isClickToHide() && !nextVisible || nextVisible && this.isClickToShow()) {
@@ -365,6 +380,11 @@ const Trigger = createReactClass({
365380
this.clickOutsideHandler.remove();
366381
this.clickOutsideHandler = null;
367382
}
383+
384+
if (this.touchOutsideHandler) {
385+
this.touchOutsideHandler.remove();
386+
this.touchOutsideHandler = null;
387+
}
368388
},
369389

370390
createTwoChains(event) {
@@ -434,9 +454,11 @@ const Trigger = createReactClass({
434454
if (this.isClickToHide() || this.isClickToShow()) {
435455
newChildProps.onClick = this.onClick;
436456
newChildProps.onMouseDown = this.onMouseDown;
457+
newChildProps.onTouchStart = this.onTouchStart;
437458
} else {
438459
newChildProps.onClick = this.createTwoChains('onClick');
439460
newChildProps.onMouseDown = this.createTwoChains('onMouseDown');
461+
newChildProps.onTouchStart = this.createTwoChains('onTouchStart');
440462
}
441463
if (this.isMouseEnterToShow()) {
442464
newChildProps.onMouseEnter = this.onMouseEnter;

0 commit comments

Comments
 (0)