GoScrobble/web/node_modules/react-overlays/cjs/useWaitForDOMRef.js

51 lines
1.4 KiB
JavaScript
Raw Normal View History

2022-04-25 02:47:15 +00:00
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports["default"] = useWaitForDOMRef;
exports.resolveContainerRef = void 0;
var _ownerDocument = _interopRequireDefault(require("dom-helpers/ownerDocument"));
var _react = require("react");
var resolveContainerRef = function resolveContainerRef(ref) {
var _ref;
if (typeof document === 'undefined') return null;
if (ref == null) return (0, _ownerDocument["default"])().body;
if (typeof ref === 'function') ref = ref();
if (ref && 'current' in ref) ref = ref.current;
if ((_ref = ref) != null && _ref.nodeType) return ref || null;
return null;
};
exports.resolveContainerRef = resolveContainerRef;
function useWaitForDOMRef(ref, onResolved) {
var _useState = (0, _react.useState)(function () {
return resolveContainerRef(ref);
}),
resolvedRef = _useState[0],
setRef = _useState[1];
if (!resolvedRef) {
var earlyRef = resolveContainerRef(ref);
if (earlyRef) setRef(earlyRef);
}
(0, _react.useEffect)(function () {
if (onResolved && resolvedRef) {
onResolved(resolvedRef);
}
}, [onResolved, resolvedRef]);
(0, _react.useEffect)(function () {
var nextRef = resolveContainerRef(ref);
if (nextRef !== resolvedRef) {
setRef(nextRef);
}
}, [ref, resolvedRef]);
return resolvedRef;
}