Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
feat: upgrade focus-trap-js to support focus on radio elements (#447)
  • Loading branch information
pradel committed Nov 8, 2020
1 parent cf4b6b3 commit d51f8e0
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
4 changes: 2 additions & 2 deletions package.json
Expand Up @@ -55,11 +55,11 @@
"size-limit": [
{
"path": "dist/react-responsive-modal.cjs.production.min.js",
"limit": "3.1 KB"
"limit": "3.3 KB"
},
{
"path": "dist/react-responsive-modal.esm.js",
"limit": "3.1 KB"
"limit": "3.3 KB"
}
],
"dependencies": {
Expand Down
31 changes: 29 additions & 2 deletions src/lib/focusTrapJs.ts
@@ -1,4 +1,4 @@
// https://github.com/alexandrzavalii/focus-trap-js/blob/master/src/index.js v1.0.9
// https://github.com/alexandrzavalii/focus-trap-js/blob/master/src/index.js v1.1.0

export const candidateSelectors = [
'input',
Expand All @@ -20,12 +20,39 @@ function isHidden(node: any) {
);
}

function getCheckedRadio(nodes: any, form: any) {
for (var i = 0; i < nodes.length; i++) {
if (nodes[i].checked && nodes[i].form === form) {
return nodes[i];
}
}
}

function isNotRadioOrTabbableRadio(node: any) {
if (node.tagName !== 'INPUT' || node.type !== 'radio' || !node.name) {
return true;
}
var radioScope = node.form || node.ownerDocument;
var radioSet = radioScope.querySelectorAll(
'input[type="radio"][name="' + node.name + '"]'
);
var checked = getCheckedRadio(radioSet, node.form);
return checked === node || (checked === undefined && radioSet[0] === node);
}

export function getAllTabbingElements(parentElem: any) {
var currentActiveElement = document.activeElement;
var tabbableNodes = parentElem.querySelectorAll(candidateSelectors.join(','));
var onlyTabbable = [];
for (var i = 0; i < tabbableNodes.length; i++) {
var node = tabbableNodes[i];
if (!node.disabled && getTabindex(node) > -1 && !isHidden(node)) {
if (
currentActiveElement === node ||
(!node.disabled &&
getTabindex(node) > -1 &&
!isHidden(node) &&
isNotRadioOrTabbableRadio(node))
) {
onlyTabbable.push(node);
}
}
Expand Down

0 comments on commit d51f8e0

Please sign in to comment.