@prop-styles/core
Version:
The library provides a static method createPropStyles to create Style objects.
8 lines (7 loc) • 4.39 kB
JavaScript
/*!
* @prop-styles/core version 1.0.3-beta
* Author: Capricorncd<capricorncd@qq.com>
* Homepage: https://github.com/capricorncd/prop-styles#readme
* Released on: 2025-11-09 10:56:17 (GMT+0000)
*/
(function(d,l){typeof exports=="object"&&typeof module<"u"?l(exports):typeof define=="function"&&define.amd?define(["exports"],l):(d=typeof globalThis<"u"?globalThis:d||self,l(d["prop-styles-core"]={}))})(this,(function(d){"use strict";const l={xs:640,sm:768,md:1024,lg:1280,xl:1536,xxl:1920},m=t=>{if(!t)return;const r=Object.entries(t===!0?l:t).sort((o,s)=>o[1]-s[1]);if(!(typeof window>"u"||typeof window.matchMedia!="function"))for(const[o,s]of r)try{if(window.matchMedia(`(min-width: ${s}px)`).matches)return o}catch{}};function u(t){return typeof t=="string"?/^-?\d+(\.\d+)?$/.test(t):b(t)}function b(t){return Number.isFinite(t)}const y=/^((-?\d+(\.\d+)?)?([a-z]+|%)?\s*)+$/i,w=/(\d+\s|\s-?\d+(\.\d+)?$)/;function c(t,e="px"){if(u(t))return`${t}${e}`;if(t&&typeof t=="string"){const r=t.trim().replace(/\s{2,}/g," ");return y.test(r)&&w.test(r)?r.trim().split(" ").map(o=>c(o,e)).join(" "):r}return""}function x(t){return t.replace(/^-?[1-9]\d{0,2}(,\d{3})+/,e=>e.replace(/,/g,""))}function h(t,e=!1,r){if(typeof e=="number"&&(r=e,e=!1),typeof r!="number"&&(r=0),b(t))return t;if(typeof t=="string"){if(!e&&/^(-?\d+(?:\.\d+)?)\D*/.test(x(t)))return h(RegExp.$1,!0);const o=Number(t);return isNaN(o)?r:o}return r}const S=/^(#[a-f0-9]{3,8}|(rgba?|hsla?|hwb|lab|lch|oklab|oklch|hwb|lch|light-dark)\(.+\))$/i,k=/^--\w+(-\w+)*$/,R=/(\s)*--\w+(-\w+)*/;function P(t){return typeof t!="string"?!1:S.test(t)}const j=t=>t?R.test(t)?t.split(/\s+/g).map(e=>k.test(e)?`var(${e})`:e).join(" "):t:"",n=(t,e,r)=>!e&&e!==0?null:{key:t,value:j(r??String(e))},_=(t,e)=>{const r={};let o;for(const s of Object.keys(t)){if(o=e[s],!o)continue;const f=o(t[s],t);if(f)if(Array.isArray(f))f.forEach(i=>{i&&(r[i.key]=i.value)});else{const{key:i,value:p}=f;r[i]=p}}return r},A=/^-?\d+(\.\d+)?[a-z]+$/i,C=t=>typeof t=="string"&&A.test(t),g=(t,e)=>{const r=e?`border${e}`:"border";return u(t)||C(t)?{key:`${r}Width`,value:c(t)}:P(t)?{key:`${r}Color`,value:t}:n(r,t)},O={display:"display",width:"width",minWidth:"minWidth",maxWidth:"maxWidth",height:"height",minHeight:"minHeight",maxHeight:"maxHeight",gap:"gap",fd:"flexDirection",ai:"alignItems",ac:"alignContent",ji:"justifyItems",jc:"justifyContent",ws:"whiteSpace",p:"padding",pt:"paddingTop",pr:"paddingRight",pb:"paddingBottom",pl:"paddingLeft",px:"paddingInline",py:"paddingBlock",m:"margin",mt:"marginTop",mr:"marginRight",mb:"marginBottom",ml:"marginLeft",mx:"marginInline",my:"marginBlock",radius:"borderRadius",fs:"fontSize",lh:"lineHeight",color:"color",bg:"background",bgs:"backgroundSize",bgo:"backgroundOrigin",fw:"fontWeight",border:"border",borderTop:"borderTop",borderRight:"borderRight",borderBottom:"borderBottom",borderLeft:"borderLeft",gtc:"gridTemplateColumns",gtr:"gridTemplateRows",ta:"textAlign",position:"position",top:"top",right:"right",bottom:"bottom",left:"left",zIndex:"zIndex",inset:"inset",transform:"transform",cursor:"cursor",whiteSpace:"whiteSpace",ratio:"aspectRatio"},T={column:t=>n("flexDirection",t,"column"),wrap:t=>n("flexWrap",t,t===!0?"wrap":String(t)),breakWord:t=>n("overflowWrap",t,"break-word"),scroll:t=>n(`overflow${typeof t=="string"?t.toUpperCase():""}`,t,"auto"),shadow:t=>n("boxShadow",t,t===!0?"0 2px 4px 0 rgba(0, 0, 0, 0.1)":String(t)),flex:t=>t===!0?n("display","flex"):n("flex",t),inline:t=>n("display",t,"inline"),nowrap:t=>n("whiteSpace",t,"nowrap")},$={...Object.entries(O).reduce((t,[e,r])=>(t[e]=o=>n(r,o,c(o)),t),{}),...T,border:t=>g(t),borderTop:t=>g(t,"Top"),borderRight:t=>g(t,"Right"),borderBottom:t=>g(t,"Bottom"),borderLeft:t=>g(t,"Left"),fw:t=>n("fontWeight",t),gtc:t=>n("gridTemplateColumns",t,c(t,"fr")),gtr:t=>n("gridTemplateRows",t,c(t,"fr")),zIndex:t=>n("zIndex",t),lh:t=>n("lineHeight",t,u(t)&&h(t)<8?String(t):c(t))},L=(t,e,r={})=>{const o={...$,...e},s=r.breakpoint??m(r.breakpoints),f=(()=>{const i={};for(const p of Object.keys(t)){const a=t[p];a!==null&&typeof a=="object"&&!Array.isArray(a)?s&&Object.prototype.hasOwnProperty.call(a,s)?i[p]=a[s]:Object.prototype.hasOwnProperty.call(a,"default")?i[p]=a.default:i[p]=a:i[p]=a}return i})();return _(f,o)};d.createPropStyles=L,d.getDefaultBreakpoint=m,d.transform=n,Object.defineProperty(d,Symbol.toStringTag,{value:"Module"})}));