Completely updated React, fixed #11, (hopefully)

This commit is contained in:
2018-03-04 19:11:49 -05:00
parent 6e0afd6e2a
commit 34e5f5139a
13674 changed files with 333464 additions and 473223 deletions

View File

@@ -3,10 +3,9 @@
import keycode from 'keycode';
import warning from 'warning';
import contains from 'dom-helpers/query/contains';
import addEventListener from '../utils/addEventListener';
import ownerDocument from 'dom-helpers/ownerDocument';
const internal = {
listening: false,
focusKeyPressed: false,
};
@@ -26,9 +25,11 @@ export function detectKeyboardFocus(instance, element, callback, attempt = 1) {
);
instance.keyboardFocusTimeout = setTimeout(() => {
const doc = ownerDocument(element);
if (
focusKeyPressed() &&
(document.activeElement === element || contains(element, document.activeElement))
(doc.activeElement === element || contains(element, doc.activeElement))
) {
callback();
} else if (attempt < instance.keyboardFocusMaxCheckTimes) {
@@ -43,15 +44,15 @@ function isFocusKey(event) {
return FOCUS_KEYS.indexOf(keycode(event)) !== -1;
}
export function listenForFocusKeys() {
// It's a singleton, we only need to listen once.
// Also, this logic is client side only, we don't need a teardown.
if (!internal.listening) {
addEventListener(window, 'keyup', event => {
if (isFocusKey(event)) {
internal.focusKeyPressed = true;
}
});
internal.listening = true;
const handleKeyUpEvent = event => {
if (isFocusKey(event)) {
internal.focusKeyPressed = true;
}
};
export function listenForFocusKeys(win) {
// The event listener will only be added once per window.
// Duplicate event listeners will be ignored by addEventListener.
// Also, this logic is client side only, we don't need a teardown.
win.addEventListener('keyup', handleKeyUpEvent);
}