Use CSS Custom Properties (aka CSS variables) theming with automatic fallback
to regular theming. Below components allow you to safely use CSS variables. You simply use <CssVarsProvider>
similar
to how you use <Theme>
, but if CSS variables are supported by user's browser, all values will be replaced
by CSS variables and on subsequent re-renders it will try to just modify the CSS variables without re-rendering the children.
If CSS Custom Properties are not supported, CssVars works like a typical theme provider.
import {CssVarsProvider, CssVars} from 'libreact/lib/cssvars';
<CssVarsProvider vars={{
color: 'tomato'
}}>
<CssVars>{vars =>
<button style={vars}>Click me!</button>
}</CssVars>
</CssVarsProvider>
In the above example vars
is replaced by something like
{
color: '---libreact-color'
}
, where ---libreact-color
is a CSS variable, which will be updated on theme changes, instead
of re-rendering the children.
CSS variable context provider.
ns
— optional, string, context namespace, defaults to empty string''
.vars
— required, plain flat JavaScript object, map of keys to CSS values, which will be replaces by CSS variables.
CSS variable context consumer render prop.
ns
— optional, string, context namespace, defaults to''
.
Render prop receives a single argument — map of variables where values are replaced by CSS variables.
Enhancer that injects vars
prop into component.
import {withCssVars} from 'libreact/lib/cssvars';
const MyCompWithVars = withCssVars(MyComp);
const MyCompWithVars = withCssVars(MyComp, 'theme', {ns: 'css-theme'});
withCssVars(Component, propName?, cssVarsProps?);
Stateful class decorator that injects vars
prop.
import {withCssVars} from 'libreact/lib/cssvars';
@withCssVars
class MyCompWithVars extends Component {
}
@withCssVars('theme', {ns: 'css-theme'})
class MyCompWithVars extends Component {
}