UNPKG

3.13 kBJavaScriptView Raw
1"use strict";
2
3var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
4Object.defineProperty(exports, "__esModule", {
5 value: true
6});
7exports.useComposeRef = exports.supportRef = exports.supportNodeRef = exports.getNodeRef = exports.fillRef = exports.composeRef = void 0;
8var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof"));
9var _react = require("react");
10var _reactIs = require("react-is");
11var _useMemo = _interopRequireDefault(require("./hooks/useMemo"));
12var fillRef = exports.fillRef = function fillRef(ref, node) {
13 if (typeof ref === 'function') {
14 ref(node);
15 } else if ((0, _typeof2.default)(ref) === 'object' && ref && 'current' in ref) {
16 ref.current = node;
17 }
18};
19
20/**
21 * Merge refs into one ref function to support ref passing.
22 */
23var composeRef = exports.composeRef = function composeRef() {
24 for (var _len = arguments.length, refs = new Array(_len), _key = 0; _key < _len; _key++) {
25 refs[_key] = arguments[_key];
26 }
27 var refList = refs.filter(Boolean);
28 if (refList.length <= 1) {
29 return refList[0];
30 }
31 return function (node) {
32 refs.forEach(function (ref) {
33 fillRef(ref, node);
34 });
35 };
36};
37var useComposeRef = exports.useComposeRef = function useComposeRef() {
38 for (var _len2 = arguments.length, refs = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
39 refs[_key2] = arguments[_key2];
40 }
41 return (0, _useMemo.default)(function () {
42 return composeRef.apply(void 0, refs);
43 }, refs, function (prev, next) {
44 return prev.length !== next.length || prev.every(function (ref, i) {
45 return ref !== next[i];
46 });
47 });
48};
49var supportRef = exports.supportRef = function supportRef(nodeOrComponent) {
50 var _type$prototype, _nodeOrComponent$prot;
51 var type = (0, _reactIs.isMemo)(nodeOrComponent) ? nodeOrComponent.type.type : nodeOrComponent.type;
52
53 // Function component node
54 if (typeof type === 'function' && !((_type$prototype = type.prototype) !== null && _type$prototype !== void 0 && _type$prototype.render) && type.$$typeof !== _reactIs.ForwardRef) {
55 return false;
56 }
57
58 // Class component
59 if (typeof nodeOrComponent === 'function' && !((_nodeOrComponent$prot = nodeOrComponent.prototype) !== null && _nodeOrComponent$prot !== void 0 && _nodeOrComponent$prot.render) && nodeOrComponent.$$typeof !== _reactIs.ForwardRef) {
60 return false;
61 }
62 return true;
63};
64function isReactElement(node) {
65 return /*#__PURE__*/(0, _react.isValidElement)(node) && !(0, _reactIs.isFragment)(node);
66}
67var supportNodeRef = exports.supportNodeRef = function supportNodeRef(node) {
68 return isReactElement(node) && supportRef(node);
69};
70
71/**
72 * In React 19. `ref` is not a property from node.
73 * But a property from `props.ref`.
74 * To check if `props.ref` exist or fallback to `ref`.
75 */
76var getNodeRef = exports.getNodeRef = Number(_react.version.split('.')[0]) >= 19 ?
77// >= React 19
78function (node) {
79 if (isReactElement(node)) {
80 return node.props.ref;
81 }
82 return null;
83} :
84// < React 19
85function (node) {
86 if (isReactElement(node)) {
87 return node.ref;
88 }
89 return null;
90};
\No newline at end of file