react-cssobj
React work with cssobj, make stylesheet localized, runtime updating, and more.
Install
npm install --save react-cssobj
Usage
import React from 'react'import ReactCSS from 'react-cssobj' const css mapClass = Component { return }
Render result: (stylesheet be rendered in <head>
, thank to cssobj)
Title
Then, when you clicked on h2
, the result:
Title
also, the stylesheet for .app
rule updated to background: yellow;
API
MainEntry [function] signature:
function( jsObject, cssobjConfig ) -> Instance of HelperClass
-
jsObject: [object] Javascript object represetation of stylesheet, same as cssobj's 1st argument.
-
cssobjConfig: [optional, object] The
config
option, same as cssobj's 2nd argument, but with default value:{local: true}
.
HelperClass [class] members:
-
css: [object] The cssobj result object.
-
mapClass: [function (jsx) -> jsx] Transform JSX into new JSX, with
className
props transformed:-
First passed into classnames
-
Then passed into cssobj result.mapClass
-
The final value is localized
className
asstring
-
Static Methods
MainEntry
has below methods as shortcuts for related modules:
-
cssobj: [function] Same as require('cssobj')
-
classNames: [function] Same as require('classnames')
-
changeProps: [function] Same as require('react-change-props')
Notes
1. No inject into sub-component
mapClass
is only current-level transformer, when met a component like <Foo/>
, it will not dig into it, keep the component clean in it's own render()
.
const css mapClass = { return <p ="foo">Hello</p>} { return }
Above example, p
will rendered as is, p.foo
will not be localized, to work, you should change Foo like below:
{ return }
2. Get localized class names
If you want get/use localized className some where (like DOM), you can use cssobj method css.mapClass
:
{ return <p = =>Hello</p>} // any where you want to query a DOM:document
Other
You may want babel-plugin-transform-cssobj if you don't hope runtime interpolate.