- Styles are plain objects passed to the
style
prop. WebComponent
hijacksstyle
.WebComponent
converts every unique declaration to single-purpose class (i.e.,.abcd { position: relative; }
) that is added to the component'sclassName
.- Either a library like
free-style
injects a<style>
element with all classes needed to render the existing document, or CSS is statically extracted. - Dynamic styles are explicitly labelled (escape hatch) and written as inline styles on the DOM node.
Author style as plain objects and pass them to the style
attribute.
element
: Function|String="div"style
: Object
import Theme from './theme';
<WebComponent element="nav" style={{
alignItems: 'center',
justifyContent: 'space-between',
marginTop: Theme.VERTICAL_SPACE_UNIT * 2
}} />
Produces this CSS (example hashes):
.a1lkd9i { align-items: center; }
.9b5klpp { justify-content: space-between; }
.apknvbu { margin-top: 2rem; }
And this rendered HTML:
<nav class="a1lkd9i 9b5klpp apknvbu">...</nav>
Wraps dynamic styles and writes them to the DOM node rather than the stylesheet.
<MyComponent style={{
opacity: ReactWebStyle.dynamic(this.state.opacity)
}} />
Produces:
<div style="opacity:0.78">...</div>
Filters out all style
properties that are not defined in the Constuctor's propTypes
. Helps define explicit style APIs, like react-native
, allowing easier SDK / UI library development.
class Example extends React.Component {
static propTypes = {
// only accept 'color' and 'width' styles
style: PropTypes.shape({
color: PropTypes.string,
width: PropTypes.oneOf(['number', 'string'])
})
}
static defaultPropTypes = {
style: {}
}
render() {
return (
<WebComponent {...this.props} style={
ReactWebStyle.filterStyleProps(this.constructor.PropTypes, this.props)
} />
);
}
}
React.render(<Example style={{ color: 'red', padding: '10px' }} />, document.body);
Produces:
<div className="aHashForColorRed">...</div>
See View.jsx
for an example of how a react-web-sdk could provide a partial web implementation of react-native
's View
.
See CustomView.jsx
for an example of how an application-specific view could define a small style
-API and build upon the SDK's View
and Text
primitives.