@isilin/react-slot
Version:
Simple and powerful typed slots for React components
2 lines • 1.02 kB
JavaScript
function m(t){return typeof t=="function"?t.name||"Component":typeof t=="object"&&t!==null&&"displayName"in t&&t.displayName||"Component"}import{pascalCase as f}from"change-case";function T(t,a){let{slots:r,extras:i,rules:s}=a,p=m(t),e=l=>t(l);e.slots=r,s&&(e.rules=a.rules);for(let[l,o]of Object.entries(r)){let n=f(l);o.displayName||=`${p}.${n}`,e[n]=o}return i&&Object.assign(e,i),e}import c from"react";function N(t,a){let r=a.slots,i=a.rules??{},s={},p=[];for(let e in r)i?.[e]==="multiple"&&(s[e]=[]);return c.Children.forEach(t,e=>{if(!c.isValidElement(e)){p.push(e);return}let l=e.type,o=Object.entries(r).find(([,n])=>l===n||l.displayName===n.displayName)?.[0];o?(i?.[o]??"single")==="multiple"?s[o].push(e):(s[o]&&process.env.NODE_ENV!=="production"&&console.warn(`Slot "${String(o)}" was provided multiple times but is declared as single. Only the last one will be used.`),s[o]=e):p.push(e)}),{...s,others:p}}export{T as defineSlotComponent,m as getDisplayName,N as getSlots};
//# sourceMappingURL=index.js.map