GithubHelp home page GithubHelp logo

nico3333fr / van11y-accessible-modal-window-aria Goto Github PK

View Code? Open in Web Editor NEW
88.0 3.0 10.0 342 KB

ES2015 accessible modal window system, using ARIA

License: MIT License

JavaScript 100.00%
modal modal-dialogs modal-windows aria accessibility a11y es2015

van11y-accessible-modal-window-aria's Introduction

Van11y accessible modal window system, using ARIA

Van11y

This script will provide you an accessible modal window using ARIA.

The demo is here: https://van11y.net/downloads/modal/demo/index.html

Website is here: https://van11y.net/accessible-modal/

La page existe aussi en français : https://van11y.net/fr/modale-accessible/

How it works

Basically, when you activate one:

  • the script wraps all the page into a div id="js-modal-page";
  • adds the noscroll class on the body element (to remove scroll with CSS if needed);
  • then inserts a dialog element at the end of your page;
  • puts the focus into it and traps focus in the modal window;
  • When you exit it, the focus is given back to the element that opened it.

You can close it using Esc, or by using Enter or Space if you’re on the close button.

Mouse users can click outside the modal window to close it (this option can be disabled if needed).

If you never activate a modal window, it won’t be anywhere in the code.

How to use it

Download the script

You may use npm command: npm i van11y-accessible-modal-window-aria. You may also use bower: bower install van11y-accessible-modal-window-aria.

Option and attributes

First, put class="js-modal" on a button or a link to activate the script. Then, here are all attributes:

  • Attribute data-modal-prefix-class: will namespace all the modal CSS element classnames.
  • Attribute data-modal-text: the text of your modal window (will be put into a p tag).
  • Attribute data-modal-content-id: the id of (hidden) content in your page that will be put into your modal window (if data-modal-text is not present).
  • Attribute data-modal-title: the main title of the modal window.
  • Attribute data-modal-close-text: the text of the close button in your modal window.
  • Attribute data-modal-close-title: the title attribute of the close button in your modal window.
  • Attribute data-modal-background-click="disabled": disable the possibility to click outside the modal window to close it.
  • Attribute data-modal-close-img: a path to a valid image for the close button.
  • Attribute data-modal-focus-toid: the id of the element in the modal you want to give the focus to, when loading the modal (closing button if not specified).
  • Attribute data-modal-describedby-id: adds aria-describedby=<the value of this attribute> to the dialog tag.

If you need to close it, add class="js-modal-close" on an element in the modal content, it will trigger a click on close button.

Remember there are some demos, it will be easier to understand: Demo of accessible modal window

The script is launched when the page is loaded. If you need to execute it on AJAX-inserted content, you may use for example on <div id="newContent">your modal launcher source</div>:

van11yAccessibleModalWindowAria(document.getElementById('newContent')[, addListeners]);

addListeners is a facultative boolean (by default set to true) to add modal listeners (should be set up only the first time in most of the cases).

Minimal styling classes

Here is the minimal set of styles needed to make it work (without data-modal-prefix-class attribute):

/* needed for old browsers */
dialog {
  display: block;
  border: 0;
}
/* removes scroll when modal is opened */
.no-scroll {
  overflow: hidden;
}
/* overlay covers everything */
.modal-overlay {
  position: fixed;
  top: 0;
  bottom: 0;
  right: 0;
  left: 0;
  z-index: 666;
}
/* modal */
.modal {
  position: fixed;
  left: 25%;
  right: auto;
  top: 15%;
  width: 50%;
  background: #fff;
  z-index: 667;
}

Styling classes example

Here are the styles (unprefixed) used for the demo, I’ve used data-modal-prefix-class="simple" and data-modal-prefix-class="simple-animated" to namespace elements, so each one will start with .simple-/.simple-animated-:

