Skip to content

[system] Fix CSS variables undefined when theme contains URL values with //#48499

Open
starboyvarun wants to merge 2 commits into
mui:masterfrom
starboyvarun:48267-css-vars-url-value
Open

[system] Fix CSS variables undefined when theme contains URL values with //#48499
starboyvarun wants to merge 2 commits into
mui:masterfrom
starboyvarun:48267-css-vars-url-value

Conversation

@starboyvarun
Copy link
Copy Markdown
Contributor

Summary

Fixes #48267.

When a Material UI theme includes string values with // — most commonly background image URLs like https://picsum.photos/200/300 — all CSS custom properties (--mui-*) become undefined in the browser.

Root cause: Stylis (the CSS preprocessor used by Emotion) treats // as a single-line comment. When cssVarsParser serializes such a value into a :root {} block as a CSS variable declaration, Stylis strips everything from // to the end of the line, corrupting all subsequent --mui-* declarations in the block.

Fix: In cssVarsParser, detect string values containing // before generating the CSS variable. Skip the variable and, in development, emit a console.error that identifies the offending theme key and points users to shouldSkipGeneratingVar as the escape hatch.

Changes:

  • cssVarsParser.ts – skip CSS var generation for string values containing //; emit a dev-mode console.error
  • cssVarsParser.test.ts – two new tests: one verifies the variable is skipped and the dev error is fired; the other verifies only the URL value is dropped while adjacent variables remain intact

Test plan

  • pnpm test:unit -- --project "*:@mui/system" — all 549 tests pass
  • TypeScript: tsc --noEmit passes with no errors
  • ESLint + Prettier: both pass with no warnings

Stylis, the CSS preprocessor used by Emotion, treats `//` as a
single-line comment. When a theme value such as a background image URL
(`https://...`) is serialized into a `:root {}` block as a CSS variable,
stylis strips everything after `//`, corrupting all subsequent CSS
variable declarations and making them undefined in the browser.

Detect string values containing `//` in `cssVarsParser` and skip
generating CSS variables for them. In development, emit a `console.error`
that names the offending theme key and points users to
`shouldSkipGeneratingVar` as the escape hatch.
@code-infra-dashboard
Copy link
Copy Markdown

code-infra-dashboard Bot commented May 8, 2026

Deploy preview

https://deploy-preview-48499--material-ui.netlify.app/

Bundle size

Bundle Parsed size Gzip size
@mui/material 🔺+47B(+0.01%) 🔺+16B(+0.01%)
@mui/lab 0B(0.00%) 0B(0.00%)
@mui/private-theming 0B(0.00%) 0B(0.00%)
@mui/system 🔺+47B(+0.07%) 🔺+14B(+0.06%)
@mui/utils 0B(0.00%) 0B(0.00%)

Details of bundle changes


Check out the code infra dashboard for more information about this PR.

Add a note to the CSS theme variables section explaining that Stylis
treats `//` as a CSS comment, so theme string values containing `//`
(e.g. background image URLs) must be excluded via `shouldSkipGeneratingVar`
to avoid corrupting the `:root {}` CSS variable block.
theme,
(keys, value: string | number | object, arrayKeys) => {
if (typeof value === 'string' || typeof value === 'number') {
if (typeof value === 'string' && value.includes('//')) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldSkipGeneratingVar doesn't seem to silence this new dev warning which contradicts the advice in the warning

@zannager zannager added the scope: system The system, the design tokens / styling foundations used across components. eg. @mui/system with MUI label May 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

scope: system The system, the design tokens / styling foundations used across components. eg. @mui/system with MUI

Projects

None yet

Development

Successfully merging this pull request may close these issues.

--mui-* css vars are undefined when augmented theme contains property with URL value

3 participants