I'm working on converting my app from rebass to chakra and currently dealing with a component that needs 16px padding on mobile, and 24px padding on tablet/desktop. In my component I have <Component px={[4, 6, 6]} />
Looking at this chart, I would expect to be able to use these numbers 4, 6 because I have confirmed the font-size is 16px. However, the actual values I'm seeing are 32px on mobile and 128px for tablet/desktop. I think something might be up with my chakra setup because I'm even unable to use inbetween values like 3.5, however I have had no problems with it so far.
My chakra setup is pretty simple at the moment because I'm early in the migration and I'm not messing with the default chakra values for the space key so I don't know what's causing these weird numbers.
const config: ThemeConfig = {
initialColorMode: 'dark',
useSystemColorMode: false,
}
const Button: ComponentStyleConfig = {
baseStyle: {
fontWeight: 'bold',
},
variants: {
primary: {
letterSpacing: '-0.19px',
borderRadius: '30px',
paddingLeft: '45px',
paddingTop: '14px',
paddingRight: '45px',
paddingBottom: '14px',
height: '56px',
backgroundColor: colors.white,
color: colors.black,
},
},
// The default size and variant values
defaultProps: {
variant: 'primary',
},
}
const theme = {
colors,
styles: globalStyles,
components: {
Button,
},
}
export default extendTheme({ ...theme, config })
Related
I want to overwrite a specific style for multiple components. For now it works for all components, but not the the Select.
What I'm doing is:
MuiSelect: {
styleOverrides: {
select: {
background: themePalette.palette.background.paper,
marginLeft: '0rem',
borderRadius: '10rem',
},
iconOutlined: {
background: themePalette.palette.background.default,
color: themePalette.palette.primary.main,
borderRadius: '10rem',
},
},
variants: [
{
props: { size: 'small' },
style: {
borderRadius: '0.4rem',
select: {
borderRadius: '0.4rem',
},
iconOutlined: {
borderRadius: '0.4rem',
},
},
},
],
},
The style in the "styleOverrides" section get overwritten like expected. But the variant never is applied.
For other elements like TextField, ToggleButtonGroups, etc. it works. But somehow it does not work for the Select.
Also when I directly overwrite the same properties in the Select control via the sx prop, it's not applied.
sx={{
input: {
borderRadius: '0.4rem',
},
select: {
borderRadius: '0.4rem',
backgroundColor: theme.palette.grey[300],
},
borderRadius: '0.4rem !important',
}}
Here I also don't have any effect. The border radius is still unchanged at '10rem' like it's defined in the stylesOverrides.
Is this a bug, or am I doing something wrong for the Select?
style in theme props and sx work with css selectors. Not with pre-defined slots like styleOverrides
sx={{
'& .MuiSelect-select': {
...styles here,
...
}
}}
I'm trying to override some MUI styles in React Material UI. This is currently working for me:
MuiTab: {
root: {
color: '#284568',
},
textColorInherit: {
opacity: 1,
},
wrapper: {
textTransform: 'capitalize',
},
'&$selected': {
backgroundColor: '#000',
border: '1px solid #CFDCE8',
},
},
However, it gives me a warning on the console:
"getStylesCreator.js:42 Material-UI: You are trying to override a
style that does not exist. Fix the &$selected key of
theme.overrides.MuiTab."
I tried to convert to:
MuiTab: {
root: {
color: '#284568',
'&$selected': {
backgroundColor: '#000',
border: '1px solid #CFDCE8',
},
},
textColorInherit: {
opacity: 1,
},
wrapper: {
textTransform: 'capitalize',
},
},
The warning was gone, but the style won't apply anymore :(
Any suggestions?
Thanks.
I am trying to apply custom styling to the component based on its variants. However, a single component can have multiple variants specifying different style-properties. For this question, I will use code snippets from the Button component.
My wrapper of the Material UI Button component (defined here as MaterialButton):
import MaterialButton from '#material-ui/core/Button'
import './button-design-tokens.module.css'
export const Button: React.FC<ButtonProps> = (
props: ButtonProps
) => {
return (
<MaterialButton {...props}>{props.children}</MaterialButton>
)
}
I am trying to apply the following styles to this component in a Storybook environment using useStyles():
const useStyles = makeStyles({
contained: {
backgroundColor: 'var(--mdh-button-background-color)',
fontFamily: 'var(--mdh-button-font-family)',
'&.primary': {
color: 'var(--mdh-button-secondary-background-color)',
},
'&.secondary': {
color: 'var(--mdh-button-secondary-background-color)'
},
'&:hover': {
backgroundColor: 'var(--mdh-button-hover-background-color)',
color: 'var(--mdh-button-hover-text-color)'
},
'&:focus': {
backgroundColor: 'var(--mdh-button-focus-background-color)',
color: 'var(--mdh-button-focus-text-color)'
},
'&$disabled': {
backgroundColor: 'var(--mdh-button-disabled-background-color)'
}
},
outlined: {
borderRightColor: 'var(--mdh-button-background-color)',
borderLeftColor: 'var(--mdh-button-background-color)',
borderTopColor: 'var(--mdh-button-background-color)',
borderBottomColor: 'var(--mdh-button-background-color)',
'&:hover': {
borderRightColor: 'var(--mdh-button-hover-background-color)',
borderLeftColor: 'var(--mdh-button-hover-background-color)',
borderTopColor: 'var(--mdh-button-hover-background-color)',
borderBottomColor: 'var(--mdh-button-hover-background-color)'
},
'&:focus': {
borderRightColor: 'var(--mdh-button-focus-background-color)',
borderLeftColor: 'var(--mdh-button-focus-background-color)',
borderTopColor: 'var(--mdh-button-focus-background-color)',
borderBottomColor: 'var(--mdh-button-focus-background-color)'
}
},
outlinedPrimary: {
color: 'var(--mdh-button-background-color)'
},
outlinedSecondary: {
color: 'var(--mdh-button-secondary-background-color)'
},
textPrimary: {
color: 'var(--mdh-button-background-color)',
'&:hover': {
color: 'var(--mdh-button-disabled-background-color)'
},
'&:focus': {
color: 'var(--mdh-button-disabled-background-color)'
}
},
textSecondary: {
color: 'var(--mdh-button-secondary-background-color)',
'&:hover': {
color: 'var(--mdh-button-disabled-background-color)'
},
'&:focus': {
color: 'var(--mdh-button-disabled-background-color)'
}
}
})
As you can see, a Material UI button has the variants contained (a normal button), outlined (a button with only a coloured border) and text (a button consisting only of text). These variants can, in turn, have the primary, secondary and default variants which change the colour of the text inside the button.
The CSS variables seen in the JSON are defined in the css file imported in the Button wrapper. This file has the following contents:
:root {
--mdh-button-background-color: rgb(34, 123, 60);
--mdh-button-secondary-background-color: #EDBF07;
--mdh-button-focus-background-color: #9a6f1e;
--mdh-button-focus-text-color: white;
--mdh-button-hover-background-color: #9a6f1e;
--mdh-button-hover-text-color: white;
--mdh-button-disabled-background-color: #666;
--mdh-button-font-family: "TheMix", sans-serif;
}
.mdh-button {
background-color: var(--mdh-button-background-color);
font-family: var(--mdh-button-font-family);
}
.mdh-button--focus, .mdh-button:focus {
background-color: var(--mdh-button-focus-background-color);
color: var(--mdh-button-focus-text-color);
}
.mdh-button--hover, .mdh-button:hover {
background-color: var(--mdh-button-hover-background-color);
color: var(--mdh-button-hover-text-color);
}
.mdh-button--disabled {
background-color: var(--mdh-button-disabled-background-color);
}
As you can see, in order to style, for example, the outlined button in the primary variant, I would have to style this by combining the two variants in a class name (i.e. OutlinedPrimary). This is fine for minor changes, but I am developing a design system that is based on MUI and will have to re-style almost all the MUI components I am using in the system. This would lead to an insanely bloated and incomprehensible stylesheet for each component. Although this works, I am trying for a more compact and (in my humble opinion) logical approach. It is already defined in the JSON above, but I will highlight it below once more:
contained: {
backgroundColor: 'var(--mdh-button-background-color)',
fontFamily: 'var(--mdh-button-font-family)',
'&.primary': {
color: 'var(--mdh-button-secondary-background-color)',
},
'&.secondary': {
color: 'var(--mdh-button-secondary-background-color)'
},
'&:hover': {
backgroundColor: 'var(--mdh-button-hover-background-color)',
color: 'var(--mdh-button-hover-text-color)'
},
'&:focus': {
backgroundColor: 'var(--mdh-button-focus-background-color)',
color: 'var(--mdh-button-focus-text-color)'
},
'&$disabled': {
backgroundColor: 'var(--mdh-button-disabled-background-color)'
}
}
The primary variant is treated here as a 'subclass' of the contained variant of the Button. However, for some reason, this is not picked up by either Storybook or Material UI. I am applying the classes to the component like so:
const Template: Story<ButtonProps> = (args: any) => {
const classes = useStyles()
args.classes = {
contained: classes.contained,
outlined: classes.outlined,
// containedPrimary: classes.containedPrimary,
// containedSecondary: classes.containedSecondary,
textPrimary: classes.textPrimary,
textSecondary: classes.textSecondary,
outlinedPrimary: classes.outlinedPrimary,
outlinedSecondary: classes.outlinedSecondary
}
return <Button {...args}>Button</Button>
}
I have tried various and browsed through countless articles and blogs on the topic but have gotten none the wiser. Is there something I am missing? As far as I am aware, the syntax is correct.
Thank you in advance!
Maybe not the right approach but I want to create some 'global' styles for headings for example. Something like this:
const myTheme = createMuiTheme({
headings: {
h1: {
fontSize: 28,
// Obviously this does not work...
[theme.breakpoints.down('sm')]: {
fontSize: 24
},
},
h2: {
fontSize: 24,
}
}
}
then I can use them in my components like this:
const styles = (theme) => ({
myElement: {
...theme.headings.h1,
// ... other styles
}
}
This does work but the issue I face is I want the headings to be responsive and respect Material UI's breakpoints, but I can't use them inside the createMuiTheme itself? What is the way to do this correctly so I can just spread in my styles that INCLUDE the responsive styles all in one?
You can use the createBreakpoints method
Example:
// theme.js
import createBreakpoints from '#material-ui/core/styles/createBreakpoints'
import { createMuiTheme } from '#material-ui/core/styles'
const breakpoints = createBreakpoints({})
const theme = createMuiTheme({
overrides: {
MuiTab: {
root: {
[breakpoints.up('md')]: {
minWidth: '200px',
backgroundColor: 'yellow',
},
},
},
},
})
export default theme
(tested: material-ui 4.0.1)
V5 update
Preferred solution is to create an intermediate theme (source):
let theme = createTheme()
theme = createTheme(theme , {
h5: {
fontSize: "1.5", //24px
fontWeight: title.fontWeight,
fontFamily: sansSerif(title.fontFamily),
letterSpacing: title.letterSpacing,
lineHeight: "2.1rem", //34px
color: "#636e72",
[theme.breakpoints.between("xs", "sm")]: {
fontSize: "1.25rem", // 20px
lineHeight: "1.9rem", // 30px
},
[theme.breakpoints.between("sm", "md")]: {
fontSize: "1.4rem", //24px
lineHeight: "2rem", // 35px
},
},
})
If you used createBreakpoints: as pointed out by #Ricardo Canelas comment, createBreakpoints has simply moved, right import is now: import createBreakpoints from "#mui/system/createTheme/createBreakpoints". However, keep in mind that this is still a private API at the time of writing so can move/break at any version update.
Preferred solution is to use an intermediate theme.
From https://github.com/mui-org/material-ui/issues/18017#issuecomment-545914925
import { createMuiTheme } from "#material-ui/core/styles";
const theme = createMuiTheme();
theme.overrides = {
MuiTypography: {
hero: {
[theme.breakpoints.up('md')]:{
fontSize: '11rem',
background: 'red',
},
fontSize: '3.75rem',
lineHeight: '5rem',
fontWeight: 700,
},
},
};
This is a side note, but if you want to change the values of the breakpoints you can edit the breakpoints object created with createBreakpoints({}):
import createBreakpoints from '#material-ui/core/styles/createBreakpoints'
const breakpoints = createBreakpoints({})
// outputs {xs: 0, sm: 600, md: 960, lg: 1280, xl: 1920}
breakpoints.values.lg = 1024
// outputs {xs: 0, sm: 600, md: 960, lg: 1024, xl: 1920}
You could also add additional breakpoints in a similar way if you didn't want to edit the existing items:
breakpoints.values['xxl'] = 3000
// outputs {xs: 0, sm: 600, md: 960, lg: 1280, xl: 1920, xxl: 3000}
In my project the breakpoints that Material-UI has set (xs: 0, sm: 600, etc.) didn't line up with the ones I was using already in my project, so I had to change them.
const theme = createMuiTheme();
theme.typography.h3 = {
fontSize: '1.2rem',
'#media (min-width:600px)': {
fontSize: '1.5rem',
},
[theme.breakpoints.up('md')]: {
fontSize: '2.4rem',
},
};
https://material-ui.com/customization/typography/#responsive-font-sizes
You can also use something like this -
MuiInputBase: {
defaultProps: {
sx: {
fontSize: {
xs: '16px',
md: '14px',
},
},
},
},
We are using 1.4.5 material-ui in our application. Now we are trying to set one font size for all of the components in our application. For example we want to set fontSize: 14px, for headers / buttons / paragraph /list... and so on.
Here is our sample theme configuration:
import { createMuiTheme } from '#material-ui/core/styles';
import purple from '#material-ui/core/colors/purple';
import green from '#material-ui/core/colors/green';
const theme = createMuiTheme({
typography: {
fontSize: 14,
},
palette: {
primary: purple,
secondary: green,
},
status: {
danger: 'orange',
},
});
If you notice we have tried to define the font size in a typography object but with this configuration, material-ui computes a font size according to the formula given at this link.
We are trying to achieve a 14px font-size application wide but we can not find an option to do this. One option can be a pxToRem function but I am not sure how should I overwrite that function and what should be my function? The other option is to define styles in every component and I think that is a bit overkill since we should able to do it with theme object.
Please help me if you can think of a way to set the font size globally.
Your help will be greatly appreciated.
NOTE: I am not sure why somebody will downvote my answer since my
answer is based in material-ui docs. Here is the link. Please
expand typography section and you will see this is how all the fonts
are set. Please comment here the reason for the downvote. Also, note
when I wrote this answer I was using Material UI 1.4.5
I will post the solution that solved my problem. Hopefully, this will help someone else as well:
I end up defining the font size in a typography property of my theme. Here is how I set it up in the theme;
import { createMuiTheme } from '#material-ui/core/styles';
import purple from '#material-ui/core/colors/purple';
import green from '#material-ui/core/colors/green';
const theme = createMuiTheme({
typography: {
fontFamily: 'Roboto, Helvetica, Arial, sans-serif',
fontSize: 13,
display4: {
fontSize: 13,
},
display3: {
fontSize: 13,
},
display2: {
fontSize: 13,
},
display1: {
fontSize: 13,
},
headline: {
fontSize: 13,
},
title: {
fontSize: 13,
},
subheading: {
fontSize: 13,
},
body2: {
fontSize: 13,
},
body1: {
fontSize: 13,
},
caption: {
fontSize: 13,
},
button: {
fontSize: 13,
},
},
palette: {
primary: purple,
secondary: green,
},
status: {
danger: 'orange',
},
});
I have noticed if you used Div instead of PAPER element sometimes formatting does not apply (bug with material-ui??). So I end up creating a global class which I applied to any elements which were overriding my theme styles.
overRidefonts: {
fontSize: 13,
fontFamily: 'Roboto, Helvetica, Arial, sans-serif',
},