Skip to content

Commit

Permalink
Get first & last Dialog children once, rather than each keydown
Browse files Browse the repository at this point in the history
  • Loading branch information
goodguyry committed Jan 11, 2021
1 parent a0efc33 commit 167cc70
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions src/Dialog/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,14 @@ export default class Dialog extends AriaComponent {
*/
setInteractiveChildren() {
this.interactiveChildElements = interactiveChildren(this.target);

const [
firstInteractiveChild,
lastInteractiveChild,
] = getFirstAndLastItems(this.interactiveChildElements);

// Save as instance properties.
Object.assign(this, { firstInteractiveChild, lastInteractiveChild });
}

/**
Expand Down Expand Up @@ -248,25 +256,21 @@ export default class Dialog extends AriaComponent {

if (expanded && keyCode === TAB) {
const { activeElement } = document;
const [
firstInteractiveChild,
lastInteractiveChild,
] = getFirstAndLastItems(this.interactiveChildElements);

if (shiftKey && firstInteractiveChild === activeElement) {
if (shiftKey && this.firstInteractiveChild === activeElement) {
event.preventDefault();
/*
* Move back from the first interactive child element to the last
* interactive child element
*/
lastInteractiveChild.focus();
} else if (! shiftKey && lastInteractiveChild === activeElement) {
this.lastInteractiveChild.focus();
} else if (! shiftKey && this.lastInteractiveChild === activeElement) {
event.preventDefault();
/*
* Move forward from the last interactive child element to the first
* interactive child element.
*/
firstInteractiveChild.focus();
this.firstInteractiveChild.focus();
}
}
}
Expand Down

0 comments on commit 167cc70

Please sign in to comment.