@patryk_walach/tailwind-variants
Version:
🦄 Tailwindcss first-class variant API
13 lines (9 loc) • 5.15 kB
JavaScript
;
var tailwindMerge = require('tailwind-merge');
var G=e=>typeof e=="boolean"?`${e}`:e===0?"0":e,g=e=>!e||typeof e!="object"||Object.keys(e).length===0,Q=(e,l)=>JSON.stringify(e)===JSON.stringify(l);function X(e,l){e.forEach(function(t){Array.isArray(t)?X(t,l):l.push(t);});}function q(e){let l=[];return X(e,l),l}var J=(...e)=>q(e).filter(Boolean),$=(e,l)=>{let t={},O=Object.keys(e),j=Object.keys(l);for(let m of O)if(j.includes(m)){let A=e[m],h=l[m];Array.isArray(A)||Array.isArray(h)?t[m]=J(h,A):typeof A=="object"&&typeof h=="object"?t[m]=$(A,h):t[m]=h+" "+A;}else t[m]=e[m];for(let m of j)O.includes(m)||(t[m]=l[m]);return t},R=e=>!e||typeof e!="string"?e:e.replace(/\s+/g," ").trim();var ce={twMerge:!0,twMergeConfig:{},responsiveVariants:!1},Z=e=>e||void 0,S=(...e)=>Z(q(e).filter(Boolean).join(" ")),F=null,C={},U=!1,M=(...e)=>l=>l.twMerge?((!F||U)&&(U=!1,F=g(C)?tailwindMerge.twMerge:tailwindMerge.extendTailwindMerge({...C,extend:{theme:C.theme,classGroups:C.classGroups,conflictingClassGroupModifiers:C.conflictingClassGroupModifiers,conflictingClassGroups:C.conflictingClassGroups,...C.extend}})),Z(F(S(e)))):S(e),Y=(e,l)=>{for(let t in l)e.hasOwnProperty(t)?e[t]=S(e[t],l[t]):e[t]=l[t];return e},oe=(e,l)=>{let{extend:t=null,slots:O={},variants:j={},compoundVariants:m=[],compoundSlots:A=[],defaultVariants:h={}}=e,d={...ce,...l},p=t!=null&&t.base?S(t.base,e==null?void 0:e.base):e==null?void 0:e.base,b=t!=null&&t.variants&&!g(t.variants)?$(j,t.variants):j,k=t!=null&&t.defaultVariants&&!g(t.defaultVariants)?{...t.defaultVariants,...h}:h;!g(d.twMergeConfig)&&!Q(d.twMergeConfig,C)&&(U=!0,C=d.twMergeConfig);let E=g(t==null?void 0:t.slots),T=g(O)?{}:{base:S(e==null?void 0:e.base,E&&(t==null?void 0:t.base)),...O},N=E?T:Y({...t==null?void 0:t.slots},g(T)?{base:e==null?void 0:e.base}:T),w=g(t==null?void 0:t.compoundVariants)?m:J(t==null?void 0:t.compoundVariants,m),v=f=>{if(g(b)&&g(O)&&E)return M(p,f==null?void 0:f.class,f==null?void 0:f.className)(d);if(w&&!Array.isArray(w))throw new TypeError(`The "compoundVariants" prop must be an array. Received: ${typeof w}`);if(A&&!Array.isArray(A))throw new TypeError(`The "compoundSlots" prop must be an array. Received: ${typeof A}`);let K=(n,a,r=[],i)=>{let s=r;if(typeof a=="string")s=s.concat(R(a).split(" ").map(o=>`${n}:${o}`));else if(Array.isArray(a))s=s.concat(a.reduce((o,c)=>o.concat(`${n}:${c}`),[]));else if(typeof a=="object"&&typeof i=="string"){for(let o in a)if(a.hasOwnProperty(o)&&o===i){let c=a[o];if(c&&typeof c=="string"){let u=R(c);s[i]?s[i]=s[i].concat(u.split(" ").map(y=>`${n}:${y}`)):s[i]=u.split(" ").map(y=>`${n}:${y}`);}else Array.isArray(c)&&c.length>0&&(s[i]=c.reduce((u,y)=>u.concat(`${n}:${y}`),[]));}}return s},W=(n,a=b,r=null,i=null)=>{var I;let s=a[n];if(!s||g(s))return null;let o=(I=i==null?void 0:i[n])!=null?I:f==null?void 0:f[n];if(o===null)return null;let c=G(o),u=Array.isArray(d.responsiveVariants)&&d.responsiveVariants.length>0||d.responsiveVariants===!0,y=k==null?void 0:k[n],V=[];if(typeof c=="object"&&u)for(let[x,L]of Object.entries(c)){let ae=s[L];if(x==="initial"){y=L;continue}Array.isArray(d.responsiveVariants)&&!d.responsiveVariants.includes(x)||(V=K(x,ae,V,r));}let re=c!=null&&typeof c!="object"?c:G(y),B=s[re||"false"];return typeof V=="object"&&typeof r=="string"&&V[r]?Y(V,B):V.length>0?(V.push(B),V):B},P=()=>b?Object.keys(b).map(n=>W(n,b)):null,ee=(n,a)=>{if(!b||typeof b!="object")return null;let r=new Array;for(let i in b){let s=W(i,b,n,a),o=n==="base"&&typeof s=="string"?s:s&&s[n];o&&(r[r.length]=o);}return r},z={};for(let n in f)f[n]!==void 0&&(z[n]=f[n]);let D=(n,a)=>{var i;let r=typeof(f==null?void 0:f[n])=="object"?{[n]:(i=f[n])==null?void 0:i.initial}:{};return {...k,...z,...r,...a}},H=(n=[],a)=>{let r=[];for(let{class:i,className:s,...o}of n){let c=!0;for(let[u,y]of Object.entries(o)){let V=D(u,a);if(Array.isArray(y)){if(!y.includes(V[u])){c=!1;break}}else if(V[u]!==y){c=!1;break}}c&&(i&&r.push(i),s&&r.push(s));}return r},te=n=>{let a=H(w,n);if(!Array.isArray(a))return a;let r={};for(let i of a)if(typeof i=="string"&&(r.base=M(r.base,i)(d)),typeof i=="object")for(let[s,o]of Object.entries(i))r[s]=M(r[s],o)(d);return r},ne=n=>{if(A.length<1)return null;let a={};for(let{slots:r=[],class:i,className:s,...o}of A){if(!g(o)){let c=!0;for(let u of Object.keys(o)){let y=D(u,n)[u];if(y===void 0||(Array.isArray(o[u])?!o[u].includes(y):o[u]!==y)){c=!1;break}}if(!c)continue}for(let c of r)a[c]=a[c]||[],a[c].push([i,s]);}return a};if(!g(O)||!E){let n={};if(typeof N=="object"&&!g(N))for(let a of Object.keys(N))n[a]=r=>{var i,s;return M(N[a],ee(a,r),((i=te(r))!=null?i:[])[a],((s=ne(r))!=null?s:[])[a],r==null?void 0:r.class,r==null?void 0:r.className)(d)};return n}return M(p,P(),H(w),f==null?void 0:f.class,f==null?void 0:f.className)(d)},_=()=>{if(!(!b||typeof b!="object"))return Object.keys(b)};return v.variantKeys=_(),v.extend=t,v.base=p,v.slots=N,v.variants=b,v.defaultVariants=k,v.compoundSlots=A,v.compoundVariants=w,v},ye=e=>(l,t)=>oe(l,t?$(e,t):e);
exports.cn = M;
exports.cnBase = S;
exports.createTV = ye;
exports.defaultConfig = ce;
exports.tv = oe;
exports.voidEmpty = Z;