Skip to content

feat(cdk-experimental/menu): add ability to close menus when clicking outside the menu tree #20064

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
Jul 27, 2020

Conversation

andy9775
Copy link
Contributor

Add functionality to close any open menus (and submenus) when a user clicks on an element outside
the open menu tree and the menu bar components.

@andy9775 andy9775 requested a review from jelbourn as a code owner July 22, 2020 00:28
@googlebot googlebot added the cla: yes PR author has agreed to Google's Contributor License Agreement label Jul 22, 2020
@andy9775 andy9775 requested a review from teflonwaffles July 22, 2020 00:29
* @return true if the given element is part of the menu module.
*/
function isCdkMenuComponent(target: Element) {
const classList = target.classList;
Copy link
Member

Choose a reason for hiding this comment

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

I think you could get away with target.className.indexOf('cdk-menu') !== -1

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Only check for cdk-menu and skip the others?

Copy link
Member

Choose a reason for hiding this comment

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

They all start with cdk-menu, so as long as that prefix is there you're detecting them all

Copy link
Contributor Author

Choose a reason for hiding this comment

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

My mistake, didn't see that it was className not classList

* @param target the element to check.
* @return true if the given element is part of the menu module.
*/
function isCdkMenuComponent(target: Element) {
Copy link
Member

Choose a reason for hiding this comment

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

How about isElementWithinMenu?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's not really checking if the element is within a menu, but rather if the element is a menu its self. If we go with the below suggestions it could be isMenu. Thoughts?

Copy link
Member

@jelbourn jelbourn Jul 22, 2020

Choose a reason for hiding this comment

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

Yeah, good point, maybe just isMenuElement
(adding "element" here to differentiate between a menu instance)

@andy9775 andy9775 force-pushed the cdk-menu-close-outside-click branch from 3c37ce9 to de4745d Compare July 22, 2020 23:58
@andy9775
Copy link
Contributor Author

@jelbourn feedback should be addressed.

Copy link
Member

@jelbourn jelbourn left a comment

Choose a reason for hiding this comment

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

LGTM

@jelbourn jelbourn added lgtm action: merge The PR is ready for merge by the caretaker target: minor This PR is targeted for the next minor release labels Jul 23, 2020
let triggers: CdkMenuItemTrigger[];

/** open the attached menu. */
const openMenu = () => {

Choose a reason for hiding this comment

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

Nit: our internal style guide recommends using 'function openMenu() {' instead of assigning lambda functions to variables.


const menuGroups = fixture.debugElement.queryAll(By.directive(CdkMenuGroup));
menuGroups[0].nativeElement.click();
menuGroups[1].nativeElement.click();

Choose a reason for hiding this comment

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

Just one may be fine. (With multiple clicks, future readers might wonder if there's a particular reason you clicked multiple times.)

* @param target the element to check.
* @return true if the given element is part of the menu module.
*/
function isMenuElement(target: Element) {

Choose a reason for hiding this comment

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

The name is a little ambiguous. Maybe a more verbose name, 'isElementAMenu', would be clearer?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@jelbourn thoughts?

Copy link
Member

Choose a reason for hiding this comment

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

I'd prefer isMenuElement

… outside the menu tree

Add functionality to close any open menus (and submenus) when a user clicks on an element outside
the open menu tree and the menu bar components.
@andy9775 andy9775 force-pushed the cdk-menu-close-outside-click branch from de4745d to bf1ffb8 Compare July 23, 2020 19:28
@andy9775
Copy link
Contributor Author

@teflonwaffles feedback should be addressed.

@jelbourn jelbourn merged commit 2980b07 into angular:master Jul 27, 2020
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Aug 27, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
action: merge The PR is ready for merge by the caretaker cla: yes PR author has agreed to Google's Contributor License Agreement target: minor This PR is targeted for the next minor release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants