-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
chrisdhanaraj
merged 2 commits into
carbon-design-system:master
from
asudoh:floating-menu-close-on-blur
Jul 20, 2017
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import on from '../misc/on'; | ||
|
||
export default function (ToMix) { | ||
class TrackBlur extends ToMix { | ||
/** | ||
* Mix-in class to add an handler for losing focus. | ||
* @param {HTMLElement} element The element working as this component. | ||
* @param {Object} [options] The component options. | ||
*/ | ||
constructor(element, options) { | ||
super(element, options); | ||
const hasFocusin = 'onfocusin' in window; | ||
const focusinEventName = hasFocusin ? 'focusin' : 'focus'; | ||
this.hFocusIn = on(this.element.ownerDocument, focusinEventName, (event) => { | ||
if (!this.element.contains(event.target)) { | ||
this.handleBlur(event); | ||
} | ||
}, !hasFocusin); | ||
} | ||
|
||
/** | ||
* The method called when this component loses focus. | ||
* @abstract | ||
*/ | ||
handleBlur() { | ||
throw new Error('Components inheriting TrackBlur mix-in must implement handleBlur() method.'); | ||
} | ||
|
||
release() { | ||
if (this.hFocusIn) { | ||
this.hFocusIn = this.hFocusIn.release(); | ||
} | ||
super.release(); | ||
} | ||
} | ||
|
||
return TrackBlur; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.There was a problem hiding this comment.
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.There was a problem hiding this comment.
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 ifselectorPrimaryFocus
isn't found try and find the firsta
,button
,input
,select
, or[tabindex]
. That won't cover all focusable elements but it will cover the vast majority of use cases.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@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.There was a problem hiding this comment.
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
:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cool! looks good!