Skip to content
This repository was archived by the owner on Aug 30, 2018. It is now read-only.

Commit 1437bad

Browse files
committed
Merge pull request #144 from Shopify/updated-accessible-nav
Cleaned up accessible nav JS
2 parents b24c835 + 36f60eb commit 1437bad

File tree

3 files changed

+53
-17
lines changed

3 files changed

+53
-17
lines changed

assets/shop.js.liquid

+51-15
Original file line numberDiff line numberDiff line change
@@ -26,42 +26,78 @@ timber.accessibleNav = function () {
2626
allLinks = nav.find('a'),
2727
topLevel = nav.children('li').find('a'),
2828
parents = nav.find('.site-nav--has-dropdown'),
29-
subMenus = nav.find('.site-nav--dropdown');
29+
subMenus = nav.find('.site-nav--dropdown'),
30+
subMenuLinks = subMenus.find('a'),
31+
activeClass = 'nav-hover',
32+
focusClass = 'nav-focus';
3033

3134
// Mouseenter
32-
parents.on('mouseenter', function() {
33-
$(this).addClass('nav-hover');
35+
parents.on('mouseenter touchstart', function(evt) {
36+
var el = $(this);
37+
38+
if (!el.hasClass(activeClass)) {
39+
evt.preventDefault();
40+
}
41+
42+
showDropdown(el);
3443
});
3544

3645
// Mouseout
37-
parents.on('mouseout', function() {
38-
$(this).removeClass('nav-hover');
46+
parents.on('mouseleave', function() {
47+
hideDropdown($(this));
3948
});
4049

41-
// Focus
42-
allLinks.focus(function(e) {
50+
subMenuLinks.on('touchstart', function(evt) {
51+
// Prevent touchstart on body from firing instead of link
52+
evt.stopImmediatePropagation();
53+
});
54+
55+
allLinks.focus(function() {
4356
handleFocus($(this));
4457
});
4558

46-
// Blur
47-
allLinks.blur(function(e) {
48-
topLevel.removeClass('nav-focus');
59+
allLinks.blur(function() {
60+
removeFocus(topLevel);
4961
});
5062

5163
// accessibleNav private methods
5264
function handleFocus (el) {
5365
var subMenu = el.next('ul');
54-
hasSubMenu = subMenu.hasClass('site-nav--dropdown') ? true : false,
55-
isSubItem = $('.site-nav--dropdown').has(el).length;
66+
hasSubMenu = subMenu.hasClass('sub-nav') ? true : false,
67+
isSubItem = $('.site-nav--dropdown').has(el).length,
68+
newFocus = null;
5669

5770
// Add focus class for top level items, or keep menu shown
5871
if ( !isSubItem ) {
59-
topLevel.removeClass('nav-focus');
60-
el.addClass('nav-focus');
72+
removeFocus(topLevel);
73+
addFocus(el);
6174
} else {
62-
el.closest('.site-nav--has-dropdown').find('a').addClass('nav-focus');
75+
newFocus = el.closest('.site-nav--has-dropdown').find('a');
76+
addFocus(newFocus);
6377
}
78+
}
79+
80+
function showDropdown (el) {
81+
el.addClass(activeClass);
82+
83+
setTimeout(function() {
84+
timber.cache.body.on('touchstart', function() {
85+
hideDropdown(el);
86+
});
87+
}, 250);
88+
}
89+
90+
function hideDropdown (el) {
91+
el.removeClass(activeClass);
92+
timber.cache.body.off('touchstart');
93+
}
94+
95+
function addFocus (el) {
96+
el.addClass(focusClass);
97+
}
6498

99+
function removeFocus (el) {
100+
el.removeClass(focusClass);
65101
}
66102
}
67103

assets/timber.scss.liquid

+1-1
Original file line numberDiff line numberDiff line change
@@ -1704,7 +1704,7 @@ label.error {
17041704
left: 0;
17051705
z-index: 5;
17061706

1707-
.site-nav--has-dropdown:hover &,
1707+
.supports-no-touch .site-nav--has-dropdown:hover &,
17081708
.site-nav--has-dropdown.nav-hover &,
17091709
.nav-focus + & {
17101710
display: block;

config/settings_data.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,4 @@
3939
"logo_use_image": false
4040
}
4141
}
42-
}
42+
}

0 commit comments

Comments
 (0)