For anyone that's super lazy and just wants to get CSS variables working quickly like me:
<script>exportletthemeSettings={font:{family:'Merriweather, serif'},main:{background:'white'},colors:{primary:{background:'#1b4738',color:'white'}}};functionencodeB64(str){if(process.browser){// For Sapperreturnbtoa(str);}else{constbuff=Buffer.from(str,'utf-8');returnbuff.toString('base64');}}functionmakeCSSVars(settings,prefix='-'){returnObject.entries(settings).flatMap(([key,value])=>{constpath=prefix+'-'+key;if(typeofvalue==='object')returnmakeCSSVars(value,path);elsereturn`${path}:${value};`;}).join('\n');}$:themeCSS=`:root {${makeCSSVars(themeSettings)}}`;</script><linkrel="stylesheet"href="data:text/css;base64,{encodeB64(themeCSS)}"/><style>main{background:var(--colors-primary-background);}</style>
Hey I'm just seeing this! This looks like an excellent approach and I hadn't thought about base64 encoding! Would love to hear your thoughts over at svelte-themer 🙂
Some time ago i was looking for a way to switch themes in SvelteKit and ended up in having separate CSS files in public dir and just linking them.
It sucked b/c it needed full webpage reload to get new css to be applied.
So this solution with base64'ing content might do that automatically.
(I think getting css file as text and sending whole text as base64 might work).
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
For anyone that's super lazy and just wants to get CSS variables working quickly like me:
Hey I'm just seeing this! This looks like an excellent approach and I hadn't thought about base64 encoding! Would love to hear your thoughts over at svelte-themer 🙂
Some time ago i was looking for a way to switch themes in SvelteKit and ended up in having separate CSS files in public dir and just linking them.
It sucked b/c it needed full webpage reload to get new css to be applied.
So this solution with base64'ing content might do that automatically.
(I think getting css file as text and sending whole text as base64 might work).