question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Feature request: Functions as values for object literals

See original GitHub issue

Hopefully 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:open
  • Created 5 years ago
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
mrmckebcommented, Jul 19, 2019

@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).

const Spinner = styled.div({
  width: 40,
});

// width: 40px

A real-life example here: VSCode: image Chrome: image

And thanks on the TypeScript side!

1reaction
probablyupcommented, Mar 21, 2019

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 into objToCss and invoke the function with it (but only if confirmed that it’s a function and not a SFC.)

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found