@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) • 3.37 kB
JavaScript
import{getGlobalState as e}from"../helper/common.helper.js";const t=Symbol.for("@meonode/ui/serverActiveTheme"),o=Symbol.for("@meonode/ui/serverThemeVariables");function r(){return e(t,()=>({})).activeTheme}function n(o){e(t,()=>({})).activeTheme=o}function s(e){return`--meonode-theme-${e.replace(/[^\w.-]/g,"-").replace(/\./g,"-")}`}function l(){return e(o,()=>({byName:new Map}))}function a(e){if(!e?.system||"object"!=typeof e.system)return;const t=l(),o=[{path:"",value:e.system}];for(;o.length>0;){const e=o.pop();if(!e||!e.value||"object"!=typeof e.value)continue;const r=e.value;for(const[n,l]of Object.entries(r)){const r=e.path?`${e.path}.${n}`:n;if(null!=l)if("string"!=typeof l&&"number"!=typeof l&&"boolean"!=typeof l){if("object"==typeof l){const e=l.default;"string"!=typeof e&&"number"!=typeof e&&"boolean"!=typeof e||t.byName.set(s(r),String(e)),o.push({path:r,value:l})}}else t.byName.set(s(r),String(l))}}}function i(){const e=l();if(0===e.byName.size)return;const t=Array.from(e.byName.entries()).sort(([e],[t])=>e.localeCompare(t)).map(([e,t])=>`${e}:${t};`).join("");return e.byName.clear(),t?{id:"theme-vars",cssText:`:root{${t}}`}:void 0}function c(e){if(!e?.system||"object"!=typeof e.system)return"";const t=[],o=[{path:"",value:e.system}];for(;o.length>0;){const e=o.pop();if(!e||!e.value||"object"!=typeof e.value)continue;const r=e.value;for(const[n,l]of Object.entries(r)){const r=e.path?`${e.path}.${n}`:n;if(null!=l)if("string"!=typeof l&&"number"!=typeof l&&"boolean"!=typeof l){if("object"==typeof l){const e=l.default;"string"!=typeof e&&"number"!=typeof e&&"boolean"!=typeof e||t.push([s(r),String(e)]),o.push({path:r,value:l})}}else t.push([s(r),String(l)])}}if(0===t.length)return"";return`:root{${t.sort(([e],[t])=>e.localeCompare(t)).map(([e,t])=>`${e}:${t};`).join("")}}`}const f=new WeakMap;function p(e){const t=/theme\.([a-zA-Z0-9_.-]+)/g,o=e=>{if(!e.includes("theme."))return e;t.lastIndex=0;let o=!1;const r=e.replace(t,(e,t)=>(o=!0,`var(${s(t)})`));return o?r:e},r=e=>{if("object"!=typeof e||null===e)return!1;const t=Object.getPrototypeOf(e);return null===t||t===Object.prototype};if("string"==typeof e)return o(e);if(!r(e)&&!Array.isArray(e))return e;const n=f.get(e);if(void 0!==n)return n;const l=[{value:e,isProcessed:!1}],a=new Map,i=new Set;for(;l.length>0;){const e=l[l.length-1],t=e.value;if(r(t)||Array.isArray(t))if(a.has(t))l.pop();else if(e.isProcessed){l.pop(),i.delete(t);let e=t;if(Array.isArray(t)){let n=null;for(let e=0;e<t.length;e++){const s=t[e];let l=s;"string"==typeof s?l=o(s):(r(s)||Array.isArray(s))&&(l=a.get(s)??s),l!==s&&(null===n&&(n=[...t]),n[e]=l)}null!==n&&(e=n)}else{let n=null;const s=t;for(const e in s)if(Object.prototype.hasOwnProperty.call(s,e)){const t=s[e];let l=t;"string"==typeof t?l=o(t):(r(t)||Array.isArray(t))&&(l=a.get(t)??t),l!==t&&(null===n&&(n={...s}),n[e]=l)}null!==n&&(e=n)}a.set(t,e)}else{e.isProcessed=!0,i.add(t);const o=Array.isArray(t)?t:Object.values(t);for(let e=o.length-1;e>=0;e--){const t=o[e];!r(t)&&!Array.isArray(t)||i.has(t)||l.push({value:t,isProcessed:!1})}}else l.pop()}const c=a.get(e)??e;return f.set(e,c),c!==e&&"object"==typeof c&&null!==c&&f.set(c,c),c}export{c as buildThemeVariablesCss,i as consumeServerThemeVariablesCss,r as getActiveServerTheme,a as registerServerThemeVariables,p as replaceThemeTokensWithCssVars,n as setActiveServerTheme};