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

[system] Add not operator to breakpoints #29311

Merged
merged 3 commits into from
Dec 8, 2021

Conversation

Philipp000
Copy link
Contributor

Allow to easily create styles, which are the inverted version of the only operator
Updated the documentation and included tests

This change is related to issue #29228

@mui-pr-bot
Copy link

mui-pr-bot commented Oct 26, 2021

Details of bundle changes

Generated by 🚫 dangerJS against bccfaf2

Copy link
Contributor

@chwallen chwallen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In addition to my suggestions, these lines

https://github.com/mui-org/material-ui/blob/f3c52b0da7296857b07782919b6ac93980ecb29d/docs/src/pages/customization/breakpoints/breakpoints.md?plain=1#L29-L34

must be updated as "four styles helpers" are now five. A link to the new not helper should also be inserted in between only and between as such:

- [theme.breakpoints.not(key)](#theme-breakpoints-not-key-media-query)

packages/mui-system/src/createTheme/createBreakpoints.js Outdated Show resolved Hide resolved
docs/src/pages/customization/breakpoints/breakpoints.md Outdated Show resolved Hide resolved
@Philipp000
Copy link
Contributor Author

I have resolved the problems you mentioned, thank you!
Is it okay this way?

@mbrookes mbrookes added new feature New feature or request package: system Specific to @mui/system labels Nov 12, 2021

it('should invert down for xs', () => {
expect(breakpoints.not('xs')).to.equal(
'@media not all and (min-width:0px) and (max-width:599.95px)',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is correct. If it should not be applied for xs it should not be applied till (max-width:600px) no? I believe we need to handle carefully the first and last breakpoint.

@chwallen what do you think?

Copy link
Contributor

@chwallen chwallen Dec 2, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mnajdova The query has the correct result but it is confusing to read not queries imo. xs is 0px to 600px, so either we write it as not between('xs', 'sm') (which is how only currently handles keys with index 0), or not down('xs').

For a more readable query, my suggestion would be to change the implementation of only to handle the 0-key as a special case. That would change the query above to to '@media not all (max-width:599.95px)' which I find slightly more readable like so (adding the keyIndex === 0 case):

function only(key) {
  const keyIndex = keys.indexOf(key);
  if (keyIndex === 0) {
    return down(keys[1]);
  } else if (keyIndex + 1 < keys.length) {
    return between(key, keys[keys.indexOf(key) + 1]);
  } else {
    return up(key);
  }
}

Otherwise, @Philipp000 handled these special cases in his initial commit which removes the not operator entirely for both the first and last breakpoint. I would modify his implementation and use between(key, key + 1) instead of constructing the inverted between query manually. The implementation is not as simple as just inverting only, but perhaps more readable.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can't change the implementation for only as it would be a breaking change.

Otherwise, @Philipp000 handled these special cases in his initial commit which removes the not operator entirely for both the first and last breakpoint. I would modify his implementation and use between(key, key + 1) instead of constructing the inverted between query manually. The implementation is not as simple as just inverting only, but perhaps more readable.

Would you like to try this? Whoever wants to continue the effort is more than welcomed :)

Copy link
Contributor

@chwallen chwallen Dec 4, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can't change the implementation for only as it would be a breaking change.

I don't see how it would be a breaking change by modifying only. The queries would be the same except for only('xs') which changes from @media (min-width:0px) and (max-width:599.95px) to @media (max-width:599.95px) (i.e. removing the redundant (min-width:0px) clause). If there are custom breakpoints, it is equivalently changed for the lowest breakpoint.

However, if you still consider it a breaking change, then we will skip only and focus on not.

Would you like to try this? Whoever wants to continue the effort is more than welcomed :)

I assume I can't push to this branch so either I re-open my PR and continue there or we wait for Philipp.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have now updated the implementation, to handle the first and last breakpoint differently. I hope, this improves the readability.
However, as @chwallen already pointed out, and as far as I'm concerned the results in the browser are identical.

Copy link
Member

@mnajdova mnajdova left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM now 👍 Thanks!

Copy link
Member

@siriwatknp siriwatknp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 This is great! Thanks for your contribution.

Allow to easily create styles, which are the inverted version of the only operator
Updated the documentation and included tests
Update the implementation of the "not" operator by using string replace
Copy link
Member

@hbjORbj hbjORbj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Philipp000 Amazing contribution. I am merging this in :)

@hbjORbj hbjORbj changed the title [system] Extend breakpoints with "not" operator [system] Add not operator to breakpoints Dec 8, 2021
@hbjORbj hbjORbj merged commit 87f82db into mui:master Dec 8, 2021
@Philipp000 Philipp000 deleted the extend-breakpoints-with-not branch December 8, 2021 10:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
new feature New feature or request package: system Specific to @mui/system
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants