Skip to content

Commit 58f638e

Browse files
gjanblaszczykgkatsev
authored andcommitted
fix: vjs-lock-showing class gets removed from menu when no longer hovering on menu-button. (#5465)
Fixes #1690
1 parent aed337a commit 58f638e

2 files changed

Lines changed: 33 additions & 12 deletions

File tree

src/js/menu/menu-button.js

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,9 @@ class MenuButton extends Component {
5151
this.on(this.menuButton_, 'click', this.handleClick);
5252
this.on(this.menuButton_, 'focus', this.handleFocus);
5353
this.on(this.menuButton_, 'blur', this.handleBlur);
54-
54+
this.on(this.menuButton_, 'mouseenter', () => {
55+
this.menu.show();
56+
});
5557
this.on('keydown', this.handleSubmenuKeyPress);
5658
}
5759

@@ -221,14 +223,6 @@ class MenuButton extends Component {
221223
* @listens click
222224
*/
223225
handleClick(event) {
224-
// When you click the button it adds focus, which will show the menu.
225-
// So we'll remove focus when the mouse leaves the button. Focus is needed
226-
// for tab navigation.
227-
228-
this.one(this.menu.contentEl(), 'mouseleave', Fn.bind(this, function(e) {
229-
this.unpressButton();
230-
this.el_.blur();
231-
}));
232226
if (this.buttonPressed_) {
233227
this.unpressButton();
234228
} else {
@@ -299,8 +293,8 @@ class MenuButton extends Component {
299293
// Set focus back to the menu button's button
300294
this.menuButton_.el_.focus();
301295
}
302-
// Up (38) key or Down (40) key press the 'button'
303-
} else if (event.which === 38 || event.which === 40) {
296+
// Enter (13) or Up (38) key or Down (40) key press the 'button'
297+
} else if (event.which === 13 || event.which === 38 || event.which === 40) {
304298
if (!this.buttonPressed_) {
305299
this.pressButton();
306300
event.preventDefault();
@@ -339,6 +333,7 @@ class MenuButton extends Component {
339333
pressButton() {
340334
if (this.enabled_) {
341335
this.buttonPressed_ = true;
336+
this.menu.show();
342337
this.menu.lockShowing();
343338
this.menuButton_.el_.setAttribute('aria-expanded', 'true');
344339

@@ -360,6 +355,7 @@ class MenuButton extends Component {
360355
if (this.enabled_) {
361356
this.buttonPressed_ = false;
362357
this.menu.unlockShowing();
358+
this.menu.hide();
363359
this.menuButton_.el_.setAttribute('aria-expanded', 'false');
364360
}
365361
}

src/js/menu/menu.js

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* @file menu.js
33
*/
44
import Component from '../component.js';
5+
import document from 'global/document';
56
import * as Dom from '../utils/dom.js';
67
import * as Fn from '../utils/fn.js';
78
import * as Events from '../utils/events.js';
@@ -45,7 +46,8 @@ class Menu extends Component {
4546
*/
4647
addItem(component) {
4748
this.addChild(component);
48-
component.on('click', Fn.bind(this, function(event) {
49+
component.on('blur', Fn.bind(this, this.handleBlur));
50+
component.on(['tap', 'click'], Fn.bind(this, function(event) {
4951
// Unpress the associated MenuButton, and move focus back to it
5052
if (this.menuButton_) {
5153
this.menuButton_.unpressButton();
@@ -97,6 +99,29 @@ class Menu extends Component {
9799
super.dispose();
98100
}
99101

102+
/**
103+
* Called when a `MenuItem` loses focus.
104+
*
105+
* @param {EventTarget~Event} event
106+
* The `blur` event that caused this function to be called.
107+
*
108+
* @listens blur
109+
*/
110+
handleBlur(event) {
111+
const relatedTarget = event.relatedTarget || document.activeElement;
112+
113+
// Close menu popup when a user clicks outside the menu
114+
if (!this.children().some((element) => {
115+
return element.el() === relatedTarget;
116+
})) {
117+
const btn = this.menuButton_;
118+
119+
if (btn && btn.buttonPressed_ && relatedTarget !== btn.el().firstChild) {
120+
btn.unpressButton();
121+
}
122+
}
123+
}
124+
100125
/**
101126
* Handle a `keydown` event on this menu. This listener is added in the constructor.
102127
*

0 commit comments

Comments
 (0)