@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.
75 lines (74 loc) • 3.12 kB
JavaScript
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';
import { AsInstance } from './as-instance';
/**
* A component that implements slot-based composition, allowing children to be rendered
* within an outlet component or render function.
*
* @template Props - The type of props that can be passed to the outlet
* @param {PropsWithChildren<AsSlotProps<Props>>} props - Component properties
* @param {React.ReactNode} props.children - The content to be rendered inside the outlet
* @param {React.ReactNode | RenderFunction<Props>} [props.outlet] - The wrapper component or render function
* @param {ObjectProps} [props.outletProps] - Additional props to pass to the outlet component
* @param {...ObjectProps} props.rest - Additional props passed to children via AsInstance
* @returns {JSX.Element} The composed component hierarchy
*
* @example
* // Basic usage with an outlet component
* <AsSlot outlet={<Card />}>
* <p>This content will be rendered inside the Card</p>
* </AsSlot>
*
* @example
* // With outlet props
* <AsSlot
* outlet={<Panel />}
* outletProps={{ title: "User Settings", elevated: true }}
* >
* <UserPreferences />
* </AsSlot>
*
* @example
* // With a render function
* <AsSlot
* outlet={(props) => <Dialog open={isOpen} {...props} />}
* className="dialog-content"
* >
* <h2>Confirm Action</h2>
* <p>Are you sure you want to continue?</p>
* </AsSlot>
*/
export function AsSlot(props) {
var children = props.children, outlet = props.outlet, outletProps = props.outletProps, rest = __rest(props, ["children", "outlet", "outletProps"]);
if (typeof outlet === 'function') {
return _jsx(_Fragment, { children: outlet(__assign(__assign({}, rest), { children: children })) });
}
if (!isValidElement(outlet)) {
return _jsx(_Fragment, { children: children });
}
var _a = outlet, olProps = _a.props, ref = _a.ref;
var newOutletProps = __assign(__assign(__assign({}, olProps), outletProps), { ref: ref });
return cloneElement(outlet, __assign(__assign({}, newOutletProps), { children: (_jsx(AsInstance, __assign({}, rest, { ref: ref, children: children }))) }));
}