Skip to content

refactor(cdk-experimental/menu): move listeners from host object to host listener decorator #20164

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
Aug 5, 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
19 changes: 16 additions & 3 deletions src/cdk-experimental/menu/menu-bar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
OnDestroy,
Optional,
NgZone,
HostListener,
} from '@angular/core';
import {Directionality} from '@angular/cdk/bidi';
import {FocusKeyManager, FocusOrigin} from '@angular/cdk/a11y';
Expand Down Expand Up @@ -46,9 +47,6 @@ function isMenuElement(target: Element) {
selector: '[cdkMenuBar]',
exportAs: 'cdkMenuBar',
host: {
'(keydown)': '_handleKeyEvent($event)',
'(document:click)': '_closeOnBackgroundClick($event)',
'(focus)': 'focusFirstItem()',
'role': 'menubar',
'class': 'cdk-menu-bar',
'tabindex': '0',
Expand Down Expand Up @@ -100,6 +98,11 @@ export class CdkMenuBar extends CdkMenuGroup implements Menu, AfterContentInit,
this._subscribeToMouseManager();
}

// In Ivy the `host` metadata will be merged, whereas in ViewEngine it is overridden. In order
// to avoid double event listeners, we need to use `HostListener`. Once Ivy is the default, we
// can move this back into `host`.
// tslint:disable:no-host-decorator-in-concrete
@HostListener('focus')
/** Place focus on the first MenuItem in the menu and set the focus origin. */
focusFirstItem(focusOrigin: FocusOrigin = 'program') {
this._keyManager.setFocusOrigin(focusOrigin);
Expand All @@ -112,6 +115,11 @@ export class CdkMenuBar extends CdkMenuGroup implements Menu, AfterContentInit,
this._keyManager.setLastItemActive();
}

// In Ivy the `host` metadata will be merged, whereas in ViewEngine it is overridden. In order
// to avoid double event listeners, we need to use `HostListener`. Once Ivy is the default, we
// can move this back into `host`.
// tslint:disable:no-host-decorator-in-concrete
@HostListener('keydown', ['$event'])
/**
* Handle keyboard events, specifically changing the focused element and/or toggling the active
* items menu.
Expand Down Expand Up @@ -248,6 +256,11 @@ export class CdkMenuBar extends CdkMenuGroup implements Menu, AfterContentInit,
return this.orientation === 'horizontal';
}

// In Ivy the `host` metadata will be merged, whereas in ViewEngine it is overridden. In order
// to avoid double event listeners, we need to use `HostListener`. Once Ivy is the default, we

Choose a reason for hiding this comment

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

You'll need a stronger condition for shifting it back -- I'd say ViewEngine needs to be deprecated before you could do this.

Copy link
Member

Choose a reason for hiding this comment

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

This is the comment we have throughout the library for this pattern, we can leave it as is (even though it's actually that ViewEngine needs to be deleted before we can do this)

// can move this back into `host`.
// tslint:disable:no-host-decorator-in-concrete
@HostListener('document:click', ['$event'])
/** Close any open submenu if there was a click event which occurred outside the menu stack. */
_closeOnBackgroundClick(event: MouseEvent) {
if (this._hasOpenSubmenu()) {
Expand Down
8 changes: 6 additions & 2 deletions src/cdk-experimental/menu/menu-item-checkbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/

import {Directive} from '@angular/core';
import {Directive, HostListener} from '@angular/core';
import {CdkMenuItemSelectable} from './menu-item-selectable';
import {CdkMenuItem} from './menu-item';

Expand All @@ -18,7 +18,6 @@ import {CdkMenuItem} from './menu-item';
selector: '[cdkMenuItemCheckbox]',
exportAs: 'cdkMenuItemCheckbox',
host: {
'(click)': 'trigger()',
'type': 'button',
'role': 'menuitemcheckbox',
'[attr.aria-checked]': 'checked || null',
Expand All @@ -30,6 +29,11 @@ import {CdkMenuItem} from './menu-item';
],
})
export class CdkMenuItemCheckbox extends CdkMenuItemSelectable {
// In Ivy the `host` metadata will be merged, whereas in ViewEngine it is overridden. In order
// to avoid double event listeners, we need to use `HostListener`. Once Ivy is the default, we
// can move this back into `host`.
// tslint:disable:no-host-decorator-in-concrete
@HostListener('click')
trigger() {
super.trigger();

Expand Down
17 changes: 15 additions & 2 deletions src/cdk-experimental/menu/menu-item-radio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,16 @@
* found in the LICENSE file at https://angular.io/license
*/
import {UniqueSelectionDispatcher} from '@angular/cdk/collections';
import {Directive, OnDestroy, ElementRef, Self, Optional, Inject, NgZone} from '@angular/core';
import {
Directive,
OnDestroy,
ElementRef,
Self,
Optional,
Inject,
NgZone,
HostListener,
} from '@angular/core';
import {Directionality} from '@angular/cdk/bidi';
import {CdkMenuItemSelectable} from './menu-item-selectable';
import {CdkMenuItem} from './menu-item';
Expand All @@ -22,7 +31,6 @@ import {CDK_MENU, Menu} from './menu-interface';
selector: '[cdkMenuItemRadio]',
exportAs: 'cdkMenuItemRadio',
host: {
'(click)': 'trigger()',
'type': 'button',
'role': 'menuitemradio',
'[attr.aria-checked]': 'checked || null',
Expand Down Expand Up @@ -60,6 +68,11 @@ export class CdkMenuItemRadio extends CdkMenuItemSelectable implements OnDestroy
);
}

// In Ivy the `host` metadata will be merged, whereas in ViewEngine it is overridden. In order
// to avoid double event listeners, we need to use `HostListener`. Once Ivy is the default, we
// can move this back into `host`.
// tslint:disable:no-host-decorator-in-concrete
@HostListener('click')
/** Toggles the checked state of the radio-button. */
trigger() {
super.trigger();
Expand Down
7 changes: 6 additions & 1 deletion src/cdk-experimental/menu/menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
Optional,
OnInit,
NgZone,
HostListener,
} from '@angular/core';
import {FocusKeyManager, FocusOrigin} from '@angular/cdk/a11y';
import {
Expand Down Expand Up @@ -51,7 +52,6 @@ import {getItemPointerEntries} from './item-pointer-entries';
selector: '[cdkMenu]',
exportAs: 'cdkMenu',
host: {
'(keydown)': '_handleKeyEvent($event)',
'role': 'menu',
'class': 'cdk-menu',
'[attr.aria-orientation]': 'orientation',
Expand Down Expand Up @@ -136,6 +136,11 @@ export class CdkMenu extends CdkMenuGroup implements Menu, AfterContentInit, OnI
this._keyManager.setLastItemActive();
}

// In Ivy the `host` metadata will be merged, whereas in ViewEngine it is overridden. In order
// to avoid double event listeners, we need to use `HostListener`. Once Ivy is the default, we
// can move this back into `host`.
// tslint:disable:no-host-decorator-in-concrete
@HostListener('keydown', ['$event'])
/** Handle keyboard events for the Menu. */
_handleKeyEvent(event: KeyboardEvent) {
const keyManager = this._keyManager;
Expand Down