Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set breakpoints when overriding theme variables in createMuiTheme

I'm trying to set the default theme typography font sizes to change based on breakpoints. For example, when the breakpoint is at the {xs} value, change theme.typography.h4.fontSize to '1.5rem'. At all other breakpoints, leave it at the default value ('2.125rem').

I can do this easily in components using the following code example:

const useStyles = makeStyles(theme => ({
    title: {
        [theme.breakpoints.down('xs')]: {
            fontSize: '1.5rem',
        }
    },
}))

However, this means having to add the same code to every single component that has <Typography variant='h4'> in it. If I decide to change the value from '1.5rem' to '1.25rem', I would need to locate every single instance of <Typography variant='h4'> and change it manually.

Is there any way to add breakpoints to createMuiTheme so it applies to all instances?

I have attempted the following, which does not work:

const defaultTheme = createMuiTheme()

const theme = createMuiTheme({
    typography: {
        h4: {
            [defaultTheme.breakpoints.down('xs')]: {
                fontSize: '1.5rem'
            }
        }
    }
})
like image 879
Jake Avatar asked Jan 27 '19 04:01

Jake


People also ask

How do you use breakpoints in makeStyles?

Using breakpoints in makeStylesFirst we setup a root class name and we assign it to the Paper component's className. We give the makeStyles root to the Paper className. When the breakpoint hits, using the breakpoint helpers, it will apply that CSS to the element and change the background color.

How do you use custom breakpoints in MUI?

In order to create custom break points we need to create a MUI Theme object. import { createMuiTheme } from '@material-ui/core/styles'; const theme = createMuiTheme({ breakpoints: { values: { xs: 0, sm: 600, md: 960, lg: 1280, xl: 1920, }, }, }); Then you will need to use that theme in a ThemeProvider.

How do I customize my material UI theme?

If you wish to customize the theme, you need to use the ThemeProvider component in order to inject a theme into your application. However, this is optional; MUI components come with a default theme.


2 Answers

You can use custom breakpoints with createBreakpoints.

Here is an example changing the MuiButton

import createBreakpoints from '@material-ui/core/styles/createBreakpoints'

const customBreakpointValues = {
  values: {
    xs: 0,
    sm: 576,
    md: 768,
    lg: 992,
    xl: 1200,
  },
}

const breakpoints = createBreakpoints({ ...customBreakpointValues })

const theme = createMuiTheme({
  breakpoints: {
    ...customBreakpointValues,
  },
  components: {
    MuiButton: {
      styleOverrides: {
        root: {
          padding: '10px',
          [breakpoints.up('lg')]: {
            padding: '20px',
          },
        },
      },
    },
  },
})
like image 123
Jorg Avatar answered Sep 20 '22 19:09

Jorg


CORRECTION: Initially I indicated that your solution didn't work because you were using an impossible condition, but I was incorrect. "down" from xs is inclusive of xs, so it does what you would want (matches 0px to 600px).

I'm not sure what I did in my initial testing that led me astray, but one thing that can cause confusion is if you have multiple down thresholds. In my own testing, I had an easier time avoiding shooting myself in the foot (e.g. by having a breakpoints.down("xs") followed by a later breakpoints.down("sm") that trumped the "xs" settings) by using breakpoints.only.

From https://material-ui.com/layout/breakpoints/#breakpoints

  • xs, extra-small: 0px or larger
  • sm, small: 600px or larger

Here is an updated sandbox that shows a little more clearly what is happening:

Edit w0591z46jl

like image 22
Ryan Cogswell Avatar answered Sep 18 '22 19:09

Ryan Cogswell