-
-
Notifications
You must be signed in to change notification settings - Fork 24
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
Manage specificity through rule insertion order #1
Comments
What I've being doing in my personal project is to sort <style>.c0 { color: red; } </style>
<style media="(max-width: 360px)">.c1 { color: red; }</style>
<style media="(max-width: 500px)">.c2 { color: red; }</style> It's easier to insert a For SSR is easier since you can concat all rules within a single In my case, I know all breakpoints (media queries) ahead of time, so I create them always in the same order because I know those won't change. Maybe this helps 😄 |
Thank you for joining the discussion, that’s a great idea in fact! I’m not yet quite sure about managing multiple style tags at once, though. It should be an additional step towards more optimized code later on. I would appreciate if you could share more of your findings from your personal project 😊 |
@kripod I'm using Anyways, the only required config I have is to provide a list of breakpoints: const breakpoints = {
values: {
xs: 0,
sm: 600,
md: 960,
lg: 1280,
xl: 1680,
},
} And I generate an instance (or Something I'm not really a fan of is the pre-defined order for selectors such as This is what I came up with:
You can see that It's kind of like: // a
generateSelectorRule({ selector: ':hover', specificity: 1 }) // .c0
generateSelectorRule({ selector: ':focus', specificity: 2 }) // .c1
generateSelectorRule({ selector: ':active', specificity: 3 }) // .c2
// b
generateSelectorRule({ selector: ':hover', specificity: 1 }) // .c0 <--- same as 'a'
generateSelectorRule({ selector: ':active', specificity: 2 }) // .c3
generateSelectorRule({ selector: ':focus', specificity: 3 }) // .c4 Sure, it creates more atomic rules but it allows me, as a user, to define the order I want instead of being forced to have it in a pre-defined way. However, I'm still considering if it's worth it or not 😅 I'm also exploring a way to be able to parse, validate, and assign multiple selectors: css({
':hover, :active, :hover': { ... },
}) Right now it's just a dirty css({
':hover::before, :hover::after': { ... },
':hover:not([disabled]):not([aria-disabled="true"])': { ... },
':hover:not([aria-disabled="true"]):not([disabled])': { ... }, // < should be same as previous
'::before:not([disabled])': { ... }, // < should fail, ":not" is not allowed after pseudo-element ::before
}) I couldn't find a CSS validator written in JS that actually validates selectors, so I'm pretty far from making this last thing work reliably 😅 |
These are very valuable thoughts, thank you for sharing them! Tracking the order of pseudos is an interesting concept and definitely worth considering. |
@eddyw The 0.3.0 release has introduced support for advanced selectors, including comma-separated lists. They are not ordered in any way right now, but I feel like your idea is the way to go, instead of e.g. raising specificities by repeating As for simple pseudos, I think the default order should remain in place, keeping the amount of generated rules at a minimum. However, overrides should happen when the order of selectors don’t match up with the default. This requires some serious re-architecting, though. |
While trying to implement progressive enhancement for the |
Instead of opening a new bug issue i respond here because i believe its what is described. You can see in the example that the Example: https://codesandbox.io/s/media-specifity-gehzo @kripod i guess that is the situation you are trying to solve in this issue. |
@namjul That’s a very important example, thank you. Unfortunately, I’m not yet ready to prepare a fix, due to all the recent saddening events in the world. |
After thinking for a while, I'm trying to conclude all the thoughts above into a single solution. The primary goal is keeping the amount of inserted rules at a minimum, so each of them shall only be inserted to the CSSOM once. Precedences are easy to enforce for properties and pseudo-classes. However, instead of duplicating class names, they should be ordered in the source by maintaining a pre-defined amount of property groups (count: Custom As for As each conditional rule is inserted, their custom-specified order should be tracked upon insertion and rules shall be re-ordered with the CSS injector if a mismatch happens. This would allow both mobile-first and desktop-first approaches to be used with minimal overhead and support for the use of media query range contexts. Rule ordering summary:
|
This has been addressed by v0.5.0. The only caveat is that a conditional rule may get inserted into the stylesheet more than once, but that shouldn't cause size issues with gzip or Brotli compression in place. |
@all-contributors please add @eddyw for ideas and @namjul for bug reporting! |
I've put up a pull request to add @eddyw! 🎉 |
No, not like that. @all-contributors please add @eddyw for ideas! |
I could not determine your intention. Basic usage: @all-contributors please add @Someone for code, doc and infra For other usages see the documentation |
@all-contributors please add @eddyw for ideas! |
I've put up a pull request to add @eddyw! 🎉 |
@all-contributors please add @namjul for bug reporting! |
I've put up a pull request to add @namjul! 🎉 |
Motivation
Currently, rule specificities are managed by repeating generated class names. However, responsive media queries can't be ordered like that, as screen sizes aren't known in advance.
Details
Similarly to the methods of React Native Web and StyleSheet, style rules should be tracked and ordered upon insertion.
The text was updated successfully, but these errors were encountered: