Skip to content

Commit 73abab3

Browse files
authored
Merge pull request #16 from styled-components/blacklist-shorthands
Allow blacklisting property shorthands
2 parents d192346 + 19aa202 commit 73abab3

File tree

3 files changed

+20
-8
lines changed

3 files changed

+20
-8
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,13 @@ getPropertyName('border-width'); // => 'borderWidth'
9393
getStylesForProperty('borderWidth', '1 0 2 0'); // => { borderTopWidth: 1, ... }
9494
```
9595

96+
Should you wish to opt-out of transforming certain shorthands, an array of property names in camelCase can be passed as a second argument to `transform`.
97+
98+
```js
99+
transform([['border-radius', '50']], ['borderRadius']);
100+
// { borderRadius: 50 } rather than { borderTopLeft: ... }
101+
```
102+
96103
## License
97104

98105
Licensed under the MIT License, Copyright © 2016 Jacob Parker and Maximilian Stoiber.

src/index.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ const transformRawValue = input => (
2727
: input
2828
);
2929

30-
export const getStylesForProperty = (propName, inputValue) => {
30+
export const getStylesForProperty = (propName, inputValue, allowShorthand) => {
3131
const value = inputValue.trim();
3232

33-
const propValue = (transforms.indexOf(propName) !== -1)
33+
const propValue = (allowShorthand && transforms.indexOf(propName) !== -1)
3434
? (new nearley.Parser(grammar.ParserRules, propName).feed(value).results[0])
3535
: transformRawValue(value);
3636

@@ -41,9 +41,9 @@ export const getStylesForProperty = (propName, inputValue) => {
4141

4242
export const getPropertyName = camelizeStyleName;
4343

44-
export default rules => rules.reduce((accum, rule) => (
45-
Object.assign(accum, getStylesForProperty(
46-
getPropertyName(rule[0]),
47-
rule[1],
48-
))
49-
), {});
44+
export default (rules, shorthandBlacklist = []) => rules.reduce((accum, rule) => {
45+
const propertyName = getPropertyName(rule[0]);
46+
const value = rule[1];
47+
const allowShorthand = shorthandBlacklist.indexOf(propertyName) === -1;
48+
return Object.assign(accum, getStylesForProperty(propertyName, value, allowShorthand));
49+
}, {});

src/index.test.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,3 +278,8 @@ it('omits line height if not specified', () => runTest([
278278
fontStyle: 'normal',
279279
fontVariant: [],
280280
}));
281+
282+
it('allows blacklisting shorthands', () => {
283+
const actualStyles = transformCss([['border-radius', '50']], ['borderRadius']);
284+
expect(actualStyles).toEqual({ borderRadius: 50 });
285+
});

0 commit comments

Comments
 (0)