Skip to content

Commit ccff8cb

Browse files
authored
Merge pull request #417 from torchbox/feature/sub-menu-behaviour-tweaks
More reliably close the menu when focus moves away from the last item
2 parents 59baba4 + 829b4f5 commit ccff8cb

File tree

2 files changed

+5
-8
lines changed

2 files changed

+5
-8
lines changed

tbx/project_styleguide/templates/patterns/navigation/components/includes/subnav-child-menu.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
</li>
1414
{% endfor %}
1515
{% if child.value.section_link %}
16-
<li class="sub-nav-desktop__item" {% if forloop.last %}data-last-menu-item-desktop{% endif %}>
16+
<li class="sub-nav-desktop__item">
1717
<a href="{{ child.value.section_link.url }}" class="sub-nav-desktop__link sub-nav-desktop__link--no-description">
1818
<span class="sub-nav-desktop__link-text">{% firstof child.value.section_link_text child.value.section_link %}</span><span class="sub-nav-desktop__tail">&nbsp;{% include "patterns/atoms/icons/icon.html" with name="arrow" classname="sub-nav-desktop__arrow" %}</span>
1919
</a>

tbx/static_src/javascript/components/desktop-sub-menu.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,8 @@ class DesktopSubMenu {
1515
'[data-desktop-menu] [data-has-subnav]',
1616
);
1717
this.activeClass = 'active';
18-
this.lastMenuItems = document.querySelectorAll(
19-
'[data-last-menu-item-desktop]',
20-
);
18+
this.lastMenuItems = this.toggleNode.querySelectorAll('a');
19+
this.lastMenuItem = this.lastMenuItems[this.lastMenuItems.length - 1];
2120
this.bindEventListeners();
2221
}
2322

@@ -64,10 +63,8 @@ class DesktopSubMenu {
6463
});
6564

6665
// Close the desktop menu when the focus moves away from the last item
67-
this.lastMenuItems.forEach((item) => {
68-
item.addEventListener('focusout', () => {
69-
this.close();
70-
});
66+
this.lastMenuItem.addEventListener('focusout', () => {
67+
this.close();
7168
});
7269
}
7370
}

0 commit comments

Comments
 (0)