Skip to content

Commit 3299370

Browse files
committed
fix(tooltip): text fields not editable if tooltip is applied in safari
* 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. Similarly to the IOS behavior, we just reset the `user-select` in Safari because we want to avoid such issues. Fixes #12953
1 parent 193c2d0 commit 3299370

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

src/lib/tooltip/tooltip.spec.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,12 @@ describe('MatTooltip', () => {
4545
let overlayContainer: OverlayContainer;
4646
let overlayContainerElement: HTMLElement;
4747
let dir: {value: Direction};
48-
let platform: {IOS: boolean, isBrowser: boolean, ANDROID: boolean};
48+
let platform: {IOS: boolean, SAFARI: boolean, isBrowser: boolean, ANDROID: boolean};
4949
let focusMonitor: FocusMonitor;
5050

5151
beforeEach(async(() => {
5252
// Set the default Platform override that can be updated before component creation.
53-
platform = {IOS: false, isBrowser: true, ANDROID: false};
53+
platform = {IOS: false, SAFARI: false, isBrowser: true, ANDROID: false};
5454

5555
TestBed.configureTestingModule({
5656
imports: [MatTooltipModule, OverlayModule, NoopAnimationsModule],
@@ -805,6 +805,21 @@ describe('MatTooltip', () => {
805805
expect(instance.textarea.nativeElement.style.webkitUserSelect).toBeFalsy();
806806
});
807807

808+
it('should clear the `user-select` when a tooltip is set on a text field in Safari', () => {
809+
platform.SAFARI = true;
810+
811+
const fixture = TestBed.createComponent(TooltipOnTextFields);
812+
const instance = fixture.componentInstance;
813+
814+
fixture.detectChanges();
815+
816+
expect(instance.input.nativeElement.style.userSelect).toBeFalsy();
817+
expect(instance.input.nativeElement.style.webkitUserSelect).toBeFalsy();
818+
819+
expect(instance.textarea.nativeElement.style.userSelect).toBeFalsy();
820+
expect(instance.textarea.nativeElement.style.webkitUserSelect).toBeFalsy();
821+
});
822+
808823
it('should clear the `-webkit-user-drag` on draggable elements', () => {
809824
const fixture = TestBed.createComponent(TooltipOnDraggableElement);
810825

src/lib/tooltip/tooltip.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -217,12 +217,16 @@ 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 ((_platform.IOS || _platform.SAFARI) &&
223+
(element.nodeName === 'INPUT' || element.nodeName === 'TEXTAREA')) {
224+
221225
// When we bind a gesture event on an element (in this case `longpress`), HammerJS
222226
// 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.
227+
// problematic on iOS and in Safari, because it will prevent users from typing in inputs.
228+
// If we're on iOS or inside of Safari and the tooltip is attached on an input or textarea,
229+
// we clear the `user-select` inline style to avoid these issues.
226230
element.style.webkitUserSelect = element.style.userSelect = '';
227231
}
228232

0 commit comments

Comments
 (0)