Skip to content
This repository was archived by the owner on Jun 20, 2022. It is now read-only.

Commit 29448df

Browse files
committed
fix(emotion): fix css function
1 parent 49e975d commit 29448df

File tree

3 files changed

+46
-10
lines changed

3 files changed

+46
-10
lines changed

.babelrc.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
const { STYLED_ENGINE = 'styled-components' } = process.env
22

33
const getStyledEnginePlugins = () => {
4+
if (process.env.NODE_ENV === 'test') return []
5+
46
switch (STYLED_ENGINE) {
57
case 'emotion':
68
return ['babel-plugin-emotion']

packages/shared/core/styled-engine/emotion.js

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,22 @@ export const withTheme = Component => {
2424
return SafeWithTheme
2525
}
2626

27-
const css = (parts, ...args) => {
28-
if (
29-
typeof parts === 'function' ||
30-
args.some(arg => typeof arg === 'function')
31-
) {
27+
const isInterpolation = func => {
28+
if (typeof func !== 'function') return false
29+
// eslint-disable-next-line no-underscore-dangle
30+
if (func.__emotion_styles) return false
31+
return true
32+
}
33+
34+
const css = (...args) => {
35+
if (args.some(arg => isInterpolation(arg))) {
3236
return props => {
3337
if (!props) {
3438
throw new Error('Wrong usage of `css` function')
3539
}
3640

3741
const transform = arg => {
38-
if (typeof arg === 'function') {
42+
if (isInterpolation(arg)) {
3943
return transform(arg(props))
4044
}
4145

@@ -47,13 +51,11 @@ const css = (parts, ...args) => {
4751
}
4852

4953
const transformedArgs = transform(args)
50-
const transformedParts =
51-
typeof parts === 'function' ? transform(parts) : parts
52-
return emotionCss(transformedParts, ...transformedArgs)
54+
return emotionCss(...transformedArgs)
5355
}
5456
}
5557

56-
return emotionCss(parts, ...args)
58+
return emotionCss(...args)
5759
}
5860

5961
function patchStyledComponent(StyledComponent) {
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import styled, { css } from './emotion'
2+
3+
describe('emotion styled engine', () => {
4+
describe('#css', () => {
5+
it('should work values without functions', () => {
6+
expect(css({ color: 'red' })).toBe('css-tokvmb')
7+
expect(css({ color: 'red' }, { fontSize: 10 })).toBe('css-ia2oof')
8+
expect(css('color: red;', 'font-size: 10px;')).toBe('css-odcmjf')
9+
})
10+
11+
it('should handle components', () => {
12+
const Dummy = styled('div')`
13+
color: blue;
14+
`
15+
expect.assertions(1)
16+
17+
try {
18+
css(Dummy, 'font-size: 10px;')
19+
} catch (error) {
20+
expect(error.message).toBe(
21+
'Component selectors can only be used in conjunction with babel-plugin-emotion.',
22+
)
23+
}
24+
})
25+
26+
it('should handle interpolations', () => {
27+
expect(
28+
css('font-size: 10px;', props => ({ color: props.color })),
29+
).toBeInstanceOf(Function)
30+
})
31+
})
32+
})

0 commit comments

Comments
 (0)