I have a modal popup opened. I have accessibility requirement. So added ARIA related labels. But when i click tab key continuously focus going to the page behind the actual page.
Added role="dialog" in html file
But when modal opened i want only the focus navigate within the modal popup.
Working on Angular4, HTML5
project. Better if we find a solution within HTML file itself. I mean without added any javascript/jQuery related stuffs to prevent this
You are asking about focus trap, it's nicely demonstrated in this demo: http://davidtheclark.github.io/focus-trap/demo/
Adding
role="dialog"
will not automatically provide trap the focus within that element. In fact, there's no native focus trap provided by browsers.You need to go with one of following options:
be careful of any method relying only on javascript events as it won't correctly handle screenreaders
However, this cannot be achieved without javascript like already indicated in multiple questions like How to keep focus within modal dialog?
You have three steps to do:
1. disable screenreader interaction with any other nodes by setting
aria-hidden=true
on themFor instance:
2. disable any keyboard interaction with them
This has to be done in Javascript / or jQuery.
This is a one-liner instruction in jQuery, using jquery-ui
The reverse can be achieved using:
3. remove any pointer event for those elements to disable mouse interaction
css sample:
Here's my solution. It traps Tab or Shift+Tab as necessary on first/last element of modal dialog (in my case found with
role="dialog"
). Eligible elements being checked are all visible input controls whose HTML may beinput,select,textarea,button
.