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: CSS variables can't be a space character

See original GitHub issue

React version: v16/v17

Steps To Reproduce

  1. https://codesandbox.io/s/empty-css-value-jmbfl?file=/src/App.js
  2. Notice the “should be black border” has a pink border
  3. Dangerously setting inner HTML works because it doesn’t go through reacts trim() flow
  4. I’ve tracked the suspect code to here https://github.com/facebook/react/blob/6cbb9394d1474e3a728b49dc7f3a11d61a421ce3/packages/react-dom/src/shared/dangerousStyleValue.js#L44

Link to code example: https://codesandbox.io/s/empty-css-value-jmbfl?file=/src/App.js

The current behavior

CSS variables have their value trimmed - resulting in ' ' become '' which then removes the CSS variable from the browser.

image

The expected behavior

The ' ' value should not be trimmed.

image

One potential option is to, for any custom properties that have a space value, don’t trim.

if (isCustomProperty && value === ' ') {
   return value;
}

return ('' + value).trim();

I’m happy to do the bug fix. I would also love for this to get released on v16.


Found a work around:

https://codesandbox.io/s/empty-css-value-forked-g5jud?file=/src/App.js

style={{ "--border-color": "var(--, )" }}

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:5
  • Comments:12 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
eps1loncommented, Dec 22, 2020

Interestingly, trimming was intentionally added and considered “correct” (see commit message): https://github.com/facebook/react/commit/2bff5c5c7e776a183ed9b429f2cb46cc044d9bc1.

Considering that whitespace is a valid token for custom properties (2. Defining Custom Properties: the --* family of properties -> 8.2. Defining Arbitrary Contents: the <declaration-value> and <any-value> productions), it doesn’t seem like trimming is correct.

Maybe it was added to fix a browser quirk? Would be interesting to see if the described quirk still happens in React 17 without trimming the values.

1reaction
eps1loncommented, Jan 1, 2021

what do we want for more evidence or investigation 😄

I guess propose the change via PR and then we’ll see what core members think about this change.

Read more comments on GitHub >

github_iconTop Results From Across the Web

[cssom][css-variables] Whitespaces in serialization of custom ...
Given that we want to preserve whitespaces for value of custom properties, I suggest we state explicitly that preceding whitespaces of property ...
Read more >
Using CSS custom properties (variables) - MDN Web Docs
Each CSS property can be assigned a defined set of values. If you try to assign a value to a property that is...
Read more >
Variable name with space is it valid ? (Example) - Treehouse
Hi Arun,. Spaces can't exist in variable names as the syntax is invalid, you can use either an underscore or camel casing instead...
Read more >
Less adds unexpected space to variable - Stack Overflow
The problem happens only for selectors which use selector interpolation and are nested within one or more parent selectors. It can be solved...
Read more >
A user's guide to CSS variables – Increment: Frontend
(Anything goes, even white space.) They're loosely parsed at declaration time, but error handling isn't done until they're used in a noncustom property....
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