Last active
August 14, 2017 16:57
-
-
Save kgcreative/88aad8c13d1bad38bab07b00cc26f2ea to your computer and use it in GitHub Desktop.
WIP - Accessible, responsive navigation with mobile toggle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
body::before { | |
display: none; | |
content: "unknown"; | |
@media screen { | |
content: "screen"; | |
} | |
@media print { | |
content: "print"; | |
} | |
@include grid-media($media-xs-only) { | |
content: "xs"; | |
} | |
@include grid-media($media-sm-only) { | |
content: "sm"; | |
} | |
@include grid-media($media-md-only) { | |
content: "md"; | |
} | |
@include grid-media($media-lg-only) { | |
content: "lg"; | |
} | |
@include grid-media($media-xl) { | |
content: "xl"; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
exports.breakpointDetect = function() { | |
var body = document.querySelector('body') | |
, result = getComputedStyle(body, ':before').content; | |
// returns contents without quotes | |
return result.replace(/\"/g, ""); | |
} | |
exports.isMobileMenu = function() { | |
var currentBreakpoint = exports.breakpointDetect() | |
return ( currentBreakpoint === 'xs' || currentBreakpoint === 'sm' || | |
currentBreakpoint === 'md' ) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var feature = require('./features') | |
; | |
function resizeEnd() { | |
var isMobileMenu = feature.isMobileMenu() | |
, primaryNav = document.querySelector('#primary-nav > .nav-menu') | |
, primaryNavContainer = document.getElementById('primary-nav') | |
, menuToggle = document.getElementById('menu-toggle') | |
, main = document.querySelector('main, #main') | |
, closeMainNavigation | |
; | |
//synchronous actions | |
if (isMobileMenu === true) { | |
$(primaryNav).attr('role', 'menu'); | |
$(primaryNav).find('> li > a').each(function(e) { | |
$(this).attr('role', 'menuitem'); | |
}); | |
if ($(primaryNavContainer).hasClass('open')) { | |
$(menuToggle).attr('aria-expanded', true); | |
$(primaryNavContainer).attr('aria-expanded', true); | |
$(main).addClass('nav-open'); | |
} else { | |
$(menuToggle).attr('aria-expanded', false); | |
$(primaryNavContainer).attr('aria-expanded', false); | |
} | |
} else { | |
$(primaryNav).find('> li > a').each(function(e) { | |
$(this).removeAttr('role'); | |
}); | |
$(primaryNav).attr('role', 'menubar'); | |
if ($(primaryNavContainer).hasClass('open')) { | |
$(menuToggle).removeAttr('aria-expanded'); | |
$(primaryNavContainer).removeAttr('aria-expanded'); | |
$(main).removeClass('nav-open'); | |
} else { | |
$(menuToggle).removeAttr('aria-expanded'); | |
$(primaryNavContainer).removeAttr('aria-expanded'); | |
} | |
} | |
console.log('%c ✔ resizeEnd', 'color: green'); | |
//asynchronous triggers | |
}; | |
// note resize events and trigger resizeEnd event when resizing stops | |
window.onresize = function() { | |
if(this.resizeTo) clearTimeout(this.resizeTo); | |
this.resizeTo = setTimeout(function() { | |
resizeEnd(); | |
}, 250); | |
}; | |
resizeEnd(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment