UNPKG

@renderlesskit/react

Version:

Collection of headless components/hooks that are accessible, composable, customizable from low level to build your own UI & Design System powered by Reakit

64 lines (50 loc) 3.93 kB
"use strict"; function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); } Object.defineProperty(exports, "__esModule", { value: true }); exports.useHideOnClickOutside = useHideOnClickOutside; var React = _interopRequireWildcard(require("react")); var _getDocument = require("reakit-utils/getDocument"); var _useEventListenerOutside = require("./useEventListenerOutside"); function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); } function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; } function useMouseDownRef(dialogRef, options) { var mouseDownRef = React.useRef(); React.useEffect(function () { if (!options.visible) return undefined; if (!options.hideOnClickOutside) return undefined; var document = (0, _getDocument.getDocument)(dialogRef.current); var onMouseDown = function onMouseDown(event) { mouseDownRef.current = event.target; }; document.addEventListener("mousedown", onMouseDown); return function () { return document.removeEventListener("mousedown", onMouseDown); }; }, [options.visible, options.hideOnClickOutside, dialogRef]); return mouseDownRef; } function useHideOnClickOutside(dialogRef, disclosureRef, nestedDialogs, options) { var mouseDownRef = useMouseDownRef(dialogRef, options); (0, _useEventListenerOutside.useEventListenerOutside)(dialogRef, disclosureRef, nestedDialogs, "click", function (event) { // Make sure the element that has been clicked is the same that last // triggered the mousedown event. This prevents the dialog from closing // by dragging the cursor (for example, selecting some text inside the // dialog and releasing the mouse outside of it). if (mouseDownRef.current === event.target) { var _options$hide; (_options$hide = options.hide) === null || _options$hide === void 0 ? void 0 : _options$hide.call(options); } }, options.visible && options.hideOnClickOutside); (0, _useEventListenerOutside.useEventListenerOutside)(dialogRef, disclosureRef, nestedDialogs, "focusin", function (event) { var document = (0, _getDocument.getDocument)(dialogRef.current); // Fix for https://github.com/reakit/reakit/issues/619 // On IE11, calling element.blur() triggers the focus event on // document.body, so we make sure to ignore it as well. if (event.target !== document && event.target !== document.body) { var _options$hide2; (_options$hide2 = options.hide) === null || _options$hide2 === void 0 ? void 0 : _options$hide2.call(options); } }, options.visible && options.hideOnClickOutside); } //# sourceMappingURL=useHideOnClickOutside.js.map