Skip to content

Commit

Permalink
Navigation menubar: Fix scroll-to-top problem for issue 1307 (pull #1308
Browse files Browse the repository at this point in the history
)

Issue #1307 reported 2 problems with the navigation menubar example.
This commit resolves the scroll to top issue by
preventing menuitem anchor elements from jumping to href="#" (i.e. top of page in Chrome) if they have a menu/submenu.

Co-authored-by: Matt King <[email protected]>
  • Loading branch information
carmacleod and mcking65 authored Feb 5, 2020
1 parent 44b1942 commit 4667318
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
9 changes: 9 additions & 0 deletions examples/menubar/menubar-1/js/MenubarItemLinks.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ MenubarItem.prototype.init = function () {
this.domNode.tabIndex = -1;

this.domNode.addEventListener('keydown', this.handleKeydown.bind(this));
this.domNode.addEventListener('click', this.handleClick.bind(this));
this.domNode.addEventListener('focus', this.handleFocus.bind(this));
this.domNode.addEventListener('blur', this.handleBlur.bind(this));
this.domNode.addEventListener('mouseover', this.handleMouseover.bind(this));
Expand Down Expand Up @@ -122,6 +123,14 @@ MenubarItem.prototype.handleKeydown = function (event) {
}
};

MenubarItem.prototype.handleClick = function (event) {
if (this.popupMenu) {
// for menuitem with menu, prevent default anchor behavior on click
// (which jumps to top of page for href="#" in some browsers)
event.preventDefault();
}
};

MenubarItem.prototype.setExpanded = function (value) {
if (value) {
this.domNode.setAttribute('aria-expanded', 'true');
Expand Down
5 changes: 5 additions & 0 deletions examples/menubar/menubar-1/js/PopupMenuItemLinks.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,11 @@ MenuItem.prototype.setExpanded = function (value) {
MenuItem.prototype.handleClick = function (event) {
this.menu.setFocusToController();
this.menu.close(true);
if (this.popupMenu) {
// for menuitem with menu, prevent default anchor behavior on click
// (which jumps to top of page for href="#" in some browsers)
event.preventDefault();
}
};

MenuItem.prototype.handleFocus = function (event) {
Expand Down

0 comments on commit 4667318

Please sign in to comment.