@@ -26,42 +26,78 @@ timber.accessibleNav = function () {
26
26
allLinks = nav.find('a'),
27
27
topLevel = nav.children('li').find('a'),
28
28
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';
30
33
31
34
// 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);
34
43
});
35
44
36
45
// Mouseout
37
- parents.on('mouseout ', function() {
38
- $(this).removeClass('nav-hover' );
46
+ parents.on('mouseleave ', function() {
47
+ hideDropdown( $(this));
39
48
});
40
49
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() {
43
56
handleFocus($(this));
44
57
});
45
58
46
- // Blur
47
- allLinks.blur(function(e) {
48
- topLevel.removeClass('nav-focus');
59
+ allLinks.blur(function() {
60
+ removeFocus(topLevel);
49
61
});
50
62
51
63
// accessibleNav private methods
52
64
function handleFocus (el) {
53
65
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;
56
69
57
70
// Add focus class for top level items, or keep menu shown
58
71
if ( !isSubItem ) {
59
- topLevel.removeClass('nav-focus' );
60
- el.addClass('nav-focus' );
72
+ removeFocus(topLevel );
73
+ addFocus(el );
61
74
} 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);
63
77
}
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
+ }
64
98
99
+ function removeFocus (el) {
100
+ el.removeClass(focusClass);
65
101
}
66
102
}
67
103
0 commit comments