Skip to content

Commit

Permalink
fix(a11y): tab to first item in opened dropdown menu
Browse files Browse the repository at this point in the history
Also set focus back to the popup trigger when it's closed and the focus
was inside the popup
  • Loading branch information
jeabakker committed Oct 20, 2023
1 parent 86c0a22 commit 7a922f3
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
7 changes: 1 addition & 6 deletions views/default/elgg/menus/dropdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,7 @@ define(['jquery', 'elgg', 'elgg/popup'], function ($, elgg, popup) {
$trigger.addClass('elgg-menu-opened')
.removeClass('elgg-menu-closed');
$trigger.parent().addClass('elgg-state-selected');
});

$target.on('close', function () {
$trigger.addClass('elgg-menu-closed')
.removeClass('elgg-menu-opened');
$trigger.parent().removeClass('elgg-state-selected');
$target.find('a:first').focus();
});
}

Expand Down
19 changes: 19 additions & 0 deletions views/default/elgg/popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ define('elgg/popup', ['jquery', 'elgg', 'elgg/hooks', 'jquery-ui/position', 'jqu
*/
init: function () {
$(document).on('click', function (e) {
// close the popup when clicked outside the popup
if (e.isDefaultPrevented()) {
return;
}
Expand All @@ -28,6 +29,16 @@ define('elgg/popup', ['jquery', 'elgg', 'elgg/hooks', 'jquery-ui/position', 'jqu

popup.close();
});

$(document).on('keydown', function (e) {
// close the popup when the escape key is pressed
if (e.isDefaultPrevented() || e.key !== "Escape") {
return;
}

popup.close();
});

// Bind events only once
popup.init = function() {};
},
Expand Down Expand Up @@ -142,6 +153,14 @@ define('elgg/popup', ['jquery', 'elgg', 'elgg/hooks', 'jquery-ui/position', 'jqu
$trigger.addClass('elgg-state-active');

$target.trigger('open');

if ($trigger.is('a')) {
$target.on('close', function() {
if ($target.find(':focus').length) {
$trigger.focus();
}
});
}
},
/**
* Hides a set of $targets. If not defined, closes all visible popup modules.
Expand Down
4 changes: 4 additions & 0 deletions views/default/icon/user/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ define(['jquery'], function ($) {
$icon.data('hovermenu', $hovermenu);
}

$hovermenu.on('open', function() {
$hovermenu.find('a:first').focus();
});

require(['elgg/popup'], function(popup) {
if ($hovermenu.is(':visible')) {
// close hovermenu if arrow is clicked & menu already open
Expand Down

0 comments on commit 7a922f3

Please sign in to comment.