Feature request: Functions as values for object literals
See original GitHub issueHopefully this isn’t something that’s been raised before, I couldn’t find a related issue.
I love the new object support in styled-components
, and while I know that this isn’t something that aligned with the project’s original goals, I wonder if this could be improved to support functions as values? There may also be some good technical reasons why this shouldn’t happen, so if that’s the case, please share those too.
As an example:
var A = styled.a(({ big }) => ({
fontSize: big ? 24 : 16,
}));
Would become:
var A = styled.a({
fontSize: ({ big }) => big ? 24 : 16,
});
Issue Analytics
- State:
- Created 5 years ago
- Comments:10 (4 by maintainers)
Top Results From Across the Web
Object Literals and Function Constructors in Javascript - Medium
The value can be a number, a string, an array, a function, or even another object. If the value is a function then...
Read more >ES6 in Action: Enhanced Object Literals - SitePoint
This article looks at what's possible with object literals in JavaScript, especially in the light of recent ECMAScript updates.
Read more >Returning Object Literals from Arrow Functions in JavaScript
Here's how to return an object literal from an expression-bodied arrow function as introduced by ECMAScript 2015.
Read more >Why Object Literals in JavaScript Are Cool - Dmitri Pavlutin
The object literals are using undefined and number 15 to setup __proto__ value. Because only an object or null are allowed to be...
Read more >Object initializer - JavaScript - MDN Web Docs - Mozilla
Object literal syntax vs. JSON · JSON only permits property definition using the "property": value syntax. · JSON object property values can only ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@d7my11 I don’t disagree, but this is not the behaviour that you see with an object.
This is also the standard behaviour whenever you set any style in JavaScript. Unit-less values default to pixels (where appropriate).
A real-life example here: VSCode: Chrome:
And thanks on the TypeScript side!
Hmm I don’t see why not, I’d accept a PR for it. You’d need to make the change in
flatten
to pass executionContext intoobjToCss
and invoke the function with it (but only if confirmed that it’s a function and not a SFC.)