Skip to content

Commit e780e21

Browse files
authored
Merge pull request #1221 from mfranzke/improvement-nav-closing-nav-on-tapping-outside-on-mobile
improvement(nav): closing nav on tapping outside on mobile #1169
2 parents d6e8356 + 84e951b commit e780e21

File tree

2 files changed

+74
-60
lines changed

2 files changed

+74
-60
lines changed

packages/docs/src/js/primary-nav.js

Lines changed: 62 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -9,75 +9,77 @@
99
* 4) If the nav dropdown trigger parent does not have an active class, add it.
1010
*/
1111
(function() {
12-
var navDropdownListItem = document.querySelector('.js-nav-dropdown');
13-
var navLink = document.querySelectorAll('.js-nav-dropdown-trigger'); /* 1 */
12+
var navDropdownListItem = document.querySelector('.js-nav-dropdown');
13+
var navLink = document.querySelectorAll('.js-nav-dropdown-trigger'); /* 1 */
1414

15-
for (i = 0; i < navLink.length; i++) {
16-
/* 1 */
15+
for (i = 0; i < navLink.length; i++) {
16+
/* 1 */
1717

18-
navLink[i].addEventListener('click', function(event) {
19-
/* 2 */
20-
event.preventDefault();
21-
var navLinkParent = this.parentNode; /* 2 */
18+
navLink[i].addEventListener('click', function(event) {
19+
/* 2 */
20+
event.preventDefault();
21+
var navLinkParent = this.parentNode; /* 2 */
2222

23-
if (navLinkParent.classList.contains('is-active')) {
24-
/* 3 */
25-
navLinkParent.classList.remove('is-active');
26-
27-
this.setAttribute("aria-expanded", "false");
28-
} else {
29-
/* 4 */
30-
navLinkParent.classList.add('is-active');
31-
32-
this.setAttribute("aria-expanded", "true");
33-
}
34-
35-
});
36-
}
23+
if (navLinkParent.classList.contains('is-active')) {
24+
/* 3 */
25+
navLinkParent.classList.remove('is-active');
3726

38-
/**
39-
* Expose docs dropdown if on a docs page
40-
*/
41-
if (window.location.href.indexOf('docs') > -1) {
42-
navDropdownListItem.classList.add('is-active');
43-
}
27+
this.setAttribute('aria-expanded', 'false');
28+
} else {
29+
/* 4 */
30+
navLinkParent.classList.add('is-active');
4431

45-
var pathName = location.pathname;
32+
this.setAttribute('aria-expanded', 'true');
33+
}
34+
});
35+
}
4636

47-
var navLinks = document.querySelectorAll('.c-tree-nav a');
37+
/**
38+
* Expose docs dropdown if on a docs page
39+
*/
40+
if (window.location.href.indexOf('docs') > -1) {
41+
navDropdownListItem.classList.add('is-active');
42+
}
4843

49-
for (i = 0; i < navLinks.length; i++) {
50-
var subnavLink = navLinks[i].getAttribute('href');
51-
if (subnavLink == pathName) {
52-
navLinks[i].classList.add('is-active');
53-
}
54-
}
44+
var pathName = location.pathname;
5545

56-
/**
57-
* Toggles active class on the primary nav panel
58-
* 1) Select all nav triggers and cycle through them
59-
* 2) On click, find the nav panel within the header
60-
* 3) If the navPanel already has active class, remove it on click.
61-
* 4) If the navPanel does not have an active class, add it on click.
62-
*/
63-
var navToggle = document.querySelectorAll('.js-nav-trigger'); /* 1 */
46+
var navLinks = document.querySelectorAll('.c-tree-nav a');
6447

65-
for (i = 0; i < navToggle.length; i++) {
66-
/* 1 */
48+
for (i = 0; i < navLinks.length; i++) {
49+
var subnavLink = navLinks[i].getAttribute('href');
50+
if (subnavLink == pathName) {
51+
navLinks[i].classList.add('is-active');
52+
}
53+
}
6754

68-
navToggle[i].addEventListener('click', function(event) {
69-
/* 2 */
70-
event.preventDefault();
71-
var navToggleParent = this.parentNode; /* 2 */
72-
var navPanel = navToggleParent.querySelector('.js-nav-panel'); /* 2 */
55+
/**
56+
* Toggles active class on the primary nav panel
57+
* 1) Select all nav triggers and cycle through them
58+
* 2) On click, find the nav panel within the header
59+
* 3) If the navPanel already has active class, remove it on click, as well as the aria-expanded attributes value.
60+
* 4) If the navPanel does not have an active class, add it on click, as well as the aria-expanded attributes value.
61+
*/
62+
var navToggle = document.querySelectorAll('.js-nav-trigger'); /* 1 */
7363

74-
if (navPanel.classList.contains('is-active')) {
75-
/* 3 */
76-
navPanel.classList.remove('is-active');
77-
} else {
78-
/* 4 */
79-
navPanel.classList.add('is-active');
80-
}
81-
});
82-
}
64+
for (i = 0; i < navToggle.length; i++) {
65+
/* 1 */
66+
67+
navToggle[i].addEventListener('click', function(event) {
68+
/* 2 */
69+
event.preventDefault();
70+
var navToggleElement = this;
71+
var navToggleParent = navToggleElement.parentNode; /* 2 */
72+
var navPanel = navToggleParent.querySelector('.js-nav-panel'); /* 2 */
73+
74+
if (navPanel.classList.contains('is-active')) {
75+
/* 3 */
76+
navPanel.classList.remove('is-active');
77+
navToggleElement.setAttribute('aria-expanded', 'false');
78+
} else {
79+
/* 4 */
80+
navPanel.classList.add('is-active');
81+
navToggleElement.setAttribute('aria-expanded', 'true');
82+
}
83+
});
84+
}
8385
})();

packages/docs/src/scss/components/_header.scss

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,18 @@
4747
.c-header__nav-btn {
4848
margin-left: auto;
4949

50+
// Pseudo / breakout element that enables clicking/tabbing outside of the menu to close it
51+
&[aria-expanded="true"]::after {
52+
position: fixed;
53+
top: 0;
54+
left: 0;
55+
56+
content: "";
57+
58+
width: 100vw;
59+
height: 100vh;
60+
}
61+
5062
@media all and (min-width: $bp-large) {
5163
display: none;
5264
}

0 commit comments

Comments
 (0)