dialog {
  display: block;
  border: 0;
}
/* removes scroll when modal is opened */
.no-scroll {
  overflow: hidden;
}
/* overlay covers everything */
.simple-modal-overlay,
.simple-animated-modal-overlay {
  position: fixed;
  top: 0;
  bottom: 0;
  right: 0;
  left: 0;
  background: #fff;
  opacity: .8;
  z-index: 666;
  cursor: pointer;
}
.simple-modal-overlay[data-background-click="disabled"],
.simple-animated-modal-overlay[data-background-click="disabled"] {
  cursor: auto;
}
.simple-animated-modal-overlay  {
  animation: fadewhite ease .5s 1 normal ;
}

@keyframes fadewhite {
  0% {
    opacity: 0;
  }
  100% {
    opacity: .8;
  }
}
/* modal */
.simple-modal,
.simple-animated-modal {
  position: fixed;
  left: 15%;
  top: 5%;
  width: 70%;
  max-height: 98vh;
  border: 2px solid #000;
  background: #fff;
  z-index: 667;
  padding: 2em;
  right: auto;
  overflow: auto;
}
.simple-modal-close,
.simple-animated-modal-close {
  float: right;
  background: #128197;
  border-radius: 1em;
  color: #fff;
  border: 0;
  font: inherit;
  padding: .25em .5em;
  cursor: pointer;
}
.simple-modal-close:focus,
.simple-modal-close:hover,
.simple-modal-close:active {
  outline: 1px dotted #fff;
}
.simple-modal-close:hover,
.simple-modal-close:active {
  background: #4d287f;
}

.simple-animated-modal {
  animation: apparition ease .5s 1 normal ;
}

@keyframes apparition {
  0% {
    opacity: 0;
    max-height: 0;
    width: 0;
    left: 50%;
  }
  100% {
    opacity: 1;
    max-height: 100%;
    width: 70%;
    left: 15%;
  }
}

/* it can be easily adapted in media-queries for tablets/mobile */

/* for this example: tablets */
@media (max-width: 55.625em) {

  .simple-modal,
  .simple-animated-modal {
    left: 5%;
    top: 5%;
    height: 90%;
    width: 90%;
  }

}

/* for this example: mobile */
@media (max-width: 44.375em) {

  .simple-modal,
  .simple-animated-modal {
    left: 1%;
    top: 1%;
    width: 98%;
    height: 98%;
  }

}

Other style example

Here are the styles (unprefixed) used for the third example of the demo, I’ve used data-modal-prefix-class="simple-left" to namespace elements, so each one will start with .simple-left-:

/* needed for old browsers */
dialog {
  display: block;
  border: 0;
}
/* removes scroll when modal is opened */
.no-scroll {
  overflow: hidden;
}

/* another modal styling example */
/* tooltip modal for it’s easy button */
.simple-left-modal-overlay {
  position: fixed;
  top: 0;
  bottom: 0;
  right: 0;
  left: 0;
  background: #fff;
  opacity: .8;
  z-index: 666;
  cursor: pointer;
}
.simple-left-modal-overlay[data-background-click="disabled"] {
  cursor: auto;
}

.simple-left-modal-overlay {
  position: fixed;
  top: 0;
  bottom: 0;
  right: 0;
  left: 0;
  background: rgba(0, 0, 0, .8);
  opacity: .8;
  z-index: 666;
  cursor: pointer;
}

.simple-left-modal {
  left: auto;
  right: 0;
  top: 0;
  bottom: 0;
  height: 100%;
  z-index: 667;
  position: fixed;
  width: 40em;
  max-width: 100%;
  padding: 0 1em 1em 1em;
  font-size: 1em;
  border: 0;
  overflow: auto;
  background-color: #aaa ; /* fallback CSS IE9 */
  background-image:
      -webkit-linear-gradient(
        top,
        #128197 3em,
        #f7f7f7 3em
      );  background-image:
      linear-gradient(
        to bottom,
        #128197 3em,
        #f7f7f7 3em
      );
  background-attachment: local;
}
.simple-left-modal-close {
  position: absolute;
  top: .5em;
  right: 0;
  background: transparent;
  color: #fff;
  border: 0;
  cursor: pointer;
}
.simple-left-modal-title {
  color: #fff;
  font-size: 1.5em;
}

