tw-clamp-css
Version:
TailwindCSS v4.0 compatible CSS clamp()
2 lines (1 loc) • 2.03 kB
JavaScript
function z(i,n){return{handler:i,config:n}}z.withOptions=function(i,n=()=>({})){function s(e){return{handler:i(e),config:n(e)}}return s.__isOptionsFunction=!0,s};var V=z;function f(i,n,s,e){let a=Number.parseFloat(i),t=Number.parseFloat(n),o=Number.parseFloat(s),l=Number.parseFloat(e),u=(t-a)/(l-o),r=Number(a.toFixed(3)),d=u>=0?`${r}rem + ${u.toFixed(4)} * (100vw - ${o}rem)`:`${r}rem - ${Math.abs(u).toFixed(4)} * (100vw - ${o}rem)`,m=Number(Math.min(a,t).toFixed(3)),c=Number(Math.max(a,t).toFixed(3));return`clamp(${m}rem, calc(${d}), ${c}rem)`}var R=V(function({matchUtilities:i,theme:n}){i({clamp:s=>{let e=s.split("-");if(e.length<3)return{};let a=e[0]==="";a&&e.shift();let t=e[0],o=e[1],l=e[2];if(e[3]!=="clamp")return{};let r=n("fontSize"),d=n("spacing"),m=n("screens"),c=e[4]?m[e[4]]:m.sm,x=e[5]?m[e[5]]:m.xl;if(t==="text"){let g=r[o][0],S=r[l][0],N=r[o][1].lineHeight,H=r[l][1].lineHeight,b=parseFloat(g),y=parseFloat(S),v=b*parseFloat(N),k=y*parseFloat(H),B=f(b,y,parseFloat(c),parseFloat(x)),M=f(v,k,parseFloat(c),parseFloat(x));return{"font-size":B,"line-height":M}}let h={p:"padding",px:"padding-left",py:"padding-top",pt:"padding-top",pr:"padding-right",pb:"padding-bottom",pl:"padding-left",m:"margin",mx:"margin-left",my:"margin-top",mt:"margin-top",mr:"margin-right",mb:"margin-bottom",ml:"margin-left",gap:"gap","gap-x":"column-gap","gap-y":"row-gap",w:"width",h:"height","min-w":"min-width","min-h":"min-height","max-w":"max-width","max-h":"max-height",inset:"inset","inset-x":"left","inset-y":"top",top:"top",right:"right",bottom:"bottom",left:"left",size:"size"}[t];if(!h)return{};let w=d[o],$=d[l],F=f(parseFloat(w),parseFloat($),parseFloat(c),parseFloat(x)),p=a?`-${F}`:F;if(t==="size")return{width:p,height:p};if(t==="px"||t==="mx"||t==="inset-x"){let g=t==="px"?"padding-right":t==="mx"?"margin-right":"right";return{[h]:p,[g]:p}}if(t==="py"||t==="my"||t==="inset-y"){let g=t==="py"?"padding-bottom":t==="my"?"margin-bottom":"bottom";return{[h]:p,[g]:p}}return{[h]:p}}})}),_=R;export{_ as default};