'use client' import type { PropsWithChildren, ReactElement, ReactNode, RefAttributes, } from 'react' import { Children, cloneElement, isValidElement } from 'react' import type { AnyProps } from 'rntwsc/components/slot/utils' import { isSlottable, mergeProps, SLOTTABLE_TYPE, } from 'rntwsc/components/slot/utils' // --------------------------------------------- // Slottable // --------------------------------------------- type SlottableProps = PropsWithChildren const SlottableUntyped = ({ children }: SlottableProps) => children const Slottable = Object.assign(SlottableUntyped, { $$typeof: SLOTTABLE_TYPE, }) // --------------------------------------------- // Slot // --------------------------------------------- type SlotProps = { children?: ReactNode [key: string]: any } const Slot = ({ ref, children, ...slotProps }: SlotProps) => { // -- Case 1: children contains a -------------------------- const childrenArray = Children.toArray(children) const slottableIndex = childrenArray.findIndex(isSlottable) if (slottableIndex !== -1) { const slottable = childrenArray[slottableIndex] as ReactElement<{ children: ReactNode }> // The "real" child lives inside const newSlottableChild = slottable.props.children // Build the final children: replace Slottable with the real child's children const newChildren = [ ...childrenArray.slice(0, slottableIndex), (newSlottableChild as any).props.children, ...childrenArray.slice(slottableIndex + 1), ] return ( {isValidElement(newSlottableChild) ? cloneElement(newSlottableChild, undefined, ...newChildren) : newChildren.length === 1 ? newChildren[0] : newChildren} ) } // -- Case 2: plain single child --------------------------------------- return ( {children} ) } Slot.displayName = 'Slot' // --------------------------------------------- // SlotClone - does the actual cloneElement // --------------------------------------------- type SlotCloneProps = { children: ReactNode [key: string]: unknown } const SlotClone = ({ ref, children, ...slotProps }: SlotCloneProps) => { if (!isValidElement(children)) { // Guard: if no valid child, render nothing (dev warning would go here) if (process.env.NODE_ENV !== 'production' && Children.count(children) > 1) { throw new Error('Slot expects a single React element as its child.') } return null } const childProps = children.props as AnyProps const merged = mergeProps(slotProps, childProps) // Attach ref if the child can hold one if (ref !== null) { merged.ref = ref } else if ((children as RefAttributes).ref) { merged.ref = (children as RefAttributes).ref } return cloneElement(children, merged) } SlotClone.displayName = 'SlotClone' // --------------------------------------------- // Exports // --------------------------------------------- export { Slot, Slottable } export type { SlotProps, SlottableProps }