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

Set CSS rule priority based on media query break points #82

Closed
liamqma opened this issue Dec 7, 2023 · 6 comments
Closed

Set CSS rule priority based on media query break points #82

liamqma opened this issue Dec 7, 2023 · 6 comments
Labels
question Further information is requested

Comments

@liamqma
Copy link

liamqma commented Dec 7, 2023

First of all, I just want to say "Awesome work". I have been looking forward to using Stylex since 2021 😃.

Is your feature request related to a problem? Please describe.
I have the below styles.

const styles = stylex.create({
   foo: {
      "@media (min-width: 10px)": {
         color: "red"
      },
      "@media (min-width: 20px)": {
         color: "green"
      }
   }
});

I am expecting

"@media (min-width: 20px)": {
  color: "green"
}

to be placed after

"@media (min-width: 10px)": {
      color: "red"
}

in the sheet.

However, the 👆 code gets babel transformed to

stylex.inject("@media (min-width: 10px){.xl9lbu0.xl9lbu0{color:red}}", 3200);
stylex.inject("@media (min-width: 20px){.xwdmb1y.xwdmb1y{color:green}}", 3200);

These two CSS rules have the same priority, which means the order of these two rules is indeterministic.

Describe a solution you'd like
When we calculate property property, can we consider the breakpoint value from media atrule?

Describe alternatives you've considered
I cannot think of anything alternative.

Additional context
I've raised a similar question in Compiled CSS-in-JS. atlassian-labs/compiled#1567

@necolas
Copy link
Contributor

necolas commented Dec 7, 2023

Please see the docs for the correct media query syntax https://stylexjs.com/docs/learn/styling-ui/defining-styles/#media-queries-and-other--rules

@liamqma
Copy link
Author

liamqma commented Dec 7, 2023

Thanks @necolas.
Unless I miss something, the syntax doesn't solve the issue

const styles = stylex.create({
  foo: {
    color: {
      default: "red",
      '@media (min-width: 10px)': "blue",
      "@media (min-width: 20px)": "green"
    }
  }
});

gets babel transformed to

stylex.inject(".x1e2nbdu{color:red}", 3000);
stylex.inject("@media (min-width: 10px){.xw7d59y.xw7d59y{color:blue}}", 3200);
stylex.inject("@media (min-width: 20px){.xwdmb1y.xwdmb1y{color:green}}", 3200);

Notice @media (min-width: 10px){.xw7d59y.xw7d59y{color:blue}} has the same priority as @media (min-width: 20px){.xwdmb1y.xwdmb1y{color:green}}.

What I look for is that @media (min-width: 20px){...} should have the higher priority than @media (min-width: 10px){...}.

@nmn
Copy link
Contributor

nmn commented Dec 7, 2023

@liamqma It is your responsibility to ensure that your "conditions" when applying various values for a property do not conflict.

const styles = stylex.create({
  foo: {
    color: {
      default: "red",
      '@media (min-width: 10px) and (max-width: 20px)': "blue",
      "@media (min-width: 20px)": "green"
    }
  }
});

There is an ESLint rule planned to detect conflicts and we're also discussing the possibility of automating more of this in the compiler itself.

@nmn nmn added the question Further information is requested label Dec 7, 2023
@nmn nmn closed this as completed Dec 7, 2023
@liamqma
Copy link
Author

liamqma commented Dec 7, 2023

Thank you for clarification!

@nmn
Copy link
Contributor

nmn commented Dec 31, 2023

Overlapping media queries can currently break. Use this instead:

gridTemplateColumns: {
      default: "repeat(4, minmax(0, 1fr))",
      "@media (max-width: 1100px) and (min-width: 900px)": "repeat(3, minmax(0, 1fr))",
      "@media (max-width: 900px) and (min-width: 700px)": "repeat(2, minmax(0, 1fr))",
      "@media (max-width: 700px)": "1fr",
    },

I know this is annoying and this will done automatically soon.

@alejandroyunes
Copy link

alejandroyunes commented Dec 31, 2023

thank you, I deleted my post since: i thought it was my fault.

got it to work following the docs:

    gridTemplateColumns: {
      default: "repeat(3, minmax(0, 1fr))",
      '@media (max-width: 700px)': "1fr",
      '@media (min-width: 1240px)': "repeat(4, minmax(0, 1fr))",
    },

your solution is better!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

4 participants