Skip to content

Commit b594e23

Browse files
authored
Merge pull request #359 from styled-components/352/object-css-prop
handle complex expressions inside CSS prop object syntax
1 parent d025725 commit b594e23

File tree

3 files changed

+56
-2
lines changed

3 files changed

+56
-2
lines changed

src/visitors/transpileCssProp.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,11 @@ export default t => (path, state) => {
122122

123123
if (!css) return
124124

125-
elem.node.attributes = elem.node.attributes.filter(attr => attr !== path.node)
125+
// strip off css prop from final output
126+
elem.node.attributes = elem.node.attributes.filter(x =>
127+
t.isJSXAttribute(x) ? x.name.name !== 'css' : false
128+
)
129+
126130
elem.node.name = t.jSXIdentifier(id.name)
127131

128132
if (elem.parentPath.node.closingElement) {
@@ -155,7 +159,10 @@ export default t => (path, state) => {
155159
// but not a object reference shorthand like css={{ color }}
156160
(t.isIdentifier(property.value)
157161
? property.key.name !== property.value.name
158-
: true))
162+
: true) &&
163+
// and not a tricky expression
164+
!t.isLogicalExpression(property.value) &&
165+
!t.isConditionalExpression(property.value))
159166
) {
160167
replaceObjectWithPropFunction = true
161168

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,3 +214,21 @@ const ObjectPropWithSpread = () => {
214214
/>
215215
)
216216
}
217+
218+
const ObjectInterpolationLogical = ({ bg, content, height, width, ...p }) => {
219+
return (
220+
<p
221+
css={{
222+
background: bg || 'red',
223+
height: height ?? '100%',
224+
width: width ? `${width}px` : '100%',
225+
'::before': {
226+
content,
227+
},
228+
}}
229+
{...p}
230+
>
231+
H
232+
</p>
233+
)
234+
}

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
var _styledComponents = _interopRequireDefault(require("styled-components"));
44

5+
var _excluded = ["bg", "content", "height", "width"];
6+
57
var _templateObject, _templateObject2, _templateObject3, _templateObject4, _templateObject5, _templateObject6, _templateObject7, _templateObject8, _templateObject9, _templateObject10, _templateObject11, _templateObject12, _templateObject13, _templateObject14, _templateObject15, _templateObject16, _templateObject17;
68

79
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
@@ -10,6 +12,10 @@ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (O
1012

1113
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; }
1214

15+
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
16+
17+
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
18+
1319
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; }
1420

1521
function _taggedTemplateLiteral(strings, raw) { if (!raw) { raw = strings.slice(0); } return Object.freeze(Object.defineProperties(strings, { raw: { value: Object.freeze(raw) } })); }
@@ -188,6 +194,18 @@ var ObjectPropWithSpread = function ObjectPropWithSpread() {
188194
} : {}} />;
189195
};
190196

197+
var ObjectInterpolationLogical = function ObjectInterpolationLogical(_ref4) {
198+
var bg = _ref4.bg,
199+
content = _ref4.content,
200+
height = _ref4.height,
201+
width = _ref4.width,
202+
p = _objectWithoutProperties(_ref4, _excluded);
203+
204+
return <_StyledP14 $_css15={bg || 'red'} $_css16={height !== null && height !== void 0 ? height : '100%'} $_css17={width ? "".concat(width, "px") : '100%'} $_css18={content}>
205+
H
206+
</_StyledP14>;
207+
};
208+
191209
var _StyledP = (0, _styledComponents["default"])("p")(_templateObject3 || (_templateObject3 = _taggedTemplateLiteral(["flex: 1;"])));
192210

193211
var _StyledP2 = (0, _styledComponents["default"])("p")(_templateObject4 || (_templateObject4 = _taggedTemplateLiteral(["\n flex: 1;\n "])));
@@ -255,3 +273,14 @@ var _StyledP13 = (0, _styledComponents["default"])("p")(function (p) {
255273
var _StyledDiv = (0, _styledComponents["default"])("div")(function (p) {
256274
return _objectSpread(_objectSpread({}, p.$_css13), p.$_css14);
257275
});
276+
277+
var _StyledP14 = (0, _styledComponents["default"])("p")(function (p) {
278+
return {
279+
background: p.$_css15,
280+
height: p.$_css16,
281+
width: p.$_css17,
282+
'::before': {
283+
content: p.$_css18
284+
}
285+
};
286+
});

0 commit comments

Comments
 (0)