UNPKG

@ffsm/compositor

Version:

A collection of declarative React utility components for simplified component composition, conditional rendering, and prop management - making React UI development more maintainable and expressive.

62 lines (61 loc) 2.4 kB
var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; import { Fragment as _Fragment, jsx as _jsx } from "react/jsx-runtime"; import { cloneElement, isValidElement, } from 'react'; /** * Clones a React element and merges additional props with its existing props. * If the child is not a valid React element, it's returned unchanged. * * @param {PropsWithChildren<AsInstanceProps>} props - Component properties * @param {React.ReactNode} props.children - Child element to enhance with additional props * @param {...unknown} props.rest - Additional props to merge with the child element * @returns {JSX.Element} Enhanced React element with merged props or unchanged children * * @example * // Basic usage - add className to a button * <AsInstance className="primary-btn"> * <button>Submit</button> * </AsInstance> * * @example * // Merge with existing props * <AsInstance disabled={true}> * <button className="btn" onClick={handleClick}>Cancel</button> * </AsInstance> * // Result: <button className="btn" onClick={handleClick} disabled={true}>Cancel</button> * * @example * // Non-element children pass through unchanged * <AsInstance className="wrapper"> * Just some text * </AsInstance> * // Result: Just some text */ export function AsInstance(props) { var children = props.children, rest = __rest(props, ["children"]); if (!isValidElement(children)) { return _jsx(_Fragment, { children: children }); } var passedProps = __assign(__assign({}, children.props), rest); return cloneElement(children, passedProps); }