52 lines
1.7 KiB
JavaScript
52 lines
1.7 KiB
JavaScript
// weak
|
|
|
|
import keycode from 'keycode';
|
|
import warning from 'warning';
|
|
import contains from 'dom-helpers/query/contains';
|
|
import ownerDocument from 'dom-helpers/ownerDocument';
|
|
|
|
const internal = {
|
|
focusKeyPressed: false
|
|
};
|
|
|
|
export function focusKeyPressed(pressed) {
|
|
if (typeof pressed !== 'undefined') {
|
|
internal.focusKeyPressed = Boolean(pressed);
|
|
}
|
|
|
|
return internal.focusKeyPressed;
|
|
}
|
|
|
|
export function detectKeyboardFocus(instance, element, callback, attempt = 1) {
|
|
process.env.NODE_ENV !== "production" ? warning(instance.keyboardFocusCheckTime, 'Material-UI: missing instance.keyboardFocusCheckTime') : void 0;
|
|
process.env.NODE_ENV !== "production" ? warning(instance.keyboardFocusMaxCheckTimes, 'Material-UI: missing instance.keyboardFocusMaxCheckTimes') : void 0;
|
|
|
|
instance.keyboardFocusTimeout = setTimeout(() => {
|
|
const doc = ownerDocument(element);
|
|
|
|
if (focusKeyPressed() && (doc.activeElement === element || contains(element, doc.activeElement))) {
|
|
callback();
|
|
} else if (attempt < instance.keyboardFocusMaxCheckTimes) {
|
|
detectKeyboardFocus(instance, element, callback, attempt + 1);
|
|
}
|
|
}, instance.keyboardFocusCheckTime);
|
|
}
|
|
|
|
const FOCUS_KEYS = ['tab', 'enter', 'space', 'esc', 'up', 'down', 'left', 'right'];
|
|
|
|
function isFocusKey(event) {
|
|
return FOCUS_KEYS.indexOf(keycode(event)) !== -1;
|
|
}
|
|
|
|
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);
|
|
} |