question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Disable tabbing on modal content when hidden

See original GitHub issue

I know that the recommendation is to have this css:

[aria-hidden="true"] {
  display: none;
}

However that solution leaves much to be desired in terms of in-out transitions using CSS.

Since the modal code already implements the ability to disable focus on elements, as used on the modals siblings, would it be possible to do the same thing for the modal content when closing the modal?

This would enable the ability to use other means of hiding the visual side of the dialog when closing it, which includes transitions in and out.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:16 (14 by maintainers)

github_iconTop GitHub Comments

3reactions
Muntercommented, Apr 7, 2017

I thought it might be a useful addition so the default unstyled version did the correct thing with tabbing, and so it could avoid forcing a styling on the users.

I thought it wouldn’t be a big addition, since the modal is already dealing with toggling tabindexes, so it would probably be a smaller implementation in the library itself than if someone would have to to it themselves.

I’m comfortable with implementing this in my own code, but should you decide to full a functionality like this into the core I’d be happy to contribute with test cases

3reactions
KittyGiraudelcommented, Apr 7, 2017

For the record, here is how I would write it:

  var FOCUSABLE_ELEMENTS = ['a[href]', 'area[href]', 'input:not([disabled])', 'select:not([disabled])', 'textarea:not([disabled])', 'button:not([disabled])', 'iframe', 'object', 'embed', '[contenteditable]', '[tabindex]:not([tabindex^="-"])'];

  function getFocusableElements (el) {
    return Array.prototype.slice.call(
      el.querySelectorAll(FOCUSABLE_ELEMENTS.join(','))
    )
  }

  function makeTabbable (el) {
    if (el.hasAttribute('data-tabindex')) {
      el.setAttribute('tabindex', el.getAttribute('data-tabindex'));
    } else {
      el.removeAttribute('tabindex');
    }
  }

  function makeUntabbable (el) {
    if (el.hasAttribute('tabindex')) {
      el.setAttribute('data-tabindex', el.getAttribute('tabindex'));
    }

    el.setAttribute('tabindex', -1);
  }

  function toggleTabIndex (parent, focusable) {
    var els = getFocusableElements(parent)
    
    focusable
      ? els.forEach(makeTabbable)
      : els.forEach(makeUntabbable)
  }

  dialog.on('hide', function (dialog) {
    toggleTabIndex(dialog, false);
  });

  dialog.on('show', function (dialog) {
    toggleTabIndex(dialog, true);
  });
Read more comments on GitHub >

github_iconTop Results From Across the Web

Disable tabbing on modal content when hidden #70 - GitHub
This would enable the ability to use other means of hiding the visual side of the dialog when closing it, which includes transitions...
Read more >
How can restrict the tab key press only within the modal popup ...
1. disable screenreader interaction with any other nodes by setting aria-hidden=true on them · 2. disable any keyboard interaction with them · 3....
Read more >
How to trap focus inside modal to make it ADA compliant
tab, shift + tab and enter key and focus should be trapped inside modal and should not go out after pressing tab key...
Read more >
A CSS Approach to Trap Focus Inside of an Element
To summarize, when inside a dialog, pressing Tab or Shift+Tab should cycle the focus within the dialog only—amongst the focusable elements ...
Read more >
Modal · Bootstrap v5.0
Modals are built with HTML, CSS, and JavaScript. They're positioned over everything else in the document and remove scroll from the <body> so...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found