Skip to content

Commit 85ba82a

Browse files
crisbetommalerba
authored andcommitted
fix(tooltip): avoid capturing the initial tap on mobile (#2423)
Currently the tooltip always binds the mouseenter and mouseleave events, however this can cause the click handlers on the tooltip host not to be fired on the first tap. These changes switch to binding the events only on devices that have touch events. Fixes #2326.
1 parent 7c1c220 commit 85ba82a

File tree

2 files changed

+27
-13
lines changed

2 files changed

+27
-13
lines changed

src/lib/tooltip/tooltip.spec.ts

+7-4
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@ import {TooltipPosition, MdTooltip, MdTooltipModule, SCROLL_THROTTLE_MS} from '.
1818
import {OverlayContainer} from '../core';
1919
import {Dir, LayoutDirection} from '../core/rtl/dir';
2020
import {OverlayModule} from '../core/overlay/overlay-directives';
21+
import {Platform} from '../core/platform/platform';
2122
import {Scrollable} from '../core/overlay/scroll/scrollable';
2223

24+
2325
const initialTooltipMessage = 'initial tooltip message';
2426

2527
describe('MdTooltip', () => {
@@ -31,6 +33,7 @@ describe('MdTooltip', () => {
3133
imports: [MdTooltipModule.forRoot(), OverlayModule],
3234
declarations: [BasicTooltipDemo, ScrollableTooltipDemo, OnPushTooltipDemo],
3335
providers: [
36+
Platform,
3437
{provide: OverlayContainer, useFactory: () => {
3538
overlayContainerElement = document.createElement('div');
3639
document.body.appendChild(overlayContainerElement);
@@ -421,10 +424,10 @@ class BasicTooltipDemo {
421424
<div cdk-scrollable style="padding: 100px; margin: 300px;
422425
height: 200px; width: 200px; overflow: auto;">
423426
<button *ngIf="showButton" style="margin-bottom: 600px"
424-
[md-tooltip]="message"
425-
[tooltip-position]="position">
426-
Button
427-
</button>
427+
[md-tooltip]="message"
428+
[tooltip-position]="position">
429+
Button
430+
</button>
428431
</div>`
429432
})
430433
class ScrollableTooltipDemo {

src/lib/tooltip/tooltip.ts

+20-9
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
NgZone,
1616
Optional,
1717
OnDestroy,
18+
Renderer,
1819
OnInit,
1920
ChangeDetectorRef
2021
} from '@angular/core';
@@ -32,6 +33,7 @@ import {MdTooltipInvalidPositionError} from './tooltip-errors';
3233
import {Observable} from 'rxjs/Observable';
3334
import {Subject} from 'rxjs/Subject';
3435
import {Dir} from '../core/rtl/dir';
36+
import {PlatformModule, Platform} from '../core/platform/index';
3537
import 'rxjs/add/operator/first';
3638
import {ScrollDispatcher} from '../core/overlay/scroll/scroll-dispatcher';
3739
import {Subscription} from 'rxjs/Subscription';
@@ -55,8 +57,6 @@ export const SCROLL_THROTTLE_MS = 20;
5557
host: {
5658
'(longpress)': 'show()',
5759
'(touchend)': 'hide(' + TOUCHEND_HIDE_DELAY + ')',
58-
'(mouseenter)': 'show()',
59-
'(mouseleave)': 'hide()',
6060
},
6161
exportAs: 'mdTooltip',
6262
})
@@ -129,12 +129,23 @@ export class MdTooltip implements OnInit, OnDestroy {
129129
get _matShowDelay() { return this.showDelay; }
130130
set _matShowDelay(v) { this.showDelay = v; }
131131

132-
constructor(private _overlay: Overlay,
133-
private _scrollDispatcher: ScrollDispatcher,
134-
private _elementRef: ElementRef,
135-
private _viewContainerRef: ViewContainerRef,
136-
private _ngZone: NgZone,
137-
@Optional() private _dir: Dir) { }
132+
constructor(
133+
private _overlay: Overlay,
134+
private _elementRef: ElementRef,
135+
private _scrollDispatcher: ScrollDispatcher,
136+
private _viewContainerRef: ViewContainerRef,
137+
private _ngZone: NgZone,
138+
private _renderer: Renderer,
139+
private _platform: Platform,
140+
@Optional() private _dir: Dir) {
141+
142+
// The mouse events shouldn't be bound on iOS devices, because
143+
// they can prevent the first tap from firing it's click event.
144+
if (!_platform.IOS) {
145+
_renderer.listen(_elementRef.nativeElement, 'mouseenter', () => this.show());
146+
_renderer.listen(_elementRef.nativeElement, 'mouseleave', () => this.hide());
147+
}
148+
}
138149

139150
ngOnInit() {
140151
// When a scroll on the page occurs, update the position in case this tooltip needs
@@ -437,7 +448,7 @@ export class TooltipComponent {
437448

438449

439450
@NgModule({
440-
imports: [OverlayModule, CompatibilityModule],
451+
imports: [OverlayModule, CompatibilityModule, PlatformModule],
441452
exports: [MdTooltip, TooltipComponent, CompatibilityModule],
442453
declarations: [MdTooltip, TooltipComponent],
443454
entryComponents: [TooltipComponent],

0 commit comments

Comments
 (0)