Skip to content

Commit

Permalink
A11y: add keyboard navigation demo for manager main nav modxcms#16612
Browse files Browse the repository at this point in the history
  • Loading branch information
jenswittmann authored and opengeek committed Sep 23, 2024
1 parent dd811eb commit 34909bb
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
13 changes: 13 additions & 0 deletions manager/assets/modext/core/modx.layout.js
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,7 @@ Ext.extend(MODx.Layout, Ext.Viewport, {
,initPopper: function() {
var el = this;
var buttons = document.getElementById('modx-navbar').getElementsByClassName('top');
var focusRestore = false;
var position = window.innerWidth <= 960 ? 'bottom' : 'right';
for (var i = 0; i < buttons.length; i++) {
var submenu = document.getElementById(buttons[i].id + '-submenu');
Expand Down Expand Up @@ -467,12 +468,21 @@ Ext.extend(MODx.Layout, Ext.Viewport, {
});
buttons[i].onclick = function(e) {
e.stopPropagation();
focusRestore = this;
el.showMenu(this);
};
}
window.addEventListener('click', function() {
el.hideMenu();
});
document.addEventListener('keydown', function(e) {
if (e.key == 'Escape') {
el.hideMenu();
setTimeout(() => {
focusRestore?.querySelectorAll('a')[0].focus();
});
}
});
if (window.innerWidth > 960) {
this.initSubPopper();
}
Expand All @@ -485,6 +495,9 @@ Ext.extend(MODx.Layout, Ext.Viewport, {
} else {
this.hideMenu();
submenu.classList.add('active');
setTimeout(() => {
submenu.querySelectorAll('a')[0].focus();
}, 250);
}
this.hideSubMenu();
}
Expand Down
2 changes: 1 addition & 1 deletion manager/controllers/default/header.php
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ public function processSubMenus(&$output, array $menus = [])
$attributes = ' href="?a='.$menu['action'].$menu['params'].'"';
}
if (!empty($menu['handler'])) {
$attributes .= ' onclick="{literal} '.str_replace('"','\'',$menu['handler']).'{/literal} "';
$attributes .= ' tabindex="0" onclick="{literal} '.str_replace('"','\'',$menu['handler']).'{/literal} "';
}
$menu['icon'] = $menu['icon'] ?? '';
$smTpl .= '<a'.$attributes.'>'.$menu['text'].$menu['icon'].$description.'</a>'."\n";
Expand Down

0 comments on commit 34909bb

Please sign in to comment.