mirror of
https://github.com/ritz078/transform.git
synced 2026-11-26 02:33:50 +03:00
added react native support to css
This commit is contained in:
1 parent
3314881fd8
commit
15c8028f15
5 files changed
+108
-10
No files matched your search
@@ -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;
|
||||
}
|
||||
|
||||
`}</style>
|
||||
|
||||
<style global jsx>{`
|
||||
@@ -206,14 +226,33 @@ export default class ConversionPanel extends PureComponent {
|
||||
background-color: #fafafa;
|
||||
}
|
||||
|
||||
.right .ace_scroller {
|
||||
padding: 0 20px;
|
||||
}
|
||||
|
||||
.right {
|
||||
box-sizing: border-box;
|
||||
padding-bottom: 10px;
|
||||
}
|
||||
|
||||
label {
|
||||
font-family: 'Lato';
|
||||
padding: 0 12px;
|
||||
cursor: pointer;
|
||||
|
||||
}
|
||||
|
||||
label input {
|
||||
margin-right: 4px;
|
||||
font-size: 16px;
|
||||
}
|
||||
`}</style>
|
||||
|
||||
<div className="content-wrapper">
|
||||
<div className="section left">
|
||||
<div className="header">
|
||||
<button className="btn" onClick={this.prettifyCode}>Prettify</button>
|
||||
</div>
|
||||
{isBrowser &&
|
||||
<AceEditor
|
||||
mode={this.props.leftMode}
|
||||
@@ -230,9 +269,16 @@ export default class ConversionPanel extends PureComponent {
|
||||
/>
|
||||
}
|
||||
|
||||
<button className="btn" onClick={this.prettifyCode}>Prettify</button>
|
||||
</div>
|
||||
<div className="section right">
|
||||
<div className="header">
|
||||
<button className="btn" onClick={this.copyCode}>Copy</button>
|
||||
{this.props.onRnToggle &&
|
||||
<label htmlFor="#text">
|
||||
<input type="checkbox" id="#text" onChange={this.toggleRight} /> React Native
|
||||
</label>
|
||||
}
|
||||
</div>
|
||||
{isBrowser &&
|
||||
<AceEditor
|
||||
mode={this.props.rightMode}
|
||||
@@ -248,7 +294,6 @@ export default class ConversionPanel extends PureComponent {
|
||||
wrapEnabled
|
||||
/>
|
||||
}
|
||||
<button className="btn" onClick={this.copyCode}>Copy</button>
|
||||
</div>
|
||||
</div>
|
||||
<div className="footer">
|
||||
|
||||
@@ -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",
|
||||
|
||||
+26
-2
@@ -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)}
|
||||
/>
|
||||
</Layout>
|
||||
)
|
||||
|
||||
+1
-1
@@ -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");
|
||||
|
||||
@@ -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"
|
||||
|
||||
Reference in new issue
Block a user