Difference between revisions of "ARIA Dialog"

From Level Access Web Labs
Jump to navigation Jump to search
Line 1: Line 1:
<html lang="en">
+
<html lang="en-US">
<head>
+
    <head>
<title>ARIA Dialog Example</title>
+
        <meta charset="utf-8">
<style>
+
        <title>ARIA Dialog Example</title>
.box-hidden {
+
        <script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
display: none;
+
        <style type="text/css">
position: absolute;
+
            #modalOverlay {
top: 19em; left:15em; width:20em; height:5em;
+
                width:100%;
border: 2px solid black;
+
                height:100%;
padding:0 1em 1em 1em;
+
                z-index:2; /* places the modal overlay between the main page and the modal dialog*/
background-color: #eee;
+
                background-color:#000;
z-index:1002;
+
                opacity:0.5;
overflow: auto;
+
                position:fixed;
}
+
                top:0;
</style>
+
                left:0;
 +
                display:none;
 +
                margin:0;
 +
                padding:0;
 +
            }
  
 +
            #modal {
 +
                width:50%;
 +
                margin-left:auto;
 +
                margin-right:auto;
 +
                padding: 5px;
 +
                border: thin #000 solid;
 +
                background-color:#fff;
 +
                z-index:3; /* places the modal dialog on top of everything else */
 +
                position:fixed;
 +
                top:25%;
 +
                left:25%;
 +
                display:none;
 +
            }
 +
            #modal h1 {
 +
                text-align:center;
 +
            }
 +
 +
            .modalCloseButton {
 +
                float:right;
 +
                position:absolute;
 +
                top:10px;
 +
                left:95%;
 +
            }
 +
            .modalCloseButton img {
 +
                border:0;
 +
            }
 +
 +
            .screen-reader-offscreen {
 +
                position:absolute;
 +
                left:-999px;
 +
                width:1px;
 +
                height:1px;
 +
                top:auto;
 +
            }
 +
 +
        </style>
 
<script>
 
<script>
var dialogOpen = false, lastFocus, dialog, okbutton, pagebackground;
+
// jQuery formatted selector to search for focusable items
 +
var focusableElementsString = "a[href], area[href], input:not([disabled]), select:not([disabled]), textarea:not([disabled]), button:not([disabled]), iframe, object, embed, *[tabindex], *[contenteditable]";
  
function showDialog(el) {
+
// store the item that has focus before opening the modal window
lastFocus = el || document.activeElement;
+
var focusedElementBeforeModal;
toggleDialog('show');
+
 
 +
$(document).ready(function() {
 +
    jQuery('#startModal').click(function(e) {
 +
        showModal($('#modal'));
 +
    });
 +
    jQuery('#cancel').click(function(e) {
 +
        hideModal();
 +
    });
 +
    jQuery('#cancelButton').click(function(e) {
 +
        hideModal();
 +
    });
 +
    jQuery('#enter').click(function(e) {
 +
        enterButtonModal();
 +
    });
 +
    jQuery('#modalCloseButton').click(function(e) {
 +
        hideModal();
 +
    });
 +
    jQuery('#modalCloseButton').keydown(function(event) {
 +
        trapSpaceKey($(this), event, hideModal);
 +
    })
 +
    jQuery('#modal').keydown(function(event) {
 +
        trapTabKey($(this), event);
 +
    })
 +
    jQuery('#modal').keydown(function(event) {
 +
        trapEscapeKey($(this), event);
 +
    })
 +
 
 +
});
 +
 
 +
 
 +
function trapSpaceKey(obj, evt, f) {
 +
    // if space key pressed
 +
    if (evt.which == 32) {
 +
        // fire the user passed event
 +
        f();
 +
        evt.preventDefault();
 +
    }
 
}
 
}
function hideDialog(el) {
+
 
toggleDialog('hide');
+
function trapEscapeKey(obj, evt) {
 +
 
 +
    // if escape pressed
 +
    if (evt.which == 27) {
 +
 
 +
        // get list of all children elements in given object
 +
        var o = obj.find('*');
 +
 
 +
        // get list of focusable items
 +
        var cancelElement;
 +
        cancelElement = o.filter("#cancel")
 +
 
 +
        // close the modal window
 +
        cancelElement.click();
 +
        evt.preventDefault();
 +
    }
 +
 
 
}
 
}
  
