UEA-PRODEM
This commit is contained in:
+69
-51
@@ -1,77 +1,78 @@
|
||||
// src/slot.tsx
|
||||
import * as React from "react";
|
||||
import { composeRefs } from "@radix-ui/react-compose-refs";
|
||||
import { Fragment as Fragment2, jsx } from "react/jsx-runtime";
|
||||
var REACT_LAZY_TYPE = Symbol.for("react.lazy");
|
||||
var use = React[" use ".trim().toString()];
|
||||
function isPromiseLike(value) {
|
||||
return typeof value === "object" && value !== null && "then" in value;
|
||||
}
|
||||
function isLazyComponent(element) {
|
||||
return element != null && typeof element === "object" && "$$typeof" in element && element.$$typeof === REACT_LAZY_TYPE && "_payload" in element && isPromiseLike(element._payload);
|
||||
}
|
||||
import { useComposedRefs } from "@radix-ui/react-compose-refs";
|
||||
// @__NO_SIDE_EFFECTS__
|
||||
function createSlot(ownerName) {
|
||||
const SlotClone = /* @__PURE__ */ createSlotClone(ownerName);
|
||||
const Slot2 = React.forwardRef((props, forwardedRef) => {
|
||||
let { children, ...slotProps } = props;
|
||||
let slottableElement = null;
|
||||
let hasSlottable = false;
|
||||
const newChildren = [];
|
||||
if (isLazyComponent(children) && typeof use === "function") {
|
||||
children = use(children._payload);
|
||||
}
|
||||
const childrenArray = React.Children.toArray(children);
|
||||
const slottable = childrenArray.find(isSlottable);
|
||||
if (slottable) {
|
||||
const newElement = slottable.props.children;
|
||||
const newChildren = childrenArray.map((child) => {
|
||||
if (child === slottable) {
|
||||
if (React.Children.count(newElement) > 1) return React.Children.only(null);
|
||||
return React.isValidElement(newElement) ? newElement.props.children : null;
|
||||
} else {
|
||||
return child;
|
||||
React.Children.forEach(children, (maybeSlottable) => {
|
||||
if (isSlottable(maybeSlottable)) {
|
||||
hasSlottable = true;
|
||||
const slottable = maybeSlottable;
|
||||
let child = "child" in slottable.props ? slottable.props.child : slottable.props.children;
|
||||
if (isLazyComponent(child) && typeof use === "function") {
|
||||
child = use(child._payload);
|
||||
}
|
||||
});
|
||||
return /* @__PURE__ */ jsx(SlotClone, { ...slotProps, ref: forwardedRef, children: React.isValidElement(newElement) ? React.cloneElement(newElement, void 0, newChildren) : null });
|
||||
slottableElement = getSlottableElementFromSlottable(slottable, child);
|
||||
newChildren.push(slottableElement?.props?.children);
|
||||
} else {
|
||||
newChildren.push(maybeSlottable);
|
||||
}
|
||||
});
|
||||
if (slottableElement) {
|
||||
slottableElement = React.cloneElement(slottableElement, void 0, newChildren);
|
||||
} else if (
|
||||
// A `Slottable` was found but it didn't resolve to a single element (e.g.
|
||||
// it wrapped multiple elements, text, or a render-prop `child` that
|
||||
// wasn't an element). Don't fall back to treating the `Slottable` wrapper
|
||||
// itself as the slot target — throw a descriptive error below instead.
|
||||
!hasSlottable && React.Children.count(children) === 1 && React.isValidElement(children)
|
||||
) {
|
||||
slottableElement = children;
|
||||
}
|
||||
return /* @__PURE__ */ jsx(SlotClone, { ...slotProps, ref: forwardedRef, children });
|
||||
const slottableElementRef = slottableElement ? getElementRef(slottableElement) : void 0;
|
||||
const composedRef = useComposedRefs(forwardedRef, slottableElementRef);
|
||||
if (!slottableElement) {
|
||||
if (children || children === 0) {
|
||||
throw new Error(
|
||||
hasSlottable ? createSlottableError(ownerName) : createSlotError(ownerName)
|
||||
);
|
||||
}
|
||||
return children;
|
||||
}
|
||||
const mergedProps = mergeProps(slotProps, slottableElement.props ?? {});
|
||||
if (slottableElement.type !== React.Fragment) {
|
||||
mergedProps.ref = forwardedRef ? composedRef : slottableElementRef;
|
||||
}
|
||||
return React.cloneElement(slottableElement, mergedProps);
|
||||
});
|
||||
Slot2.displayName = `${ownerName}.Slot`;
|
||||
return Slot2;
|
||||
}
|
||||
var Slot = /* @__PURE__ */ createSlot("Slot");
|
||||
// @__NO_SIDE_EFFECTS__
|
||||
function createSlotClone(ownerName) {
|
||||
const SlotClone = React.forwardRef((props, forwardedRef) => {
|
||||
let { children, ...slotProps } = props;
|
||||
if (isLazyComponent(children) && typeof use === "function") {
|
||||
children = use(children._payload);
|
||||
}
|
||||
if (React.isValidElement(children)) {
|
||||
const childrenRef = getElementRef(children);
|
||||
const props2 = mergeProps(slotProps, children.props);
|
||||
if (children.type !== React.Fragment) {
|
||||
props2.ref = forwardedRef ? composeRefs(forwardedRef, childrenRef) : childrenRef;
|
||||
}
|
||||
return React.cloneElement(children, props2);
|
||||
}
|
||||
return React.Children.count(children) > 1 ? React.Children.only(null) : null;
|
||||
});
|
||||
SlotClone.displayName = `${ownerName}.SlotClone`;
|
||||
return SlotClone;
|
||||
}
|
||||
var SLOTTABLE_IDENTIFIER = Symbol("radix.slottable");
|
||||
var SLOTTABLE_IDENTIFIER = Symbol.for("radix.slottable");
|
||||
// @__NO_SIDE_EFFECTS__
|
||||
function createSlottable(ownerName) {
|
||||
const Slottable2 = ({ children }) => {
|
||||
return /* @__PURE__ */ jsx(Fragment2, { children });
|
||||
};
|
||||
const Slottable2 = (props) => "child" in props ? props.children(props.child) : props.children;
|
||||
Slottable2.displayName = `${ownerName}.Slottable`;
|
||||
Slottable2.__radixId = SLOTTABLE_IDENTIFIER;
|
||||
return Slottable2;
|
||||
}
|
||||
var Slottable = /* @__PURE__ */ createSlottable("Slottable");
|
||||
function isSlottable(child) {
|
||||
return React.isValidElement(child) && typeof child.type === "function" && "__radixId" in child.type && child.type.__radixId === SLOTTABLE_IDENTIFIER;
|
||||
}
|
||||
var getSlottableElementFromSlottable = (slottable, child) => {
|
||||
if ("child" in slottable.props) {
|
||||
const child2 = slottable.props.child;
|
||||
if (!React.isValidElement(child2)) return null;
|
||||
return React.cloneElement(child2, void 0, slottable.props.children(child2.props.children));
|
||||
}
|
||||
return React.isValidElement(child) ? child : null;
|
||||
};
|
||||
function mergeProps(slotProps, childProps) {
|
||||
const overrideProps = { ...childProps };
|
||||
for (const propName in childProps) {
|
||||
@@ -109,6 +110,23 @@ function getElementRef(element) {
|
||||
}
|
||||
return element.props.ref || element.ref;
|
||||
}
|
||||
function isSlottable(child) {
|
||||
return React.isValidElement(child) && typeof child.type === "function" && "__radixId" in child.type && child.type.__radixId === SLOTTABLE_IDENTIFIER;
|
||||
}
|
||||
var REACT_LAZY_TYPE = Symbol.for("react.lazy");
|
||||
function isLazyComponent(element) {
|
||||
return element != null && typeof element === "object" && "$$typeof" in element && element.$$typeof === REACT_LAZY_TYPE && "_payload" in element && isPromiseLike(element._payload);
|
||||
}
|
||||
function isPromiseLike(value) {
|
||||
return typeof value === "object" && value !== null && "then" in value;
|
||||
}
|
||||
var createSlotError = (ownerName) => {
|
||||
return `${ownerName} failed to slot onto its children. Expected a single React element child or \`Slottable\`.`;
|
||||
};
|
||||
var createSlottableError = (ownerName) => {
|
||||
return `${ownerName} failed to slot onto its \`Slottable\`. Expected \`Slottable\` to receive a single React element child.`;
|
||||
};
|
||||
var use = React[" use ".trim().toString()];
|
||||
export {
|
||||
Slot as Root,
|
||||
Slot,
|
||||
|
||||
Reference in New Issue
Block a user