1 | "use strict";
|
2 |
|
3 | var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
4 |
|
5 | exports.__esModule = true;
|
6 | exports["default"] = useWaitForDOMRef;
|
7 | exports.resolveContainerRef = void 0;
|
8 |
|
9 | var _ownerDocument = _interopRequireDefault(require("dom-helpers/ownerDocument"));
|
10 |
|
11 | var _react = require("react");
|
12 |
|
13 | var resolveContainerRef = function resolveContainerRef(ref) {
|
14 | var _ref;
|
15 |
|
16 | if (typeof document === 'undefined') return null;
|
17 | if (ref == null) return (0, _ownerDocument["default"])().body;
|
18 | if (typeof ref === 'function') ref = ref();
|
19 | if (ref && 'current' in ref) ref = ref.current;
|
20 | if ((_ref = ref) != null && _ref.nodeType) return ref || null;
|
21 | return null;
|
22 | };
|
23 |
|
24 | exports.resolveContainerRef = resolveContainerRef;
|
25 |
|
26 | function useWaitForDOMRef(ref, onResolved) {
|
27 | var _useState = (0, _react.useState)(function () {
|
28 | return resolveContainerRef(ref);
|
29 | }),
|
30 | resolvedRef = _useState[0],
|
31 | setRef = _useState[1];
|
32 |
|
33 | if (!resolvedRef) {
|
34 | var earlyRef = resolveContainerRef(ref);
|
35 | if (earlyRef) setRef(earlyRef);
|
36 | }
|
37 |
|
38 | (0, _react.useEffect)(function () {
|
39 | if (onResolved && resolvedRef) {
|
40 | onResolved(resolvedRef);
|
41 | }
|
42 | }, [onResolved, resolvedRef]);
|
43 | (0, _react.useEffect)(function () {
|
44 | var nextRef = resolveContainerRef(ref);
|
45 |
|
46 | if (nextRef !== resolvedRef) {
|
47 | setRef(nextRef);
|
48 | }
|
49 | }, [ref, resolvedRef]);
|
50 | return resolvedRef;
|
51 | } |
\ | No newline at end of file |