Home

Awesome

Accessible Modal Dialog

A vanilla JavaScript, accessible modal dialog script, written in ES5.

See the live demo and test pages.

Please read Having an open dialog, which provides information on the support for the native <dialog> element. As browser support (particularly Safari 15.4 and up adoption) increases, the need for custom dialogs should significantly decrease, rendering custom solutions such as this one here unnecessary.

NOTE: this script is not under active development. I would recommend either using the native <dialog> element, or you should check out a11y-dialog for a script that is being actively maintained and delivers a good modal dialog experience.

Install

You can get this package on npm:

$ npm i aria-modal-dialog

Clone it:

$ git clone https://github.com/scottaohara/accessible_modal_window.git

Or download a zip of the repository.

The CSS for this component is included in assets/css/. The classes are added to the base markup when the script/page loads. The base CSS has only the most necessary styling to visually convey it as a modal dialog. You will need to modify the CSS to integrate the dialog into your project's visual aesthetic.

Standard Usage

Include the a11y.modal.js file at the bottom of your document, or concatenated into your primary .js file, as part of your build process.

Ideally modal dialogs are activated by a purposeful user interaction with a button, or an element that has been modified to have the semantics and keyboard functionality of a button. While this script does allow dialogs to be activated by other means, a dialog opening without a purposeful action can be a confusing and frustrating user experience to many.

Modal Dialog Trigger

The baseline for a button to open a modal dialog should consist of the following:

<button type="button"
  class="_your_class(es)_here_"
  data-modal-open="IDREF_of_modal_to_open"
  disabled>
  Launch My Modal
</button>
<!--
  Swap out disabled with the hidden attribute if you want the trigger to be 
  completely hidden if JavaScript is unavailable for some reason.  

  The hidden attribute and/or the disabled attribute will be removed 
  during the script's setup function.
-->

The data-modal-open attribute is used to point to the specific modal dialog the button should activate. Adding a disabled attribute helps ensure that if JavaScript is blocked, or fails, users will be presented with a button that correctly conveys current functionality.

If JavaScript is unavailable, and the contents of a modal dialog would still make sense as part of the document, then an <a> element may be used as the dialog trigger, and progressively enhanced to be a button. This would then allow the <a> to act as an in-page link to the would-be dialog's contents.

<a href="#unique_ID_2"
  class="_your_class(es)_here_" 
  data-modal-open>
  Launch My Modal
</a>

The href attribute of a link may act as the identifier for the target dialog, if the data-modal-open attribute value is empty. However, if you would like this link to go to another location, if JavaScript were disabled, then set the href to the appropriate URL, and set the data-modal-open to the IDREF of the dialog within the document. Note, the href is removed from the <a> when the script runs, and a tabindex=0 is added, as "buttons" shouldn't allow for right click to open in a new window.

Modal Dialog Base Markup

The minimum markup for a modal dialog would be the following:

<div id="unique_ID_to_match_data-modal-open" data-modal>
  <h1>
    A descriptive title for the dialog
  </h1>
  <div>
    <!-- primary content of the dialog here -->
  </div>
</div>

If you need to support NVDA prior to 2017.4, then consider the following minimum markup pattern, which will add a role="document" to the div that wraps the contents of the modal dialog. This will correctly allow users to navigate the contents of a dialog with NVDA's virtual cursor, and not automatically enter users into forms/application mode:

Update: This pattern should also be considered for macOS VoiceOver users, as without a role=document wrapping all the contents within a role=dialog, VoiceOver's quick nav may not be able to access all elements. Without quick nav on, VoiceOver should have no issues accessing content of a role=dialog.

<div id="unique_ID_to_match_data-modal-open" data-modal>
  <div data-modal-document>
    <h1>
      A descriptive title for the dialog
    </h1>
    <div>
      <!-- primary content of the dialog here -->
    </div>
  </div>
</div>

Lack of features are not bugs

This script does not presently use the aria-haspopup="dialog" on a dialog's trigger(s). Nor does it use aria-modal on the element with role="dialog". The code to add these attributes are currently in the dialog script, but commented out until they receive more robust support (see Screen Reader quirks).

Update August 15, 2018: Testing with Safari Technology Preview (Release 63 (Safari 12.1, WebKit 13607.1.2.1)) on macOS 10.13.6, aria-modal="true" will begin to work properly with VoiceOver. This is great news for the future, but until a good percentage of people update from Safari 11.x to 12+, aria-modal should continued to be used with caution.

Inert Polyfill

For this script to provide peak accessibility, it must also utilize the inert polyfill from Google. While the dialogs have a function to keep focus within the dialog, looping through any focusable elements within itself, the inert polyfill will help prevent a user from accessing the browser's chrome (e.g. the address bar) and then being able to navigate back into the obscured document. The dialog script doubles down on the elements with inert="true" and also add an aria-hidden="true" as well. This ensures that not only can users not access elements within the obscured document by keyboard navigation, but that these elements will not be revealed in screen reader listings of elements within a document (e.g. listings of landmarks/regions, headings or form controls with NVDA and JAWS, or be revealed in VoiceOver's rotor menus.)

Configuration attributes

The following attributes are used to setup instances of the dialog triggers (buttons), the dialog container, and any necessary child elements of the dialogs.

Trigger Attributes

Dialog Attributes

Dialog Children Attributes

Screen Reader Quirks

Things of note for why certain decisions were made, and how different screen reader and browser combos have their own inconsistencies in announcing modal dialogs.

More information about modal dialogs

Articles I've written about modal dialog accessibility.

License & Such

This script was written by Scott O'Hara: Website, Twitter.

It has an MIT license.

Use it, modify it, contribute to it to help make your project more accessible :)