@kopexa/react-utils
Version:
A set of utilities for react on client side
63 lines (61 loc) • 2.02 kB
JavaScript
"use client";
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/dom.ts
var dom_exports = {};
__export(dom_exports, {
getSubtree: () => getSubtree,
mergeRefs: () => mergeRefs
});
module.exports = __toCommonJS(dom_exports);
var import_react = require("react");
function mergeRefs(...inputRefs) {
const filteredInputRefs = inputRefs.filter(Boolean);
if (filteredInputRefs.length <= 1) {
const firstRef = filteredInputRefs[0];
return firstRef || null;
}
return function mergedRefs(ref) {
for (const inputRef of filteredInputRefs) {
if (typeof inputRef === "function") {
inputRef(ref);
} else if (inputRef) {
inputRef.current = ref;
}
}
};
}
function getSubtree(options, content) {
const { asChild, children } = options;
if (!asChild)
return typeof content === "function" ? content(children) : content;
const firstChild = import_react.Children.only(children);
return (0, import_react.cloneElement)(firstChild, {
// @ts-expect-error
children: typeof content === "function" ? (
// @ts-expect-error
content(firstChild.props.children)
) : content
});
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
getSubtree,
mergeRefs
});