Universal, Tree-Shakeable Themes Provider
npm install themes-provider:root, optionally namespaced.
Zikojs, Vanjs, React, Preact, Svelte, Vue, Solid, Vanilla JS, and more.
Vanjs : https://stackblitz.com/fork/github/zakarialaoui10/themes-provider/tree/main/examples/vanjs?file=src%2FApp.js
bash
npm i themes-provider
`
Process
- Themes Declarations
`js
const Themes = {
T1 : {
border : '1px darkblue solid',
color : 'darkblue',
bg : 'white'
},
T2 : {
border : '1px darkblue solid',
color : 'darkblue',
bg : 'white'
}
}
`
- Applying a Theme to :root with useTheme
`js
import {useTheme} from 'themes-provider'
const T = useTheme(Themes.T1, {namespace : th})
`
This assigns the properties from Themes.T1 as CSS variables on :root, It's equivalent to:
`css
:root{
--tt-border : 1px darkblue solid;
--tt-color : darkblue;
--tt-bg : white;
}
`
- Applying the style
You can use two approaches:
- Using destructuring (when applying styles directly in JavaScript)
`js
const {border, color, bg} = T
`
- Using Css Variable derictly
`css
[selector]{
border : var(--tt-border);
}
`
The choice of approach depends on the targeted framework itself.
- Updating the theme dynamically
`js
A.use(Themes.T2)
``