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.

[bug] Style value conflicts with alias

See original GitHub issue

What do we do if the style value we want to pass conflicts with the key for one of our aliases?

const theme = {
  space: [0, 4, 8, 16, 32, 64, 128, 256, 512]
}

If we have the above theme, then calling space[1] = 4, and space[6] = 128.

Situation 1:

Let’s say I didn’t want to use the 128 value, but instead, I just wanted to pass padding of 6 pixels.

<View
  sx={{
    padding: 6 // returns 128
  }}
/>

So, how do I get 6 in there?

The answer: I could just pass the correct value to style instead of sx. However, style currently is treated as an sx prop. Since this doesn’t make much sense, I should get rid of this line and just use the raw style object.

<View
  style={{
    padding: 6 // returns 128, should return 6 after refactor
  }}
/>

Situation 2:

While that solves situation 1, what if I want to use theme values to calculate 6px?

In my case, I want to grab space[1] (i.e. 4px), and multiply it by 1.5 to get 6px.

<View
  sx={{
    // this is 4 * 1.5 = 6
    // but wait, it gives 128, from the theme!
    padding: (theme) => theme.space[1] * 1.5
  }}
/>

Alright, so once again, I end up with 128px, not 6px.

To borrow from situation 1:

const { theme } = useThemeUI()

<View
  style={{
    padding: theme.space[1] * 1.5 // this would solve it after the refactor of changing sx -> style
  }}
/>

Hmm…there’s still one edge case, though. What if I wanted to use that value in a responsive array?

const { theme } = useThemeUI()
<View
  sx={{
    padding: [theme.space[1] * 1.5, theme.space[2] * 1.5]
  }}
/>

In this case, the only way to use responsive arrays is via the sx prop. But if I use the sx prop, I am stuck with theme values, and can’t use the 6px work-around from web.

What does theme-ui do?

This problem is easily solved in theme-ui. You can just pass a raw '6px' string, and boom, you’re done.

But React Native doesn’t allow '6px', because it only uses numbers.

Solution

Could we edit the css function to check for <number>px values, and turn these into numbers to avoid making RN angry?

If so, then this would work:

<View
  sx={{
    padding: theme => [`${theme.space[1] * 1.5}px`, `${theme.space[2] * 1.5}px`]
  }}
/>

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
nandorojocommented, Jan 5, 2021

Thought of a solution. Allow 16px values, and in the css function, check if a value is a string that endsWith(px), and if so, make it a raw number that avoids an alias.

1reaction
nandorojocommented, Sep 15, 2020

Yeah, it’s a bit tricky at the moment…I’m sticking with the annoying workaround for now. It’s pretty uncommon to run into this, and I like using the array syntax, so I’ll close for the time being.

Read more comments on GitHub >

github_iconTop Results From Across the Web

RuboCop `Style/MutableConstant` conflicts with `T.type_alias`.
Not really relevant to this bug, but I found this bug while adding type information to a method which expects recursive types, i.e....
Read more >
Resolving alias conflicts - IBM
When an alias conflict exists, select a business object in the Source Objects table. · Select the Add/Modify Alias action. If a duplicate...
Read more >
Full Text Bug Listing - GCC, the GNU Compiler Collection - GNU.org
When a memory tag does not have aliases, we generate a virtual operand of the base symbol. The basic idea in the new...
Read more >
jQuery.noConflict() | jQuery API Documentation
Create a different alias instead of jQuery to use in the rest of the script. $( "content" ). style. display = "none";
Read more >
What is the expected behavior when Username Alias conflicts ...
1) If an alias value of an existing user matches the primary username OR an existing alias of an existing user during a...
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