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

feat(floating-menu): add close-on-blur behavior #219

Merged

Conversation

asudoh
Copy link
Contributor

@asudoh asudoh commented Jul 11, 2017

Overview

With this change, floating menu closes when it loses focus, and sets the focus back to the trigger button.

Added

  • A feature to close floating menu when it loses focus
  • track-blur mix-in which calls handleBlur method when component's root element loses focus

Testing / Reviewing

Testing should make sure dropdown, overflow-menu and tooltip are not broken.

This change introduces track-blur mix-in which calls `handleBlur` method when component's root element loses focus.

With this change, floating menu closes when it loses focus, and sets the focus back to the trigger button.
@asudoh asudoh force-pushed the floating-menu-close-on-blur branch from 31ef994 to 5f3d80a Compare July 11, 2017 22:09
@@ -169,6 +183,7 @@ class FloatingMenu extends mixin(createComponent, eventedShowHideState) {
}
this._getContainer().appendChild(this.element);
this._place();
(this.element.querySelector(this.options.selectorPrimaryFocus) || this.element).focus();
Copy link
Contributor

Choose a reason for hiding this comment

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

If selectorPrimaryFocus is unspecified then focus should be placed on the first focusable element inside the menu.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@iangfleming First focusable element is more complex topic than a human would imagine. A good interpretation of HTML spec can be found here.

Instead, this line does the following; If there is no element matching this.options.selectorPrimaryFocus, try to focus on the floating menu itself. If the floating menu is focusable, user clicking on tab key after floating menu gets open is automatically navigated to the first focusable element.

Copy link
Contributor

Choose a reason for hiding this comment

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

@asudoh My concern is that if a dev is dynamically loading or changing the menu items and doesn't properly add selectorPrimaryFocus then the end user's experience will be extremely broken. When I was researching this I had come across that stackoverflow post you mentioned so I definitely understand that "first focusable element" is tricky. Maybe a better addition would be if selectorPrimaryFocus isn't found try and find the first a, button, input, select, or [tabindex]. That won't cover all focusable elements but it will cover the vast majority of use cases.

Copy link
Contributor Author

@asudoh asudoh Jul 19, 2017

Choose a reason for hiding this comment

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

My concern is that if a dev is dynamically loading or changing the menu items and doesn't properly add selectorPrimaryFocus then the end user's experience will be extremely broken

@iangfleming I can see the concern. There is a logic to alleviate that - I have made a change to the sample HTML to demonstrate how it works: 230d500

You can remove data-floating-menu-primary-focus attributes from the HTML to see that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Below shows how it works without data-floating-menu-primary-focus:

floating-menu-without-primary-focus

Copy link
Contributor

Choose a reason for hiding this comment

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

cool! looks good!

@iangfleming
Copy link
Contributor

this is actually amazing! thank you! 👍 ✅

@chrisdhanaraj chrisdhanaraj merged commit eafa055 into carbon-design-system:master Jul 20, 2017
joshblack pushed a commit to joshblack/carbon that referenced this pull request May 8, 2019
…on-design-system#219)

* Update $productive-heading-05 to Light weight 

$expressive-heading-04 and -05 are using the light weight at 28px; $productive-heading-05 needs to be updated to match.

* chore(type): sync to styles and snapshots
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants