UNPKG

@loke/ui

Version:
126 lines (125 loc) • 5.8 kB
// src/components/slot/slot.tsx import { useComposedRefs } from "@loke/ui/compose-refs"; import React from "react"; function createSlot(ownerName) { let Slot = React.forwardRef((props, forwardedRef) => { let { children, ...slotProps } = props, slottableElement = null, hasSlottable = !1, newChildren = []; if (isLazyComponent(children) && typeof use === "function") children = use(children._payload); if (React.Children.forEach(children, (maybeSlottable) => { if (isSlottable(maybeSlottable)) { hasSlottable = !0; let slottable = maybeSlottable, child = "child" in slottable.props ? slottable.props.child : slottable.props.children; if (isLazyComponent(child) && typeof use === "function") child = use(child._payload); slottableElement = getSlottableElementFromSlottable(slottable, child), newChildren.push(slottableElement?.props.children); } else newChildren.push(maybeSlottable); }), slottableElement) slottableElement = React.cloneElement(slottableElement, void 0, newChildren); else if (!hasSlottable && React.Children.count(children) === 1 && React.isValidElement(children)) slottableElement = children; let slottableElementRef = slottableElement ? getElementRef(slottableElement) : void 0, composedRef = useComposedRefs(forwardedRef, slottableElementRef); if (!slottableElement) { if (children || children === 0) throw Error(hasSlottable ? createSlottableError(ownerName) : createSlotError(ownerName)); return children; } let mergedProps = mergeProps(slotProps, slottableElement.props); if (slottableElement.type !== React.Fragment) mergedProps.ref = forwardedRef ? composedRef : slottableElementRef; return React.cloneElement(slottableElement, mergedProps); }); return Slot.displayName = `${ownerName}.Slot`, Slot; } var Slot = createSlot("Slot"), SLOTTABLE_IDENTIFIER = Symbol.for("loke.slottable"); function createSlottable(ownerName) { let Slottable = (props) => ("child" in props) ? props.children(props.child) : props.children; return Slottable.displayName = `${ownerName}.Slottable`, Slottable.__lokeId = SLOTTABLE_IDENTIFIER, Slottable; } var Slottable = createSlottable("Slottable"); function getSlottableElementFromSlottable(slottable, child) { if ("child" in slottable.props) { let renderChild = slottable.props.child; if (!React.isValidElement(renderChild)) return null; return React.cloneElement(renderChild, void 0, slottable.props.children(renderChild.props.children)); } return React.isValidElement(child) ? child : null; } var HANDLER_REGEX = /^on[A-Z]/; function isEventHandler(value) { return typeof value === "function"; } function mergeProps(slotProps, childProps) { let overrideProps = { ...childProps }; for (let propName in childProps) { if (!Object.hasOwn(childProps, propName)) continue; let slotPropValue = slotProps[propName], childPropValue = childProps[propName]; if (HANDLER_REGEX.test(propName)) { if (isEventHandler(slotPropValue) && isEventHandler(childPropValue)) overrideProps[propName] = (...args) => { let result = childPropValue(...args); return slotPropValue(...args), result; }; else if (isEventHandler(slotPropValue)) overrideProps[propName] = slotPropValue; } else if (propName === "style") overrideProps[propName] = { ...isRecord(slotPropValue) ? slotPropValue : {}, ...isRecord(childPropValue) ? childPropValue : {} }; else if (propName === "className") overrideProps[propName] = [slotPropValue, childPropValue].filter(Boolean).join(" "); else if (propName === "aria-describedby" && slotPropValue !== void 0) overrideProps[propName] = concatAriaDescribedby(childPropValue, slotPropValue); } return { ...slotProps, ...overrideProps }; } function isRecord(value) { return typeof value === "object" && value !== null; } function getElementRef(element) { let getter = Object.getOwnPropertyDescriptor(element.props, "ref")?.get, mayWarn = getter && "isReactWarning" in getter && getter.isReactWarning; if (mayWarn) return element.ref; if (getter = Object.getOwnPropertyDescriptor(element, "ref")?.get, mayWarn = getter && "isReactWarning" in getter && getter.isReactWarning, mayWarn) return element.props.ref; return element.props.ref || element.ref; } function isSlottable(child) { return React.isValidElement(child) && typeof child.type === "function" && "__lokeId" in child.type && child.type.__lokeId === 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 ARIA_ID_SEPARATOR = /\s+/, concatAriaDescribedby = (...values) => { let ids = /* @__PURE__ */ new Set; for (let value of values) { if (typeof value !== "string") continue; for (let id of value.trim().split(ARIA_ID_SEPARATOR)) if (id) ids.add(id); } return ids.size > 0 ? Array.from(ids).join(" ") : void 0; }; function createSlotError(ownerName) { return `${ownerName} failed to slot onto its children. Expected a single React element child or \`Slottable\`.`; } function 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, Slottable, createSlot, createSlottable };