@clayui/shared
Version:
ClayShared component
192 lines (154 loc) • 7.78 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.useFocusVisible = useFocusVisible;
exports.useInteractionFocus = useInteractionFocus;
var _react = require("react");
var _platform = require("./platform");
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e2) { throw _e2; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e3) { didErr = true; err = _e3; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
var currentInteraction = null;
var hasSetupGlobalListeners = false;
var hasEventBeforeFocus = false;
var hasBlurredWindowRecently = false;
var handlers = new Set();
function isValidKey(event) {
// Control and Shift keys trigger when navigating back to the tab with keyboard.
return !(event.metaKey || !(0, _platform.isMac)() && event.altKey || event.ctrlKey || event.key === 'Control' || event.key === 'Shift' || event.key === 'Meta');
}
function isVirtualClick(event) {
if (event.mozInputSource === 0 && event.isTrusted) {
return true;
}
return event.detail === 0 && !event.pointerType;
}
function isFocusVisible() {
return currentInteraction !== 'pointer';
}
function getInteraction() {
return currentInteraction;
}
function callHandlers(interaction) {
var _iterator = _createForOfIteratorHelper(handlers),
_step;
try {
for (_iterator.s(); !(_step = _iterator.n()).done;) {
var handler = _step.value;
handler(interaction);
}
} catch (err) {
_iterator.e(err);
} finally {
_iterator.f();
}
}
/**
* Detects what type of interaction the user is doing with the page, using the
* keyboard, pointer or using screen reader. This works like a singleton even
* if it is declared more than once.
*
* This is inspired by the implementation:
* https://github.com/adobe/react-spectrum/blob/d10f20a3f4ca7ffa807fcaceb944274da825a7b9/packages/%40react-aria/interactions/src/useFocusVisible.ts
*/
function useInteractionFocus() {
(0, _react.useEffect)(function () {
if (hasSetupGlobalListeners) {
return;
}
var onKeyboard = function onKeyboard(event) {
hasEventBeforeFocus = true;
if (isValidKey(event)) {
currentInteraction = 'keyboard';
callHandlers(currentInteraction);
}
};
var onClick = function onClick(event) {
if (isVirtualClick(event)) {
hasEventBeforeFocus = true;
currentInteraction = 'virtual';
callHandlers(currentInteraction);
}
};
var onFocus = function onFocus(event) {
if (event.target === window || event.target === document) {
return;
}
if (!hasEventBeforeFocus && !hasBlurredWindowRecently) {
currentInteraction = 'virtual';
callHandlers(currentInteraction);
}
hasEventBeforeFocus = false;
hasBlurredWindowRecently = false;
};
var onBlur = function onBlur() {
hasEventBeforeFocus = false;
hasBlurredWindowRecently = true;
};
var onPointer = function onPointer(event) {
currentInteraction = 'pointer';
if (event.type === 'mousedown' || event.type === 'pointerdown') {
hasEventBeforeFocus = true;
callHandlers(currentInteraction);
}
};
document.addEventListener('keydown', onKeyboard, true);
document.addEventListener('keyup', onKeyboard, true);
document.addEventListener('click', onClick, true);
window.addEventListener('focus', onFocus, true);
window.addEventListener('blur', onBlur, false);
if (typeof PointerEvent !== 'undefined') {
document.addEventListener('pointerdown', onPointer, true);
document.addEventListener('pointermove', onPointer, true);
document.addEventListener('pointerup', onPointer, true);
} else {
document.addEventListener('mousedown', onPointer, true);
document.addEventListener('mousemove', onPointer, true);
document.addEventListener('mouseup', onPointer, true);
}
hasSetupGlobalListeners = true;
return function () {
document.removeEventListener('keydown', onKeyboard, true);
document.removeEventListener('keyup', onKeyboard, true);
document.removeEventListener('click', onClick, true);
window.removeEventListener('focus', onFocus, true);
window.removeEventListener('blur', onBlur, false);
if (typeof PointerEvent !== 'undefined') {
document.removeEventListener('pointerdown', onPointer, true);
document.removeEventListener('pointermove', onPointer, true);
document.removeEventListener('pointerup', onPointer, true);
} else {
document.removeEventListener('mousedown', onPointer, true);
document.removeEventListener('mousemove', onPointer, true);
document.removeEventListener('mouseup', onPointer, true);
}
hasSetupGlobalListeners = false;
};
}, []);
return {
getInteraction: getInteraction,
isFocusVisible: isFocusVisible
};
}
function useFocusVisible() {
useInteractionFocus();
var _useState = (0, _react.useState)(isFocusVisible()),
_useState2 = _slicedToArray(_useState, 2),
interaction = _useState2[0],
setInteraction = _useState2[1];
(0, _react.useEffect)(function () {
var handler = function handler(interaction) {
setInteraction(interaction !== 'pointer');
};
handlers.add(handler);
return function () {
handlers.delete(handler);
};
}, []);
return interaction;
}