Bug: CSS variables can't be a space character
See original GitHub issueReact version: v16/v17
Steps To Reproduce
- https://codesandbox.io/s/empty-css-value-jmbfl?file=/src/App.js
- Notice the “should be black border” has a pink border
- Dangerously setting inner HTML works because it doesn’t go through reacts
trim()
flow - 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.
The expected behavior
The ' '
value should not be trimmed.
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:
- Created 3 years ago
- Reactions:5
- Comments:12 (1 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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.
I guess propose the change via PR and then we’ll see what core members think about this change.