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.

RFC: Replace array value fallbacks with array value concatenation

See original GitHub issue

The problem

Currently, in emotion when an array value is encountered the values are treated as a primary value with fallback values that are duplicated for the same property.

Example:

<div
  css={{
    backgroundColor: ['hsla(134, 96%, 68%, 1.0)', 'rgb(95, 252, 132, 1)']
  }}
/>

image

This is a little known feature that is, anecdotally, rarely used. Further, I don’t believe it is as useful as it once would have been due to the current browser css support landscape.

The current parsing behavior for array values prevents the syntax from being used for anything else.

Proposed solution

One of the pain points while leading teams using object styles and using them extensively myself is having to use string concatenation/template literals and/or long hand for properties that support shorthand.

I would like to allow values to accept arrays which would get concatenated by emotion in a somewhat intelligent fashion.

Some examples

padding & margin

potential to automatically append px

All three of these would result in the same padding values

<div
  css={{
    padding: `${space[1]}px ${space[3]}px ${space[2]}px ${space[2]}px`
  }}
/>

<div
  css={{
    paddingTop: space[1],
    paddingRight: space[3],
    paddingBottom: space[2],
    paddingLeft: space[2]
  }}
/>

<div
  css={{
    padding: [space[1], space[3], space[2], space[2]] // <-- new use for array
  }}
/>

border

Properties such as border that have multiple value types would get concatenated.

could detect number types and append px

<div
  css={{
    border: `1px solid ${theme.colors.blue}`
  }}
/>

<div
  css={{
    border: [`1px solid`, theme.colors.border]
  }}
/>

<div
  css={{
    border: [1, `solid`, theme.colors.blue] // Numbers get px added automatically (?)
  }}
/>

Alternative solutions

An alternative would be to simply concatenate the values or to remove the fallback array syntax altogether and let the behavior be defined by the user if/when we get some sort of plugin system in the future.

EDIT

After thinking about this for some time I think the best solution would be to just remove the fallback value so that these types of values can be used for any situation later on. It could be used for the solution above or media query values such as those used in styled-system or facepaint.

Additional context

As more users leverage the power of themes I think we should make it as easy as possible to work with them while using object styles. I’m excited to hear everyone’s thoughts on this and their alternative idea/solutions.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:4
  • Comments:9 (8 by maintainers)

github_iconTop GitHub Comments

3reactions
zslabscommented, Feb 6, 2020

Really like this idea! One other use-case that I find pretty monotonous right now is writing transforms. Using the array syntax, this could work like this:

transform: [['rotate', '40deg'], ['translateY', theme.space.large + theme.space.base]]
1reaction
tkh44commented, Feb 13, 2020

After thinking about this for some time I think the best solution would be to just remove the fallback value so that these types of values can be used for any situation later on. It could be used for the solution above or media query values such as those used in styled-system or facepaint.

Read more comments on GitHub >

github_iconTop Results From Across the Web

RFC 7049: Concise Binary Object Representation (CBOR)
When the value of the additional information is less than 24, it is directly used ... The array's length follows the rules for...
Read more >
How do I replace an array's element? - ruby
This iterates over the values of a , and when it finds a value of 5 , it replaces that value with array...
Read more >
XPath and XQuery Functions and Operators 3.1
In cases where an option is list-valued, by convention the value may be supplied either as a sequence or as an array. Accepting...
Read more >
Node.js v19.3.0 Documentation
Specifically, the TypedArray variants accept a second argument that is a mapping function that is invoked on every element of the typed array:...
Read more >
Concise Binary Object Representation (CBOR) RFC 7049
Floating-Point Numbers and Values with No Content . . . . 12 2.4. Optional Tagging of Items . ... Items in an array...
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