function toggleDialog(sh) {
+
function trapTabKey(obj, evt) {
dialog = document.getElementById("box");
+
 
okbutton = document.getElementById("ok");
+
    // if tab or shift-tab pressed
pagebackground = document.getElementById("bg");
+
    if (evt.which == 9) {
 +
 
 +
        // get list of all children elements in given object
 +
        var o = obj.find('*');
  
if (sh == "show") {
+
        // get list of focusable items
dialogOpen = true;
+
        var focusableItems;
 +
        focusableItems = o.filter(focusableElementsString).filter(':visible')
 +
 
 +
        // get currently focused item
 +
        var focusedItem;
 +
        focusedItem = jQuery(':focus');
 +
 
 +
        // get the number of focusable items
 +
        var numberOfFocusableItems;
 +
        numberOfFocusableItems = focusableItems.length
 +
 
 +
        // get the index of the currently focused item
 +
        var focusedItemIndex;
 +
        focusedItemIndex = focusableItems.index(focusedItem);
 +
 
 +
        if (evt.shiftKey) {
 +
            //back tab
 +
            // if focused on first item and user preses back-tab, go to the last focusable item
 +
            if (focusedItemIndex == 0) {
 +
                focusableItems.get(numberOfFocusableItems - 1).focus();
 +
                evt.preventDefault();
 +
            }
 +
 
 +
        } else {
 +
            //forward tab
 +
            // if focused on the last item and user preses tab, go to the first focusable item
 +
            if (focusedItemIndex == numberOfFocusableItems - 1) {
 +
                focusableItems.get(0).focus();
 +
                evt.preventDefault();
 +
            }
 +
        }
 +
    }
  
// show the dialog
 
dialog.style.display = 'block';
 
 
// after displaying the dialog, focus an element inside it
 
okbutton.focus();
 
 
// only hide the background *after* you've moved focus out of the content that will be "hidden"
 
pagebackground.setAttribute("aria-hidden","true");
 
 
} else {
 
dialogOpen = false;
 
dialog.style.display = 'none';
 
pagebackground.setAttribute("aria-hidden","false");
 
lastFocus.focus();
 
}
 
 
}
 
}
  
 +
function setInitialFocusModal(obj) {
 +
    // get list of all children elements in given object
 +
    var o = obj.find('*');
  
document.addEventListener("focus", function(event) {
+
    // set focus to first focusable item
 +
    var focusableItems;
 +
    focusableItems = o.filter(focusableElementsString).filter(':visible').first().focus();
  
     var d = document.getElementById("box");
+
}
 +
 
 +
function enterButtonModal() {
 +
    // BEGIN logic for executing the Enter button action for the modal window
 +
    alert('form submitted');
 +
    // END logic for executing the Enter button action for the modal window
 +
     hideModal();
 +
}
  
     if (dialogOpen && !d.contains(event.target)) {
+
function showModal(obj) {
        event.stopPropagation();
+
     jQuery('#mainPage').attr('aria-hidden', 'true'); // mark the main page as hidden
        d.focus();
+
    jQuery('#modalOverlay').css('display', 'block'); // insert an overlay to prevent clicking and make a visual change to indicate the main apge is not available
     }
+
    jQuery('#modal').css('display', 'block'); // make the modal window visible
 +
    jQuery('#modal').attr('aria-hidden', 'false'); // mark the modal window as visible
 +
 
 +
    // save current focus
 +
    focusedElementBeforeModal = jQuery(':focus');
 +
 
 +
    // get list of all children elements in given object
 +
    var o = obj.find('*');
 +
 
 +
    // Safari and VoiceOver shim
 +
    // if VoiceOver in Safari is used, set the initial focus to the modal window itself instead of the first keyboard focusable item. This causes VoiceOver to announce the aria-labelled attributes. Otherwise, Safari and VoiceOver will not announce the labels attached to the modal window.
 +
 
 +
     // set the focus to the first keyboard focusable item
 +
    o.filter(focusableElementsString).filter(':visible').first().focus();
  
}, true);
 
  
 +
}
  
