Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use breakpoints inside createMuiTheme? Material UI & React.js

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?

like image 606
Taylor A. Leach Avatar asked Apr 25 '19 18:04

Taylor A. Leach


1 Answers

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)

like image 100
Ricardo Canelas Avatar answered Oct 01 '22 00:10

Ricardo Canelas