diff --git a/.changeset/nice-apples-arrive.md b/.changeset/nice-apples-arrive.md new file mode 100644 index 0000000000..dcf9b1e70f --- /dev/null +++ b/.changeset/nice-apples-arrive.md @@ -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)}; +` +``` diff --git a/packages/native/test/__snapshots__/native-styled.test.js.snap b/packages/native/test/__snapshots__/native-styled.test.js.snap index 10c1d69041..3c29e1ac52 100644 --- a/packages/native/test/__snapshots__/native-styled.test.js.snap +++ b/packages/native/test/__snapshots__/native-styled.test.js.snap @@ -68,6 +68,20 @@ exports[`Emotion native styled should render primitive with style prop 1`] = ` `; +exports[`Emotion native styled should render styles correctly from all nested style factories 1`] = ` + + Hello World + +`; + exports[`Emotion native styled should render the primitive on changing the props 1`] = ` { "'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(Hello World) + .toJSON() + + expect(tree).toMatchSnapshot() + }) }) diff --git a/packages/primitives-core/src/css.js b/packages/primitives-core/src/css.js index 6f820d526f..308a993d23 100644 --- a/packages/primitives-core/src/css.js +++ b/packages/primitives-core/src/css.js @@ -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<*>) { @@ -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 @@ -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) }