UNPKG

@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.93 kB
"use strict";var e=require("../helper/common.helper.cjs");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,l,s={})=>{const{processFunctions:n=!1,themeStringsMode:i="resolve"}=s;if(!l||!l.system||"object"!=typeof l.system||0===Object.keys(l.system).length||!r||0===Object.keys(r).length)return r;const o=l.system,a=[{value:r,isProcessed:!1}],c=new Map,u=new Set,f=/theme\.([a-zA-Z0-9_.-]+)/g,p=(t,r)=>{f.lastIndex=0;let l=!1;const s=t.replace(f,(t,s)=>{if(r)return l=!0,`var(${n=s,`--meonode-theme-${n.replace(/[^\w.-]/g,"-").replace(/\./g,"-")}`})`;var n;const i=e.getValueByPath(o,s);if(null!=i){if(l=!0,"object"==typeof i){if(!Array.isArray(i)&&"default"in i)return i.default;throw new Error("The provided theme path is invalid!")}return i}return t});return l?s: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(),u.delete(r);let e=r;if(Array.isArray(r)){let t=null;for(let e=0;e<r.length;e++){const l=r[e],s=c.get(l)??l;s!==l&&(null===t&&(t=[...r]),t[e]=s)}null!==t&&(e=t)}else{let t=null;for(const e in r)if(Object.prototype.hasOwnProperty.call(r,e)){const s=r[e];let o=c.get(s)??s,a=e;"string"==typeof e&&e.includes("theme.")&&(a=p(e,!1));const u="vars"===i;if("function"==typeof o&&n){const e=o(l);o="string"==typeof e&&e.includes("theme.")?p(e,u):e}else"string"==typeof o&&o.includes("theme.")&&(o=p(o,u));o===s&&a===e||(null===t&&(t={...r}),a!==e&&delete t[e],t[a]=o)}null!==t&&(e=t)}c.set(r,e)}else{e.isProcessed=!0,u.add(r);const l=Array.isArray(r)?r:Object.values(r);for(let e=l.length-1;e>=0;e--){const r=l[e];!t.isPlainObject(r)&&!Array.isArray(r)||u.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,...l}=e,s="flex"===l.display||"inline-flex"===l.display,n=!!(l.overflow||l.overflowY||l.overflowX),i=l.flexFlow?.includes("wrap")||"wrap"===l.flexWrap||"wrap-reverse"===l.flexWrap,o="flexShrink"in e&&void 0!==e.flexShrink,a=r?t.parseFlexShorthand(r):null;let c;if(!o)if(a)c=a.shrink;else if(s){if(!n){const e="column"===l.flexDirection||"column-reverse"===l.flexDirection,t="row"===l.flexDirection||"row-reverse"===l.flexDirection||!l.flexDirection;(e&&!i||t&&!i)&&(c=0)}}else c=0;return{flex:r,flexShrink:c,minHeight:0,minWidth:0,...l}}}exports.ThemeUtil=t;