Skip to content

fix(tooltip): unable to type in input with tooltip on iOS #8534

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 29, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 39 additions & 1 deletion src/lib/tooltip/tooltip.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ describe('MatTooltip', () => {
BasicTooltipDemo,
ScrollableTooltipDemo,
OnPushTooltipDemo,
DynamicTooltipsDemo
DynamicTooltipsDemo,
TooltipOnTextFields
],
providers: [
{provide: Platform, useValue: {IOS: false, isBrowser: true}},
Expand Down Expand Up @@ -667,6 +668,25 @@ describe('MatTooltip', () => {
}));
});

describe('special cases', () => {
it('should clear the `user-select` when a tooltip is set on a text field in iOS', () => {
TestBed.overrideProvider(Platform, {
useValue: {IOS: true, isBrowser: true}
});

const fixture = TestBed.createComponent(TooltipOnTextFields);
const instance = fixture.componentInstance;

fixture.detectChanges();

expect(instance.input.nativeElement.style.userSelect).toBeFalsy();
expect(instance.input.nativeElement.style.webkitUserSelect).toBeFalsy();

expect(instance.textarea.nativeElement.style.userSelect).toBeFalsy();
expect(instance.textarea.nativeElement.style.webkitUserSelect).toBeFalsy();
});
});

});

@Component({
Expand Down Expand Up @@ -752,6 +772,24 @@ class DynamicTooltipsDemo {
}
}

@Component({
template: `
<input
#input
style="user-select: none; -webkit-user-select: none"
matTooltip="Something">

<textarea
#textarea
style="user-select: none; -webkit-user-select: none"
matTooltip="Another thing"></textarea>
`,
})
class TooltipOnTextFields {
@ViewChild('input') input: ElementRef;
@ViewChild('textarea') textarea: ElementRef;
}

/** Asserts whether a tooltip directive has a tooltip instance. */
function assertTooltipInstance(tooltip: MatTooltip, shouldExist: boolean): void {
// Note that we have to cast this to a boolean, because Jasmine will go into an infinite loop
Expand Down
11 changes: 10 additions & 1 deletion src/lib/tooltip/tooltip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,8 @@ export class MatTooltip implements OnDestroy {
@Inject(MAT_TOOLTIP_SCROLL_STRATEGY) private _scrollStrategy,
@Optional() private _dir: Directionality) {

const element: HTMLElement = _elementRef.nativeElement;

// The mouse events shouldn't be bound on iOS devices, because
// they can prevent the first tap from firing its click event.
if (!_platform.IOS) {
Expand All @@ -184,9 +186,16 @@ export class MatTooltip implements OnDestroy {

this._manualListeners
.forEach((listener, event) => _elementRef.nativeElement.addEventListener(event, listener));
} else if (element.nodeName === 'INPUT' || element.nodeName === 'TEXTAREA') {
// When we bind a gesture event on an element (in this case `longpress`), HammerJS
// will add some inline styles by default, including `user-select: none`. This is
// problematic on iOS, because it will prevent users from typing in inputs. If
// we're on iOS and the tooltip is attached on an input or textarea, we clear
// the `user-select` to avoid these issues.
element.style.webkitUserSelect = element.style.userSelect = '';
}

_focusMonitor.monitor(_elementRef.nativeElement, false).subscribe(origin => {
_focusMonitor.monitor(element, false).subscribe(origin => {
// Note that the focus monitor runs outside the Angular zone.
if (!origin) {
_ngZone.run(() => this.hide(0));
Expand Down