Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions .changeset/nice-apples-arrive.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
'@emotion/native': patch
'@emotion/primitives': patch
'@emotion/primitives-core': patch
---

Fixed an issue with styles being lost for nested factory calls like:

```js
const bgColor = color => css`
background-color: ${color};
`

const Text = styled.Text`
color: hotpink;
${({ backgroundColor }) => bgColor(backgroundColor)};
`
```
14 changes: 14 additions & 0 deletions packages/native/test/__snapshots__/native-styled.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,20 @@ exports[`Emotion native styled should render primitive with style prop 1`] = `
</Text>
`;

exports[`Emotion native styled should render styles correctly from all nested style factories 1`] = `
<Text
backgroundColor="blue"
style={
Object {
"backgroundColor": "blue",
"color": "hotpink",
}
}
>
Hello World
</Text>
`;

exports[`Emotion native styled should render the primitive on changing the props 1`] = `
<Text
decor="hotpink"
Expand Down
19 changes: 18 additions & 1 deletion packages/native/test/native-styled.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as React from 'react'
import renderer from 'react-test-renderer'
import { ThemeProvider } from '@emotion/react'
import styled from '@emotion/native'
import styled, { css } from '@emotion/native'
import reactNative from 'react-native'

const StyleSheet = reactNative.StyleSheet
Expand Down Expand Up @@ -150,4 +150,21 @@ describe('Emotion native styled', () => {
"'padding' shorthand property requires units for example - padding: 20px or padding: 10px 20px 40px 50px"
)
})

it('should render styles correctly from all nested style factories', () => {
const bgColor = color => css`
background-color: ${color};
`

const Text = styled.Text`
color: hotpink;
${({ backgroundColor }) => bgColor(backgroundColor)};
`

const tree = renderer
.create(<Text backgroundColor="blue">Hello World</Text>)
.toJSON()

expect(tree).toMatchSnapshot()
})
})
9 changes: 7 additions & 2 deletions packages/primitives-core/src/css.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { interleave } from './utils'
// this is done so we don't create a new
// handleInterpolation function on every css call
let styles
let buffer
let buffer = ''
let lastType

function handleInterpolation(interpolation: *, i: number, arr: Array<*>) {
Expand Down Expand Up @@ -80,6 +80,7 @@ function handleInterpolation(interpolation: *, i: number, arr: Array<*>) {
// This enables us to use the css``/css({}) in any environment (Native | Sketch | Web)
export function createCss(StyleSheet: Object) {
return function css(...args: any) {
const prevBuffer = buffer
let vals

// these are declared earlier
Expand All @@ -95,7 +96,11 @@ export function createCss(StyleSheet: Object) {
vals = interleave(args)
}

vals.forEach(handleInterpolation, this)
try {
vals.forEach(handleInterpolation, this)
} finally {
buffer = prevBuffer
}

return StyleSheet.flatten(styles)
}
Expand Down