How to keep focus within modal dialog?
Asked Answered
O

15

32

I'm developing an app with Angular and Semantic-UI. The app should be accessible, this means it should be compliant with WCAG 2.0. To reach this purpose the modals should keep focus within the dialog and prevents users from going outside or move with "tabs" between elements of the page that lays under the modal.

I have found some working examples, like the following:

Here is my try to create an accessible modal with Semantic-UI: https://plnkr.co/edit/HjhkZg

As you can see I used the following attributes:

role="dialog"

aria-labelledby="modal-title"

aria-modal="true"

But they don't solve my issue. Do you know any way to make my modal keeping focus and lose it only when user click on cancel/confirm buttons?

Override answered 9/6, 2017 at 7:42 Comment(4)
Better user bootstrap modal, it will weightless and easy to handle. Please Refer: #33010997Thrilling
@smartmouse, please accept an answer to resolve the question or explain what's missing from the available answers.Polygon
While not Angular, React-Modal has the features you seek: github.com/reactjs/react-modalOrnamented
check this out: #50178919Whitherward
A
24

Edit February 1, 2024:

The inert attribute is now fully stable and can be used to prevent a user escaping a modal by making everything outside the modal inert. Additionally, you can use the dialog element with the showModal function to handle modal dialog focus automatically. Using showModal will open the dialog element as a modal dialog and the browser will disable the focus of everything else on the page.

Original answer

There is currently no easy way to achieve this. The inert attribute was proposed to try to solve this problem by making any element with the attribute and all of it's children inaccessible. However, adoption has been slow and only recently did it land in Chrome Canary behind a flag.

Another proposed solution is making a native API that would keep track of the modal stack, essentially making everything not currently the top of the stack inert. I'm not sure the status of the proposal, but it doesn't look like it will be implemented any time soon.

So where does that leave us?

Unfortunately without a good solution. One solution that is popular is to create a query selector of all known focusable elements and then trap focus to the modal by adding a keydown event to the last and first elements in the modal. However, with the rise of web components and shadow DOM, this solution can no longer find all focusable elements.

