UNPKG

@clayui/shared

Version:
159 lines (157 loc) 7.65 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.useFocusVisible = useFocusVisible; exports.useInteractionFocus = useInteractionFocus; var _react = require("react"); var _platform = require("./platform"); function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _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(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0) { ; } } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } } function _arrayWithHoles(r) { if (Array.isArray(r)) return r; } function _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, 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 o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t.return || t.return(); } finally { if (u) throw o; } } }; } function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } } function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) { n[e] = r[e]; } return n; } /** * SPDX-FileCopyrightText: © 2019 Liferay, Inc. <https://liferay.com> * SPDX-License-Identifier: BSD-3-Clause */ 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; }