Skip to content

Commit 9ccf47f

Browse files
author
Maximilian Franzke
committed
improvement(nav): closing nav on tapping outside on mobile #1169
1 parent de9b6fc commit 9ccf47f

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@
5656
* Toggles active class on the primary nav panel
5757
* 1) Select all nav triggers and cycle through them
5858
* 2) On click, find the nav panel within the header
59-
* 3) If the navPanel already has active class, remove it on click.
60-
* 4) If the navPanel does not have an active class, add it on click.
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.
6161
*/
6262
var navToggle = document.querySelectorAll('.js-nav-trigger'); /* 1 */
6363

@@ -67,15 +67,18 @@
6767
navToggle[i].addEventListener('click', function(event) {
6868
/* 2 */
6969
event.preventDefault();
70-
var navToggleParent = this.parentNode; /* 2 */
70+
var navToggleElement = this;
71+
var navToggleParent = navToggleElement.parentNode; /* 2 */
7172
var navPanel = navToggleParent.querySelector('.js-nav-panel'); /* 2 */
7273

7374
if (navPanel.classList.contains('is-active')) {
7475
/* 3 */
7576
navPanel.classList.remove('is-active');
77+
navToggleElement.setAttribute('aria-expanded', 'false');
7678
} else {
7779
/* 4 */
7880
navPanel.classList.add('is-active');
81+
navToggleElement.setAttribute('aria-expanded', 'true');
7982
}
8083
});
8184
}

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)