Skip to content

Commit

Permalink
[fixed] ensuring usage of Web Components functions in all browsers (S…
Browse files Browse the repository at this point in the history
…afari, specifically)
  • Loading branch information
stevematney authored and diasbruno committed Nov 10, 2021
1 parent b049feb commit f5783ca
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
15 changes: 11 additions & 4 deletions src/helpers/scopeTab.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import findTabbable from "./tabbable";

function getActiveElement(el = document) {
return el.activeElement.shadowRoot
? getActiveElement(el.activeElement.shadowRoot)
: el.activeElement;
}

export default function scopeTab(node, event) {
const tabbable = findTabbable(node);

Expand All @@ -14,19 +20,20 @@ export default function scopeTab(node, event) {
const shiftKey = event.shiftKey;
const head = tabbable[0];
const tail = tabbable[tabbable.length - 1];
const activeElement = getActiveElement();

// proceed with default browser behavior on tab.
// Focus on last element on shift + tab.
if (node === document.activeElement) {
if (node === activeElement) {
if (!shiftKey) return;
target = tail;
}

if (tail === document.activeElement && !shiftKey) {
if (tail === activeElement && !shiftKey) {
target = head;
}

if (head === document.activeElement && shiftKey) {
if (head === activeElement && shiftKey) {
target = tail;
}

Expand Down Expand Up @@ -57,7 +64,7 @@ export default function scopeTab(node, event) {
// the focus
if (!isSafariDesktop) return;

var x = tabbable.indexOf(document.activeElement);
var x = tabbable.indexOf(activeElement);

if (x > -1) {
x += shiftKey ? -1 : 1;
Expand Down
17 changes: 15 additions & 2 deletions src/helpers/tabbable.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ function visible(element) {
let rootNode = element.getRootNode && element.getRootNode();
while (parentElement) {
if (parentElement === document.body) break;
if (rootNode && parentElement === rootNode) parentElement = rootNode.host;

// if we are not hidden yet, skip to checking outside the Web Component
if (rootNode && parentElement === rootNode)
parentElement = rootNode.host.parentNode;

if (hidesContents(parentElement)) return false;
parentElement = parentElement.parentNode;
}
Expand All @@ -61,5 +65,14 @@ function tabbable(element) {
}

export default function findTabbableDescendants(element) {
return [].slice.call(element.querySelectorAll("*"), 0).filter(tabbable);
const descendants = [].slice
.call(element.querySelectorAll("*"), 0)
.reduce(
(finished, el) => [
...finished,
...(!el.shadowRoot ? [el] : findTabbableDescendants(el.shadowRoot))
],
[]
);
return descendants.filter(tabbable);
}

0 comments on commit f5783ca

Please sign in to comment.