@meonode/ui
Version:
A structured approach to component composition, direct CSS-first prop styling, built-in theming, smart prop handling (including raw property pass-through), and dynamic children.
2 lines (1 loc) • 2.92 kB
JavaScript
import{getValueByPath as e}from"../helper/common.helper.js";class t{constructor(){}static parseFlexShorthand(e){if(null==e)return null;if("number"==typeof e)return{grow:e,shrink:1,basis:"0%"};if("string"!=typeof e)return null;const t=e.trim().toLowerCase();if(!t)return null;switch(t){case"none":return{grow:0,shrink:0,basis:"auto"};case"auto":return{grow:1,shrink:1,basis:"auto"};case"initial":return{grow:0,shrink:1,basis:"auto"};default:return null}}static isPlainObject=e=>{if("object"!=typeof e||null===e)return!1;const t=Object.getPrototypeOf(e);return null===t||t===Object.prototype};static resolveObjWithTheme=(r,n,l={})=>{const{processFunctions:s=!1,themeStringsMode:o="resolve"}=l;if(!n||!n.system||"object"!=typeof n.system||0===Object.keys(n.system).length||!r||0===Object.keys(r).length)return r;const i=n.system,a=[{value:r,isProcessed:!1}],c=new Map,f=new Set,u=/theme\.([a-zA-Z0-9_.-]+)/g,p=(t,r)=>{u.lastIndex=0;let n=!1;const l=t.replace(u,(t,l)=>{if(r)return n=!0,`var(${s=l,`--meonode-theme-${s.replace(/[^\w.-]/g,"-").replace(/\./g,"-")}`})`;var s;const o=e(i,l);if(null!=o){if(n=!0,"object"==typeof o){if(!Array.isArray(o)&&"default"in o)return o.default;throw new Error("The provided theme path is invalid!")}return o}return t});return n?l:t};for(;a.length>0;){const e=a[a.length-1],r=e.value;if(t.isPlainObject(r)||Array.isArray(r))if(c.has(r))a.pop();else if(e.isProcessed){a.pop(),f.delete(r);let e=r;if(Array.isArray(r)){let t=null;for(let e=0;e<r.length;e++){const n=r[e],l=c.get(n)??n;l!==n&&(null===t&&(t=[...r]),t[e]=l)}null!==t&&(e=t)}else{let t=null;for(const e in r)if(Object.prototype.hasOwnProperty.call(r,e)){const l=r[e];let i=c.get(l)??l,a=e;"string"==typeof e&&e.includes("theme.")&&(a=p(e,!1));const f="vars"===o;if("function"==typeof i&&s){const e=i(n);i="string"==typeof e&&e.includes("theme.")?p(e,f):e}else"string"==typeof i&&i.includes("theme.")&&(i=p(i,f));i===l&&a===e||(null===t&&(t={...r}),a!==e&&delete t[e],t[a]=i)}null!==t&&(e=t)}c.set(r,e)}else{e.isProcessed=!0,f.add(r);const n=Array.isArray(r)?r:Object.values(r);for(let e=n.length-1;e>=0;e--){const r=n[e];!t.isPlainObject(r)&&!Array.isArray(r)||f.has(r)||a.push({value:r,isProcessed:!1})}}else a.pop()}return c.get(r)??r};static resolveDefaultStyle=e=>{if(null==e||"string"==typeof e||"number"==typeof e||"boolean"==typeof e)return{};const{flex:r,...n}=e,l="flex"===n.display||"inline-flex"===n.display,s=!!(n.overflow||n.overflowY||n.overflowX),o=n.flexFlow?.includes("wrap")||"wrap"===n.flexWrap||"wrap-reverse"===n.flexWrap,i="flexShrink"in e&&void 0!==e.flexShrink,a=r?t.parseFlexShorthand(r):null;let c;if(!i)if(a)c=a.shrink;else if(l){if(!s){const e="column"===n.flexDirection||"column-reverse"===n.flexDirection,t="row"===n.flexDirection||"row-reverse"===n.flexDirection||!n.flexDirection;(e&&!o||t&&!o)&&(c=0)}}else c=0;return{flex:r,flexShrink:c,minHeight:0,minWidth:0,...n}}}export{t as ThemeUtil};