babel-plugin-transform-jsx-to-hy-stylesheet
v1.0.53
Published
Transform stylesheet selector to style in JSX Elements.
Downloads
343
Readme
babel-plugin-transform-jsx-hy-stylesheet
Transform StyleSheet selector to style in JSX Elements for hycss.
Installation
npm install --save-dev babel-plugin-transform-jsx-to-hy-stylesheet
Usage
Via .babelrc
.babelrc
{
"plugins": ["transform-jsx-to-hy-stylesheet"]
}
Example
Your component.js
that contains this code:
import { Component } from 'React';
import './app.hycss';
class App extends Component {
render() {
return <div className="header" />
}
}
Will be transpiled into something like this:
import { Component } from 'React';
import appStyleSheet from './app.hycss';
class App extends Component {
render() {
return <div style={styleSheet.header} />;
}
}
const styleSheet = appStyleSheet;
Can write multiple classNames like this:
import { Component } from 'React';
import './app.hycss';
class App extends Component {
render() {
return <div className="header1 header2" />;
}
}
Will be transpiled into something like this:
import { Component } from 'React';
import appStyleSheet from './app.hycss';
class App extends Component {
render() {
return <div style={[styleSheet.header1, styleSheet.header2]} />;
}
}
const styleSheet = appStyleSheet;
Also support array, object and expressions like this:
import { Component } from 'React';
import './app.hycss';
class App extends Component {
render() {
return (
<div className={'header'}>
<div className={{ active: this.props.isActive }} />
<div className={['header1 header2', 'header3', { active: this.props.isActive }]} />
<div className={this.props.visible ? 'show' : 'hide'} />
<div className={getClassName()} />
</div>
);
}
}
Will be transpiled into something like this:
import { Component } from 'React';
import appStyleSheet from './app.hycss';
class App extends Component {
render() {
return (
<div style={styleSheet.header}>
<div style={_getStyle({ active: this.props.isActive })} />
<div style={_getStyle(['header1 header2', 'header3', { active: this.props.isActive }])} />
<div style={_getStyle(this.props.visible ? 'show' : 'hide')} />
<div style={_getStyle(getClassName())} />
</div>
);
}
}
const styleSheet = appStyleSheet;
function _getClassName() { /* codes */ }
function _getStyle(className) {
return styleSheet[_getClassName(className)]; // not real code
}
And can also import multiple hycss file:
import { Component } from 'React';
import 'app1.hycss';
import 'app2.hycss';
class App extends Component {
render() {
return <div className="header1 header2" />;
}
}
Will be transpiled into something like this:
import { Component } from 'React';
import app1StyleSheet from 'app1.hycss'
import app2StyleSheet from 'app2.hycss'
function _mergeStyles(a, b) { /* codes */ }
const styleSheet = _mergeStyles(app1StyleSheet, app2StyleSheet);
class App extends Component {
render() {
return <div style={[styleSheet.header1, styleSheet.header2]} />;
}
}