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(menu): use passive touch listener #14041

Merged
merged 1 commit into from
Nov 10, 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
1 change: 1 addition & 0 deletions src/lib/menu/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ ng_module(
"//src/cdk/keycodes",
"//src/cdk/overlay",
"//src/cdk/portal",
"//src/cdk/platform",
"//src/lib/core",
],
)
Expand Down
17 changes: 16 additions & 1 deletion src/lib/menu/menu-trigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import {
Self,
ViewContainerRef,
} from '@angular/core';
import {normalizePassiveListenerOptions} from '@angular/cdk/platform';
import {asapScheduler, merge, of as observableOf, Subscription} from 'rxjs';
import {delay, filter, take, takeUntil} from 'rxjs/operators';
import {MatMenu} from './menu-directive';
Expand Down Expand Up @@ -60,6 +61,9 @@ export const MAT_MENU_SCROLL_STRATEGY_FACTORY_PROVIDER = {
/** Default top padding of the menu panel. */
export const MENU_PANEL_TOP_PADDING = 8;

/** Options for binding a passive event listener. */
const passiveEventListenerOptions = normalizePassiveListenerOptions({passive: true});

// TODO(andrewseguin): Remove the kebab versions in favor of camelCased attribute selectors

/**
Expand All @@ -72,7 +76,6 @@ export const MENU_PANEL_TOP_PADDING = 8;
'aria-haspopup': 'true',
'[attr.aria-expanded]': 'menuOpen || null',
'(mousedown)': '_handleMousedown($event)',
'(touchstart)': '_openedBy = "touch"',
'(keydown)': '_handleKeydown($event)',
'(click)': '_handleClick($event)',
},
Expand All @@ -87,6 +90,12 @@ export class MatMenuTrigger implements AfterContentInit, OnDestroy {
private _menuCloseSubscription = Subscription.EMPTY;
private _scrollStrategy: () => ScrollStrategy;

/**
* Handles touch start events on the trigger.
* Needs to be an arrow function so we can easily use addEventListener and removeEventListener.
*/
private _handleTouchStart = () => this._openedBy = 'touch';

// Tracking input type is necessary so it's possible to only auto-focus
// the first item of the list when the menu is opened via the keyboard
_openedBy: 'mouse' | 'touch' | null = null;
Expand Down Expand Up @@ -161,6 +170,9 @@ export class MatMenuTrigger implements AfterContentInit, OnDestroy {
// @breaking-change 8.0.0
private _focusMonitor?: FocusMonitor) {

_element.nativeElement.addEventListener('touchstart', this._handleTouchStart,
passiveEventListenerOptions);

if (_menuItemInstance) {
_menuItemInstance._triggersSubmenu = this.triggersSubmenu();
}
Expand All @@ -179,6 +191,9 @@ export class MatMenuTrigger implements AfterContentInit, OnDestroy {
this._overlayRef = null;
}

this._element.nativeElement.removeEventListener('touchstart', this._handleTouchStart,
passiveEventListenerOptions);

this._cleanUpSubscriptions();
}

Expand Down