mirror of
https://github.com/idanoo/GoScrobble.git
synced 2024-11-23 00:45:16 +00:00
51 lines
1.4 KiB
JavaScript
51 lines
1.4 KiB
JavaScript
|
"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;
|
||
|
}
|