Why Material-UI is not overriding styles? - reactjs

Im using Material-UI within my application. The typography and palletes overriding is working perfectly. But when I try to overrides the MUIButton, nothing happens. This is my Style file:
const theme = createMuiTheme(SculpStyle);
export const SculpStyle = {
typography: {
fontFamily: ["Circular", "Open Sans", "Arial", "sans-serif"].join(","),
//fontSize: 20
// htmlFontSize: 12,
},
palette: {
primary: {
// light: will be calculated from palette.primary.main,
main: "#50be9c"
// dark: will be calculated from palette.primary.main,
// contrastText: will be calculated to contrast with palette.primary.main
},
secondary: {
light: "#1e8fb2",
main: "#ee4d73",
// dark: will be calculated from palette.secondary.main,
contrastText: "#ffcc00"
},
// contrastThreshold: 3,
// tonalOffset: 0.2
overrides: {
MuiButton: {
root: {
fontWeight: "bold",
backgroundColor: "red",
margin: "10px",
"&:hover": {
backgroundColor: "green"
}
}
}
}
}
};
Im following exactly the Material-UI API, but the Button is never overrided. What Im doing wrong here?

It looks like you defined your overrides on the wrong level, in your code those are nested within palette, but according to the documentation, the overrides for a specific component should be present on the "root" level of the configuration object (See here).
const theme = createMuiTheme(SculpStyle);
export const SculpStyle = {
typography: {
fontFamily: ["Circular", "Open Sans", "Arial", "sans-serif"].join(","),
//fontSize: 20
// htmlFontSize: 12,
},
palette: {
primary: {
// light: will be calculated from palette.primary.main,
main: "#50be9c"
// dark: will be calculated from palette.primary.main,
// contrastText: will be calculated to contrast with palette.primary.main
},
secondary: {
light: "#1e8fb2",
main: "#ee4d73",
// dark: will be calculated from palette.secondary.main,
contrastText: "#ffcc00"
},
// contrastThreshold: 3,
// tonalOffset: 0.2
},
overrides: {
MuiButton: {
root: {
fontWeight: "bold",
backgroundColor: "red",
margin: "10px",
"&:hover": {
backgroundColor: "green"
}
}
}
}
};

Related

Issue incorporating components into custom MUI theme in order to style buttons