van11y-accessible-modal-window-aria's People

Contributors

nico3333fr avatar oliv- avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

van11y-accessible-modal-window-aria's Issues

Conflit entre`.no-scroll` et `overflow-x: hidden;` sur `html`

Coucou M'sieu, :)

Je me suis rendue compte que lorsque tu as la règle overflow-x: hidden; qui est aplliquée à la balise html, ta classe no-scroll (appliquée à body devient alors non fonctionnelle).
Pour résoudre le problème j'ai appliqué la classe no-scroll à la racine htmldirectement, sans rencontrer d'effet de bord.

Merci encore pour ce super script !

ability to add aria-describedby on the modal

Hello, I am are looking for the ability to provide aria-describedby on the modal. This establishes a relationship between dialog and text that described them. Additionally provides descriptive information about the dialog once defined.

Open and close modal through javascript

Hello,

I would like to open and close modal(s) through javascript. I mean without the user clicking on a specific button. My question is: is there something that looks like this: open_modal('modal_id);?
I have seen a previous question closed to this one but I think it was about detection which is not the point here.

And thank you for your plugin!

bug lorsque le button est composé d'une image

Lorsque le bouton est composé de HTML (une image dans mon cas), le script bug :

<button class="js-modal"><img src='[...]' alt='[...]' /><button>

Ce test renvoi false :

hasClass(e.target, MODAL_JS_CLASS)

Et le script ne construit pas la modale

Additional 'close' buttons

I use this plugin for a confirmation window, which has additional buttons to close/confirm an action.

After confirmation or cancellation I want to close the modal window, but I can't seem to find a way to this except for adding id="js-modal-close" to the buttons, but this is technically against the spec as there would be multiple elements with the same id.

So I was wondering if there's a class I can add (class="js-modal-close" doesn't work) to close the modal? Or if there's a function I can call to close the modal?

How to re bind js when opened in modal and when closing

Hi,

I think it's not an issue but more a question. What would be the best way to re init a script when openend in this modal.
Like i have an image slider and want to use this modal for a full screen version, but then the slider does not work any more.

I hope you could help point me in the good direction.

Cannot change tab order

Hello, I have tested a fix to my problem, and am wondering if it can be implemented. I need to be able to tab to a button in my content first before the X button. Currently the X button takes taborder precedence because it is implemented first in the modal creation. By simply creating the X button html after the content, this can be done. This could be a customized feature if you believe X button should be first in some cases.

Screen readers are reading the x button first, which is not the primary action to be taken, only secondary.

Provide a function to display a modal window

Hi,

It's required to use a link or button to display the modal window.

It would be nice to be able to display one with a JS call, something like showVan11y(config).

