2

For readability reasons, I want to split the components object passed in the createTheme function (components may have large variants) and I do have light/dark mode.

According to docs, this is how we get the design tokens:

const getDesignTokens = (mode: PaletteMode) => ({
  palette: {
    mode,
    ...(mode === 'light'
      ? {
          // palette values for light mode
          primary: amber,
          divider: amber[200],
          text: {
            primary: grey[900],
            secondary: grey[800],
          },
        }
      : {
          // palette values for dark mode
          primary: deepOrange,
          divider: deepOrange[700],
          background: {
            default: deepOrange[900],
            paper: deepOrange[900],
          },
          text: {
            primary: '#fff',
            secondary: grey[500],
          },
        }),
  },
});

After that, I create the theme depending on whether the mode is light or dark.

The problem is when I'm trying to add a component to the theme (as stated before, each component will be only referenced in the createTheme, the definition would be somewhere else) I cannot use colors from the theme without wrapping the component is a function which has the parameter the mode used.

I wonder if there is any solution like with the sx prop when you're referencing the color as a string, let's say sx={{ backgroundColor: 'button.background' }} and that would automatically be used from the theme.

Wrapping each component in a function with a parameter does the job, but I would like to know if there is any better solution.

How the code is now:

const dashedVariants = (palette) => ({
  props: {variant: 'dashed'},
  style: {
    border: `1px dashed ${palette.dashColor}`
  }
})

const Button = (palette) => ({
  styleOverrides: {},
  variants: [dashedVariants(palette)]
})

vs what I'm trying to acheive:

const dashedVariants = {
  props: {variant: 'dashed'},
  style: {
    border: `1px dashed palette.dashColor` //something like that??
  }
}

Note: I've looked over this existing question, but unfortunately this does seem to help.

1 Answer 1

3
+50
  1. Due to complexity of CSS properties, many UI toolkit and its themes only parse singular properties(or provide a detour utility); MUI's one of them. Use separate border properties to make parser working. Palette color property parser only works for the type of React.CSSProperties['color'] property. .border property is Property.Border type. The color parser won't work in this case.

  2. Palette type only works with appropriate properties. it does not provide dashColor property. according to MUI doc, working properties are:

.palette.primary.light
.palette.primary.main
.palette.primary.dark
.palette.primary.contrastText
// ...
.palette.secondary
.palette.error
.palette.warning
.palette.info
.palette.success
const theme = {
  palette: {
    secondary: {
      main: '#...' // user defiend color
    }
  }
}

const dashedVariants = {
  props: {variant: 'dashed'},
  style: {
    borderColor: 'secondary.main',
    borderWidth: '1px',
    borderStyle: 'dashed',
  }
}
  1. There's an experimental CSS variable feature. With this, it is possible to define CSS variable inside complex property. This is probably the closest to the goal but it's currently experimental stage, might be unstable for production use. I am also looking forward to using this in the future.
1
  • 1
    about dashColor, it was just an example + it was added to the theme. My only issue is that neither something like secondary.main work. I know about the CSS variables feature, but since it's not stable I'm not going to use it yet. Jul 11, 2022 at 15:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.