Last active
June 3, 2018 22:11
-
-
Save Dzordzu/106293f57b459e6c42a69697fcb2dbac to your computer and use it in GitHub Desktop.
Accessible navbar (VanillaJS + BEM)
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
// Example of the Trigger usage | |
function toggle(trigger, target) { | |
if(target.classList.contains("navbar__dropdown--hidden")){ | |
target.classList.remove("navbar__dropdown--hidden"); | |
target.querySelector("a").focus(); | |
} | |
else { | |
target.classList.add("navbar__dropdown--hidden"); | |
} | |
} | |
function hide(target) { | |
target.classList.add("navbar__dropdown--hidden"); | |
} | |
var menuTrigger = new Trigger({ | |
trigger: document.querySelector(".navbar__trigger--menu"), | |
target: document.querySelector(".navbar__dropdown--menu"), | |
toggleFunc: toggle, | |
hideFunc: hide | |
}); | |
var langTrigger = new Trigger({ | |
trigger: document.querySelector(".navbar__trigger--languageApp"), | |
target: document.querySelector(".navbar__dropdown--languageApp"), | |
toggleFunc: toggle, | |
hideFunc: hide | |
}); |
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
<!-- Assuming you've implemented trigger.js and code.js --> | |
<!-- Currently there is no default styling (this is gist, isn't it?) --> | |
<nav class="navbar"> | |
<!-- Triggers. Use buttons for better accessibility --> | |
<div class="navbar__triggers navbar__triggers--hidden"> | |
<button class="navbar__trigger navbar__trigger--menu"> | |
<span aria-label="Open menu" aria-role="img" class="noMargin">☰</span> | |
</button> | |
<button class="navbar__trigger navbar__trigger--languageApp"> | |
<img class="navbar__lang noMargin" alt="Wybierz język / Choose language" src="src/media/images/Flag_of_Poland.png"/> | |
</button> | |
</div> | |
<!-- Dropdowns. Lists that should be hidden by default --> | |
<ul class="navbar__dropdown navbar__dropdown--menu"> | |
<li class="navbar__elem"> <a class="navbar__link" href="#1">Lorem ipsum</a></li> | |
<li class="navbar__elem"> <a class="navbar__link" href="#1">dolor</a></li> | |
<li class="navbar__elem"> <a class="navbar__link" href="#1">amet</a></li> | |
</ul> | |
<!-- No alt's here (for img). This approach guarantees no additional comment to the following language name --> | |
<ul class="navbar__dropdown navbar__dropdown--languageApp"> | |
<li class="navbar__elem"> <a class="navbar__link" href="?lang=pl"> <img class="navbar__lang" src="src/media/images/Flag_of_Poland.svg"/> Polish </a> </li> | |
<li class="navbar__elem"> <a class="navbar__link" href="?lang=uk"> <img class="navbar__lang" src="src/media/images/Flag_of_Ukraine.png"/> Ukrainian </a> </li> | |
<li class="navbar__elem"> <a class="navbar__link" href="?lang=en"> English </a> </li> | |
</ul> | |
</nav> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment