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: styles object using css variables and both a shorthand and a specific property renders incorrectly

See original GitHub issue

React does not produce the correct css inline styles when using css variables for both the shorthand property and another specific one (like padding and paddingRight).

The styles object:

{
  padding: "calc(var(--spacing) * 1)",
  paddingRight: "calc(var(--spacing) * 3)",
  paddingBottom: "calc(var(--spacing) * 4)"
};

produces the following styles:

image

and the following html:

<span style="padding-top: ; padding-right: calc(var(--spacing) * 3); padding-bottom: calc(var(--spacing) * 4); padding-left: ;">App</span>

even though the computed properties tab of the dev-tools appear to be correct and the padding is properly rendered in the screen:

image

If I remove the css-variable, everything works as expected.

React version: From v15.0.0 to 16.12.0

Note: Below v15.0.0 the styles are correctly produced:

<span style="padding:calc(var(--spacing) * 1);padding-right:calc(var(--spacing) * 3);padding-bottom:calc(var(--spacing) * 4);">App</span>

Steps To Reproduce

  1. Add a style object to a component that has both a property shorthand and a specific one (like padding and paddingRight) and uses a css variable (like var(--spacing).
  2. Render that component and inspect using dev-tools.

Link to code example: https://codesandbox.io/s/heuristic-wood-bjr1y

styles object:

{
  padding: "calc(var(--spacing) * 1)",
  paddingRight: "calc(var(--spacing) * 3)",
  paddingBottom: "calc(var(--spacing) * 4)"
};

The current behavior

React does not produces the correct css inline styles when using css variables for both the shorthand property and another specific one:

<span style="padding-top: ; padding-right: calc(var(--spacing) * 3); padding-bottom: calc(var(--spacing) * 4); padding-left: ;">App</span>

The expected behavior

Inline styles using css variables that have both a shorthand and a specific one should produce the correct styles.

<span style="padding: calc(var(--spacing) * 1); padding-right: calc(var(--spacing) * 3); padding-bottom: calc(var(--spacing) * 4);">App</span>

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
syranidecommented, Jan 23, 2020

@bvaughn Hey! I can’t speak for the CSS-variable part of this question. But not supporting overlapping styles is simply put a performance/complexity problem. If React does not support overlapping styles it can naively iterate the prev/next and apply any changes as a property, very quick and simple. If overlapping styles are supported then React must have knowledge of how shorthand properties are decomposed and to be able to figure out whether a changed property shadows/overlaps another property and apply them in correct order. This is significantly slower, more complex and rather fragile. I’m not aware of any way of solving this issue without incurring significant overhead costs given the current set of features exposed by browsers, which depending on what kind of performance/use-case one expects out of React makes it unacceptable.

Since I’m not actively involved nowadays I can’t speak for why there still isn’t a warning, but I would positively think it’s because “no one has got around to building it” as you say. The warning should be rather straightforward to implement for at least the current/common set of properties.

1reaction
bvaughncommented, Jan 23, 2020

I don’t know the history of this particular part of ReactDOM, so I’m not sure what thought has gone into this before and if there are any good reasons for the current behavior (and lack of warning) other than “no one has got around to building it”

I agree though that a warning would be nice.

I’m going to tag it for discussion and see if others can share more context.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using CSS custom properties (variables) - MDN Web Docs
Custom properties (sometimes referred to as CSS variables or cascading variables) are entities defined by CSS authors that contain specific ...
Read more >
Find invalid, overridden, inactive, and other CSS
The shorthand property with a drop-down list. In this example, two of four shortened properties are actually overridden. # Non-editable. The ...
Read more >
A Complete Guide to Custom Properties | CSS-Tricks
Everything important and useful to know about CSS Custom Properties. Like that they are often referred to as "CSS Variables" but that's not ......
Read more >
Roadmap - Prince XML
Support space-separated syntax for colors and with CSS variables. ... Fix bug in which Windows-style CR/LF newlines in <pre> elements in HTML documents...
Read more >
CSS - Wikipedia
Cascading Style Sheets (CSS) is a style sheet language used for describing the presentation of a document written in a markup language such...
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