Skip to content

Commit c7dbdb5

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 90ba31d commit c7dbdb5

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
@@ -213,13 +213,15 @@ export class MatTooltip implements OnDestroy {
213213
.set('mouseenter', () => this.show())
214214
.set('mouseleave', () => this.hide())
215215
.forEach((listener, event) => element.addEventListener(event, listener));
216-
} else if (_platform.IOS && (element.nodeName === 'INPUT' || element.nodeName === 'TEXTAREA')) {
216+
}
217+
218+
if (element.nodeName === 'INPUT' || element.nodeName === 'TEXTAREA') {
217219
// When we bind a gesture event on an element (in this case `longpress`), HammerJS
218220
// will add some inline styles by default, including `user-select: none`. This is
219-
// problematic on iOS, because it will prevent users from typing in inputs. If
220-
// we're on iOS and the tooltip is attached on an input or textarea, we clear
221-
// the `user-select` to avoid these issues.
222-
element.style.webkitUserSelect = element.style.userSelect = '';
221+
// problematic on iOS and in Safari, because it will prevent users from typing in inputs.
222+
// Since `user-select: none` is not needed for the `longpress` event and can cause unexpected
223+
// behavior for text fields, we always clear the `user-select` to avoid such issues.
224+
element.style.webkitUserSelect = element.style.userSelect = element.style.msUserSelect = '';
223225
}
224226

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

0 commit comments

Comments
 (0)