Files
front-ui-registry/.tooling/lib/as-child.tsx

13 lines
412 B
TypeScript

import { cloneElement, isValidElement, type HTMLAttributes, type ReactNode } from "react";
export function renderAsChild(
asChild: boolean | undefined,
children: ReactNode,
props: HTMLAttributes<HTMLElement>
) {
if (asChild && isValidElement<HTMLAttributes<HTMLElement>>(children)) {
return cloneElement(children, { ...props, ...children.props });
}
return <span {...props}>{children}</span>;
}