Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow custom color variants for Chips #26695

Closed
i-tozer opened this issue Jun 11, 2021 · 5 comments
Closed

Allow custom color variants for Chips #26695

i-tozer opened this issue Jun 11, 2021 · 5 comments
Labels
component: chip This is the name of the generic UI component, not the React module! support: question Community support but can be turned into an improvement

Comments

@i-tozer
Copy link

i-tozer commented Jun 11, 2021

Please allow custom color variants for Chips.

Allowing custom color variants is a known issue: #13875

There is a fix already quoted here: #13875 (comment)

It works for Buttons but it does not work for Chips.

@i-tozer i-tozer added the status: waiting for maintainer These issues haven't been looked at yet by a maintainer label Jun 11, 2021
@siriwatknp
Copy link
Member

siriwatknp commented Jun 11, 2021

Can you provide a codesandbox, so that we can reproduce?

Here is the template for Chip. https://codesandbox.io/s/og19w?file=/demo.js

@siriwatknp siriwatknp added the status: waiting for author Issue with insufficient information label Jun 11, 2021
@i-tozer
Copy link
Author

i-tozer commented Jun 11, 2021

@siriwatknp
Copy link
Member

You need to add all of the properties to the palette

const theme = createTheme({
  palette: {
    neutral: {
      main: "#546e7a",
      light: '#90a4ae',
      dark: '#37474f',
      contrastText: '#ffffff',
    }
  }
});

https://codesandbox.io/embed/dazzling-bartik-j1gh6?fontsize=14&hidenavigation=1&theme=dark

will add the to the documentation.

@siriwatknp siriwatknp removed status: waiting for author Issue with insufficient information status: waiting for maintainer These issues haven't been looked at yet by a maintainer labels Jun 11, 2021
@oliviertassinari oliviertassinari added the support: question Community support but can be turned into an improvement label Jun 13, 2021
@oliviertassinari
Copy link
Member

will add the to the documentation.

@siriwatknp We might be able to do more about it in the future. Assuming that we push #12827 further. I believe we will need to move all the color transformations to the createTheme() time, a place where we can add runtime warnings about wrong user input. It will also remove the crash when the component mounts. Lastly, for the CSS variables to work, we will render var() in the CSS, making it a lot easier to debug:

Capture d’écran 2021-06-14 à 00 19 18

vs.

Capture d’écran 2021-06-14 à 00 24 44

diff --git a/packages/material-ui/src/styles/ThemeProvider.js b/packages/material-ui/src/styles/ThemeProvider.js
index d13c4406f2..cdaf612d64 100644
--- a/packages/material-ui/src/styles/ThemeProvider.js
+++ b/packages/material-ui/src/styles/ThemeProvider.js
@@ -2,7 +2,7 @@ import * as React from 'react';
 import PropTypes from 'prop-types';
 import { ThemeProvider as MuiThemeProvider } from '@material-ui/private-theming';
 import { exactProp } from '@material-ui/utils';
-import { ThemeContext as StyledEngineThemeContext } from '@material-ui/styled-engine';
+import { ThemeContext as StyledEngineThemeContext, GlobalStyles } from '@material-ui/styled-engine';
 import useTheme from './useTheme';

 function InnerThemeProvider(props) {
@@ -28,9 +28,19 @@ InnerThemeProvider.propTypes = {
 function ThemeProvider(props) {
   const { children, theme: localTheme } = props;

+  const secondary = localTheme.palette.secondary.main;
+  localTheme.palette.secondary.main = 'var(--mui-palette-secondary-main)';
+
   return (
     <MuiThemeProvider theme={localTheme}>
-      <InnerThemeProvider>{children}</InnerThemeProvider>
+      <InnerThemeProvider>
+        <GlobalStyles styles={{
+          ":host, :root": {
+            '--mui-palette-secondary-main': secondary,
+          },
+        }} />
+        {children}
+      </InnerThemeProvider>
     </MuiThemeProvider>
   );
 }

cc @mnajdova

@oliviertassinari oliviertassinari added the component: chip This is the name of the generic UI component, not the React module! label Jun 17, 2021
@oliviertassinari
Copy link
Member

Note that this API is also available:

const defaultTheme = createTheme();
const theme = createTheme({
  palette: {
    neutral: defaultTheme.palette.augmentColor({
      color: {
        main: "#E2410D"
      }
    })
  }
});

https://codesandbox.io/s/billowing-platform-17k8o?file=/src/App.tsx:231-420

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: chip This is the name of the generic UI component, not the React module! support: question Community support but can be turned into an improvement
Projects
None yet
Development

No branches or pull requests

3 participants