Skip to content

Commit 6c9317d

Browse files
committed
teach plugin about spread variables in css prop
1 parent 264a3b5 commit 6c9317d

File tree

5 files changed

+87
-5
lines changed

5 files changed

+87
-5
lines changed

src/visitors/transpileCssProp.js

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -180,11 +180,28 @@ export default t => (path, state) => {
180180

181181
acc.push(property)
182182
} else if (t.isSpreadElement(property)) {
183-
// recurse for objects within objects (e.g. {'::before': { content: x }})
184-
property.argument.properties = property.argument.properties.reduce(
185-
propertiesReducer,
186-
[]
187-
)
183+
// handle spread variables and such
184+
185+
if (t.isObjectExpression(property.argument)) {
186+
property.argument.properties = property.argument.properties.reduce(
187+
propertiesReducer,
188+
[]
189+
)
190+
} else {
191+
replaceObjectWithPropFunction = true
192+
193+
const identifier = getLocalIdentifier(path)
194+
195+
elem.node.attributes.push(
196+
t.jSXAttribute(
197+
t.jSXIdentifier(identifier.name),
198+
t.jSXExpressionContainer(property.argument)
199+
)
200+
)
201+
202+
property.argument = t.memberExpression(p, identifier)
203+
}
204+
188205
acc.push(property)
189206
} else if (
190207
// if a non-primitive value we have to interpolate it

test/fixtures/transpile-css-prop-all-options-on/code.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,3 +255,17 @@ const ObjectFnSimpleInterpolationInKey = p => {
255255
</Thing3>
256256
)
257257
}
258+
259+
const ObjectPropWithSpread = () => {
260+
const css = { color: 'red' }
261+
const playing = true
262+
263+
return (
264+
<div
265+
css={{
266+
...css,
267+
...(playing ? { opacity: 0, bottom: '-100px' } : {}),
268+
}}
269+
/>
270+
)
271+
}

test/fixtures/transpile-css-prop-all-options-on/output.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,17 @@ var ObjectFnSimpleInterpolationInKey = function ObjectFnSimpleInterpolationInKey
251251
</_StyledThing6>;
252252
};
253253

254+
var ObjectPropWithSpread = function ObjectPropWithSpread() {
255+
var css = {
256+
color: 'red'
257+
};
258+
var playing = true;
259+
return <_StyledDiv $_css20={css} $_css21={playing ? {
260+
opacity: 0,
261+
bottom: '-100px'
262+
} : {}} />;
263+
};
264+
254265
var _StyledP = (0, _styledComponents["default"])("p").withConfig({
255266
displayName: "code___StyledP",
256267
componentId: "sc-7evkve-10"
@@ -403,3 +414,10 @@ var _StyledP14 = (0, _styledComponents["default"])("p").withConfig({
403414
color: p.$_css15
404415
};
405416
});
417+
418+
var _StyledDiv = (0, _styledComponents["default"])("div").withConfig({
419+
displayName: "code___StyledDiv",
420+
componentId: "sc-7evkve-30"
421+
})(function (p) {
422+
return _objectSpread(_objectSpread({}, p.$_css20), p.$_css21);
423+
});

test/fixtures/transpile-css-prop/code.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,3 +200,17 @@ const ObjectPropMixedInputs = p => {
200200
</p>
201201
)
202202
}
203+
204+
const ObjectPropWithSpread = () => {
205+
const css = { color: 'red' }
206+
const playing = true
207+
208+
return (
209+
<div
210+
css={{
211+
...css,
212+
...(playing ? { opacity: 0, bottom: '-100px' } : {}),
213+
}}
214+
/>
215+
)
216+
}

test/fixtures/transpile-css-prop/output.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ var _templateObject, _templateObject2, _templateObject3, _templateObject4, _temp
66

77
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
88

9+
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
10+
11+
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
12+
913
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
1014

1115
function _taggedTemplateLiteral(strings, raw) { if (!raw) { raw = strings.slice(0); } return Object.freeze(Object.defineProperties(strings, { raw: { value: Object.freeze(raw) } })); }
@@ -173,6 +177,17 @@ var ObjectPropMixedInputs = function ObjectPropMixedInputs(p) {
173177
</_StyledP13>;
174178
};
175179

180+
var ObjectPropWithSpread = function ObjectPropWithSpread() {
181+
var css = {
182+
color: 'red'
183+
};
184+
var playing = true;
185+
return <_StyledDiv $_css13={css} $_css14={playing ? {
186+
opacity: 0,
187+
bottom: '-100px'
188+
} : {}} />;
189+
};
190+
176191
var _StyledP = (0, _styledComponents["default"])("p")(_templateObject3 || (_templateObject3 = _taggedTemplateLiteral(["flex: 1;"])));
177192

178193
var _StyledP2 = (0, _styledComponents["default"])("p")(_templateObject4 || (_templateObject4 = _taggedTemplateLiteral(["\n flex: 1;\n "])));
@@ -236,3 +251,7 @@ var _StyledP13 = (0, _styledComponents["default"])("p")(function (p) {
236251
}
237252
};
238253
});
254+
255+
var _StyledDiv = (0, _styledComponents["default"])("div")(function (p) {
256+
return _objectSpread(_objectSpread({}, p.$_css13), p.$_css14);
257+
});

0 commit comments

Comments
 (0)