material-ui: CSS Media Queries don't work with Custom breakpoints
According to the documentation, we can create custom breakpoints::
const theme = createMuiTheme({
breakpoints: {
values: {
tablet: 640,
laptop: 1024,
desktop: 1280,
},
},
});
But CSS Media Queries don’t understand these new values
theme.breakpoints.up(key)
theme.breakpoints.down(key)
theme.breakpoints.only(key)
theme.breakpoints.between(start, end)
- The issue is present in the latest release.
- I have searched the issues of this repository and believe that this is not a duplicate.
Current Behavior 😯
The following code
[theme.breakpoints.down("laptop")]: {
padding: "10px",
},
produces the next css
@media (max-width:NaNpx) {
.makeStyles-root-5 {
padding: 10px;
}
}
Context 🔦
I think the reason is in the wrong function createBreakpoints.js because we never update an array of keys. It should be smth like that
export const predefinedKeys = ['xs', 'sm', 'md', 'lg', 'xl'];
// Keep in mind that @media is inclusive by the CSS specification.
export default function createBreakpoints(breakpoints) {
const {
// The breakpoint **start** at this value.
// For instance with the first breakpoint xs: [xs, sm[.
values = {
xs: 0,
sm: 600,
md: 960,
lg: 1280,
xl: 1920,
},
unit = 'px',
step = 5,
keys = predefinedKeys,
...other
} = breakpoints;
}
| Tech | Version |
|---|---|
| Material-UI | v4.11.0 |
| React | v16.13.1 |
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 12
- Comments: 24 (13 by maintainers)
If anyone stumbles upon this and doesn’t want to use alpha / wait for full release, you can get around this by manually passing the breakpoint value. For example, instead of
theme.breakpoints.down('tablet')writetheme.breakpoints.down(theme.breakpoints.values.tablet)The issue is closed but custom break point still doesn’t work as described in the docs. Also as @oliviertassinari suggested here to try alpha version to see it working I think this must be reflected in the docs as well.
This is still not working for me on v5
This doesn’t work for me:
theme.breakpoints.down(theme.breakpoints.values.tablet)Hello, you need to change the keys too
Hello, am I right in saying this isn’t fixed still in v4? The comments on the last closure indicate you are just supporting it for 5?
@alanszp Yes, a pull request would be great.
@TheAussieStew Could you open a new issue for this case? We have done our best to handle custom breakpoints, but the API is large, we forgot a few.
It seems that we could make it work with: