-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(floating-menu): add close-on-blur behavior
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.
- Loading branch information
Showing
7 changed files
with
198 additions
and
25 deletions.
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