Skip to content

Commit b4d8110

Browse files
authored
Merge pull request styled-components#28 from styled-components/bool-values
Allow boolean values in CSS (fixes styled-components#26)
2 parents a944c5a + faa8879 commit b4d8110

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

src/index.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,20 @@ const transforms = require('./transforms');
55
const TokenStream = require('./TokenStream');
66

77
// Note if this is wrong, you'll need to change tokenTypes.js too
8-
const numberOrLengthRe = /^([+-]?(?:\d*\.)?\d+(?:[Ee][+-]?\d+)?)(?:px)?$/;
8+
const numberOrLengthRe = /^([+-]?(?:\d*\.)?\d+(?:[Ee][+-]?\d+)?)(?:px)?$/i;
9+
const boolRe = /^true|false$/i;
910

1011
// Undocumented export
1112
export const transformRawValue = (input) => {
12-
const value = input.trim().match(numberOrLengthRe);
13-
return value ? Number(value[1]) : input;
13+
const value = input.trim();
14+
15+
const numberMatch = value.match(numberOrLengthRe);
16+
if (numberMatch) return Number(numberMatch[1]);
17+
18+
const boolMatch = input.match(boolRe);
19+
if (boolMatch) return boolMatch[0].toLowerCase() === 'true';
20+
21+
return value;
1422
};
1523

1624
const baseTransformShorthandValue = (propName, inputValue) => {

src/index.test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,18 @@ it('allows pixels in unspecialized transform', () => runTest([
1717
['top', '0px'],
1818
], { top: 0 }));
1919

20+
it('allows boolean values values', () => runTest([
21+
['boolTrue1', 'true'],
22+
['boolTrue2', 'TRUE'],
23+
['boolFalse1', 'false'],
24+
['boolFalse2', 'FALSE'],
25+
], {
26+
boolTrue1: true,
27+
boolTrue2: true,
28+
boolFalse1: false,
29+
boolFalse2: false,
30+
}));
31+
2032
it('allows percent in unspecialized transform', () => runTest([
2133
['top', '0%'],
2234
], { top: '0%' }));

0 commit comments

Comments
 (0)