Skip to content

Commit c2d6c07

Browse files
authored
Merge pull request #22 from streamich/feat/inline-style-prefixer
Feat/inline style prefixer
2 parents a44f8bf + b30f612 commit c2d6c07

7 files changed

Lines changed: 123 additions & 2 deletions

File tree

.storybook/prefixer.stories.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import {createElement as h} from 'react';
2+
import {storiesOf} from '@storybook/react';
3+
const {action} = require('@storybook/addon-actions');
4+
const {linkTo} = require('@storybook/addon-links');
5+
const {create} = require('../index');
6+
const {addon: addonRule} = require('../addon/rule');
7+
const {addon: addonPrefixer} = require('../addon/prefixer');
8+
9+
const renderer = create();
10+
addonRule(renderer);
11+
addonPrefixer(renderer);
12+
const {rule} = renderer;
13+
14+
const className1 = rule({
15+
flex: 1,
16+
display: 'flex',
17+
alignItems: 'center',
18+
boxShadow: '0 0 5px red',
19+
'text-shadow': '2px 2px #ff0000',
20+
});
21+
22+
storiesOf('Addons/prefixer', module)
23+
.add('Default', () =>
24+
h('div', {className: className1}, 'Hello world')
25+
)

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ __Tiny [5<sup>th</sup> generation](https://github.com/streamich/freestyler/blob/
99
- __Isomorphic__ &mdash; render on server and browser, generates stable class names, and re-hydrates
1010
- __Performant__ &mdash; does not create wrapper components, does not use inline styles or inline `<style>` elements in the document body, but caches all class names for re-use and injects CSS using `.insertRule()` for performance
1111
- __`@media` queries__ and __animation `@keyframes`__ are supported
12+
- __Auto-prefixes__ your styles
1213

1314

1415
## Reference
@@ -33,6 +34,7 @@ __Tiny [5<sup>th</sup> generation](https://github.com/streamich/freestyler/blob/
3334
- [`nesting`](./docs/nesting.md)
3435
- [`keyframes()`](./docs/keyframes.md)
3536
- [`hydrate`](./docs/hydrate.md)
37+
- [`prefixer`](./docs/prefixer.md)
3638
- [`stylis`](./docs/stylis.md)
3739
- [`unitless`](./docs/unitless.md)
3840
- [`!important`](./docs/important.md)

addon/prefixer.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
'use strict';
2+
3+
var prefixAll = require('inline-style-prefixer/static');
4+
5+
var CAMEL_REGEX = /-[a-z\u00E0-\u00F6\u00F8-\u00FE]/g;
6+
7+
var matchCallback = function (match) {
8+
return match.slice(1).toUpperCase();
9+
};
10+
11+
exports.addon = function (renderer) {
12+
var decl = renderer.decl;
13+
14+
renderer.camel = function (prop) {
15+
return prop.replace(CAMEL_REGEX, matchCallback);
16+
};
17+
18+
renderer.prefix = function (prop, value) {
19+
var obj = {};
20+
obj[renderer.camel(prop)] = value;
21+
obj = prefixAll(obj);
22+
23+
var str = '';
24+
25+
for (var propPrefixed in obj) {
26+
value = obj[propPrefixed];
27+
propPrefixed = renderer.kebab(propPrefixed);
28+
29+
if (value instanceof Array) {
30+
str += propPrefixed + ':' + value.join(';' + propPrefixed + ':') + ';';
31+
} else {
32+
str += propPrefixed + ':' + value + ';';
33+
}
34+
}
35+
36+
return str;
37+
};
38+
39+
renderer.decl = function (prop, value) {
40+
var str = decl(prop, value);
41+
var declarations = str.split(';');
42+
43+
if (!declarations.length) {
44+
return str;
45+
}
46+
47+
var prefixed = '';
48+
49+
for (var i = 0; i < declarations.length; i++) {
50+
var declaration = declarations[i];
51+
52+
if (declaration) {
53+
var pos = declaration.indexOf(':');
54+
55+
prefixed += renderer.prefix(declaration.substr(0, pos), declaration.substr(pos + 1));
56+
}
57+
}
58+
59+
return prefixed;
60+
};
61+
};

docs/Addons.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ plenty more to chose from. Below is a list of addons shipped with `nano-css`.
2121
- [`nesting`](./nesting.md) &mdash; better nesting features
2222
- [`keyframes()`](./keyframes.md) &mdash; adds `@keyframes` support
2323
- [`hydrate`](./hydrate.md) &mdash; adds support for re-hydration on client side
24+
- [`prefixer`](./prefixer.md) &mdash; auto-prefixes your styles with correct vendor prefixes
2425
- [`stylis`](./stylis.md) &mdash; write CSS as strings
2526
- [`unitless`](./unitless.md) &mdash; automatically adds `px` to styles
2627
- [`!important`](./important.md) &mdash; adds `!important` to all declarations

docs/prefixer.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# `prefixer` Addon
2+
3+
Uses [`inline-style-prefixer`](https://github.com/rofrischmann/inline-style-prefixer) library
4+
to auto-prefix your styles on the server and browser.
5+
6+
Example:
7+
8+
```js
9+
nano.put('.foo', {
10+
flex: 1
11+
});
12+
```
13+
14+
Result:
15+
16+
```css
17+
.foo {
18+
-webkit-flex:1;
19+
flex:1;
20+
}
21+
```
22+
23+
24+
## Installation
25+
26+
Simply install `prefixer` addon.
27+
28+
Read more about the [Addons Installation](./Addons.md#addon-installation).

index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,11 @@ exports.create = function (config) {
2020
client: typeof window === 'object',
2121
assign: assign,
2222
stringify: JSON.stringify,
23+
kebab: function (prop) {
24+
return prop.replace(KEBAB_REGEX, '-$&').toLowerCase();
25+
},
2326
decl: function (key, value) {
24-
key = key.replace(KEBAB_REGEX, '-$&').toLowerCase();
27+
key = renderer.kebab(key);
2528
return key + ':' + value + ';';
2629
},
2730
hash: function (obj) {

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@
3737
},
3838
"dependencies": {
3939
"fastest-stable-stringify": "^1.0.1",
40-
"stylis": "3.5.0"
40+
"stylis": "3.5.0",
41+
"inline-style-prefixer": "^4.0.0"
4142
},
4243
"devDependencies": {
4344
"@types/react": "16.0.40",

0 commit comments

Comments
 (0)