mui / mui/material-ui

CSS Variables and custom colors: can't access property "main" #46549

Open
#46,555 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

customization: theme scope: all components support: question
Dominant language
JavaScript
Stars
99.1k
Forks
32.5k
Avg merge
2d 17h
Merged PRs (30d)
106

Description

Steps to reproduce

Steps:

  1. Open this link to live example: https://stackblitz.com/edit/github-ym4w3uq7?file=src%2FApp.tsx
Current behavior

A crash in Button/Button.js:

   "--variant-textColor": (theme.vars || theme).palette[color].main,

Uncaught TypeError: can't access property "main", (intermediate value).palette[color] is undefined

Expected behavior

A purple button.

Context

I'm trying to use custom palette colors and CSS variables but I can't figure out how from the documentation.

My previous ticket got closed with this comment:

You don't need a baseTheme. Do this instead:

const theme = createTheme({
  cssVariables: true,
  palette: {
    purple: baseTheme.palette.augmentColor({
      color: { main: '#7f007f' },
      name: 'purple',
    }),
  },
});

If you add the palette to the 2nd argument, it will replace the default palette and that's why the error occur because the default Button is trying to access the primary color but there is no more primary palette.

But there are a few issues with that suggestion:

  • It uses baseTheme itself as there's no other way to access augmentColor
  • If there were settings in baseTheme this discards them
  • Without cssVariables: true my example works and the palette is deep-merged correctly.

Given that the theme merging works without cssVariables: true, what's the correct pattern with cssVariables: true?

Your environment

This is the Stackblitz container on Firefox.

npx @mui/envinfo

System:
OS: Linux 5.0 undefined
Binaries:
Node: 20.19.1 - /usr/local/bin/node
npm: 10.8.2 - /usr/local/bin/npm
pnpm: 8.15.6 - /usr/local/bin/pnpm
Browsers:
Chrome: Not Found
npmPackages:
@emotion/react: latest => 11.14.0
@emotion/styled: latest => 11.14.1
@mui/core-downloads-tracker: 7.2.0
@mui/icons-material: latest => 7.2.0
@mui/material: latest => 7.2.0
@mui/private-theming: 7.2.0
@mui/styled-engine: 7.2.0
@mui/system: 7.2.0
@mui/types: 7.4.4
@mui/utils: 7.2.0
@types/react: latest => 19.1.8
react: latest => 19.1.0
react-dom: latest => 19.1.0
typescript: latest => 5.8.3

Search keywords: can't access property "main" cssvariables custom colors

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked StackBlitz reproduction and the failing lookup in Button/Button.js. Compare how the custom palette is merged with and without cssVariables: true, including the documented augmentColor pattern. Done means the purple button renders without the palette[color] crash while the default palette remains available.

Written by the indexing model from the issue text.

Assessment

Tech stack
react, typescript
Domain
design, frontend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.