UNPKG

@loke/ui

Version:
2 lines (1 loc) 3.65 kB
import{composeRefs}from"@loke/ui/compose-refs";import React from"react";import{jsx,Fragment}from"react/jsx-runtime";var REACT_LAZY_TYPE=Symbol.for("react.lazy"),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)}function createSlot(ownerName){let SlotClone=createSlotClone(ownerName),Slot=React.forwardRef((props,forwardedRef)=>{let{children,...slotProps}=props;if(isLazyComponent(children)&&typeof use==="function")children=use(children._payload);let childrenArray=React.Children.toArray(children),slottable=childrenArray.find(isSlottable);if(slottable){let newElement=slottable.props.children,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}return child});return jsx(SlotClone,{...slotProps,ref:forwardedRef,children:React.isValidElement(newElement)?React.cloneElement(newElement,void 0,newChildren):null})}return jsx(SlotClone,{...slotProps,ref:forwardedRef,children})});return Slot.displayName=`${ownerName}.Slot`,Slot}var Slot=createSlot("Slot");function createSlotClone(ownerName){let SlotClone=React.forwardRef((props,forwardedRef)=>{let{children,...slotProps}=props;if(isLazyComponent(children)&&typeof use==="function")children=use(children._payload);if(React.isValidElement(children)){let childrenRef=getElementRef(children),mergedProps=mergeProps(slotProps,children.props);if(children.type!==React.Fragment)mergedProps.ref=forwardedRef?composeRefs(forwardedRef,childrenRef):childrenRef;return React.cloneElement(children,mergedProps)}return React.Children.count(children)>1?React.Children.only(null):null});return SlotClone.displayName=`${ownerName}.SlotClone`,SlotClone}var SLOTTABLE_IDENTIFIER=Symbol("loke.slottable");function createSlottable(ownerName){let Slottable=({children})=>{return jsx(Fragment,{children})};return Slottable.displayName=`${ownerName}.Slottable`,Slottable.__lokeId=SLOTTABLE_IDENTIFIER,Slottable}var Slottable=createSlottable("Slottable"),HANDLER_REGEX=/^on[A-Z]/;function isSlottable(child){return React.isValidElement(child)&&typeof child.type==="function"&&"__lokeId"in child.type&&child.type.__lokeId===SLOTTABLE_IDENTIFIER}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(slotPropValue&&childPropValue)overrideProps[propName]=(...args)=>{let result=childPropValue(...args);return slotPropValue(...args),result};else if(slotPropValue)overrideProps[propName]=slotPropValue}else if(propName==="style")overrideProps[propName]={...slotPropValue,...childPropValue};else if(propName==="className")overrideProps[propName]=[slotPropValue,childPropValue].filter(Boolean).join(" ")}return{...slotProps,...overrideProps}}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}export{createSlottable,createSlot,Slottable,Slot,Slot as Root};