Skip to content
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

fix(ripple): don't launch ripple for fake mouse events #11997

Merged
merged 1 commit into from
Aug 27, 2018
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
4 changes: 4 additions & 0 deletions src/cdk/testing/event-objects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ export function createMouseEvent(type: string, x = 0, y = 0, button = 0) {
button, /* button */
null /* relatedTarget */);

// `initMouseEvent` doesn't allow us to pass the `buttons` and
// defaults it to 0 which looks like a fake event.
Object.defineProperty(event, 'buttons', {get: () => 1});

return event;
}

Expand Down
1 change: 1 addition & 0 deletions src/lib/core/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ ng_module(
":option/optgroup.css",
] + glob(["**/*.html"]),
deps = [
"//src/cdk/a11y",
"//src/cdk/bidi",
"//src/cdk/coercion",
"//src/cdk/keycodes",
Expand Down
6 changes: 5 additions & 1 deletion src/lib/core/ripple/ripple-renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/
import {ElementRef, NgZone} from '@angular/core';
import {Platform, supportsPassiveEventListeners} from '@angular/cdk/platform';
import {isFakeMousedownFromScreenReader} from '@angular/cdk/a11y';
import {RippleRef, RippleState} from './ripple-ref';

export type RippleConfig = {
Expand Down Expand Up @@ -246,10 +247,13 @@ export class RippleRenderer {

/** Function being called whenever the trigger is being pressed using mouse. */
private onMousedown = (event: MouseEvent) => {
// Screen readers will fire fake mouse events for space/enter. Skip launching a
// ripple in this case for consistency with the non-screen-reader experience.
const isFakeMousedown = isFakeMousedownFromScreenReader(event);
const isSyntheticEvent = this._lastTouchStartEvent &&
Date.now() < this._lastTouchStartEvent + ignoreMouseEventsTimeout;

if (!this._target.rippleDisabled && !isSyntheticEvent) {
if (!this._target.rippleDisabled && !isFakeMousedown && !isSyntheticEvent) {
this._isPointerDown = true;
this.fadeInRipple(event.clientX, event.clientY, this._target.rippleConfig);
}
Expand Down
10 changes: 10 additions & 0 deletions src/lib/core/ripple/ripple.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
createTouchEvent,
dispatchMouseEvent,
dispatchTouchEvent,
createMouseEvent,
} from '@angular/cdk/testing';
import {defaultRippleAnimationConfig, RippleAnimationConfig} from './ripple-renderer';
import {
Expand Down Expand Up @@ -166,6 +167,15 @@ describe('MatRipple', () => {
expect(rippleTarget.querySelectorAll('.mat-ripple-element').length).toBe(0);
}));

it('should ignore fake mouse events from screen readers', () => fakeAsync(() => {
const event = createMouseEvent('mousedown');
Object.defineProperty(event, 'buttons', {get: () => 0});

dispatchEvent(rippleTarget, event);
tick(enterDuration);
expect(rippleTarget.querySelector('.mat-ripple-element')).toBeFalsy();
}));

it('removes ripple after timeout', fakeAsync(() => {
dispatchMouseEvent(rippleTarget, 'mousedown');
dispatchMouseEvent(rippleTarget, 'mouseup');
Expand Down