Skip to content

Commit 0389d12

Browse files
devversionjelbourn
authored andcommitted
fix(tooltip): text fields not editable if tooltip is applied in safari (#12959)
Applying `matTooltip` directly on a `<input>` or `<textarea>` element causes HammerJS to add a `user-select: none` inline style. This prevents users from typing into the text fields in Safari. Since the `user-select: none` is not needed for the `longpress` event, we can just **always reset** the `user-select` if a tooltip is applied on a text-field because we want to avoid such issues. Fixes #12953
1 parent be51014 commit 0389d12

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

src/lib/tooltip/tooltip.spec.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -790,19 +790,20 @@ describe('MatTooltip', () => {
790790
});
791791

792792
describe('special cases', () => {
793-
it('should clear the `user-select` when a tooltip is set on a text field in iOS', () => {
794-
platform.IOS = true;
795793

794+
it('should clear the `user-select` when a tooltip is set on a text field', () => {
796795
const fixture = TestBed.createComponent(TooltipOnTextFields);
797796
const instance = fixture.componentInstance;
798797

799798
fixture.detectChanges();
800799

801800
expect(instance.input.nativeElement.style.userSelect).toBeFalsy();
802801
expect(instance.input.nativeElement.style.webkitUserSelect).toBeFalsy();
802+
expect(instance.input.nativeElement.style.msUserSelect).toBeFalsy();
803803

804804
expect(instance.textarea.nativeElement.style.userSelect).toBeFalsy();
805805
expect(instance.textarea.nativeElement.style.webkitUserSelect).toBeFalsy();
806+
expect(instance.textarea.nativeElement.style.msUserSelect).toBeFalsy();
806807
});
807808

808809
it('should clear the `-webkit-user-drag` on draggable elements', () => {

src/lib/tooltip/tooltip.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -217,13 +217,15 @@ export class MatTooltip implements OnDestroy {
217217
.set('mouseenter', () => this.show())
218218
.set('mouseleave', () => this.hide())
219219
.forEach((listener, event) => element.addEventListener(event, listener));
220-
} else if (_platform.IOS && (element.nodeName === 'INPUT' || element.nodeName === 'TEXTAREA')) {
220+
}
221+
222+
if (element.nodeName === 'INPUT' || element.nodeName === 'TEXTAREA') {
221223
// When we bind a gesture event on an element (in this case `longpress`), HammerJS
222224
// will add some inline styles by default, including `user-select: none`. This is
223-
// problematic on iOS, because it will prevent users from typing in inputs. If
224-
// we're on iOS and the tooltip is attached on an input or textarea, we clear
225-
// the `user-select` to avoid these issues.
226-
element.style.webkitUserSelect = element.style.userSelect = '';
225+
// problematic on iOS and in Safari, because it will prevent users from typing in inputs.
226+
// Since `user-select: none` is not needed for the `longpress` event and can cause unexpected
227+
// behavior for text fields, we always clear the `user-select` to avoid such issues.
228+
element.style.webkitUserSelect = element.style.userSelect = element.style.msUserSelect = '';
227229
}
228230

229231
// Hammer applies `-webkit-user-drag: none` on all elements by default,

0 commit comments

Comments
 (0)