diff --git a/components/ConversionPanel.js b/components/ConversionPanel.js index 64e8722..47c22b9 100644 --- a/components/ConversionPanel.js +++ b/components/ConversionPanel.js @@ -83,6 +83,17 @@ export default class ConversionPanel extends PureComponent { }) } + toggleRight = (e) => { + const checked = e.currentTarget.checked + if (this.props.onRnToggle) { + this.props.onRnToggle(checked, () => { + this.setState({ + resultValue: this.props.getTransformedValue(this.state.value) + }) + }) + } + } + prettifyCode = () => { const value = this.props.leftMode === 'css' ? beauty(this.state.value) : js_beautify(this.state.value) this.setState({ @@ -137,12 +148,11 @@ export default class ConversionPanel extends PureComponent { .section { flex: 1; position: relative; - height: calc(100vh - 50px); + height: calc(100vh - 100px); } .right { border-left: 1px solid #eee; - padding: 20px; } @media screen and (max-width: 1000px) { @@ -163,9 +173,6 @@ export default class ConversionPanel extends PureComponent { } .btn { - position: absolute; - top: 20px; - right: 20px; background-color: #2196F3; font-size: 14px; padding: 7px 14px; @@ -176,12 +183,25 @@ export default class ConversionPanel extends PureComponent { outline: none; color: #fff; line-height: 16px; + height: 32px; } .btn:hover { background-color: #2380ca; } + .header { + height: 50px; + background-color: white; + border-bottom: 1px solid #eee; + display: flex; + flex-direction: row-reverse; + align-items: center; + padding: 0 10px; + z-index: 9999; + position: relative; + } + `}
+
+ +
{isBrowser && } -
+
+ + {this.props.onRnToggle && + + } +
{isBrowser && } -
diff --git a/package.json b/package.json index 0f7502e..52d4b3b 100644 --- a/package.json +++ b/package.json @@ -18,6 +18,7 @@ "babili-webpack-plugin": "^0.1.2", "copy-text-to-clipboard": "^1.0.2", "css-beauty": "^0.2.0", + "css-to-react-native": "^2.0.4", "is-in-browser": "^1.1.3", "js-beautify": "^1.6.14", "json2dts": "^0.0.2", diff --git a/pages/css-to-js.js b/pages/css-to-js.js index ff0ebb6..5b5e81e 100644 --- a/pages/css-to-js.js +++ b/pages/css-to-js.js @@ -2,6 +2,8 @@ import React, { PureComponent } from 'react' import plugin from '../utils/css-to-js' import Layout from '../components/Layout' import ConversionPanel from '../components/ConversionPanel' +import transform from 'css-to-react-native' +import kebabCase from 'lodash/kebabCase' const defaultText = ` .main-wrapper { @@ -20,16 +22,37 @@ ul { } li { + font-family:'Lato'; color: whitesmoke; - font-family: 'Lato', sans-serif; line-height: 44px; cursor: pointer; } ` export default class Css2Js extends PureComponent { + state = { + isRn: false + } + + getRnCode = (newValue) => { + const styles = {} + const code = eval('(' + plugin(newValue).split('=')[1] + ')') + Object.keys(code).forEach(key => { + styles[key] = {} + const arr = [] + Object.keys(code[key]).forEach(key2 => { + arr.push([ + kebabCase(key2), + code[key][key2] + ]) + }) + styles[key] = transform(arr) + }) + return styles + } + getTransformedValue = (newValue) => { - return plugin(newValue) + return this.state.isRn ? `const styles = StyleSheet.create(${JSON.stringify(this.getRnCode(newValue))})` : plugin(newValue) } render () { @@ -41,6 +64,7 @@ export default class Css2Js extends PureComponent { getTransformedValue={this.getTransformedValue} name={'css_to_js'} defaultText={defaultText} + onRnToggle={(checked, cb) => this.setState({isRn: checked}, cb)} /> ) diff --git a/utils/css-to-js.js b/utils/css-to-js.js index 4391825..b8ccba8 100644 --- a/utils/css-to-js.js +++ b/utils/css-to-js.js @@ -161,7 +161,7 @@ function convertoToJS(tokens){ function renderJS(items){ - let objects = ['var styles = {'] + let objects = ['const styles = {'] objects = objects.concat(items.map(renderItem).join(",")); objects.push("}"); return objects.join("\n"); diff --git a/yarn.lock b/yarn.lock index 1e8ac6c..5839aa4 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1396,6 +1396,18 @@ css-beauty@^0.2.0: dependencies: css "^2.2.1" +css-color-keywords@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/css-color-keywords/-/css-color-keywords-1.0.0.tgz#fea2616dc676b2962686b3af8dbdbe180b244e05" + +css-to-react-native@^2.0.4: + version "2.0.4" + resolved "https://registry.yarnpkg.com/css-to-react-native/-/css-to-react-native-2.0.4.tgz#cf4cc407558b3474d4ba8be1a2cd3b6ce713101b" + dependencies: + css-color-keywords "^1.0.0" + fbjs "^0.8.5" + postcss-value-parser "^3.3.0" + css-tree@1.0.0-alpha17: version "1.0.0-alpha17" resolved "https://registry.yarnpkg.com/css-tree/-/css-tree-1.0.0-alpha17.tgz#7ab95ab72c533917af8be54313fec81841c5223a" @@ -1792,6 +1804,18 @@ fast-deep-equal@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/fast-deep-equal/-/fast-deep-equal-1.0.0.tgz#96256a3bc975595eb36d82e9929d060d893439ff" +fbjs@^0.8.5: + version "0.8.14" + resolved "https://registry.yarnpkg.com/fbjs/-/fbjs-0.8.14.tgz#d1dbe2be254c35a91e09f31f9cd50a40b2a0ed1c" + dependencies: + core-js "^1.0.0" + isomorphic-fetch "^2.1.1" + loose-envify "^1.0.0" + object-assign "^4.1.0" + promise "^7.1.1" + setimmediate "^1.0.5" + ua-parser-js "^0.7.9" + fbjs@^0.8.9: version "0.8.12" resolved "https://registry.yarnpkg.com/fbjs/-/fbjs-0.8.12.tgz#10b5d92f76d45575fd63a217d4ea02bea2f8ed04" @@ -3127,6 +3151,10 @@ pkg-up@2.0.0: dependencies: find-up "^2.1.0" +postcss-value-parser@^3.3.0: + version "3.3.0" + resolved "https://registry.yarnpkg.com/postcss-value-parser/-/postcss-value-parser-3.3.0.tgz#87f38f9f18f774a4ab4c8a232f5c5ce8872a9d15" + preserve@^0.2.0: version "0.2.0" resolved "https://registry.yarnpkg.com/preserve/-/preserve-0.2.0.tgz#815ed1f6ebc65926f865b310c0713bcb3315ce4b"