Material UI : Reference other properties of createMuiTheme inside theme.ts file? - reactjs

When importing theme like this (in filename.style.ts) :
import theme from 'common/theme';
I can access the different properties, like for example
theme.breakpoints.down('md')
I am trying to reference the same property inside the theme.ts file, but ofcourse.. theme. is not valid here, so i'm trying to to find a way i can re-use / reference it.
As you see on MuiTable i'm trying to access breakpoints and palette/primary.
theme.ts
import createMuiTheme from '#material-ui/core/styles/createMuiTheme';
export const MuiPaperBackgroundColor = '#f7f8f6';
export default createMuiTheme({
spacing: 8,
breakpoints: {
values: {
xs: 0, sm: 600, md: 960, lg: 1280, xl: 1650,
},
},
palette: {
primary: {
main: '#3f18aa',
extraLight: 'rgb(193, 181, 227)',
noDataColor: '#cccccc',
cardBgColor: '#ECECEC',
chartColors: [
'#E77F42',
'#F3C3A3',
],
},
overrides: {
MuiTable: {
root: {
whiteSpace: 'nowrap',
[theme.breakpoints.down('md')]: {
'& tr': {
'& td:first-child, & th:first-child': {
position: 'sticky',
left: 0,
backgroundColor: theme.palette.header.main,
color: theme.palette.primary.contrastText,
zIndex: 2,
},
},
},
},
},
},
});

Build your theme out of the individual material-ui packages. Here's how I did it:
import createMuiTheme from '#material-ui/core/styles/createMuiTheme';
import createBreakpoints from '#material-ui/core/styles/createBreakpoints';
const breakpoints = createBreakpoints({
// your settings
});
const theme = createMuiTheme({
breakpoints,
overrides: {
MuiTable: {
root: {
[breakpoints.down('md')]: {
// style
},
},
},
},
});

Ricky's solution is nice in praxis but Material warns against importing deeper than two levels (#material-ui/core/styles/foo in this case) because that is considered private and not part of any public contract. I.e., it can change any release.
It might not work with Breakpoints, but colours or fontWeights and such can be easily shared as plain constants declared before. But I am also sceptical if it's a good idea to include responsiveness in this declaration in first place.

You can attach the props after the theme is declared.
let theme = createMuiTheme({
overrides: {
MuiAppBar: {
root: {
transform: 'translateZ(0)'
}
}
},
props: {
MuiIconButton: {
disableRipple: true
}
}
});
theme = responsiveFontSizes(theme);
theme.overrides.MuiCssBaseline = {
'#global': {
'.testMe': {
color: 'red'
},
'.container-std': {
[theme.breakpoints.up('lg')]: {
maxWidth: '1200px',
marginLeft: 'auto',
marginRight: 'auto'
}
},
'.container-wide': {
margin: theme.spacing(2, 2)
}
}
};

Related

Access custom palette from Material UI theme in sx/style props

Having trouble accessing my custom Material UI palette for some reason. I have created a custom theme in Theme.js like so:
import { createTheme, ThemeProvider } from "#mui/material/styles";
import { NextRequest } from "next/server";
import Inter from "typeface-inter";
const theme = createTheme({
palette: {
background: {
default: "#120C18",
},
primary: {
main: "#21172a",
},
secondary: {
main: "#33283e",
},
info: {
main: "#756c7c",
},
},
typography: {
fontFamily:
"Inter",
h1: {
font: "Inter",
fontSize: "min(3vw, 70px)",
fontWeight: "bold",
lineHeight: "1.15",
},
breakpoints: {
values: {
xs: 0,
sm: 600,
md: 1000,
lg: 1200,
xl: 1600,
},
},
export default function Theme({ children }) {
return <ThemeProvider theme={theme}>{children}</ThemeProvider>;
}
then I imported and wrapped App.js in and all of my overrides work fine. But for the custom palette, I can't seem to access it in the sx or style props of certain components. I am trying to use something like theme.palette.primary.main or color="primary" but can't get anything to work.
How can I use the colors from the palette in my sx or style props?

How to change default typography for Textfields and such via theme in MUI v5?

Is there a way to map default typography for TextField and all my inputs via theme? I know I can do this
components: {
MuiInput: {
styleOverrides: {
root: {
fontSize: '16px',
lineHeight: '25px'
}
}
},
but I wish to find a way to map it to 'body2'
MuiInput: {
defaultProps: {
typographyProps???: {
variant: 'body2'
}
}
For using body2 font size value in another component, You need to access the theme object's typography property.
Then You will set the MUI TextFiled's font-size equal to that value from the theme object.
import { createTheme } from "#mui/material";
const theme = createTheme({});
theme!.components!.MuiTextField = {
styleOverrides: {
root: {
"& .MuiInputBase-input": {
fontSize: theme.typography.body2.fontSize, // this is the default mui body2 font-size
lineHeight: "25px", // and any other styles you want...
},
},
},
};
And You can change the body2 fontSize when defining the theme object:
const theme = createTheme({
typography: {
body2: {
fontSize: 80,
},
});
theme!.components!.MuiTextField = {
styleOverrides: {
root: {
"& .MuiInputBase-input": {
fontSize: theme.typography.body2.fontSize, // this will be 80px
lineHeight: "25px", // and any other styles you want...
},
},
},
};
EDIT:
If You are not ok with overriding the theme after defining it, You can use this approach:
import { createTheme } from "#mui/material";
const myBody2FontSize = 20px;
const theme = createTheme({
typography: {
body2: {
fontSize: myBody2FontSize,
},
},
components: {
MuiTextField: {
styleOverrides: {
root: {
"& .MuiInputBase-input": {
fontSize: myBody2FontSize , // this is the default mui body2 font-size
lineHeight: "25px", // and any other styles you want...
},
},
},
},
},
});
You could try setting it via classname. Its a bit awkward because an input doesnt actually use Typography internally.
MuiInput: {
defaultProps: {
className: 'MuiTypography-body2'
}
}
Or possibly
MuiTextField: {
defaultProps: {
InputProps: {className: 'MuiTypography-body2' }
}
}

Material-ui DateRangePicker custom styling with Typescript

I am using material ui DateRangePicker and want to customize MuiPickersDesktopDateRangeCalendar style. But I am unable to override this with global theme as this is not recognized in ThemeOptions with typescript.
I tried to override from this reference (https://next.material-ui-pickers.dev/guides/css-overrides), but I am getting below error "..is missing the following properties from type 'ThemeOptions': MuiPickersDesktopDateRangeCalendar, MuiPickersDay"
Option 1:
interface Theme {
//for typescript usage, I think it should go inside overrides, but there is no overrides option supported
MuiPickersDesktopDateRangeCalendar: {
arrowSwitcher: {
padding: React.CSSProperties['padding']
},
calendar: {
minWidth: React.CSSProperties['minWidth']
minHeight: React.CSSProperties['minHeight']
},
},
MuiPickersDay: {
root: {
width: React.CSSProperties['width']
height: React.CSSProperties['height']
},
},
}
interface ThemeOptions {
//for typescript usage, I think it should go inside overrides, but there is no overrides option supported
MuiPickersDesktopDateRangeCalendar: {
arrowSwitcher: {
padding: React.CSSProperties['padding']
},
calendar: {
minWidth: React.CSSProperties['minWidth']
minHeight: React.CSSProperties['minHeight']
},
},
MuiPickersDay: {
root: {
width: React.CSSProperties['width']
height: React.CSSProperties['height']
},
},
}
const theme = createMuiTheme({
overrides: {
MuiPickersDesktopDateRangeCalendar: {
arrowSwitcher: {
padding: '0.8rem 0.8rem 0.4rem 0.8rem',
},
calendar: {
minWidth: '20rem',
minHeight: '19rem',
},
},
MuiPickersDay: {
root: {
width: '1.4rem',
height: '1.4rem',
},
},
},
})
Option 2: There is no need to declare in theme interface, and upgrade #material-ui/core/styles to any specific version. I tried that as well, but it is not working.
Please let me know if there is any compatible version of "#material-ui/core/styles" with "#material-ui/pickers": "^4.0.0-alpha.12".

Material-ui overwrite components gutter breakpoint styles TypeError: Cannot read property 'breakpoints' of undefined

I am doing something like this.
// theme.js
import { createMuiTheme } from '#material-ui/core/styles';
const theme = createMuiTheme({
overrides: {
MuiToolbar: {
gutters: {
[theme.breakpoints.up('sm')]: {
paddingLeft: '16px',
paddingRight: '16px',
},
},
},
},
palette: {
type: 'dark',
},
});
export default theme;
Error message: TypeError: Cannot read property 'breakpoints' of undefined.
I wanna get the theme style value and use for overwrite, how can i fixed this error.
24px gutter is too much for me for all theme style / components like paper, how can i easy to overwrite all gutter with 16px replace?
Thanks so much.
As answered in this question, you need to create an instance of the default theme, so you have an object to get breakpoints from:
import { createMuiTheme } from '#material-ui/core/styles';
const defaultTheme = createMuiTheme();
const theme = createMuiTheme({
overrides: {
MuiToolbar: {
gutters: {
[defaultTheme.breakpoints.up('sm')]: {
paddingLeft: '16px',
paddingRight: '16px',
},
},
},
},
palette: {
type: 'dark',
},
});
export default theme;
Regarding the "global" gutter property, Toolbar uses theme.mixins.gutters() to get the default gutter, so I think you have to override that. Looking into this function source, this should be the right override to set the gutter to 16px:
import { createMuiTheme } from '#material-ui/core/styles';
const defaultTheme = createMuiTheme();
const theme = createMuiTheme({
mixins: {
gutters: (styles = {}) => ({
paddingLeft: defaultTheme.spacing.unit * 2,
paddingRight: defaultTheme.spacing.unit * 2,
...styles,
[defaultTheme.breakpoints.up('sm')]: {
paddingLeft: defaultTheme.spacing.unit * 2,
paddingRight: defaultTheme.spacing.unit * 2,
...styles[defaultTheme.breakpoints.up('sm')],
},
}),
},
palette: {
type: 'dark',
},
});
export default theme;

Override Mui theme nested in theme.breakpoints.up("md")

Code
MuiTab: {
root: {
height: 140,
[theme.breakpoints.up("md")]: {
minWidth: 72,
}
}
}
Error
[ts] Cannot find name 'theme'.
Problem
I don't know where to declare "theme".
The snippet you posted probably comes from a method where you create your custom theme by calling createMuiTheme(). The trick is to assign the result of createMuiTheme() to a local variable and then set the overrides. Then you can use the breakpoints:
import { createMuiTheme } from 'material-ui/styles';
import { orange } from 'material-ui/colors';
export function getCustomTheme() {
let theme: any = createMuiTheme({
palette: {
primary: orange,
},
overrides: {}
});
theme.overrides.MuiTab = {
root: {
height: 140,
[theme.breakpoints.up('md')]: {
minWidth: 72,
backgroundColor: 'red'
}
}
};
return theme;
}

Resources