Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Media queries in MUI components

I am using MUI components in ReactJs project, for some reason I need customization in some components to make it responsive according to screen width.

I have added media query and pass it as style attribute in the components but not working, any idea?

I am using code like this:

const drawerWidth = {   width: '50%',   '@media(minWidth: 780px)' : {     width: '80%'   } }  <Drawer   .....   containerStyle = {drawerStyle}  >  </Drawer> 

Code is working for web only, on mobile device no effect. Even CSS code is not applying I've checked in developer console. I am using MUI version 0.18.7.

Any help would be appreciated.

PS: As per requirement I need to make some changes according to screen size using CSS.

like image 450
Pardeep Jain Avatar asked Aug 23 '17 18:08

Pardeep Jain


People also ask

How do I use media queries in MUI?

To use the useMediaQuery hook, first import it from Material-UI. import { useMediaQuery } from '@material-ui/core'; In the component, call the useMediaQuery hook and pass in a media query as the argument. This will return a true or false value.

What is media query in React?

This is a CSS media query hook for React. It listens for matches to a CSS media query. It allows the rendering of components based on whether the query matches or not. Some of the key features: ⚛️ It has an idiomatic React API.

What is Xs and SM in material UI?

xs, extra-small: 0px. sm, small: 600px. md, medium: 900px.


1 Answers

By using the breakpoints attribute of the theme, you can utilize the same breakpoints used for the Grid and Hidden components directly in your component.

API

theme.breakpoints.up(key) => media query 

Arguments

key (String | Number): A breakpoint key (xs, sm, etc.) or a screen width number in pixels.

Returns media query: A media query string ready to be used with JSS.

Examples

const styles = theme => ({    root: {      backgroundColor: 'blue',      [theme.breakpoints.up('md')]: {        backgroundColor: 'red',      },    },  });

for more information check this out

like image 144
amin khademian Avatar answered Sep 26 '22 08:09

amin khademian