HTML preprocessors can make writing HTML more powerful or convenient. For instance, Markdown is designed to be easier to write and read for text documents and you could write a loop in Pug.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. So you don't have access to higher-up elements like the <html>
tag. If you want to add classes there that can affect the whole document, this is the place to do it.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. If you need things in the <head>
of the document, put that code here.
The resource you are linking to is using the 'http' protocol, which may not work when the browser is using https.
CSS preprocessors help make authoring CSS easier. All of them offer things like variables and mixins to provide convenient abstractions.
It's a common practice to apply CSS to a page that styles elements such that they are consistent across all browsers. We offer two of the most popular choices: normalize.css and a reset. Or, choose Neither and nothing will be applied.
To get the best cross-browser support, it is a common practice to apply vendor prefixes to CSS properties and values that require them to work. For instance -webkit-
or -moz-
.
We offer two popular choices: Autoprefixer (which processes your CSS server-side) and -prefix-free (which applies prefixes via a script, client-side).
Any URLs added here will be added as <link>
s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.
You can apply CSS to your Pen from any stylesheet on the web. Just put a URL to it here and we'll apply it, in the order you have them, before the CSS in the Pen itself.
You can also link to another Pen here (use the .css
URL Extension) and we'll pull the CSS from that Pen and include it. If it's using a matching preprocessor, use the appropriate URL Extension and we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
JavaScript preprocessors can help make authoring JavaScript easier and more convenient.
Babel includes JSX processing.
Any URL's added here will be added as <script>
s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.
If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
Search for and use JavaScript packages from npm here. By selecting a package, an import
statement will be added to the top of the JavaScript editor for this package.
Using packages here is powered by esm.sh, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ESM usage.
All packages are different, so refer to their docs for how they work.
If you're using React / ReactDOM, make sure to turn on Babel for the JSX processing.
If active, Pens will autosave every 30 seconds after being saved once.
If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.
If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.
Visit your global Editor Settings.
<div class="page">
<!-- NOTE: In an actual website, the header element would be a top level element, i.e., it would have the body as its scope.
If the scope of the header element were body, browsers would automatically treat the header as an ARIA banner,
so the header would not need the role="banner". -->
<header role="banner">
<div class="title">Mythical University</div>
<div class="tagline">Using a Menubar for navigation links</div>
</header>
<nav aria-label="Mythical University">
<ul class="menubar-navigation" role="menubar" aria-label="Mythical University">
<li role="none">
<a role="menuitem" href="#home">
Home
</a>
</li>
<li role="none">
<a role="menuitem" aria-haspopup="true" aria-expanded="false" href="#about">
About
<svg xmlns="http://www.w3.org/2000/svg" class="down" width="12" height="9" viewBox="0 0 12 9">
<polygon points="1 0, 11 0, 6 8"></polygon>
</svg>
</a>
<ul role="menu" aria-label="About">
<li role="none">
<a role="menuitem" href="#overview">Overview</a>
</li>
<li role="none">
<a role="menuitem" href="#administration">Administration</a>
</li>
<li role="none">
<a role="menuitem" aria-haspopup="true" aria-expanded="false" href="#facts">
Facts
<svg xmlns="http://www.w3.org/2000/svg" class="right" width="9" height="12" viewBox="0 0 9 12">
<polygon points="0 1, 0 11, 8 6"></polygon>
</svg>
</a>
<ul role="menu" aria-label="Facts">
<li role="none">
<a role="menuitem" href="#history">History</a>
</li>
<li role="none">
<a role="menuitem" href="#current-statistics">Current Statistics</a>
</li>
<li role="none">
<a role="menuitem" href="#awards">Awards</a>
</li>
</ul>
</li>
<li role="none">
<a role="menuitem" aria-haspopup="true" aria-expanded="false" href="#campus-tours">
Campus Tours
<svg xmlns="http://www.w3.org/2000/svg" class="right" width="9" height="12" viewBox="0 0 9 12">
<polygon points="0 1, 0 11, 8 6"></polygon>
</svg>
</a>
<ul role="menu" aria-label="Campus Tours">
<li role="none">
<a role="menuitem" href="#for-prospective-students">For prospective students</a>
</li>
<li role="none">
<a role="menuitem" href="#for-alumni">For alumni</a>
</li>
<li role="none">
<a role="menuitem" href="#for-visitors">For visitors</a>
</li>
</ul>
</li>
</ul>
</li>
<li role="none">
<a role="menuitem" aria-haspopup="true" aria-expanded="false" href="#admissions">
Admissions
<svg xmlns="http://www.w3.org/2000/svg" class="down" width="12" height="9" viewBox="0 0 12 9">
<polygon points="1 0, 11 0, 6 8"></polygon>
</svg>
</a>
<ul role="menu" aria-label="Admissions">
<li role="none">
<a role="menuitem" href="#apply">Apply</a>
</li>
<li role="none">
<a role="menuitem" aria-haspopup="true" aria-expanded="false" href="#tuition">
Tuition
<svg xmlns="http://www.w3.org/2000/svg" class="right" width="9" height="12" viewBox="0 0 9 12">
<polygon points="0 1, 0 11, 8 6"></polygon>
</svg>
</a>
<ul role="menu" aria-label="Tuition">
<li role="none">
<a role="menuitem" href="#undergraduate">Undergraduate</a>
</li>
<li role="none">
<a role="menuitem" href="#graduate">Graduate</a>
</li>
<li role="none">
<a role="menuitem" href="#professional-schools">Professional Schools</a>
</li>
</ul>
</li>
<li role="none">
<a role="menuitem" href="#sign-up">Sign Up</a>
</li>
<li role="separator"></li>
<li role="none">
<a role="menuitem" href="#visit">Visit</a>
</li>
<li role="none">
<a role="menuitem" href="#photo-tour">Photo Tour</a>
</li>
<li role="none">
<a role="menuitem" href="#connect">Connect</a>
</li>
</ul>
</li>
<li role="none">
<a role="menuitem" aria-haspopup="true" aria-expanded="false" href="#academics">
Academics
<svg xmlns="http://www.w3.org/2000/svg" class="down" width="12" height="9" viewBox="0 0 12 9">
<polygon points="1 0, 11 0, 6 8"></polygon>
</svg>
</a>
<ul role="menu" id="sub-menu-academics" aria-label="Academics">
<li role="none">
<a role="menuitem" href="#colleges-and-schools">Colleges & Schools</a>
</li>
<li role="none">
<a role="menuitem" href="#programs-of-study">Programs of Study</a>
</li>
<li role="none">
<a role="menuitem" href="#honors-programs">Honors Programs</a>
</li>
<li role="none">
<a role="menuitem" href="#online-courses">Online Courses</a>
</li>
<li role="separator"></li>
<li role="none">
<a role="menuitem" href="#course-explorer">Course Explorer</a>
</li>
<li role="none">
<a role="menuitem" href="#register-for-class">Register for Class</a>
</li>
<li role="none">
<a role="menuitem" href="#academic-calendar">Academic Calendar</a>
</li>
<li role="none">
<a role="menuitem" href="#tanscripts">Transcripts</a>
</li>
</ul>
</li>
</ul>
</nav>
<!-- NOTE: The following SECTION element would be a MAIN element
in an actual website -->
<div class="main">
<section aria-labelledby="id-page-title">
<h1 id="id-page-title" class="page_title">Mythical University</h1>
<div class="content">
<p></p>
</div>
</section>
</div>
<!-- NOTE: In an actual website, the footer element would be a top level element, i.e., it would have the body as its scope.
If the scope of the footer element were body, browsers would automatically treat the footer as an ARIA contentinfo landmark,
so the footer would not need the role="contentinfo". -->
<footer role="contentinfo">
Mythical University footer information
</footer>
</div>
.page header {
border: #005a9c solid 2px;
background: #005a9c;
color: white;
text-align: center;
}
.page header .title {
font-size: 2.5em;
font-weight: bold;
font-family: serif;
}
.page header .tagline {
font-style: italic;
}
.page .main {
padding: 1em 5% 5em;
border-left: 2px solid #eee;
border-right: 2px solid #eee;
}
.page footer {
border: #005a9c solid 2px;
background: #005a9c;
font-family: serif;
color: white;
font-style: italic;
padding-left: 1em;
}
.page nav {
margin: 0;
padding: 0;
border: 2px solid #eee;
}
.menubar-navigation {
margin: 0;
padding: 2px;
font-size: 110%;
list-style: none;
background-color: #eee;
border: #eee solid 1px;
}
.menubar-navigation li {
margin: 0;
padding: 0;
border: 0 solid black;
list-style: none;
}
.menubar-navigation > li {
display: inline-block;
position: relative;
}
.menubar-navigation > li li {
display: block;
}
.menubar-navigation > li > [role="menuitem"] {
display: inline-block;
margin: 2px;
padding: 4px;
padding-bottom: 8px;
background-color: #eee;
border: 0 solid #eee;
color: black;
}
.menubar-navigation [role="separator"] {
padding-top: 3px;
background-image: url("data:image/svg+xml,%3C%3Fxml version='1.0' encoding='utf-8'%3F%3E%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'%3E%3Cline x1='0' y1='6' x2='12' y2='6' style='stroke:black;stroke-width:1' /%3E%3C/svg%3E%0A");
background-position: center;
background-repeat: repeat-x;
}
.menubar-navigation [role="menu"] [role="menuitem"],
.menubar-navigation [role="menu"] [role="separator"] {
display: block;
width: 12em;
margin: 2px;
padding: 4px;
padding-left: 8px;
background-color: #eee;
border: 0 solid #eee;
color: black;
}
.menubar-navigation [role="menuitem"] svg {
fill: currentColor;
stroke: currentColor;
}
.menubar-navigation [role="menuitem"] svg.down {
padding-left: 0.125em;
}
.menubar-navigation [role="menuitem"] svg.right {
position: absolute;
padding-top: 0.35em;
right: 0.75em;
}
.menubar-navigation [role="menuitem"][aria-expanded="true"] svg.down {
transform: rotate(180deg);
}
.menubar-navigation [role="menuitem"][aria-expanded="true"] svg.right {
transform: rotate(90deg) translate(5px, -5px);
}
.menubar-navigation [role="menu"] {
display: none;
position: absolute;
margin: 0;
padding: 0;
padding: 7px 4px;
border: 2px solid #034575;
background-color: #eee;
}
.menubar-navigation [role="group"] {
margin: 0;
padding: 0;
}
/* aria-current styling */
.menubar-navigation > li > [role="menuitem"][aria-current],
.menubar-navigation > li > [role="menuitem"].aria-current-path {
padding-bottom: 2px;
border-bottom: 4px solid #034575;
}
.menubar-navigation [role="menu"] [role="menuitem"].aria-current-path,
.menubar-navigation [role="menu"] [role="menuitem"][aria-current] {
padding-left: 4px;
border-left: 4px solid #034575;
}
/* focus styling */
.menubar-navigation.focus {
padding: 0;
border: #034575 solid 3px;
}
.menubar-navigation > li > [aria-expanded="true"],
.menubar-navigation > li > [role="menuitem"]:focus,
.menubar-navigation > li > [role="menuitem"]:hover {
outline: none;
background-color: #ccc;
}
.menubar-navigation > li > [role="menuitem"]:focus,
.menubar-navigation > li > [role="menuitem"]:hover {
padding: 2px;
padding-bottom: 4px;
border: 2px solid #034575;
}
.menubar-navigation [role="menu"] [aria-expanded="true"],
.menubar-navigation [role="menu"] [role="menuitem"]:focus,
.menubar-navigation [role="menu"] [role="menuitem"]:hover {
outline: none;
background-color: #ccc;
}
.menubar-navigation [role="menu"] [role="menuitem"]:focus,
.menubar-navigation [role="menu"] [role="menuitem"]:hover {
padding: 2px;
padding-left: 6px;
border: 2px solid #034575;
}
.menubar-navigation > li > [aria-expanded="true"].aria-current-path,
.menubar-navigation > li > [role="menuitem"].aria-current-path:focus,
.menubar-navigation > li > [role="menuitem"].aria-current-path:hover,
.menubar-navigation > li > [role="menuitem"][aria-current]:focus,
.menubar-navigation > li > [role="menuitem"][aria-current]:hover {
padding-bottom: 2px;
border-bottom: 4px solid #034575;
}
.menubar-navigation [role="menu"] [aria-expanded="true"].aria-current-path,
.menubar-navigation [role="menu"] [role="menuitem"].aria-current-path:focus,
.menubar-navigation [role="menu"] [role="menuitem"].aria-current-path:hover,
.menubar-navigation [role="menu"] [role="menuitem"][aria-current]:focus,
.menubar-navigation [role="menu"] [role="menuitem"][aria-current]:hover {
padding-left: 4px;
border-left: 4px solid #034575;
}
/*
* This content is licensed according to the W3C Software License at
* https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document
*
* File: menubar-navigation.js
*
* Desc: Creates a menubar of hierarchical set of links
*/
'use strict';
class NavigationContentGenerator {
constructor(siteURL, siteName) {
this.siteName = siteName;
this.siteURL = siteURL;
this.fillerTextSentences = [];
this.fillerTextSentences.push(
'The content on this page is associated with the <a href="$linkURL">$linkName</a> link for <a href="$siteURL">$siteName</a>.'
);
}
renderParagraph(linkURL, linkName) {
var content = '';
this.fillerTextSentences.forEach(
(s) =>
(content += s
.replace('$siteName', this.siteName)
.replace('$siteURL', this.siteURL)
.replace('$linkName', linkName)
.replace('$linkURL', linkURL))
);
return content;
}
}
class MenubarNavigation {
constructor(domNode) {
var linkURL, linkTitle;
this.domNode = domNode;
this.menuitems = [];
this.popups = [];
this.menuitemGroups = {};
this.menuOrientation = {};
this.isPopup = {};
this.isPopout = {};
this.openPopups = false;
this.firstChars = {}; // see Menubar init method
this.firstMenuitem = {}; // see Menubar init method
this.lastMenuitem = {}; // see Menubar init method
this.initMenu(domNode, 0);
domNode.addEventListener('focusin', this.handleMenubarFocusin.bind(this));
domNode.addEventListener('focusout', this.handleMenubarFocusout.bind(this));
window.addEventListener(
'mousedown',
this.handleBackgroundMousedown.bind(this),
true
);
domNode.querySelector('[role=menuitem]').tabIndex = 0;
// Initial content for page
if (location.href.split('#').length > 1) {
linkURL = location.href;
linkTitle = getLinkNameFromURL(location.href);
} else {
linkURL = location.href + '#home';
linkTitle = 'Home';
}
this.contentGenerator = new NavigationContentGenerator(
'#home',
'Mythical University'
);
this.updateContent(linkURL, linkTitle, false);
function getLinkNameFromURL(url) {
function capitalize(str) {
return str.charAt(0).toUpperCase() + str.slice(1);
}
var name = url.split('#')[1];
if (typeof name === 'string') {
name = name.split('-').map(capitalize).join(' ');
} else {
name = 'Home';
}
return name;
}
}
getParentMenuitem(menuitem) {
var node = menuitem.parentNode;
if (node) {
node = node.parentNode;
if (node) {
node = node.previousElementSibling;
if (node) {
if (node.getAttribute('role') === 'menuitem') {
return node;
}
}
}
}
return false;
}
updateContent(linkURL, linkName, moveFocus) {
var h1Node, paraNodes, pathNode;
if (typeof moveFocus !== 'boolean') {
moveFocus = true;
}
// Update content area
h1Node = document.querySelector('.page .main h1');
if (h1Node) {
h1Node.textContent = linkName;
h1Node.tabIndex = -1;
if (moveFocus) {
h1Node.focus();
}
}
paraNodes = document.querySelectorAll('.page .main p');
paraNodes.forEach(
(p) =>
(p.innerHTML = this.contentGenerator.renderParagraph(linkURL, linkName))
);
// Update aria-current
this.menuitems.forEach((item) => {
item.removeAttribute('aria-current');
item.classList.remove('aria-current-path');
item.title = '';
});
this.menuitems.forEach((item) => {
if (item.href === linkURL) {
item.setAttribute('aria-current', 'page');
pathNode = this.getParentMenuitem(item);
while (pathNode) {
pathNode.classList.add('aria-current-path');
pathNode.title = 'Contains current page link';
pathNode = this.getParentMenuitem(pathNode);
}
}
});
}
getMenuitems(domNode, depth) {
var nodes = [];
var initMenu = this.initMenu.bind(this);
var popups = this.popups;
function findMenuitems(node) {
var role, flag;
while (node) {
flag = true;
role = node.getAttribute('role');
if (role) {
role = role.trim().toLowerCase();
}
switch (role) {
case 'menu':
node.tabIndex = -1;
initMenu(node, depth + 1);
flag = false;
break;
case 'menuitem':
if (node.getAttribute('aria-haspopup') === 'true') {
popups.push(node);
}
nodes.push(node);
break;
default:
break;
}
if (
flag &&
node.firstElementChild &&
node.firstElementChild.tagName !== 'svg'
) {
findMenuitems(node.firstElementChild);
}
node = node.nextElementSibling;
}
}
findMenuitems(domNode.firstElementChild);
return nodes;
}
initMenu(menu, depth) {
var menuitems, menuitem, role;
var menuId = this.getMenuId(menu);
menuitems = this.getMenuitems(menu, depth);
this.menuOrientation[menuId] = this.getMenuOrientation(menu);
this.isPopup[menuId] = menu.getAttribute('role') === 'menu' && depth === 1;
this.isPopout[menuId] = menu.getAttribute('role') === 'menu' && depth > 1;
this.menuitemGroups[menuId] = [];
this.firstChars[menuId] = [];
this.firstMenuitem[menuId] = null;
this.lastMenuitem[menuId] = null;
for (var i = 0; i < menuitems.length; i++) {
menuitem = menuitems[i];
role = menuitem.getAttribute('role');
if (role.indexOf('menuitem') < 0) {
continue;
}
menuitem.tabIndex = -1;
this.menuitems.push(menuitem);
this.menuitemGroups[menuId].push(menuitem);
this.firstChars[menuId].push(
menuitem.textContent.trim().toLowerCase()[0]
);
menuitem.addEventListener('keydown', this.handleKeydown.bind(this));
menuitem.addEventListener('click', this.handleMenuitemClick.bind(this), {
capture: true,
});
menuitem.addEventListener(
'mouseover',
this.handleMenuitemMouseover.bind(this)
);
if (!this.firstMenuitem[menuId]) {
if (this.hasPopup(menuitem)) {
menuitem.tabIndex = 0;
}
this.firstMenuitem[menuId] = menuitem;
}
this.lastMenuitem[menuId] = menuitem;
}
}
setFocusToMenuitem(menuId, newMenuitem) {
this.closePopupAll(newMenuitem);
if (this.menuitemGroups[menuId]) {
this.menuitemGroups[menuId].forEach(function (item) {
if (item === newMenuitem) {
item.tabIndex = 0;
newMenuitem.focus();
} else {
item.tabIndex = -1;
}
});
}
}
setFocusToFirstMenuitem(menuId) {
this.setFocusToMenuitem(menuId, this.firstMenuitem[menuId]);
}
setFocusToLastMenuitem(menuId) {
this.setFocusToMenuitem(menuId, this.lastMenuitem[menuId]);
}
setFocusToPreviousMenuitem(menuId, currentMenuitem) {
var newMenuitem, index;
if (currentMenuitem === this.firstMenuitem[menuId]) {
newMenuitem = this.lastMenuitem[menuId];
} else {
index = this.menuitemGroups[menuId].indexOf(currentMenuitem);
newMenuitem = this.menuitemGroups[menuId][index - 1];
}
this.setFocusToMenuitem(menuId, newMenuitem);
return newMenuitem;
}
setFocusToNextMenuitem(menuId, currentMenuitem) {
var newMenuitem, index;
if (currentMenuitem === this.lastMenuitem[menuId]) {
newMenuitem = this.firstMenuitem[menuId];
} else {
index = this.menuitemGroups[menuId].indexOf(currentMenuitem);
newMenuitem = this.menuitemGroups[menuId][index + 1];
}
this.setFocusToMenuitem(menuId, newMenuitem);
return newMenuitem;
}
setFocusByFirstCharacter(menuId, currentMenuitem, char) {
var start, index;
char = char.toLowerCase();
// Get start index for search based on position of currentItem
start = this.menuitemGroups[menuId].indexOf(currentMenuitem) + 1;
if (start >= this.menuitemGroups[menuId].length) {
start = 0;
}
// Check remaining slots in the menu
index = this.getIndexFirstChars(menuId, start, char);
// If not found in remaining slots, check from beginning
if (index === -1) {
index = this.getIndexFirstChars(menuId, 0, char);
}
// If match was found...
if (index > -1) {
this.setFocusToMenuitem(menuId, this.menuitemGroups[menuId][index]);
}
}
// Utilities
getIndexFirstChars(menuId, startIndex, char) {
for (var i = startIndex; i < this.firstChars[menuId].length; i++) {
if (char === this.firstChars[menuId][i]) {
return i;
}
}
return -1;
}
isPrintableCharacter(str) {
return str.length === 1 && str.match(/\S/);
}
getIdFromAriaLabel(node) {
var id = node.getAttribute('aria-label');
if (id) {
id = id.trim().toLowerCase().replace(' ', '-').replace('/', '-');
}
return id;
}
getMenuOrientation(node) {
var orientation = node.getAttribute('aria-orientation');
if (!orientation) {
var role = node.getAttribute('role');
switch (role) {
case 'menubar':
orientation = 'horizontal';
break;
case 'menu':
orientation = 'vertical';
break;
default:
break;
}
}
return orientation;
}
getMenuId(node) {
var id = false;
var role = node.getAttribute('role');
while (node && role !== 'menu' && role !== 'menubar') {
node = node.parentNode;
if (node) {
role = node.getAttribute('role');
}
}
if (node) {
id = role + '-' + this.getIdFromAriaLabel(node);
}
return id;
}
getMenu(menuitem) {
var menu = menuitem;
var role = menuitem.getAttribute('role');
while (menu && role !== 'menu' && role !== 'menubar') {
menu = menu.parentNode;
if (menu) {
role = menu.getAttribute('role');
}
}
return menu;
}
// Popup menu methods
isAnyPopupOpen() {
for (var i = 0; i < this.popups.length; i++) {
if (this.popups[i].getAttribute('aria-expanded') === 'true') {
return true;
}
}
return false;
}
setMenubarDataExpanded(value) {
this.domNode.setAttribute('data-menubar-item-expanded', value);
}
isMenubarDataExpandedTrue() {
return this.domNode.getAttribute('data-menubar-item-expanded') === 'true';
}
openPopup(menuId, menuitem) {
// set aria-expanded attribute
var popupMenu = menuitem.nextElementSibling;
if (popupMenu) {
var rect = menuitem.getBoundingClientRect();
// Set CSS properties
if (this.isPopup[menuId]) {
popupMenu.parentNode.style.position = 'relative';
popupMenu.style.display = 'block';
popupMenu.style.position = 'absolute';
popupMenu.style.left = rect.width + 10 + 'px';
popupMenu.style.top = '0px';
popupMenu.style.zIndex = 100;
} else {
popupMenu.style.display = 'block';
popupMenu.style.position = 'absolute';
popupMenu.style.left = '0px';
popupMenu.style.top = rect.height + 8 + 'px';
popupMenu.style.zIndex = 100;
}
menuitem.setAttribute('aria-expanded', 'true');
this.setMenubarDataExpanded('true');
return this.getMenuId(popupMenu);
}
return false;
}
closePopout(menuitem) {
var menu,
menuId = this.getMenuId(menuitem),
cmi = menuitem;
while (this.isPopup[menuId] || this.isPopout[menuId]) {
menu = this.getMenu(cmi);
cmi = menu.previousElementSibling;
menuId = this.getMenuId(cmi);
menu.style.display = 'none';
}
cmi.focus();
return cmi;
}
closePopup(menuitem) {
var menu,
menuId = this.getMenuId(menuitem),
cmi = menuitem;
if (this.isMenubar(menuId)) {
if (this.isOpen(menuitem)) {
menuitem.setAttribute('aria-expanded', 'false');
menuitem.nextElementSibling.style.display = 'none';
}
} else {
menu = this.getMenu(menuitem);
cmi = menu.previousElementSibling;
cmi.setAttribute('aria-expanded', 'false');
cmi.focus();
menu.style.display = 'none';
}
return cmi;
}
doesNotContain(popup, menuitem) {
if (menuitem) {
return !popup.nextElementSibling.contains(menuitem);
}
return true;
}
closePopupAll(menuitem) {
if (typeof menuitem !== 'object') {
menuitem = false;
}
for (var i = 0; i < this.popups.length; i++) {
var popup = this.popups[i];
if (this.doesNotContain(popup, menuitem) && this.isOpen(popup)) {
var cmi = popup.nextElementSibling;
if (cmi) {
popup.setAttribute('aria-expanded', 'false');
cmi.style.display = 'none';
}
}
}
}
hasPopup(menuitem) {
return menuitem.getAttribute('aria-haspopup') === 'true';
}
isOpen(menuitem) {
return menuitem.getAttribute('aria-expanded') === 'true';
}
isMenubar(menuId) {
return !this.isPopup[menuId] && !this.isPopout[menuId];
}
isMenuHorizontal(menuitem) {
return this.menuOrientation[menuitem] === 'horizontal';
}
hasFocus() {
return this.domNode.classList.contains('focus');
}
// Menu event handlers
handleMenubarFocusin() {
// if the menubar or any of its menus has focus, add styling hook for hover
this.domNode.classList.add('focus');
}
handleMenubarFocusout() {
// remove styling hook for hover on menubar item
this.domNode.classList.remove('focus');
}
handleKeydown(event) {
var tgt = event.currentTarget,
key = event.key,
flag = false,
menuId = this.getMenuId(tgt),
id,
popupMenuId,
mi;
switch (key) {
case ' ':
case 'Enter':
if (this.hasPopup(tgt)) {
this.openPopups = true;
popupMenuId = this.openPopup(menuId, tgt);
this.setFocusToFirstMenuitem(popupMenuId);
} else {
if (tgt.href !== '#') {
this.closePopupAll();
this.updateContent(tgt.href, tgt.textContent.trim());
this.setMenubarDataExpanded('false');
}
}
flag = true;
break;
case 'Esc':
case 'Escape':
this.openPopups = false;
mi = this.closePopup(tgt);
id = this.getMenuId(mi);
this.setMenubarDataExpanded('false');
flag = true;
break;
case 'Up':
case 'ArrowUp':
if (this.isMenuHorizontal(menuId)) {
if (this.hasPopup(tgt)) {
this.openPopups = true;
popupMenuId = this.openPopup(menuId, tgt);
this.setFocusToLastMenuitem(popupMenuId);
}
} else {
this.setFocusToPreviousMenuitem(menuId, tgt);
}
flag = true;
break;
case 'ArrowDown':
case 'Down':
if (this.isMenuHorizontal(menuId)) {
if (this.hasPopup(tgt)) {
this.openPopups = true;
popupMenuId = this.openPopup(menuId, tgt);
this.setFocusToFirstMenuitem(popupMenuId);
}
} else {
this.setFocusToNextMenuitem(menuId, tgt);
}
flag = true;
break;
case 'Left':
case 'ArrowLeft':
if (this.isMenuHorizontal(menuId)) {
mi = this.setFocusToPreviousMenuitem(menuId, tgt);
if (this.isAnyPopupOpen() || this.isMenubarDataExpandedTrue()) {
this.openPopup(menuId, mi);
}
} else {
if (this.isPopout[menuId]) {
mi = this.closePopup(tgt);
id = this.getMenuId(mi);
mi = this.setFocusToMenuitem(id, mi);
} else {
mi = this.closePopup(tgt);
id = this.getMenuId(mi);
mi = this.setFocusToPreviousMenuitem(id, mi);
this.openPopup(id, mi);
}
}
flag = true;
break;
case 'Right':
case 'ArrowRight':
if (this.isMenuHorizontal(menuId)) {
mi = this.setFocusToNextMenuitem(menuId, tgt);
if (this.isAnyPopupOpen() || this.isMenubarDataExpandedTrue()) {
this.openPopup(menuId, mi);
}
} else {
if (this.hasPopup(tgt)) {
popupMenuId = this.openPopup(menuId, tgt);
this.setFocusToFirstMenuitem(popupMenuId);
} else {
mi = this.closePopout(tgt);
id = this.getMenuId(mi);
mi = this.setFocusToNextMenuitem(id, mi);
this.openPopup(id, mi);
}
}
flag = true;
break;
case 'Home':
case 'PageUp':
this.setFocusToFirstMenuitem(menuId, tgt);
flag = true;
break;
case 'End':
case 'PageDown':
this.setFocusToLastMenuitem(menuId, tgt);
flag = true;
break;
case 'Tab':
this.openPopups = false;
this.setMenubarDataExpanded('false');
this.closePopup(tgt);
break;
default:
if (this.isPrintableCharacter(key)) {
this.setFocusByFirstCharacter(menuId, tgt, key);
flag = true;
}
break;
}
if (flag) {
event.stopPropagation();
event.preventDefault();
}
}
handleMenuitemClick(event) {
var tgt = event.currentTarget;
var menuId = this.getMenuId(tgt);
if (this.hasPopup(tgt)) {
if (this.isOpen(tgt)) {
this.closePopup(tgt);
} else {
this.closePopupAll(tgt);
this.openPopup(menuId, tgt);
}
} else {
this.updateContent(tgt.href, tgt.textContent.trim());
this.closePopupAll();
}
event.stopPropagation();
event.preventDefault();
}
handleMenuitemMouseover(event) {
var tgt = event.currentTarget;
var menuId = this.getMenuId(tgt);
if (this.hasFocus()) {
this.setFocusToMenuitem(menuId, tgt);
}
if (this.isAnyPopupOpen() || this.hasFocus()) {
this.closePopupAll(tgt);
if (this.hasPopup(tgt)) {
this.openPopup(menuId, tgt);
}
}
}
handleBackgroundMousedown(event) {
if (!this.domNode.contains(event.target)) {
this.closePopupAll();
}
}
}
// Initialize menubar editor
window.addEventListener('load', function () {
var menubarNavs = document.querySelectorAll('.menubar-navigation');
for (var i = 0; i < menubarNavs.length; i++) {
new MenubarNavigation(menubarNavs[i]);
}
});
Also see: Tab Triggers