My current usecase : display a modal window at a form submit for data validation (choosing a corrected address among several guesses from the user's data).

Invalid License Format

The license is currently defined as follows:

 "license": [
    {
      "name": "MIT",
      "url": "https://github.com/nico3333fr/van11y-accessible-modal-window-aria/blob/master/LICENSE"
    }
  ]

Per npm documentation, this is an old/invalid format for a license. It also breaks a standard license parser. Is it possible to correct this to a valid format?

Set class per modal

I would like to change modal styles per modal instance. Is there anyway to add a data-attribute on the trigger button that would apply a class to the dialog?

Modals within modals

Hi

I want to open a modal from inside another modal. This totally messes with the contentBackId and HTML is not moved back to the original container.

Is there some way I can close the first modal before opening the second?

Error : Vite + van11y-accessible-modal

J'essaie d'importer vani11y dans un projet Astro.build + Vite.
Que penser de ce message @nico3333fr ?

van11y-accessible-modal-window-aria doesn't appear to be written in CJS, but also doesn't appear to be a valid ES module (i.e. it doesn't have "type": "module" or an .mjs extension for the entry point). Please contact the package author to fix.

Modal body [object HTMLDivElement]

Hey,

cf bug

    1. Go => demo modal
    1. Click Montre-moi une autre modale or C’est si facile
    1. Close the modal via ESC_KEY
    1. Click Montre-moi une autre modale or C’est si facile

What is the expected result ?

screenshot at 2017-04-19 15-20-47

What happens instead ?

screenshot at 2017-04-19 15-20-59

the innerHTML of #modal_id_2nd_example changes:
output

Focus trap goes out of modal when the modal content is an iframe

Hello,

the focus is somewhat not trapped when the content of the modal is an iframe. When you press tab after the last element, the focus goes out. It focuses on the browser controls, address bar, and then back to the modal. Any insights would be very much appreciated!

Modal auto opening

Bonjour,

Tout d'abord merci pour ce super plugin qui marche à merveille.

Je cherche néanmoins un moyen de pouvoir gérer une ouverture de la modale sans passer par un trigger click sur le button mais avoir une fonction qui détectera son ouverture.

j'espère que c'est assez clair.

Merci d'avance

Modal : callback après ouverture ?

Bonjour @nico3333fr ,
comment me conseillerais-tu de procéder pour ajouter un écouteur d'événement à un élément dans la modale ? Est-ce que Van11y propose, par exemple, une callback une fois ouverte ?…

Merci de ton script et de ton aide,
Bonne journée

data-modal-background-click="disabled" extended

this feature may be use in case of alertdialog instead of dialog so if it's not currently the case this must also prevent to close the dialog using esc key.

Otherwise maybe you can do extra data attributes like :

  • data-modal-esc-key="disabled"
  • data-modal-role="alertdialog" -> will automatically, change role to alertdialog instead of dialog, disable click on background and esc key to close, remove close button and move focus on the dialog itself instead of on the close button

Cannot specify initial focus

Hello, I would like to request the ability to specify an initial focus. I actually need it to be able to focus on the entire layer initially, then tab loop inside the modal as currently does. The problem is screen readers read the X button first, which isn't the intent. We want to ensure that the message is read out first, then the user can cycle between content button and X button.

Issue with role="document" on the modal

While testing the modal for accessibility with various screen reader and browser combinations (NVDA + FIREFOX, JAWS + IE, VOICEOVER + IOS) the results are not as expected.

When the modal window displays, NVDA reads the whole content automatically which is ok but with JAWS and VOICEOVER the content is not read at all.

Moving the focus to the content will read the content but not the inputs inside the modal and the aria spec says If a dialog is limited to interactions that either provide additional information or continue processing, it may be advisable to set focus to the element that is likely to be most frequently used, such as an OK or Continue button.

Using aria-describedBy on the modal and point it to the content on the layer at least fixes the issue in JAWS and VOICEOVER but this introduces and issue with NVDA + FIREFOX where the content is read twice.

After doing some research found that role="document" is causing the issues across multiple screen readers. Removing the role and adding an aria-describedBy pointing to the dialog content will give the consistent behavior among all the screen reader and browser combinations.

Here is an example from deque university which suggest not to use role="document" - Alert Dialog (under Dialog source code).

Allow choice of when to begin rendering modal

Currently, the modal begins to render on DOMContentLoaded. We have some code that wants to use the modal, but the modal won't be immediately launched. As so, we don't want to bother rendering it out initially, so we load the JavaScript with 'async'. This means the script may be loaded after the DomContentLoaded event is fired. As such, we'd prefer to have it run on the window.load event.

Can we have the option to choose to render it on the load event instead of DOMContentLoaded, or just disable the initial render and let us call a render function manually on load?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.