I am trying to style buttons in a custom MUI theme however, when I try to incorporate components into my code I get the following error:
This is strange to me because I am directly following the documentation here but still getting this error.
I can't tell what I might be doing wrong below is the code I am currently working with, did I forget to import something?
import { createTheme } from '#mui/material/styles';
const theme = createTheme({
palette: {
primary: {
light: '#6826FB',
main: '#6826FB',
dark: '#CDBDFF',
},
secondary: {
light: '#625883',
main: '#615B71',
dark: '#CCC0F1',
contrastText: '#ffcc00', //not sure
},
contrastThreshold: 3,
tonalOffset: 0.2,
},
typography: {
subtitle1: {
fontSize: 12,
},
body1: {
fontWeight: 500,
},
button: {
fontFamily: 'Roboto',
fontSize: '12px',
fontStyle: 'SemiBold',
},
},
components: {
MuiButton: {
styleOverrides: {
root: {
fontSize: '0.75',
background: '#5B00EF',
borderRadius: '4px',
},
},
},
});
Is this the best way to do this? I found another way to apply styling to components in the code base which I can refer to below but it believe it might be deprecated? What is the difference between Overides and StyleOverides?
const theme = createTheme({
palette: {
primary: {
light: "rgb(234, 227, 243)",
main: "#5B00EF",
},
secondary: {
main: "#1C1B1F",
},
},
overrides: {
MuiButton: {
root: {
textTransform: "capitalize",
fontSize: 12,
lineHeight: "14px",
fontWeight: 600,
},
},
},
});
I tried changing styleOverides (even though this is what the documentation in react suggests) and tried to instead use overrides and this removed the issue but I feel as though I am confused why the documentation suggests one way versus the other unless this way is deprecated? Is the documentation not up to date?

React & MUI - How to apply specific MUI theme palette color to fill MUI icon SVG?

I'm using MUI & REACT for my web application but was wondering how could you fill a SVG with the color value defined in my MUI palette.
My palette looks like this:
const theme = createTheme(
{
palette: {
mode: "light",
primary: {
main: "rgba(33, 150, 243, 1)",
dark: "rgb(31,141,229)",
light: "rgb(35,157,253)"
},
secondary: {
main: "rgba(255, 82, 82, 1)",
dark: "rgb(232,74,74)",
light: "rgb(253,92,92)"
},
background: {
main: "hsl(0, 0%, 87%)",
default: "hsl(0,0%,95%)"
},
common: {
main: "hsl(0, 0%, 96%)",
white: "hsl(0,0%,100%)"
},
text: {
primary: "#232323",
secondary: "#444444"
}
}
},
localsMui[lang]
)
And the SVG which I try to apply the color:
<LogoutIcon fill="primary.main"/>
Does anyone have an idea?
I tried to use useTheme() but it doesn't work either.

Difficulty implementing Typescript Augmentation for my MUI theme

I have a custom theme with some additional key/values added to the theme. This is causing TS errors when I go to use the design tokens in my app. I know I need to use module augmentation to fix it but honestly I am so confused as to where this goes, or how this would be done properly. I think the docs are really scarce on the topic, and other stackoverflows are just as brief with little context as to why or where they are getting the elements defined within their module augmented themes. The theme I built is below for reference. Thanks in advance, any and all help is appreciated.
The error I'm receiving is a variation of this below for each custom theme property:
Type '{ main: string; secondary: string; nav: { main: string; active: string; inactive: string; }; }' is not assignable to type 'Partial<TypeBackground>'.
Object literal may only specify known properties, and 'main' does not exist in type 'Partial<TypeBackground>'
My current theme is below:
const muiTheme = createTheme({
palette: {
primary: { // orange
main: '#ff5e3d',
contrastText: '#e3e8f0'
},
secondary: { // black
main: '#585858',
contrastText: '#e3e8f0'
},
background: {
//#ts-ignore
main: '#FFFFFF', //white
secondary: "#f1f5f9", //lightblue
nav: {
main: '#1e293b',
active: '#0f172a',
inactive: '#1e293b'
}
},
info: undefined,
text: {
primary: "#11142d",
secondary: "#fff",
//#ts-ignore
nav: "#64748b",
meta: "#94a3b8"
},
icons: {
active: {
primary: '#ff5e3d',
secondary: '#ffffff',
},
inactive: {
primary: '#94a3b8',
secondary: '#475569',
}
},
contrastThreshold: 1,
tonalOffset: 0.2,
},
typography: {
fontFamily: ['Gotham', 'Mulish', 'Helvetica', 'Arial', 'sans-serif'].join(","),
h1: {
fontFamily: "Gotham, Helvetica",
fontWeight: 700,
lineHeight: '24px',
fontSize: '16px',
textTransform: "uppercase",
},
h2: {
fontFamily: "Gotham, Helvetica",
fontWeight: 400,
lineHeight: '24px',
fontSize: '16px',
},
h3: {
fontFamily: "Mulish, Arial",
fontWeight: 700,
lineHeight: '24px',
fontSize: '16px',
textTransform: "uppercase",
},
subtitle1: {
fontFamily: "Gotham, Helvetica",
fontWeight: 400,
lineHeight: '19.5px',
fontSize: '13px',
},
body1: {
fontFamily: "Mulish, Arial",
fontWeight: 400,
lineHeight: '21px',
fontSize: '14px',
},
h4: undefined,
h5: undefined,
h6: undefined,
body2: undefined,
subtitle2: undefined,
caption: undefined,
},
components: {
MuiCssBaseline: {
styleOverrides: `${[Gotham, Mulish]}`,
},
MuiButton: {
styleOverrides: {
root: ({ ownerState, theme }) => ({
fontFamily: "Mulish, Arial",
fontWeight: 700,
lineHeight: '15.6px',
fontSize: '13px',
paddingY: 8,
paddingX: 12,
//#ts-ignore
borderColor: theme.palette[ownerState.color].main,
borderRadius: 22.5,
borderWidth: 2,
"&:hover": {
borderWidth: 2
}
}),
},
},
// change other base components here
},
});
EDIT:
I am also receiving a type error indicating that icon does not exist on type Palette. I'm using the useTheme hook in order to fetch the them object so I can get the theme values for an SVG that takes two color props. If active, the colors of the SVG change. This object is passed to the custom icon component and colors deconstructed, it currently works minus the TS error. The code for the active switch where the TS error is, is below:
const isActive = (
active: boolean,
theme: Theme,
): {
background: string;
iconPrimary: string;
iconSecondary: string;
} => {
return active
? {
//#ts-ignore
iconPrimary: theme.palette.icons.navIcon.active.primary,
//#ts-ignore
iconSecondary: theme.palette.icons.navIcon.active.secondary,
}
: {
//#ts-ignore
iconPrimary: theme.palette.icons.navIcon.inactive.primary,
//#ts-ignore
iconSecondary: theme.palette.icons.navIcon.inactive.secondary,
};
};
Try the following. It should work!
The code below augments the needed interfaces (More about augmentation in the official docs):
declare module "#mui/material" {
export interface TypeBackground {
main: string;
secondary: string;
nav: {
main: string;
active: string;
inactive: string;
}
}
export interface PaletteOptions {
icons: {
active: {
primary: string;
secondary: string;
},
inactive: {
primary: string;
secondary: string;
},
}
}
export interface TypeText {
nav: string;
meta: string;
}
export interface Palette {
icons: PaletteOptions['icons'];
}
}
The snippets below fix the errors:
borderColor: ownerState.color && ownerState.color !== 'inherit'
? theme.palette[ownerState.color].main
: undefined,
And:
const isActive = (
active: boolean,
theme: Theme,
): {
background?: string;
iconPrimary: string;
iconSecondary: string;
} => {
return active
? {
iconPrimary: theme.palette.icons.active.primary,
iconSecondary: theme.palette.icons.active.secondary,
}
: {
iconPrimary: theme.palette.icons.inactive.primary,
iconSecondary: theme.palette.icons.inactive.secondary,
};
};

White-labeling in reactjs with Materail-UI and Tailwind

I have a react setup in which I am using Tailwind and Material-UI. I have a component library in the Storybook which has MUI Theme in it to style the variants like button outline. I am trying to find a way of how can I use these two things to make the react app white-labeled. Basically, Which properties I can set in my theme that it can be directly applied to the components by the user/providers/companies. Currently, my theme looks something like this in the component library.
import { createTheme, ThemeOptions } from '#mui/material/styles';
import { Colours } from '../common';
export const MyThemeOptions: ThemeOptions = {
palette: {
primary: {
main: Colours.primary,
},
secondary: {
main: Colours.secondary,
},
error: {
main: Colours.error,
},
warning: {
main: Colours.warning,
},
info: {
main: Colours.info,
},
success: {
main: Colours.success,
},
},
components: {
MuiButton: {
styleOverrides: {
root: {
borderRadius: '4px',
},
contained: {
padding: '4px 12px',
},
outlined: {
border: '1px solid #bdbdbd',
textTransform: 'none',
minWidth: 0,
color: '#212121',
},
outlinedSizeSmall: {
padding: '2px',
},
outlinedSizeMedium: {
padding: '6px',
},
outlinedSizeLarge: {
padding: '10px'
},
},
},
MuiButtonGroup: {
styleOverrides: {
grouped: {
minWidth: 0,
},
},
},
MuiCard: {
styleOverrides: {
root: {
boxShadow: '0 3px 7px 0 rgba(98, 125, 152, 0.16), 0 0 2px 0 rgba(36, 59, 83, 0.04)',
},
},
},
MuiToggleButton: {
styleOverrides: {
root: {
border: '1px solid #bdbdbd',
borderRadius: '4px',
textTransform: 'none',
color: '#212121',
minWidth: 0,
},
sizeSmall: {
padding: '2px',
},
sizeMedium: {
padding: '6px',
},
sizeLarge: {
padding: '10px',
}
}
}
},
};
const MyTheme = createTheme(MyThemeOptions);
export default MyTheme;
I am not able to find an article or tutorial or something which could lead me in a direction of how can I achieve the white labeling? If you know any knowledge asset in this direction, please share with me. Thank you!

Loading custom font Open Sans and its variants(bold ,semibold) in material ui

I'm using Material UI with Create React App and trying to include the "Open Sans" font that I downloaded in ttf format. Following the documentation on Material UI for self-hosted fonts, my current theme.js file looks like this:
import OpenSansRegular from "./fonts/OpenSans-Regular.ttf";
import OpenSansBold from "./fonts/OpenSans-Bold.ttf";
import OpenSansSemiBold from "./fonts/OpenSans-SemiBold.ttf";
import { createMuiTheme } from "#material-ui/core/styles";
const openSansRegular = {
fontFamily: "Open Sans",
fontStyle: "normal",
fontDisplay: "swap",
src: `
local("Open Sans"),
local("OpenSans-Regular")
url(${OpenSansRegular}) format("ttf")
`,
};
const openSansSemibold = {
fontFamily: "Open Sans",
fontStyle: "normal",
fontDisplay: "swap",
src: `
local("Open Sans"),
local("OpenSans-SemiBold")
url(${OpenSansSemiBold}) format("ttf")
`,
};
const openSansBold = {
fontFamily: "Open Sans",
fontStyle: "normal",
fontDisplay: "swap",
src: `
local("Open Sans"),
local("OpenSans-Bold")
url(${OpenSansBold}) format("ttf")
`,
};
const theme = createMuiTheme({
typography: {
fontFamily: "Open Sans",
fontSize: 14,
h1: {
fontSize: 20,
fontWeight: "bold",
},
h2: {
fontSize: 18,
fontWeight: "bold",
},
h3: {
fontSize: 16,
fontWeight: "bold",
},
overrides: {
MuiCssBaseline: {
"#global": {
"#font-face": [openSansRegular]
}
}
},
});
export default theme;
I am unable to figure out how to actually use its variants such as openSansSemiBold and openSansBold. Where exactly do I specify in the theme so they are recognized? And say I want a text to be semibold in some part of the app, how exactly I am going to refer to it?
I haven't been able to find a clear answer to this so far.
You can put all fonts into array.
var customFontFamily = [openSansRegular,openSansSemibold,openSansBold];
var customTypo = {
...
body1 :{
fontFamily: 'OpenSans-Regular',
},
h1:{
fontFamily: 'OpenSans-SemiBold',
},
h2:{
fontFamily: 'OpenSans-Bold',
},
...
};
const theme = createMuiTheme({
typography: customTypo
overrides: {
MuiCssBaseline: {
"#global": {
"#font-face": [customFontFamily]
}
}
},
});

Resources