Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Breakpoints] functions down() and between() adds +1 to index #13448

Closed
2 tasks done
b5imply opened this issue Oct 30, 2018 · 22 comments · Fixed by #14527 or #22695
Closed
2 tasks done

[Breakpoints] functions down() and between() adds +1 to index #13448

b5imply opened this issue Oct 30, 2018 · 22 comments · Fixed by #14527 or #22695
Labels
Milestone

Comments

@b5imply
Copy link

b5imply commented Oct 30, 2018

  • This is not a v0.x issue.
  • I have searched the issues of this repository and believe that this is not a duplicate.

Question

Why does the breakpoints functions down(key) and between(start, end) iterate the key and end +1 ?

Beheviour

If I want to style something below, say md (960px) for example, it adds +1 to the index and gets lg (1280px) instead. Dosen't down(key) means <=key?

between(start, end) always adds 1 to the end index. So if you want to style something between 960px and 1280px I need to call between('md', 'md'); which adds +1 to the end index of the last argument which gives @media (min-width: 960px) (max-width: 1280px ).

Link to reproduction

Link: https://codesandbox.io/s/20w4v142lj

  1. Adjust window to see box color change based on width

Context

From createBreakpoints.js in @material-ui/core/styles/:

function  down(key) {
    var  endIndex  =  keys.indexOf(key) +  1; // HERE
    var  upperbound  =  values[keys[endIndex]];
    
    if (endIndex  ===  keys.length) {
	    // xl down applies to all sizes
	    return  up('xs');
    }
    
    var  value  =  typeof  upperbound  ===  'number'  &&  endIndex  >  0  ?  upperbound  :  key;
    return  "@media (max-width:".concat(value  -  step  /  100).concat(unit, ")");
}

The same applies to the between(start, end) function

Environment

Tech Version
Material-UI v3.3.2
React v16.5.2
Browser Chrome v70.0.3538.77
@b5imply b5imply changed the title Breakpoints functions down() and between() adds +1 to index [Breakpoints] functions down() and between() adds +1 to index Oct 30, 2018
@oliviertassinari oliviertassinari added the support: question Community support but can be turned into an improvement label Oct 30, 2018
@oliviertassinari

This comment has been minimized.

@oliviertassinari oliviertassinari added docs Improvements or additions to the documentation and removed support: question Community support but can be turned into an improvement labels Oct 30, 2018
@b5imply

This comment has been minimized.

@oliviertassinari

This comment has been minimized.

@jamesots

This comment has been minimized.

@oliviertassinari

This comment has been minimized.

@oliviertassinari oliviertassinari added the good first issue Great for first contributions. Enable to learn the contribution process. label Feb 13, 2019
@matthewjwhitney

This comment has been minimized.

@oliviertassinari

This comment has been minimized.

oliviertassinari pushed a commit that referenced this issue Feb 16, 2019
* Update breakpoints.md for clarity

As proposed in the discussion from issue #13448 [Breakpoints] functions down() and between() adds +1 to index, the breakpoint widths are described as a range. Furthermore, I found some broken hash links that I attempted to fix.

* alternative
@aditya81070

This comment has been minimized.

@guix77
Copy link

guix77 commented Mar 29, 2019

After reading I still find it very strange to just not implement in the natural way which is

theme.breakpoints.down('md')

===

@media (max-width: 960px)

Why make it simple when it can be complex ?

Anyways, some examples could be added for clarity or maybe improve again the doc like for instance:

theme.breakpoints.down('md')

= matches screen widths less than and including the md screen widths range, which is (960 - 1279 px)

===

@media (max-width: 1279px)

Nothing strange, here, really ? ;-)

@oliviertassinari

This comment has been minimized.

@guix77

This comment has been minimized.

@oliviertassinari

This comment has been minimized.

@guix77

This comment has been minimized.

@oliviertassinari
Copy link
Member

oliviertassinari commented Apr 25, 2019

@guix77 What would be a correct logic?

@guix77
Copy link

guix77 commented Apr 25, 2019

Maybe this ?

theme.breakpoints.down('sm') => max-width: (sm-1) px
theme.breakpoints.downOrEqual('sm') => max-width: sm px
theme.breakpoints.up('sm') => min-width: (sm+1) px
theme.breakpoints.upOrEqual('sm') => min-width: sm px
theme.breakpoints.between('md', 'sm') => (min-width: md px and max-width: sm px)

@oliviertassinari

This comment has been minimized.

@oliviertassinari oliviertassinari removed docs Improvements or additions to the documentation good first issue Great for first contributions. Enable to learn the contribution process. labels Apr 25, 2019
@oliviertassinari oliviertassinari added this to the v5 milestone Apr 25, 2019
@oliviertassinari oliviertassinari added new feature New feature or request priority: important This change can make a difference labels Oct 15, 2019
@oliviertassinari
Copy link
Member

oliviertassinari commented Oct 15, 2019

Thank you, everybody, for the feedback on this issue. The best proposal I can come up with is to change the mental model behind breakpoints. Instead of considering breakpoints as values and ranges. It will be simpler to consider them as values exclusively. In this context, theme.breakpoints.down(x) will match the screen width strictly below the breakpoint value of x. The between helper would follow the same rule. The only case that might be confusing is when .only(x) is used. It would be equivalent to .between(x, breakpoint after x).
In practice, it's very close to the approach Bootstrap uses and what the authors of #13448 and #17875 propose. Hopefully, it's more intuitive.

This is a breaking change. We should wait for v5 to consider implementing it. The impact might be important but is limited by the fact that the usage of .up(x) should be significantly more frequent (our demos + mobile-first approach) and won't be impacted by the proposal.

@aditya81070
Copy link

@oliviertassinari Thanks a lot for this and importantly using hooks in v4. The material-UI is now more easy to use because of hooks.

@oliviertassinari
Copy link
Member

oliviertassinari commented Oct 27, 2019

I was wrong, Bootstrap v4 and Material-UI v4 have the same breakpoint behavior (I don't recall, but that might have influenced the current behavior).
However, they might apply the point this issue support in v5: twbs/bootstrap#29148.

@adybaby
Copy link

adybaby commented Apr 21, 2020

I've overwritten up, down, and between in theme.breakpoints as follows:

breakpoints: {
  get down() {
    return (key) => `@media (max-width:${this.values[key] - 0.5}px)`;
  },
  get up() {
    return (key) => `@media (min-width:${this.values[key] + 0.5}px)`;
  },
  get between() {
    return (keyA, keyB) =>
      `@media (min-width:${this.values[keyA]}px) and (max-width:${this.values[keyB] - 0.5}px)`;
  },
},

It seems to be working fine so far (and far less confusing, to my mind), but can anyone see any problems with this down the road?

@VenomFate-619
Copy link

When it will be solved , I also facing this issue

@oliviertassinari
Copy link
Member

@VenomFate-619 It's already solved, you can upgrade to v5.0.0-beta.5.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
8 participants