Skip to content

Commit

Permalink
Fix isHidden function for elements with position: fixed (#52)
Browse files Browse the repository at this point in the history
Also, add FirefoxHeadless to browsers test array.

Co-authored-by: Stefan Cameron <stefan@stefcameron.com>
  • Loading branch information
shamsartem and stefcameron committed Sep 8, 2020
1 parent f454973 commit 8a25135
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/breezy-spiders-kiss.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'tabbable': patch
---

Fixed: Tabbable elements in fixed-position (`position: fixed`) containers should now be consistently found in supported browsers.
2 changes: 1 addition & 1 deletion karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ module.exports = function (config) {
colors: true,
logLevel: config.LOG_INFO,
autoWatch: true,
browsers: ['ChromeHeadless'],
browsers: ['ChromeHeadless', 'FirefoxHeadless'],
singleRun: true,
concurrency: Infinity,
});
Expand Down
13 changes: 8 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,11 +189,14 @@ function isTabbableRadio(node) {
}

function isHidden(node) {
// offsetParent being null will allow detecting cases where an element is invisible or inside an invisible element,
// as long as the element does not use position: fixed. For them, their visibility has to be checked directly as well.
return (
node.offsetParent === null || getComputedStyle(node).visibility === 'hidden'
);
if (getComputedStyle(node).visibility === 'hidden') return true;

while (node) {
if (getComputedStyle(node).display === 'none') return true;
node = node.parentElement;
}

return false;
}

export { tabbable, focusable, isTabbable, isFocusable };
2 changes: 2 additions & 0 deletions test/fixtures/jqueryui.html
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@
<span tabindex="1" id="displayNoneAncestor-span">.</span>
</div>

<button id="positionFixedButton" style="position: fixed;"></button>

<div id="visibilityHiddenAncestor" style="visibility: hidden;">
<input id="visibilityHiddenAncestor-input" />
<span tabindex="1" id="visibilityHiddenAncestor-span">.</span>
Expand Down
2 changes: 2 additions & 0 deletions test/tabbable.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ describe('tabbable', () => {
'visibleAncestor-select',
'visibleAncestor-textarea',
'visibleAncestor-anchorWithHref',
'positionFixedButton',
'inputTabindex0',
'spanTabindex0',
'dimensionlessParent',
Expand Down Expand Up @@ -303,6 +304,7 @@ describe('tabbable', () => {
'inputTabindex-50',
'spanTabindex0',
'spanTabindex10',
'positionFixedButton',
'spanTabindex-1',
'spanTabindex-50',
'visibleAncestor-inputTypeNone',
Expand Down

0 comments on commit 8a25135

Please sign in to comment.