Trap tab in modals

This commit is contained in:
Thibaut Girka 2021-08-28 14:30:58 +02:00 committed by marcin mikołajczak
parent 2ef0cdb71b
commit e43692b4ce
1 changed files with 24 additions and 0 deletions

View File

@ -75,8 +75,31 @@ class ModalRoot extends React.PureComponent {
}
};
handleKeyDown = (e) => {
if (e.key === 'Tab') {
const focusable = Array.from(this.node.querySelectorAll('button:not([disabled]), [href], input:not([disabled]), select:not([disabled]), textarea:not([disabled]), [tabindex]:not([tabindex="-1"])')).filter((x) => window.getComputedStyle(x).display !== 'none');
const index = focusable.indexOf(e.target);
let element;
if (e.shiftKey) {
element = focusable[index - 1] || focusable[focusable.length - 1];
} else {
element = focusable[index + 1] || focusable[0];
}
if (element) {
element.focus();
e.stopPropagation();
e.preventDefault();
}
}
}
componentDidMount() {
window.addEventListener('keyup', this.handleKeyUp, false);
window.addEventListener('keydown', this.handleKeyDown, false);
}
componentDidUpdate(prevProps) {
@ -102,6 +125,7 @@ class ModalRoot extends React.PureComponent {
componentWillUnmount() {
window.removeEventListener('keyup', this.handleKeyUp);
window.removeEventListener('keydown', this.handleKeyDown);
}
getSiblings = () => {