1 line
3.3 KiB
JSON
1 line
3.3 KiB
JSON
|
{"ast":null,"code":"import contains from 'dom-helpers/contains';\nimport listen from 'dom-helpers/listen';\nimport { useCallback, useEffect, useRef } from 'react';\nimport useEventCallback from '@restart/hooks/useEventCallback';\nimport warning from 'warning';\nvar escapeKeyCode = 27;\n\nvar noop = function noop() {};\n\nfunction isLeftClickEvent(event) {\n return event.button === 0;\n}\n\nfunction isModifiedEvent(event) {\n return !!(event.metaKey || event.altKey || event.ctrlKey || event.shiftKey);\n}\n/**\n * The `useRootClose` hook registers your callback on the document\n * when rendered. Powers the `<Overlay/>` component. This is used achieve modal\n * style behavior where your callback is triggered when the user tries to\n * interact with the rest of the document or hits the `esc` key.\n *\n * @param {Ref<HTMLElement>|HTMLElement} ref The element boundary\n * @param {function} onRootClose\n * @param {object} options\n * @param {boolean} options.disabled\n * @param {string} options.clickTrigger The DOM event name (click, mousedown, etc) to attach listeners on\n */\n\n\nfunction useRootClose(ref, onRootClose, _temp) {\n var _ref = _temp === void 0 ? {} : _temp,\n disabled = _ref.disabled,\n _ref$clickTrigger = _ref.clickTrigger,\n clickTrigger = _ref$clickTrigger === void 0 ? 'click' : _ref$clickTrigger;\n\n var preventMouseRootCloseRef = useRef(false);\n var onClose = onRootClose || noop;\n var handleMouseCapture = useCallback(function (e) {\n var currentTarget = ref && ('current' in ref ? ref.current : ref);\n warning(!!currentTarget, 'RootClose captured a close event but does not have a ref to compare it to. ' + 'useRootClose(), should be passed a ref that resolves to a DOM node');\n preventMouseRootCloseRef.current = !currentTarget || isModifiedEvent(e) || !isLeftClickEvent(e) || contains(currentTarget, e.target);\n }, [ref]);\n var handleMouse = useEventCallback(function (e) {\n if (!preventMouseRootCloseRef.current) {\n onClose(e);\n }\n });\n var handleKeyUp = useEventCallback(function (e) {\n if (e.keyCode === escapeKeyCode) {\n onClose(e);\n }\n });\n useEffect(function () {\n if (disabled || ref == null) return undefined; // Use capture for this listener so it fires before React's listener, to\n // avoid false positives in the contains() check below if the target DOM\n // element is removed in the React mouse callback.\n\n var removeMouseCaptureListener = listen(document, clickTrigger, handleMouseCapture, true);\n var removeMouseListener = listen(document, clickTrigger, handleMouse);\n var removeKeyupListener = listen(document, 'keyup', handleKeyUp);\n var mobileSafariHackListeners = [];\n\n if ('ontouchstart' in document.documentElement) {\n mobileSafariHackListeners = [].slice.call(document.body.children).map(function (el) {\n return listen(el, 'mousemove', noop);\n });\n }\n\n return function () {\n removeMouseCaptureListener();\n removeMouseListener();\n removeKeyupListener();\n mobileSafariHackListeners.forEach(function (remove) {\n return remove();\n });\n };\n }, [ref, disabled, clickTrigger, handleMouseCapture, handleMouse, handleKeyUp]);\n}\n\nexport default useRootClose;","map":null,"metadata":{},"sourceType":"module"}
|