medialize / ally.js

JavaScript library to help modern web applications with accessibility concerns
http://allyjs.io/
MIT License
1.53k stars 82 forks source link

How to trap tab focus in element but allow tabbing to browser UI? #167

Closed Ambient-Impact closed 5 years ago

Ambient-Impact commented 5 years ago

Hi there. Apologies if this has been answered elsewhere (searched but couldn't find anything): is there an easy way to trap keyboard focus like ally.maintain.tabFocus does, but allowing tabbing to the browser UI?

I've been using ally.maintain.disabled, but it seems a bit heavy handed and can cause a noticeable delay on pages that have many elements. I'm working on an offcanvas component which covers part of the document like a modal dialog, and I'd prefer to trap tabbing and use ally.maintain.hidden rather than ally.maintain.disabled for performance.

rodneyrehm commented 5 years ago

Hey there,

https://allyjs.io/tutorials/accessible-dialog.html#trapping-focus-inside-the-dialog and https://allyjs.io/tutorials/accessible-dialog.html#reacting-to-kbdtabkbd-and-kbdshift-tabkbd try to explain this.

But you're already on the right track - ally.maintain.disabled is what you need. If the performance hit is too much, you might want to have a look at wicg-inert. I've been using that in my VueJS based projects for the past 2 years…

Ambient-Impact commented 5 years ago

Ah, thanks. Do you find that the wicg-inert polyfill is significantly faster than ally.maintain.disabled?

rodneyrehm commented 5 years ago

I haven't benchmarked it. But as I recall their implementation it should be faster. Afair they chose to ignore some obscure cases allowing them to go with a faster implementation (than literally testing every node in the document).

Ambient-Impact commented 5 years ago

I see. I'll have to give that a try sometime. Thanks!