by crazygringo · 12y ago · 0 comments
Having a "polyfill" is certainly a valid justification. But this doesn't come close to LESS/Sass -- I'd argue that the main feature of those is nested rules, and then mixins.
Variables and calculations are great, but most LESS code I've encountered uses nesting and mixins to a far greater extent. Advertising the project as "the benefits of tools like LESS and Sass" seems misleading, and seems to set up expectations that Myth doesn't fulfill.
From everything I've seen nesting rules is an anti-pattern. It's useful for writing code as fast as possible, but for any project of reasonable size maintenance is a far bigger worry than LOC per second, and nesting makes for some horrible to maintain CSS. Until we have native extending in CSS you're much better off going the "one-class-per-element" route for maintenance.
As for mixins, I actually think they are an anti-pattern too. They're usually used for two different things. The first is to simply make vendor-prefixing easier, which this library already eliminates. For example the LESS homepage shows mixins being used when really it's just a regular old `border-radius` property. And the second is for extending classes with groups of properties, but mixins are a bad practice there because they just duplicate the code instead of doing any smart inheritance, so you end up with huge code bloat.
The real missing feature from preprocessors in my opinion is the idea of "extending", but I wanted to wait for the CSS spec for extensions to be more thought out before adding it, in case it varies a lot from how preprocessors do it.
---
The reason this came about is because we went back to writing pure CSS at Segment.io (after having used LESS, Stylus and Sass at different points) and realized that we shouldn't need to give up things like variables just because we don't want the extra weight of a new syntax.
Likewise, I feel like you're missing one of the main points of mixins -- that they allow you to repeat commonly used calculations that may involve multiple properties in complex ways. For example, a mixin I often use lets me create a vertical gradient by specifying a base color and a brightess spread amount, instead of start/end colors. It's incredibly valuable, and it also allows me to automatically support IE and specify a fallback base color. Mixins are hugely valuable for creating classes based on sprite positioning too. Going back to pure CSS without mixins would be a huge step backwards in maintainability for any project of decent size which I've worked on.
Obviously, both nesting and mixins can be abused or used inappropriately, but I'd never call them anti-patterns -- to the contrary, they're essential for front-end development on sites that reach a certain level of complexity, for cleanly factored code.
This is a contentious subject, so take everything I say as just, like, my opinion. Using generic class names like `titlebar` is likely to result in namespace collision. I define a project by modules, and use a slightly bastardized version of the block, element, modifier naming scheme to prevent this. It's verbose, but I consider the extra typing a reasonable price to pay for the ease of maintenance. For example: `.mod-modal`, `.mod-modal--title`, `.mod-modal--button_large_danger`. A sparse library of grid and spacing utility classes allow flexible formatting without having to write new definitions for every variation of your modules. Content hooks are provided for edge cases where you really do need to override some module styles for a specific page or design element. I use a `myproj-contentdescriptor` naming scheme. I apply these liberally but rarely define them in the stylesheet. The excessively convoluted naming scheme prevents leakage and interference from 3rd party libraries. It makes for some ugly-ass markup, but this is a pattern I've settled on after 10 years of CSS day job.
Edit: Ian just argued the same case more eloquently. This methodology seems to be gaining traction amongst framework authors over the last two years.
You're often better off just using BEM style selectors and using the selector name for nesting.
.titlebar {} .titlebar-button {}
As for mixins, yeah, you get some benefit if you're trying to be super tricky. But that button you've created is used once or twice, plus you could just use CSS variables and colour functions.
The question is now whether the added complexity everywhere else is worth that tiny benefit. There are plenty of people who have built large front-end sites and found that Sass/Less in fact make it harder to maintain.
But honestly, this stuff is totally subjective in a lot of cases. Different projects, different tools. If it works for you then stick with it, for other people, Myth and Rework are perfect.
Having written CSS for a decade and decided what semantics CSS had and how it should be structured, I was very surprised when I actually gave BEM/OOCSS/Whatever a go. It works. And I don't like going back now.
---
For nesting, the problem with nesting is that is messed with specificity way too much. Since adding an extra class inside a nested block is trivial, people start adding more and more. But even just 2 classes is already too much in my opinion. Ideally each component would have classes on each of its inner elements, so that the component-level styles always have a specificity of 0010:
<div class="textfield">
<label class="textfield-label"></label>
<input class="textfield-input">
</div>
If you have a setup like that, you never need to nest because your CSS ends up looking like this: .textfield {}
.textfield-label {}
.textfield-input {}
And that way all of your selectors are minimally invasive in terms of specificity. The problem with nesting is that you end up with selectors that have a specificity of 0020, which means anyone implements those components needs to be sure to achieve that level of specificity or greater to override them.But honestly, when working with small components there just isn't that much typing to justify the possible downsides. Sure I have to type out the extra selectors in the beginning a few more times, but its worth it to do without the unintended consequences of the magic.
The one case where nesting really does come in handy is for page-specific or app-specific stylesheet files. Places where you are adding the "glue" CSS that should always be namespaced to avoid collisions.
---
For mixins, I feel like they only seem useful if the components you're using are broken into small enough pieces to begin with. For example, if you want a complex gradient system on your buttons, put that inside your button styles and then don't touch it from anywhere else. If you're constantly needing to use a mixin to re-define those styles, something else is wrong. Because realistically how many different color variations of those buttons do you need to make? It should be somewhat limited, and they should be able to exist in a single file without issue.
Where things really get screwed is if you aren't versioning your mixins (which most people aren't doing). At that point, you're just tightly coupling changes to that mixins across your entire codebase. If you want to change the way the buttons implement the gradient, with the mixin system you go change the mixin, but now you need to visit every other component that uses that mixin and double-check that you didn't break anything.
Instead, if you didn't use a mixin and the component just contained its gradient logic (slightly more verbose maybe, but way more maintainable) then you don't have that problem.
The other solution is to strictly version your mixins using something like Component and maintain your mixins in a separate repository on GitHub that gets versioned, then that also works because each other component can peg the version of the mixin it is using.
---
My big takeaway from CSS though is that none of the solutions for it are nice right now. And most of these problems should disappear (I think) with proper native extension.
.textfield {
&-label {
...
}
&-input {
...
}
}http://csswizardry.com/2013/01/mindbemding-getting-your-head...
Basically it's anti-nesting and proposes being very specific with regards to class naming:
.block{} .block__element{} .block--modifier{}
I've experimented a bit with this lately but haven't come to a conclusion yet myself. Yes, it does decrease the chances of accidently styling the wrong elements, but it also makes the html more verbose and can in some cases make it more difficult to implement the website w/jQuery (because of the different classes in use, ie. instead of a .selected class you might have .selected__orange, .selected__apple so to switch the selected item you can't just do an .removeClass('selected')/.addClass('selected)
.textfield {}
.textfield input {}
.textfield label {}
.textfield {
label {}
input {}
}
etc.But you say:
> If you're constantly needing to use a mixin to re-define those styles, something else is wrong
> realistically how many different color variations of those buttons do you need to make?
> if you didn't use a mixin... (slightly more verbose maybe, but way more maintainable)
That's an awful lot of presumption, and it's just totally wrong, at least in my case. To use my gradient example: I probably apply this model to 50 or 100 types of elements across a site that's not even that big -- to buttons, to backgrounds, to panels, etc. because the designer likes subtle background variations -- flat design, but not too flat. So nothing is wrong, there are tons of variations, and using mixins is the just following the "dont-repeat-yourself" model.
Obviously, you could call this a model of "tight coupling", but that's a good thing, not bad -- you define a single model of mixins for your site, just like you have a single global CSS reset. It just makes up for certain common deficiencies in CSS, and helps in the creation of a common visual identity of your site where CSS classes themselves just aren't flexible enough. (There's another whole reason to use mixins used exclusively within specific components, but you seem to agree on that above.)
The "slightly more verbose" alternative you propose would actually be incredibly verbose (maybe 7 CSS properties per element instead of 1, if used on 100 types of elements, meaning 700 lines of code instead of 100), because nothing's being reused. If a new browser comes out which requires a slightly different tweak, you would need to manually fix it in 100 places instead of 1, which is horrible. Mixins simply allow you not to repeat yourself, which should be the goal with all good programming. CSS without mixins is kind of like programming in Python without being able to define functions.
(And asking people to version their mixins makes as much sense as versioning functions in a C++ project because changing a function changes its behavior. I.e., not much with rare exceptions. If you change a mixin you've been using for a while, then it goes without saying you inspect where it's used in the site to make sure it doesn't break things.)
.blah > .blorp
than it is with .blah .blorp
without resorting to .blah-blorp prefixing. .my-class {
font-weight: bold;
// use "placeholder selector":
& > .other-class {
// selector compiles to:
// .my-class > .other-class
border-width: 0;
}
}While it's more difficult to replicate the same issues in native CSS due to the friction involved of copying/pasting the same elements/classes over and over, I don't think that should be a reason to entirely avoid a tool that can be a great benefit in the long run.
On the other hand, per our email I have to say I'm more and more skeptical of the benefit and long-term maintainability of SCSS @extends and @mixin except in special circumstances.
Sass defaults to .scss syntax, which is exactly CSS syntax. Okay, exactly a superset. Since it merely adds $variables and other features, you are free to ignore them and just write “pure CSS” but with variables.
What am I missing?
This way, I can write SASS to target modern browsers and not worry about backward compatibility so much, because I know the postprocessor is there to catch the older browsers. e.g.: I don't want to worry about specifying a solid block colour for CSS gradients, I just want to specify a gradient.
But yes, I agree the implication that this could replace preprocessors is unrealistic.
But even if that is the case, it doesn't make any sense to me, and I can't begin to understand how they could be compatible with each other. How are you going to write "var(...)" or "color(...)" declarations within LESS, that LESS will ignore, just to "pass through" to Myth? And why would you? As far as I can tell, Myth doesn't do anything that LESS doesn't already do, so I don't see why anyone would add Myth on top of LESS.
The complexity of Sass/Less is starting to out-weigh the benefit of their features, for me at least.
Having said that, I don't know if this polyfills the var spec exactly.