If you always control all the elements within the dialog (and you're not creating a generic dialog library), then probably the easiest way to go is to add an event listener for keydown on the first and last focusable elements, check if tab or shift tab was used, and then focus the first or last element to trap focus.

If you're creating a generic dialog library, the only thing I have found that works reasonably well is to either use the inert polyfill or make everything outside of the modal have a tabindex=-1.

var nonModalNodes;

function openDialog() {    
  var modalNodes = Array.from( document.querySelectorAll('dialog *') );

  // by only finding elements that do not have tabindex="-1" we ensure we don't
  // corrupt the previous state of the element if a modal was already open
  nonModalNodes = document.querySelectorAll('body *:not(dialog):not([tabindex="-1"])');

  for (var i = 0; i < nonModalNodes.length; i++) {
    var node = nonModalNodes[i];
    
    if (!modalNodes.includes(node)) {
      
      // save the previous tabindex state so we can restore it on close
      node._prevTabindex = node.getAttribute('tabindex');
      node.setAttribute('tabindex', -1);
      
      // tabindex=-1 does not prevent the mouse from focusing the node (which
      // would show a focus outline around the element). prevent this by disabling
      // outline styles while the modal is open
      // @see https://www.sitepoint.com/when-do-elements-take-the-focus/
      node.style.outline = 'none';
    }
  }
}

function closeDialog() {
  
  // close the modal and restore tabindex
  if (this.type === 'modal') {
    document.body.style.overflow = null;
    
    // restore or remove tabindex from nodes
    for (var i = 0; i < nonModalNodes.length; i++) {
      var node = nonModalNodes[i];
      if (node._prevTabindex) {
        node.setAttribute('tabindex', node._prevTabindex);
        node._prevTabindex = null;
      }
      else {
        node.removeAttribute('tabindex');
      }
      node.style.outline = null;
    }
  }
}
Attire answered 11/6, 2017 at 7:11 Comment(2)
It's still in Canary mode. After 5 years :DEneidaenema
inert is now mostly supported, but it doesn't help because the user can still tab to browser components that are outside of the content window.Marquand
O
8

The different "working examples" do not work as expected with a screenreader.

They do not trap the screenreader visual focus inside the modal.

For this to work, you have to :

  1. Set the aria-hidden attribute on any other nodes
  2. disable keyboard focusable elements inside those trees (links using tabindex=-1, controls using disabled, ...)

  3. add a transparent layer over the page to disable mouse selection.

    • or you can use the css pointer-events: none property when the browser handles it with non SVG elements, not in IE
Oman answered 12/6, 2017 at 7:34 Comment(1)
@Pants like one aria-hidden attribute, one jQuery call ($(".disabled :focusable").attr("tabindex", -1);), one css class (pointer-events: none).Oman
A
6

This focus-trap plugin is excellent at making sure that focus stays trapped inside of dialogue elements.

Altheaalthee answered 11/7, 2018 at 2:31 Comment(1)
This plugin helped me to trap focus on my bootstrap 4 modal.Gabrielson
L
4

It sounds like your problem can be broken down into 2 categories:

  1. focus on dialog box

Add a tabindex of -1 to the main container which is the DOM element that has role="dialog". Set the focus to the container.

  1. wrapping the tab key

I found no other way of doing this except by getting the tabbable elements within the dialog box and listening it on keydown. When I know the element in focus (document.activeElement) is the last one on the list, I make it wrap

Locate answered 17/3, 2018 at 21:49 Comment(0)
P
2

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 be input,select,textarea,button.

$(document).on('keydown', function(e) {
    var target = e.target;
    var shiftPressed = e.shiftKey;
    // If TAB key pressed
    if (e.keyCode == 9) {
        // If inside a Modal dialog (determined by attribute role="dialog")
        if ($(target).parents('[role=dialog]').length) {                            
            // Find first or last input element in the dialog parent (depending on whether Shift was pressed). 
            // Input elements must be visible, and can be Input/Select/Button/Textarea.
            var borderElem = shiftPressed ?
                                $(target).closest('[role=dialog]').find('input:visible,select:visible,button:visible,textarea:visible').first() 
                             :
                                $(target).closest('[role=dialog]').find('input:visible,select:visible,button:visible,textarea:visible').last();
            if ($(borderElem).length) {
                if ($(target).is($(borderElem))) {
                    return false;
                } else {
                    return true;
                }
            }
        }
    }
    return true;
});
Pietro answered 28/1, 2019 at 15:31 Comment(2)
this is nice but could be improved by removing the internal check to see if a tabbed element is a child of the modal by instead listening to keydown events of the modal itself: $('#modal[role=dialog]').on('keydown', e => { // dosomething });Nihon
I like it as it is, as this allows for a single 'keydown' event listener that can be used for many purposes.Castor
S
1

I've been successful using Angular Material's A11yModule.

Using your favorite package manager install these to packages into your Angular app.

**"@angular/material": "^10.1.2"**

**"@angular/cdk": "^10.1.2"**

In your Angular module where you import the Angular Material modules add this:

**import {A11yModule} from '@angular/cdk/a11y';**

In your component HTML apply the cdkTrapFocus directive to any parent element, example: div, form, etc.

Run the app, tabbing will now be contained within the decorated parent element.

Showdown answered 18/8, 2020 at 20:16 Comment(0)
C
1

This might help someone who is looking for solution in Angular.

Step 1: Add keydown event on dialog component

  @HostListener('document:keydown', ['$event'])
  handleTabKeyWInModel(event: any) {
       this.sharedService.handleTabKeyWInModel(event, '#modal_id', this.elementRef.nativeElement, 'input,button,select,textarea,a,[tabindex]:not([tabindex="-1"])');
  }

This will filters the elements which are preseneted in the Modal dialog.

Step 2: Add common method to handle focus in shared service (or you can add it in your component as well)

handleTabKeyWInModel(e, modelId: string, nativeElement, tagsList: string) {
        if (e.keyCode === 9) {
            const focusable = nativeElement.querySelector(modelId).querySelectorAll(tagsList);
            if (focusable.length) {
               const first = focusable[0];
               const last = focusable[focusable.length - 1];
               const shift = e.shiftKey;
               if (shift) {
                  if (e.target === first) { // shift-tab pressed on first input in dialog
                     last.focus();
                     e.preventDefault();
                  }
                } else {
                    if (e.target === last) { // tab pressed on last input in dialog
                        first.focus();
                        e.preventDefault();
                    }
                }
            }
        }
    }

Now this method will take the modal dialog native element and start evaluate on every tab key. Finally we will filter the event on first and last so that we can focus on appropriate elements (on first after last element tab click and on last shift+tab event on first element).

Happy Coding.. :)

Carafe answered 23/2, 2021 at 6:2 Comment(2)
After reading various convoluted examples, this answer is what made it click in my mind. Thanks.On
@On Glad to hear.. Cheers :)Carafe
D
1

"focus" events can be intercepted in the capture phase, so you can listen for them at the document.body level, squelch them before they reach the target element, and redirect focus back to a control in your modal dialog. This example assumes a modal dialog with an input element gets displayed and assigned to the variable currDialog:

document.body.addEventListener("focus", (event) => {
    if (currDialog && !currDialog.contains(event.target)) {
        event.preventDefault();
        event.stopPropagation();
        currDialog.querySelector("input").focus();
    }
}, {capture: true});

You may also want to contain such a dialog in a fixed-position, clear (or low-opacity) backdrop element that takes up the full screen in order to capture and suppress mouse/pointer events, so that no browser feedback (hover, etc.) occurs that could give the user the impression that the background is active.

Divisibility answered 11/4, 2021 at 6:17 Comment(0)
L
0

Don't use any solution requiring you to look up "tabbable" elements. Instead, use keydown and either click events or a backdrop in an effective manor.

(Angular1)

See Asheesh Kumar's answer at https://mcmap.net/q/454914/-trap-focus-in-html-container-with-angular for something similar to what I am going for below.

(Angular2-x, I haven't done Angular1 in a while)

Say you have 3 components: BackdropComponent, ModalComponent (has an input), and AppComponent (has an input, the BackdropComponent, and the ModalComponent). You display BackdropComponent and ModalComponent with the correct z-index, both are currently displayed/visible.

What you need to do is have a general window.keydown event with preventDefault() to stop all tabbing when the backdrop/modal component is displayed. I recommend you put that on a BackdropComponent. Then you need a keydown.tab event with stopPropagation() to handle tabbing for the ModalComponent. Both the window.keydown and keydown.tab could probably be in the ModalComponent but there is purpose in a BackdropComponent further than just modals.

This should prevent clicking and tabbing to the AppComponent input and only click or tab to the ModalComponent input [and browser stuffs] when the modal is shown.

If you don't want to use a backdrop to prevent clicking, you can use use click events similarly to the keydown events described above.

Backdrop Component:

@Component({
selector: 'my-backdrop',
host: {
    'tabindex': '-1',
    '(window:keydown)': 'preventTabbing($event)'
},
...
})
export class BackdropComponent {
    ...
    private preventTabbing(event: KeyboardEvent) {
        if (event.keyCode === 9) { // && backdrop shown?
            event.preventDefault();
        }
    }
    ...
}

Modal Component:

@Component({
selector: 'my-modal',
host: {
    'tabindex': '-1',
    '(keydown.tab)': 'onTab($event)'
},
...
})
export class ModalComponent {
    ...
    private onTab(event: KeyboardEvent) {
        event.stopPropagation();
    }
    ...
}
Lithometeor answered 13/10, 2017 at 21:50 Comment(0)
S
0

I used one of the methods suggested by Steven Lambert, namely, listening to keydown events and intercepting "tab" and "shift+tab" keys. Here's my sample code (Angular 5):

import { Directive, ElementRef, Attribute, HostListener, OnInit } from '@angular/core';

/**
 * This directive allows to override default tab order for page controls.
 * Particularly useful for working around the modal dialog TAB issue
 * (when tab key allows to move focus outside of dialog).
 *
 * Usage: add "custom-taborder" and "tab-next='next_control'"/"tab-prev='prev_control'" attributes
 * to the first and last controls of the dialog.
 *
 * For example, the first control is <input type="text" name="ctlName">
 * and the last one is <button type="submit" name="btnOk">
 *
 * You should modify the above declarations as follows:
 * <input type="text" name="ctlName" custom-taborder tab-prev="btnOk">
 * <button type="submit" name="btnOk" custom-taborder tab-next="ctlName">
 */

@Directive({
  selector: '[custom-taborder]'
})
export class CustomTabOrderDirective {

  private elem: HTMLInputElement;
  private nextElemName: string;
  private prevElemName: string;
  private nextElem: HTMLElement;
  private prevElem: HTMLElement;

  constructor(
    private elemRef: ElementRef
    , @Attribute('tab-next') public tabNext: string
    , @Attribute('tab-prev') public tabPrev: string
  ) {
    this.elem = this.elemRef.nativeElement;
    this.nextElemName = tabNext;
    this.prevElemName = tabPrev;
  }

  ngOnInit() {
    if (this.nextElemName) {
      var elems = document.getElementsByName(this.nextElemName);
      if (elems && elems.length && elems.length > 0)
        this.nextElem = elems[0];
    }

    if (this.prevElemName) {
      var elems = document.getElementsByName(this.prevElemName);
      if (elems && elems.length && elems.length > 0)
        this.prevElem = elems[0];
    }
  }

  @HostListener('keydown', ['$event'])
  onKeyDown(event: KeyboardEvent) {

    if (event.key !== "Tab")
      return;

    if (!event.shiftKey && this.nextElem) {
      this.nextElem.focus();
      event.preventDefault();
    }

    if (event.shiftKey && this.prevElem) {
      this.prevElem.focus();
      event.preventDefault();
    }

  }

}

To use this directive, just import it to your module and add to Declarations section.

Spatola answered 28/2, 2018 at 2:34 Comment(0)
F
0

we can use the focus trap npm package.

npm i focus-trap

Formant answered 15/5, 2020 at 8:0 Comment(2)
For code blocks, use code formatter instead of header formatter. Just a tip because I'm not really sure whether that's a piece of code.Shifra
@ArdentCoder I suppose it is but its wrong. i is quite different from -i. Also this answer is redundant as this answer already noted it.Sacerdotal
K
0

For jquery users:

  1. Assign role="dialog" to your modal
  2. Find first and last interactive element inside the dialog modal.
  3. Check if current target is one of them(depending on shift key is pressed or not).
  4. If target element is one of first or last interactive element of the dialog, return false

Working code sample:

//on keydown inside dialog
$('.modal[role=dialog]').on('keydown', e => {
    let target = e.target;
    let shiftPressed = e.shiftKey;
    // If TAB is pressed
    if (e.keyCode === 9) {

        // Find first and last element in the ,modal-dialog parent.
        // Elements must be interactive i.e. visible, and can be Input/Select/Button/Textarea.

        let first =  $(target).closest('[role=dialog]').find('input:visible,select:visible,button:visible,textarea:visible').first();
        let last =  $(target).closest('[role=dialog]').find('input:visible,select:visible,button:visible,textarea:visible').last();
            let borderElem = shiftPressed ? first : last //border element on the basis of shift key pressed
            if ($(borderElem).length) {
                return !$(target).is($(borderElem)); //if target is border element , return false
            }
    }
    return true;
});
Kingsbury answered 2/11, 2021 at 12:3 Comment(0)
F
0

I read through most of the answers, while the package focus-trap seems like a good option. @BenVida shared a very simple VanillaJS solution here in another Stack Overflow post.

Here is the code:

const container=document.querySelector("_selector_for_the_container_")

//optional: needed only if the container element is not focusable already
container.setAttribute("tabindex","0")

container.addEventListener("focusout", (ev)=>{
  if (!container.contains(ev.relatedTarget)) container.focus()
})
Fiddlededee answered 23/9, 2022 at 18:52 Comment(0)
F
0

This solution works even for multiple bounds:

Supposing the modal (or any desired bounding element) has the attribute data-accessibility-bound-time whose value can be the bound's initialization time, we can run the below code once at the app's startup and after that, the focus will always become limited to the newest visible bound. If there's no visible bound, the focus becomes free:

const isElemAccessible = (elem) => {
  if (elem) {
    var computed = getComputedStyle(elem)
    if (
        computed.display == "none" ||
        computed.visibility == "hidden" ||
        computed.opacity == "0"
    ) {
      return false
    } else {
      return true
    }
  } else
    return false
}

const limitFocusToBound = (e) => {
  var bounds = document.querySelectorAll("[data-accessibility-bound-time]")
  var validBounds = []
  
  bounds.forEach(e => {
    if (isElemAccessible(e))
      validBounds.push(e)
  })
  
  if (!validBounds.length)
    return
  
  var activeBound = validBounds.reduce((max, a) =>
    max.getAttribute('data-accessibility-bound-time') >
      a.getAttribute('data-accessibility-bound-time') ?
      max : a)
  
  // Also `elem1.contains(elem1)` gives `true`
  if (!activeBound.contains(document.activeElement))
    activeBound.focus()
}

// Assigned once even if called multiple times
document.addEventListener('focusin', limitFocusToBound)
Fidel answered 10/1 at 19:15 Comment(0)
C
0

With this basic structure,

<div class='modal'>
  <p>I'm in the modal!</p>
  <input class='first_focus' type='text' name='input1' />
  <input type='text' name='input2' />
  <button type='submit' />Submit</button>
  <button class='last_focus btn_close' />X</button>  
</div>

When you open the modal,

  1. Give div .modal a class of .open.
  2. Set focus on .first_focus to get the user tabbing in the modal.
  3. Listen for tab and shiftKey + tab and call a function to loop appropriately. preventDefault is needed to keep from double-tabbing.
addListener(window, 'keydown', function (event) {
  if (event.key !== undefined) {
    if (event.shiftKey && event.key === 'Tab') {
      stayInModal(event, true);
    }
    else if (event.key === 'Tab') {
      stayInModal(event);
    }
  } else if (event.keyCode !== undefined) {
    if (event.shiftKey && event.keyCode === 9) {
      stayInModal(event, true);
    }
    else if (event.keyCode === 9) {
      stayInModal(event);
    }
  }
}, true);

function stayInModal(event, reverse = false) {
  var openModal = document.querySelector('.modal.open');
  if (openModal) {
    if (!reverse && event.target.className.indexOf('last_focus') !== -1) {
      event.preventDefault();
      openModalChild.querySelector('.first_focus').focus();
    }
    if (reverse && event.target.className.indexOf('first_focus') !== -1) {
      event.preventDefault();
      openModalChild.querySelector('.last_focus').focus();
    }
  }
}
Castor answered 16/1 at 8:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.