@@ -26,6 +26,7 @@ import {
26
26
UP_ARROW ,
27
27
} from '@angular/cdk/keycodes' ;
28
28
import { _getEventTarget } from '@angular/cdk/platform' ;
29
+ import { InputModalityDetector } from '@angular/cdk/a11y' ;
29
30
import { fromEvent } from 'rxjs' ;
30
31
import { filter , takeUntil } from 'rxjs/operators' ;
31
32
import { CDK_MENU , Menu } from './menu-interface' ;
@@ -51,7 +52,7 @@ import {CdkMenuTriggerBase, MENU_TRIGGER} from './menu-trigger-base';
51
52
'(focusin)' : '_setHasFocus(true)' ,
52
53
'(focusout)' : '_setHasFocus(false)' ,
53
54
'(keydown)' : '_toggleOnKeydown($event)' ,
54
- '(click)' : 'toggle ()' ,
55
+ '(click)' : '_handleClick ()' ,
55
56
} ,
56
57
inputs : [
57
58
'menuTemplateRef: cdkMenuTriggerFor' ,
@@ -65,24 +66,18 @@ import {CdkMenuTriggerBase, MENU_TRIGGER} from './menu-trigger-base';
65
66
] ,
66
67
} )
67
68
export class CdkMenuTrigger extends CdkMenuTriggerBase implements OnDestroy {
68
- /** The host element. */
69
69
private readonly _elementRef : ElementRef < HTMLElement > = inject ( ElementRef ) ;
70
-
71
- /** The CDK overlay service. */
72
70
private readonly _overlay = inject ( Overlay ) ;
73
-
74
- /** The Angular zone. */
75
71
private readonly _ngZone = inject ( NgZone ) ;
72
+ private readonly _directionality = inject ( Directionality , { optional : true } ) ;
73
+ private readonly _inputModalityDetector = inject ( InputModalityDetector ) ;
76
74
77
75
/** The parent menu this trigger belongs to. */
78
76
private readonly _parentMenu = inject ( CDK_MENU , { optional : true } ) ;
79
77
80
78
/** The menu aim service used by this menu. */
81
79
private readonly _menuAim = inject ( MENU_AIM , { optional : true } ) ;
82
80
83
- /** The directionality of the page. */
84
- private readonly _directionality = inject ( Directionality , { optional : true } ) ;
85
-
86
81
constructor ( ) {
87
82
super ( ) ;
88
83
this . _setRole ( ) ;
@@ -136,7 +131,6 @@ export class CdkMenuTrigger extends CdkMenuTriggerBase implements OnDestroy {
136
131
case SPACE :
137
132
case ENTER :
138
133
if ( ! hasModifierKey ( event ) ) {
139
- event . preventDefault ( ) ;
140
134
this . toggle ( ) ;
141
135
this . childMenu ?. focusFirstItem ( 'keyboard' ) ;
142
136
}
@@ -177,6 +171,15 @@ export class CdkMenuTrigger extends CdkMenuTriggerBase implements OnDestroy {
177
171
}
178
172
}
179
173
174
+ /** Handles clicks on the menu trigger. */
175
+ _handleClick ( ) {
176
+ // Don't handle clicks originating from the keyboard since we
177
+ // already do the same on `keydown` events for enter and space.
178
+ if ( this . _inputModalityDetector . mostRecentModality !== 'keyboard' ) {
179
+ this . toggle ( ) ;
180
+ }
181
+ }
182
+
180
183
/**
181
184
* Sets whether the trigger's menu stack has focus.
182
185
* @param hasFocus Whether the menu stack has focus.
0 commit comments