@clayui/modal
Version:
ClayModal component
87 lines (83 loc) • 4.35 kB
JavaScript
;
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.useUserInteractions = void 0;
var _shared = require("@clayui/shared");
var _react = _interopRequireWildcard(require("react"));
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof(e) && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) { if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } } return n.default = e, t && t.set(e, n), n; }
/**
* SPDX-FileCopyrightText: © 2019 Liferay, Inc. <https://liferay.com>
* SPDX-License-Identifier: BSD-3-Clause
*/
/**
* A hook that takes care of controlling click, keyup and keydown events
* respectively close the modal after a click on the overlay, close the
* modal by pressing the ESC key and control the focus within the Modal.
*/
var useUserInteractions = exports.useUserInteractions = function useUserInteractions(modalElementRef, modalBodyElementRef, onClick, show, content) {
var mouseEventTargetRef = _react.default.useRef(null);
var getFocusableNodes = function getFocusableNodes() {
if (modalBodyElementRef.current) {
var nodes = modalBodyElementRef.current.querySelectorAll(_shared.FOCUSABLE_ELEMENTS);
return Object.keys(nodes).map(function (key) {
return nodes[key];
});
}
return [];
};
var handleKeydown = function handleKeydown(event) {
if (event.key === _shared.Keys.Esc && _shared.stack[_shared.stack.length - 1] === modalElementRef) {
onClick();
}
if (event.key === _shared.Keys.Tab) {
if (modalElementRef.current && event.target !== null && !modalElementRef.current.contains(event.target)) {
modalBodyElementRef.current.focus();
} else {
var focusableNodes = getFocusableNodes();
var focusedItemIndex = focusableNodes.indexOf(document.activeElement);
if (event.shiftKey && focusedItemIndex === 0) {
focusableNodes[focusableNodes.length - 1].focus();
event.preventDefault();
}
if (!event.shiftKey && focusedItemIndex === focusableNodes.length - 1) {
focusableNodes[0].focus();
event.preventDefault();
}
}
}
};
var handleDocumentMouseDown = function handleDocumentMouseDown(event) {
// We keep the `event.target` to check later in the click event if
// the target is the same, otherwise, we are assuming that the element
// has been removed from the DOM.
mouseEventTargetRef.current = event.target;
};
var handleDocumentMouseUp = function handleDocumentMouseUp(event) {
if (event.defaultPrevented) {
mouseEventTargetRef.current = null;
return;
}
if (event.target === modalElementRef.current && mouseEventTargetRef.current === event.target) {
mouseEventTargetRef.current = null;
onClick();
}
mouseEventTargetRef.current = null;
};
/**
* Just listen for keyup, keydown, and click when
* changeAttachEvent is true.
*/
(0, _react.useEffect)(function () {
document.addEventListener('keydown', handleKeydown);
document.addEventListener('mousedown', handleDocumentMouseDown);
document.addEventListener('mouseup', handleDocumentMouseUp);
return function () {
document.removeEventListener('keydown', handleKeydown);
document.removeEventListener('mousedown', handleDocumentMouseDown);
document.removeEventListener('mouseup', handleDocumentMouseUp);
};
}, [show, content]);
};