Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

a11y: focus modals and esc closes err modal #310

Merged
merged 12 commits into from
Dec 18, 2019
10 changes: 5 additions & 5 deletions editor.html
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
UPY_VERSION = "1.0.1";
</script>
<script id="files-template" type="x-tmpl-mustache">
<div tabindex="-1" role="dialog" aria-label="load/save modal" aria-modal="true" class="modal-div">
<div id="files-modal" tabindex="-1" role="dialog" aria-label="load/save modal" aria-modal="true" class="modal-div">
<h2 class="modal-title"><i class="fa fa-upload"></i> <strong>{{ load-title }}</strong></h2>
<div class="load-drag-target" id="load-drag-target">
<input type="file" style="display: none" name="load-form-file-upload" id="file-upload-input">
Expand Down Expand Up @@ -140,7 +140,7 @@ <h2 class="modal-title"><i class="fa fa-download"></i> <strong>{{ files-title }}
</div>
</script>
<script id="snippet-template" type="x-tmpl-mustache">
<div tabindex="-1" role="dialog" aria-label="snippets modal" aria-modal="true" class="modal-div">
<div id="snippet-modal" tabindex="-1" role="dialog" aria-label="snippets modal" aria-modal="true" class="modal-div">
<h2><i class="fa fa-cogs"></i> <strong>{{ title }}</strong></h2>
<p>{{ description }}</p>
<p>{{ instructions }}</p>
Expand Down Expand Up @@ -395,7 +395,7 @@ <h2><i class="fa fa-unlock-alt"></i> <strong>{{ title }}</strong></h2>
<div id="editor" class="flex3" tabindex="-1"></div>
</div>
<div id="flashing-overlay-container" class="modal-overlay-container">
<div id="flashing-overlay" class="modal-overlay" tabindex="-1" role="dialog" aria-labelledby="flashing-overlay" aria-modal="true">
<div id="flashing-overlay" class="modal-overlay modal-div" tabindex="-1" role="dialog" aria-labelledby="flashing-overlay" aria-modal="true">
<div id="flashing-info">
<h2 id="flashing-text">Flashing micro:bit</h2>
<div id="webusb-flashing-loader"></div>
Expand All @@ -407,8 +407,8 @@ <h2 id="flashing-text">Flashing micro:bit</h2>
</div>
</div>
<div id="modal-msg-overlay-container" class="modal-overlay-container modal-msg-overlay-container">
<div id="modal-msg-overlay" class="modal-overlay" tabindex="-1" role="dialog" aria-labelledby="flashing-overlay" aria-modal="true">
<a class="vex-close modal-close" onclick="$('#modal-msg-overlay-container').hide()"></a>
<div id="modal-msg-overlay" class="modal-overlay modal-div" tabindex="-1" role="dialog" aria-labelledby="modal-msg-overlay" aria-modal="true">
<a class="vex-close modal-close" onclick="modalMsgClose()"></a>
<div id="modal-msg-text">
<h2 id="modal-msg-title"></h2>
<div id="modal-msg-content"></div>
Expand Down
28 changes: 23 additions & 5 deletions python-main.js
Original file line number Diff line number Diff line change
Expand Up @@ -919,9 +919,9 @@ function web_editor(config) {
}

// Trap focus in modal and pass focus to first actionable element
function focusModal() {
function focusModal(modalId) {
document.querySelector('body > :not(.vex)').setAttribute('aria-hidden', true);
var dialog = document.querySelector('.modal-div');
var dialog = document.querySelector(modalId);
var focusableEls = dialog.querySelectorAll('a[href]:not([disabled]), button:not([disabled]), textarea:not([disabled]), input[type="text"]:not([disabled]), input[type="radio"]:not([disabled]), input[type="checkbox"]:not([disabled]), select:not([disabled])');
$(focusableEls).each(function() {
$(this).attr('tabindex', '0');
Expand Down Expand Up @@ -987,7 +987,7 @@ function web_editor(config) {
vex.open({
content: Mustache.render(template, loadStrings),
afterOpen: function(vexContent) {
focusModal();
focusModal("#files-modal");
$("#show-files").attr("title", loadStrings["show-files"] +" (" + micropythonFs.ls().length + ")");
document.getElementById("show-files").innerHTML = loadStrings["show-files"] + " (" + micropythonFs.ls().length + ") <i class='fa fa-caret-down'>";
$('#save-hex').click(function() {
Expand Down Expand Up @@ -1173,7 +1173,7 @@ function web_editor(config) {
vex.open({
content: Mustache.render(template, context),
afterOpen: function(vexContent) {
focusModal();
focusModal("#snippet-modal");
$(vexContent).find('.snippet-selection').click(function(e){
var snippet_name = $(this).find('.snippet-name').text();
EDITOR.triggerSnippet(snippet_name);
Expand Down Expand Up @@ -1327,6 +1327,7 @@ function web_editor(config) {
// Display error handler modal
$("#flashing-overlay-container").css("display", "flex");
$("#flashing-info").addClass('hidden');
focusModal("#flashing-overlay");

// Log error to console for feedback
console.log("An error occured whilst attempting to use WebUSB.");
Expand Down Expand Up @@ -1460,6 +1461,12 @@ function web_editor(config) {
};

document.dispatchEvent(new CustomEvent('webusb', { detail: details }));
// If escape key is pressed close modal
$(document).keydown(function(e) {
if (e.which == 27) {
flashErrorClose();
}
});
}

function doDisconnect() {
Expand Down Expand Up @@ -1730,6 +1737,12 @@ function web_editor(config) {
});
}
$("#modal-msg-links").html((modalLinks).join(' | '));
focusModal("#modal-msg-overlay");
$(document).keydown(function(e) {
if (e.which == 27) {
modalMsgClose();
}
});
}

function formatMenuContainer(parentButtonId, containerId) {
Expand Down Expand Up @@ -1936,9 +1949,14 @@ function web_editor(config) {
}

/*
* Function to close flash error box
* Functions to close error modals
*/
function flashErrorClose() {
$('#flashing-overlay-error').html("");
$('#flashing-overlay-container').hide();
$(document).off("keydown");
}
function modalMsgClose() {
$('#modal-msg-overlay-container').hide()
$(document).off("keydown");
}