-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Emotion Primitives #658
Emotion Primitives #658
Changes from 1 commit
ec93579
3404e55
f989ea1
a886b94
775917e
a06d8aa
281b64e
231e581
4694063
97f5ac6
8772f90
2b49c02
74492fd
879e5eb
7a0bdc8
21ce603
608134b
3059db8
9018019
01f25cf
43f823a
3f1e514
93f89af
fca98c6
989c582
3ccc32e
7ff2302
372a94e
ff42dda
2a915b3
d61b23e
a670594
290e250
21c98fb
6a40c31
d6f458f
3f665fc
34f1e42
dc918d7
d9eeac7
4a645c0
e22c152
3e332c1
747acb3
b27ceb8
0fa0929
49c8172
51eecab
e05a360
a0c8a79
801c69d
1632b26
9c0b724
6df7787
99434b7
be28620
841a01a
b010d10
00c5108
e5408dd
1e5519f
b67bab7
c7bab6b
e91ab38
633761f
0ffcb92
5f9b040
75c57c9
0c5eb89
8a84d7c
53b984d
779c8c0
b6d4c17
a912bff
b3b5e93
437a9ab
477b0e2
2cfe87a
9b1920a
c46233c
d8f9810
33d236f
67fd321
e3a2055
38a0243
d2fe4c4
04e4bb3
cbdbf0f
b97b54e
e41ce0f
2254dd2
79ba84f
d2d17a2
30349a7
6538b79
f8e6958
e572f96
feeda5f
bab8aec
5714031
eb4b4fa
aa41a29
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,71 +3,82 @@ import transform from 'css-to-react-native' | |
import { StyleSheet } from 'react-primitives' | ||
import { interleave } from './utils' | ||
|
||
export function css(...args: any) { | ||
let vals | ||
let styles | ||
let buffer | ||
let lastType | ||
|
||
if (args[0] == null || args[0].raw === undefined) { | ||
vals = args | ||
} else { | ||
vals = interleave(args) | ||
} | ||
function handleInterpolation(interpolation: *, i: number, arr: Array<*>) { | ||
let type = typeof interpolation | ||
|
||
let styles = [] | ||
let buffer = '' | ||
let lastType | ||
if (type === 'function') { | ||
if (this === undefined && process.env.NODE_ENV !== 'production') { | ||
console.error( | ||
'Interpolating functions in css calls is not allowed.\n' + | ||
'If you want to have a css call based on props, create a function that returns a css call like this\n' + | ||
'let dynamicStyle = (props) => css`color: ${props.color}`\n' + | ||
'It can be called directly with props or interpolated in a styled call like this\n' + | ||
'let SomeComponent = styled.View`${dynamicStyle}`' | ||
) | ||
} else { | ||
handleInterpolation.call( | ||
this, | ||
interpolation( | ||
// $FlowFixMe | ||
this.mergedProps | ||
), | ||
i, | ||
arr | ||
) | ||
} | ||
return | ||
} | ||
let isIrrelevant = interpolation == null || type === 'boolean' | ||
let isRnStyle = | ||
(type === 'object' && !Array.isArray(interpolation)) || type === 'number' | ||
if (lastType === 'string' && (isRnStyle || isIrrelevant)) { | ||
let converted = convertStyles(buffer) | ||
if (converted !== undefined) { | ||
styles.push(converted) | ||
} | ||
buffer = '' | ||
} | ||
if (isIrrelevant) { | ||
return | ||
} | ||
|
||
let handleInterpolation = (interpolation: *, i: number, arr: Array<*>) => { | ||
let type = typeof interpolation | ||
if (type === 'string') { | ||
buffer += interpolation | ||
|
||
if (type === 'function') { | ||
if (this === undefined && process.env.NODE_ENV !== 'production') { | ||
console.error( | ||
'Interpolating functions in css calls is not allowed.\n' + | ||
'If you want to have a css call based on props, create a function that returns a css call like this\n' + | ||
'let dynamicStyle = (props) => css`color: ${props.color}`\n' + | ||
'It can be called directly with props or interpolated in a styled call like this\n' + | ||
'let SomeComponent = styled.View`${dynamicStyle}`' | ||
) | ||
} else { | ||
handleInterpolation(interpolation(this.mergedProps), i, arr) | ||
} | ||
return | ||
} | ||
let isFalsy = interpolation == null || type === 'boolean' | ||
let isRnStyle = | ||
(type === 'object' && !Array.isArray(interpolation)) || type === 'number' | ||
if (lastType === 'string' && (isRnStyle || isFalsy)) { | ||
if (arr.length - 1 === i) { | ||
let converted = convertStyles(buffer) | ||
if (converted !== undefined) { | ||
styles.push(converted) | ||
} | ||
buffer = '' | ||
} | ||
if (isFalsy) { | ||
return | ||
} | ||
} | ||
if (isRnStyle) { | ||
styles.push(interpolation) | ||
} | ||
if (Array.isArray(interpolation)) { | ||
interpolation.forEach(handleInterpolation, this) | ||
} | ||
lastType = type | ||
} | ||
|
||
if (type === 'string') { | ||
buffer += interpolation | ||
export function css(...args: any) { | ||
let vals | ||
styles = [] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we could maybe reuse There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure if it's really worth it/will make a difference. We create a bunch of arrays since |
||
buffer = '' | ||
lastType = undefined | ||
|
||
if (arr.length - 1 === i) { | ||
let converted = convertStyles(buffer) | ||
if (converted !== undefined) { | ||
styles.push(converted) | ||
} | ||
buffer = '' | ||
} | ||
} | ||
if (isRnStyle) { | ||
styles.push(interpolation) | ||
} | ||
if (Array.isArray(interpolation)) { | ||
interpolation.forEach(handleInterpolation) | ||
} | ||
lastType = type | ||
if (args[0] == null || args[0].raw === undefined) { | ||
vals = args | ||
} else { | ||
vals = interleave(args) | ||
} | ||
|
||
vals.forEach(handleInterpolation) | ||
vals.forEach(handleInterpolation, this) | ||
|
||
return StyleSheet.flatten(styles) | ||
} | ||
|
@@ -77,7 +88,7 @@ let propertyValuePattern = /\s*([^\s]+)\s*:\s*(.+?)\s*$/ | |
function convertStyles(str: string) { | ||
if (str.trim() === '') return | ||
|
||
const styleObj = [] | ||
const stylePairs = [] | ||
|
||
const parsedString = str.split(';') | ||
|
||
|
@@ -90,12 +101,9 @@ function convertStyles(str: string) { | |
// the first value in the array will | ||
// be the whole string so we remove it | ||
match.shift() | ||
styleObj.push(match) | ||
stylePairs.push(match) | ||
} | ||
}) | ||
if (styleObj.length === 0) { | ||
return | ||
} | ||
|
||
return transform(styleObj) | ||
return transform(stylePairs) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hm, i guess this is most efficient way to do this, but this code (as well as similar code in original emotion) is really hard to follow because of this leaking state, maybe we could at least comment those lines here (state declaration) and its initialization in
css
?