UNPKG

@kopexa/react-utils

Version:

A set of utilities for react on client side

42 lines (39 loc) 972 B
"use client"; // src/dom.ts import { Children, cloneElement } from "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 = Children.only(children); return cloneElement(firstChild, { // @ts-expect-error children: typeof content === "function" ? ( // @ts-expect-error content(firstChild.props.children) ) : content }); } export { mergeRefs, getSubtree };