Skip to content

Commit 81a1398

Browse files
committed
initial commit
0 parents  commit 81a1398

File tree

11 files changed

+5323
-0
lines changed

11 files changed

+5323
-0
lines changed

.babelrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"presets": ["es2015"]
3+
}

.editorconfig

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# EditorConfig is awesome: http://EditorConfig.org
2+
3+
# top-most EditorConfig file
4+
root = true
5+
6+
# Unix-style newlines with a newline ending every file
7+
[*]
8+
end_of_line = lf
9+
insert_final_newline = true
10+
trim_trailing_whitespace = true
11+
indent_style = space
12+
indent_size = 2

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
dist

LICENSE.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# The MIT License
2+
3+
Copyright (c) 2017 Krister Kari
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of
6+
this software and associated documentation files (the "Software"), to deal in
7+
the Software without restriction, including without limitation the rights to
8+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9+
the Software, and to permit persons to whom the Software is furnished to do so,
10+
subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# css-to-react-native-transform
2+
3+
A lightweight wrapper on top of
4+
[css-to-react-native](https://github.com/styled-components/css-to-react-native)
5+
to allow valid CSS to be turned into React Native Stylesheet objects.
6+
7+
Example:
8+
9+
```css
10+
.my-class {
11+
font-size: 18px;
12+
line-height: 24px;
13+
color: red;
14+
}
15+
16+
.other {
17+
padding: 1rem;
18+
}
19+
```
20+
21+
is transformed to:
22+
23+
```js
24+
{
25+
myClass: {
26+
fontSize: 18,
27+
lineHeight: 24,
28+
color: "red"
29+
},
30+
other: {
31+
paddingBottom: 16,
32+
paddingLeft: 16,
33+
paddingRight: 16,
34+
paddingTop: 16
35+
}
36+
}
37+
```
38+
39+
## API
40+
41+
```js
42+
import transform from "css-to-react-native-transform";
43+
// or const transform = require("css-to-react-native-transform").default;
44+
45+
transform(`
46+
.foo {
47+
color: #f00;
48+
}
49+
`); // => { foo: { color: "#f00" } }
50+
```
51+
52+
## Limitations
53+
54+
* For `rem` unit the root element `font-size` is currently set to 16 pixels. A
55+
setting needs to be implemented to allow the user to define the root element
56+
`font-size`.
57+
* There is also support for the `box-shadow` shorthand, and this converts into
58+
`shadow-` properties. Note that these only work on iOS.

package.json

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"name": "css-to-react-native-transform",
3+
"description": "Convert CSS text to a React Native stylesheet object",
4+
"version": "1.0.0",
5+
"main": "dist/index.js",
6+
"author": "Krister Kari",
7+
"license": "MIT",
8+
"scripts": {
9+
"build": "babel src --ignore index.spec.js --out-dir dist",
10+
"test": "jest",
11+
"prepublish": "npm run build",
12+
"release": "np"
13+
},
14+
"devDependencies": {
15+
"babel-cli": "^6.26.0",
16+
"babel-core": "^6.26.0",
17+
"babel-jest": "^21.2.0",
18+
"babel-preset-es2015": "^6.24.1",
19+
"jest": "^21.2.1",
20+
"np": "^2.18.2"
21+
},
22+
"jest": {
23+
"transform": {
24+
"^.+\\.jsx?$": "babel-jest"
25+
}
26+
},
27+
"dependencies": {
28+
"css": "^2.2.1",
29+
"css-to-react-native": "^2.0.4"
30+
},
31+
"repository": {
32+
"type": "git",
33+
"url":
34+
"git+https://github.com/kristerkari/css-to-react-native-transform.git"
35+
},
36+
"keywords": ["React", "ReactNative", "styles", "CSS"],
37+
"files": ["dist", "src", "README.md"]
38+
}

src/index.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import parseCSS from "css/lib/parse";
2+
import transformCSS from "css-to-react-native";
3+
import { boxShadowToShadowProps } from "./transforms/boxShadow";
4+
import { remToPx } from "./transforms/rem";
5+
6+
const transform = css => {
7+
const { stylesheet } = parseCSS(css);
8+
9+
const result = {};
10+
11+
for (const rule of stylesheet.rules) {
12+
for (let selector of rule.selectors) {
13+
selector = selector.replace(/\.|#/g, "");
14+
15+
let styles;
16+
17+
styles = result[selector] = result[selector] || {};
18+
19+
for (const declaration of rule.declarations) {
20+
if (declaration.type !== "declaration") continue;
21+
22+
const property = declaration.property;
23+
const value = remToPx(declaration.value);
24+
25+
// box-shadow is implemented in css-to-react-native,
26+
// but not released yet. Remove after it is supported.
27+
if (property === "box-shadow") {
28+
Object.assign(styles, boxShadowToShadowProps(value));
29+
} else {
30+
Object.assign(styles, transformCSS([[property, value]]));
31+
}
32+
}
33+
}
34+
}
35+
return result;
36+
};
37+
38+
export default transform;

0 commit comments

Comments
 (0)