document.addEventListener("keydown", function(event) {
+
function hideModal() {
     if (dialogOpen && event.keyCode == 27) {
+
     jQuery('#modalOverlay').css('display', 'none'); // remove the overlay in order to make the main screen available again
        toggleDialog('hide');
+
    jQuery('#modal').css('display', 'none'); // hide the modal window
     }
+
    jQuery('#modal').attr('aria-hidden', 'true'); // mark the modal window as hidden
}, true);
+
     jQuery('#mainPage').attr('aria-hidden', 'false'); // mark the main page as visible
  
 +
    // set focus back to element that had it before the modal was opened
 +
    focusedElementBeforeModal.focus();
 +
}
 
</script>
 
</script>
</head>
+
    </head>
<body>
+
 
<div role="dialog" aria-labelledby="myDialog" id="box" class="box-hidden" tabindex="-1">
+
    <body>
<h3 id="myDialog">Just an example.</h3>
+
        <div id="mainPage" aria-hidden="false">
<button id="ok" onclick="hideDialog(this);" class="close-button">OK</button>
+
            <h1>ARIA Dialog Example</h1>
<button onclick="hideDialog(this);" class="close-button">Cancel</button>
+
            <p>This page demonstrates how to make a modal window as accessible as possible to assistive technology users. Modal windows are especially problematic for screen reader users. Often times the user is able to &quot;escape&quot; the window and interact with other parts of the page when they should not be able to. This is partially due to the way screen reader software interacts with the Web browser.</p>
</div>
+
            <h2>The Accessible Modal Window in Action</h2>
 +
            <p>To see this in action, you just need to <button id="startModal">view the modal window</button>. If the modal window works as planned, once the modal window is visible you should not be able to interact with other links on the main page like <a href="http://www.google.com">going to Google's site</a>. If you can interact with the page behind the modal window, guidance is given for how to get back to the modal window.</p>
 +
        </div>
 +
        <div id="modal" aria-hidden="true" aria-labelledby="modalTitle" aria-describedby="modalDescription" role="dialog">
 +
            <div role="document"> <!-- Optional role if you want screen reader users to be able to interact with objects other than focusable elements and their labels.  -->
 +
                <div id="modalDescription" class="screen-reader-offscreen">Beginning of dialog window. It begins with a heading 1 called &quot;Registration Form&quot;. Escape will cancel and close the window. This form does not collect any actual information.</div>
 +
                <h1 id="modalTitle">Registration Form</h1>
 +
                <p>These are the onscreen instructions that are not attached explicitly to a focusable element. Can screen reader users read this text with the virtual cursor?</p>
 +
                <form name="form1" method="post" action="">
 +
                    <p>
 +
                        <label for="firstName">First Name</label>
 +
                        <input type="text" name="firstName" id="firstName">
 +
                    </p>
 +
                    <p>
 +
                        <label for="lastName">Last Name</label>
 +
                        <input type="text" name="lastName" id="lastName">
 +
                    </p>
 +
                    <p>
 +
                        <label for="email">Email Address</label>
 +
                        <input type="text" name="email" id="email">
 +
                    </p>
 +
                    <p>
 +
                        <input type="button" name="button" id="enter" value="Submit">
 +
                        <input type="button" name="cancelButton" id="cancelButton" value="Cancel">
 +
                    </p>
 +
                    <a id="modalCloseButton" class="modalCloseButton" href="javascript:void(0)" title="Close registration form"><img id="cancel" src="x.png" alt="close the registration form"></a>
  
<div id="bg">
+
                </form>
<p><a onclick="showDialog(this); return false;" href="#" role="button">Display a dialog</a></p>
+
            </div> <!-- Matching div for optional role="document" -->
 +
        </div>
 +
        <div id="modalOverlay" tabindex="-1"></div>
  
<p>Other page content with <a href="#">a dummy focusable element</a></p>
+
    </body>
</div>
 
</body>
 
 
</html>
 
</html>
 
[[Category:ARIA]]
 
[[Category:ARIA]]

Revision as of 16:14, 28 March 2014

ARIA Dialog Example

ARIA Dialog Example

This page demonstrates how to make a modal window as accessible as possible to assistive technology users. Modal windows are especially problematic for screen reader users. Often times the user is able to "escape" the window and interact with other parts of the page when they should not be able to. This is partially due to the way screen reader software interacts with the Web browser.

The Accessible Modal Window in Action

To see this in action, you just need to . If the modal window works as planned, once the modal window is visible you should not be able to interact with other links on the main page like going to Google's site. If you can interact with the page behind the modal window, guidance is given for how to get back to the modal window.