Skip to content

fix(slide-toggle): clicks not landing correctly in some cases on Chrome #18285

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
Jan 29, 2020
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
1 change: 1 addition & 0 deletions src/material/slide-toggle/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ ng_test_library(
),
deps = [
":slide-toggle",
"//src/cdk/a11y",
"//src/cdk/bidi",
"//src/cdk/observers",
"//src/cdk/testing/private",
Expand Down
15 changes: 15 additions & 0 deletions src/material/slide-toggle/slide-toggle.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ import {
flushMicrotasks,
TestBed,
tick,
inject,
} from '@angular/core/testing';
import {FormControl, FormsModule, NgModel, ReactiveFormsModule} from '@angular/forms';
import {By} from '@angular/platform-browser';
import {FocusMonitor} from '@angular/cdk/a11y';
import {MatSlideToggle, MatSlideToggleChange, MatSlideToggleModule} from './index';
import {MAT_SLIDE_TOGGLE_DEFAULT_OPTIONS} from './slide-toggle-config';

Expand Down Expand Up @@ -310,6 +312,19 @@ describe('MatSlideToggle without forms', () => {
expect(document.activeElement).toBe(inputElement);
});

it('should not manually move focus to underlying input when focus comes from mouse or touch',
inject([FocusMonitor], (focusMonitor: FocusMonitor) => {
expect(document.activeElement).not.toBe(inputElement);

focusMonitor.focusVia(slideToggleElement, 'mouse');
fixture.detectChanges();
expect(document.activeElement).not.toBe(inputElement);

focusMonitor.focusVia(slideToggleElement, 'touch');
fixture.detectChanges();
expect(document.activeElement).not.toBe(inputElement);
}));

it('should set a element class if labelPosition is set to before', () => {
expect(slideToggleElement.classList).not.toContain('mat-slide-toggle-label-before');

Expand Down
9 changes: 7 additions & 2 deletions src/material/slide-toggle/slide-toggle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ const _MatSlideToggleMixinBase:
'[class.mat-disabled]': 'disabled',
'[class.mat-slide-toggle-label-before]': 'labelPosition == "before"',
'[class._mat-animation-noopable]': '_animationMode === "NoopAnimations"',
'(focus)': '_inputElement.nativeElement.focus()',
},
templateUrl: 'slide-toggle.html',
styleUrls: ['slide-toggle.css'],
Expand Down Expand Up @@ -193,7 +192,13 @@ export class MatSlideToggle extends _MatSlideToggleMixinBase implements OnDestro
this._focusMonitor
.monitor(this._elementRef, true)
.subscribe(focusOrigin => {
if (!focusOrigin) {
// Only forward focus manually when it was received programmatically or through the
// keyboard. We should not do this for mouse/touch focus for two reasons:
// 1. It can prevent clicks from landing in Chrome (see #18269).
// 2. They're already handled by the wrapping `label` element.
if (focusOrigin === 'keyboard' || focusOrigin === 'program') {
this._inputElement.nativeElement.focus();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Related to that, did we consider forwarding the focus origin here? (through focusVia)?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't mind changing it if you feel strongly about it. I didn't do it, because there's nothing reacting to different focus origins on the input anyway so it seemed redundant.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it was mostly for sanity. I don't feel strongly about it. Let's just leave it as is.

} else if (!focusOrigin) {
// When a focused element becomes disabled, the browser *immediately* fires a blur event.
// Angular does not expect events to be raised during change detection, so any state
// change (such as a form control's 'ng-touched') will cause a changed-after-checked
Expand Down