mui / mui/material-ui

[theme] Remove hardcoded breakpoints

Open
#21,778 11 comments 6 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

design scope: all components type: new feature
Dominant language
JavaScript
Stars
99.1k
Forks
32.5k
Avg merge
2d 17h
Merged PRs (30d)
106

Description

There are several places in the codebase where breakpoints are hardcoded.
Since we have the ability to override breakpoints, we should not have hardcoded values in the code

  • 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 😯

  1. PropTypes
PropTypes.oneOf(['xs', 'sm', 'md', 'lg', 'xl']),
PropTypes.arrayOf(PropTypes.oneOf(['xs', 'sm', 'md', 'lg', 'xl'])),
  1. Styles
[theme.breakpoints.only('xs')]: {
  ...
}
  1. Functions in withWidth.js
import { breakpointKeys } from '../styles/createBreakpoints';
function isWidthUp(...){ 
  return breakpointKeys.indexOf(...)
}
function isWidthDown(...){ 
  return breakpointKeys.indexOf(...)
}

Expected Behavior 🤔

  1. PropTypes
PropTypes.string
PropTypes.arrayOf(PropTypes.string),
  1. Styles

a) Use a media query only if there is such a breakpoint in the theme

[theme.breakpoints.only(theme.breakpoints.keys['xs'] ?? 0)]: {
  ...
}

b) Add a new property alias to createBreakpoints function to match breakpoints

createBreakpoints({
  values: {
      tablet: 640,
      laptop: 1024,
      desktop: 1280,
    },
    alias: {
      xs: 'tablet',
      sm: 'tablet',
    },
  }
})
  1. Functions in withWidth.js
    a) Show the necessity for breakpoints
    Remove import { breakpointKeys } from '../styles/createBreakpoints';
function isWidthUp(breakpoints, breakpoint, width, inclusive = true){ .. }

b) Add breakpoints to options implicitly

import { breakpointKeys } from '../styles/createBreakpoints'
function isWidthUp(breakpoint, width, opts = {}) {
  let {breakpoints = breakpointKeys, inclusive = true } = opts;
  ...
}

Your Environment 🌎

Tech Version
Material-UI v4.11.0

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading styles/createBreakpoints and withWidth.js, then locate the listed PropTypes and style usages that hardcode xs through xl. Compare the proposed theme override and alias approaches before choosing a consistent direction. Done means breakpoint validation, media-query styles, and withWidth helpers no longer assume fixed breakpoint names.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, react
Domain
frontend
Issue type
Refactor
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.