<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>David Clark Develops the Web</title>
    <description>David Clark is a web developer and this is where he writes about developing the web.</description>
    <link>https://davidtheclark.com/</link>
    <atom:link href="https://davidtheclark.com/feed.xml" rel="self" type="application/rss+xml" />
    <pubDate>Sun, 08 Apr 2018 07:44:36 -0700</pubDate>
    <lastBuildDate>Sun, 08 Apr 2018 07:44:36 -0700</lastBuildDate>
    <generator>Jekyll v3.7.3</generator>
    
      <item>
        <title>Lessons I May Have Learned From Working on Stylelint</title>
        <description>&lt;p&gt;&lt;a href=&quot;https://github.com/stylelint/stylelint/graphs/contributors&quot;&gt;A little over a year ago&lt;/a&gt; I started working on &lt;a href=&quot;https://stylelint.io/&quot;&gt;stylelint&lt;/a&gt;, a &lt;a href=&quot;http://postcss.org/&quot;&gt;PostCSS&lt;/a&gt;-powered stylesheet linter. (To learn more about stylelint, you can read &lt;a href=&quot;https://css-tricks.com/stylelint&quot;&gt;my article in CSS-Tricks about it&lt;/a&gt;.)&lt;/p&gt;

&lt;p&gt;It’s a fairly large open source project, and continues to grow at a fairly quick pace. I say “large” referring not only to codebase size, but also to exposed functionality and general ambition; and I say “grow” referring not only to code written, but also to attention received, issues opened, contributions offered, extensions created, etc.&lt;/p&gt;

&lt;p&gt;What I’ve heard has proven true: writing and maintaining open source code can be an effective course of study. The main reason I’ve been working on stylelint is to actively learn. And I definitely have. So lately I’ve been reflecting a little on some ideas that have crystallized for me during this experience with stylelint — that is, while working with a few international collaborators on a large open source project written in JavaScript, about CSS.&lt;/p&gt;

&lt;p&gt;I’m going to try to write down a few of these lessons I may have learned, for my own edification, and for anybody who comes across this.&lt;/p&gt;

&lt;p&gt;(Note that I call them “lessons I &lt;em&gt;may have learned&lt;/em&gt;” because just saying “lessons I have learned” sounds a little too confident for me, as though I tapped into Authority and now channel it. These may not be very good lessons, or I may not have learned them well. Judge for yourself. I know you want to.)&lt;/p&gt;

&lt;h2 id=&quot;1-one-of-the-best-ways-to-learn-about-a-language-is-to-statically-analyze-it&quot;&gt;1. One of the best ways to learn about a language is to statically analyze it&lt;/h2&gt;

&lt;p&gt;I now know a million or so times more about CSS than I did before starting stylelint. A million. Working on this project involving intense static analysis has compelled me to learn and repeatedly use precise terminology about language features; to read specs; to make fine distinctions and really understand subtle differences.&lt;/p&gt;

&lt;p&gt;And all that has led me to a judgment …&lt;/p&gt;

&lt;h2 id=&quot;2-non-standard-syntaxes-are-a-huge-obstacle-to-css-tooling&quot;&gt;2. Non-standard syntaxes are a huge obstacle to CSS tooling&lt;/h2&gt;

&lt;p&gt;I’ve come to believe that the wild popularity of preprocessors with non-standard syntactic constructs has made it so-so-so-so much harder to create powerful CSS tools. &lt;em&gt;Too&lt;/em&gt; much harder, for entirely inadequate payoff.&lt;/p&gt;

&lt;p&gt;Effective CSS tools rely on effective stylesheet parsing, and non-standard syntax spoils (or dramatically complicates) that parsing. This means that any attempt at a CSS tool must make countless exceptions and accommodations to avoid stumbling. And everybody seems to use Sass or Less, so if you want to build a general-purpose tool you just can’t ignore them.&lt;/p&gt;

&lt;p&gt;Someone prone to bold and punchy Twitter pronouncements should make one blaming Sass and Less for the CSS ecosystem not being better than it is. Sass and Less have won huge followings because they are good at what they do, but some portion of what they do effectively stunts other tools — and a lot of that portion is not actually very helpful. Without those behemoths running the show, who knows what it might have become by now? (We certainly would have had a great, widely used linter long before stylelint.)&lt;/p&gt;

&lt;p&gt;CSS has at least two built-in syntactic constructs that in theory allow for indefinite extension: functions and at-rules. You could create all the custom functions and at-rules you want &lt;em&gt;without violating the grammar of CSS — that is, without breaking tools that rely on parsing&lt;/em&gt;. But preprocessors went too far, introducing non-standard constructs — operators, &lt;code class=&quot;highlighter-rouge&quot;&gt;//&lt;/code&gt;-comments, variable interpolation, lists and maps, etc. — and thereby hampering awhatever is not deliberately, specifically tailored to their eccentricities. They’ve fragmented the landscape, and required every tool-building effort to be dramatically more complex and ambitious than it would otherwise be, burdening creators and maintainers.&lt;/p&gt;

&lt;p&gt;Ever since stylelint decided to support non-standard syntaxes, it seems that the majority of our actual bug reports have been — surprise, surprise — related to those non-standard syntaxes. I know those preprocessors were built on good intentions, but my work on stylelint makes me resent them. That’s demotivating. And most of the time (all of the time, really) I don’t think those constructs are worth the trouble they introduce. Not even close. &lt;a href=&quot;https://sass-lang.com/documentation/file.SASS_REFERENCE.html#maps&quot;&gt;Sass maps&lt;/a&gt; are not worth it. &lt;a href=&quot;http://lesscss.org/features/#features-overview-feature-mixins&quot;&gt;Less’s mixin syntax&lt;/a&gt; is not worth it (Sass got it right with at-rules). Even &lt;code class=&quot;highlighter-rouge&quot;&gt;//&lt;/code&gt;-comments are really not worth it (more on that below).&lt;/p&gt;

&lt;h2 id=&quot;3-i-prefer-to-write-node-modules-without-es2015-without-babel-compilation&quot;&gt;3. I prefer to write Node modules without ES2015, without Babel compilation&lt;/h2&gt;

&lt;p&gt;ES2015 is wonderful; and Babel is wonderful. We’re all better off that both exist. And for browser code that is already going to be compiled (because I use modules and bundle things), I have no qualms, I think, about using them. But for Node modules, I do have qualms.&lt;/p&gt;

&lt;p&gt;Many times I’ve regretted the early decision to write stylelint in ES2015, compiling with Babel. It introduced various complexities, tooling incompatibilities, bug-sources, and other slight burdens that could have been entirely avoided. And for what payoff? For sugary syntax changes that are nice enough but definitely not necessary. (The really helpful stuff, like new data structures, can be polyfilled without compilation).&lt;/p&gt;

&lt;p&gt;By the time I regretted this decision, I already felt like it was too late to turn back.&lt;/p&gt;

&lt;p&gt;From now on I will not be writing Node modules that require compilation, if I can help it. I will write them using the syntax available in the Node versions that I plan to support.&lt;/p&gt;

&lt;h2 id=&quot;4-its-nice-to-establish-a-standard-way-to-do-a-thing-even-if-its-a-trivial-thing&quot;&gt;4. It’s nice to establish a standard way to do a thing, even if it’s a trivial thing&lt;/h2&gt;

&lt;p&gt;Usually this means writing a function and reusing it instead of repeating a procedural pattern. In stylelint we’re always trying to figure out what a string of CSS means, so we find ourselves writing little heuristics to determine whether a value is a Sass variable, a hex color is valid, a character is “whitespace,” a word is a font family name, and on and on. We’ve ended up accumulating &lt;a href=&quot;https://github.com/stylelint/stylelint/tree/master/src/utils&quot;&gt;a utility belt of functions&lt;/a&gt;, and in the process found that there’s almost always at least one little trick to each of these games that would have been neglected half the time had we just re-implemented some logic, repeated a pattern. Also, these functions can be independently tested. I like them quite a bit.&lt;/p&gt;

&lt;p&gt;But a reusable function is not the only “standard way to do a thing.” Writing documentation gets easier the more formulaic you make it (and the documentation itself gets more consistent, and probably clearer and more accurate). The same goes for writing tests (more on that below). And reviewing code. And writing lots of very similar modules (like linter rules).&lt;/p&gt;

&lt;p&gt;If you think you may have done something well once, try to make it easy to do that same thing just as well every time.&lt;/p&gt;

&lt;p&gt;Also: If you contemplate what you’re doing as Establishing The Way To Do A Thing, you will probably analyze and solve the problem better than you would have if you’d just whipped off a few lines that apparently work in one context.&lt;/p&gt;

&lt;p&gt;(Keep in mind that you can always change your mind, and substitute a new way for the old. There is no eternally enduring and unchanging Way.)&lt;/p&gt;

&lt;p&gt;Then there’s all the minutiae, some that is arbitrary (like deciding whether or not to capitalize “stylelint”) and just needs to be decided upon; and some not so arbitrary — but still tiny, buried among similar minutiae, as easy to overlook as to frenetically fuss over. With these details, I find it’s often best to make a decision and stick with it — whether that means picking arbitrarily, or really weighing options — at the risk of seeming a little pedantic or over-strict. That way you don’t have to think about that minutiae anymore until a problem arises. &lt;em&gt;That’s one of the major benefits of linters in the first place&lt;/em&gt;, or of usage manuals in writing, or of some minor Good Habits in Life: reduce the cognitive load from minor, repeated decisions so that you can focus on the less minor, less common problems.&lt;/p&gt;

&lt;p&gt;I’m especially thankful for the patience and thoroughness of one of my collaborators, Richard Hallows, in figuring out standard ways to do things.&lt;/p&gt;

&lt;h2 id=&quot;5-make-meanings-and-motivations-explicit&quot;&gt;5. Make meanings and motivations explicit&lt;/h2&gt;

&lt;p&gt;This comes up all the time in all the large project I’ve worked on, including stylelint. I’ll find myself looking at a file I haven’t touched for 10 months, or looking at a file somebody else wrote and I reviewed quickly over breakfast while grumpy and half-watching hummingbirds. In these cases, I cannot rely on any remembered context (“… this line solved that one issue with operators in &lt;code class=&quot;highlighter-rouge&quot;&gt;calc()&lt;/code&gt; … and this one addressed the fact that units can be uppercase…”).&lt;/p&gt;

&lt;p&gt;There are always odd edge-cases to address, and the code that addresses them can be harmfully hermetic to a reader without context. So when I write or review code, I’m now trying to look at it as though I’ve already lost context, even when I have not (yet).&lt;/p&gt;

&lt;p&gt;The more time passes, the more code you and others pile up, the less you are able to assume contextual knowledge. Instead, whenever reasonable, you should &lt;em&gt;assume the lack of context&lt;/em&gt; — which means you must be explicit about the meanings of values and the motivations for decisions.&lt;/p&gt;

&lt;p&gt;Look back at your code and imagine someone else or your future self asking “What does this mean?” and “Why did you do that?” Then figure out a way to communicate with that hypothetical interlocutor.&lt;/p&gt;

&lt;p&gt;I’ve found a few practices that help with this.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Try to name variables and functions with purpose and precision. It is always better to type more letters than to rely on implicit context.&lt;/li&gt;
  &lt;li&gt;Write comments whenever variable and function names don’t mostly explain what’s going on.&lt;/li&gt;
  &lt;li&gt;Write down what you’re thinking and what you’ve done in issues and pull requests.&lt;/li&gt;
  &lt;li&gt;Write thorough tests. (Keep in mind, though, that tests can sometimes prove as mysterious as the source code if they do not include clear names and comments.)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If some bit of code is poorly named, uncommented, and untested, you can safely, sadly assume that someone is going to be confused and annoyed about it at some point — maybe tomorrow, maybe a year from now.&lt;/p&gt;

&lt;h2 id=&quot;6-more-trivial-preferences&quot;&gt;6. More trivial preferences&lt;/h2&gt;

&lt;h3 id=&quot;6a-i-prefer-object-options-arguments-to-long-argument-lists&quot;&gt;6a. I prefer object “options” arguments to long argument lists&lt;/h3&gt;

&lt;p&gt;Option keys can describe their values’ meanings, and that’s nice (see above). Also, unlike with an argument list, the order of properties in an object is arbitrary, the number indefinite. With an options object, it’s easy for function consumers to skip optional “arguments” they don’t need, or for you the author to add another “argument” in the future without worrying about where it falls in an already unwieldy list.&lt;/p&gt;

&lt;p&gt;As stylelint changed and expanded, I found myself again and again refactoring functions to accept options objects instead of argument lists. Now, whenever I realize that a function is going to require more than a couple of arguments, I consider an object, instead.&lt;/p&gt;

&lt;h3 id=&quot;6b-return-early-when-you-can&quot;&gt;6b. Return early when you can&lt;/h3&gt;

&lt;p&gt;Much of the logic in stylelint involves deciding what to ignore and what to analyze, because most of the rules only pertain to very specific chunks of your CSS. If a linting rule focuses on &lt;code class=&quot;highlighter-rouge&quot;&gt;rgba()&lt;/code&gt; arguments, for example, it can ignore any text that’s not in the value of a declaration in a function named &lt;code class=&quot;highlighter-rouge&quot;&gt;rgba&lt;/code&gt;, as well as any text in comments and strings. One way to think about this whittling down is as a series of nested conditionals:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;if x is in a declaration
  and x is in the value of that declaration
    and x is in a function in that value
      and the function's name is rgba
        and x is not a comment
          and x is not a string
            check x
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;By inverting the statements and returning early, you flatten the logic:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;if x is not in a declaration, ignore it
if x is not in the value of that declaration, ignore it
if x is not in a function in that value, ignore it
if that function's name is not rgba, ignore it
if x is in a comment, ignore it
if x is in a string, ignore it
check x
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This reduces indentation and curly-braced blocks in the code, which everyone appreciates. I also find that it eases reading, at least a little bit, because I don’t have to think of each point in the sequence as the continuation of one long coordination.&lt;/p&gt;

&lt;h3 id=&quot;6c-make-it-as-easy-as-possible-to-write-tests-and-you-will-have-more-tests&quot;&gt;6c. Make it as easy as possible to write tests, and you will have more tests&lt;/h3&gt;

&lt;p&gt;We have a lot of unit tests in stylelint. It looks like right now we’re at 21,174 assertions. We are able to write so many tests because we worked out a system that makes it as easy as can be to write them. This was a very good move, I think, because all of those tests we now have are very, very helpful. And new contributors don’t have much trouble writing new tests, because the process is so standard, so widely exemplified, so simple.&lt;/p&gt;

&lt;p&gt;Whenever I find myself hesitant to write tests in a project, I now wonder whether the reason is because I’m worried that the act of writing those tests is going to be a pain in the ass. If that’s the case, if I fear that my ass will be pained, then I wonder if I should be dissatisfied with the testing setup, and should try to figure out some way to make writing tests for that project less painful.&lt;/p&gt;

&lt;p&gt;In some cases, the ass pain may be inescapable. Some programs demand it. But in many cases, like stylelint’s, the pain can be ameliorated with a little planning and systematization upfront.&lt;/p&gt;

&lt;h3 id=&quot;6d-dont-worry-about-typing-extra-characters&quot;&gt;6d. Don’t worry about typing extra characters&lt;/h3&gt;

&lt;p&gt;Above I mentioned that I don’t think the &lt;code class=&quot;highlighter-rouge&quot;&gt;//&lt;/code&gt;-comments provided by stylesheet preprocessors are important enough to justify the troubles caused by non-standard syntax. I’m sure that plenty of Sass users would protest this with a complaint approximating this: “It’s tedious to close comments with &lt;code class=&quot;highlighter-rouge&quot;&gt;*/&lt;/code&gt;.”&lt;/p&gt;

&lt;p&gt;If I embedded GIFs into these posts, I’d embed one here showing somebody cocking an eyebrow suspiciously. Maybe next time I’ll think about GIFs.&lt;/p&gt;

&lt;p&gt;Many common justifications for non-standard syntax features are essentially the same as this. You may think you need Sass lists because you can then make a list of states and loop through it to create 50 selectors like &lt;code class=&quot;highlighter-rouge&quot;&gt;.state-[statename]&lt;/code&gt; without having to repeat that &lt;code class=&quot;highlighter-rouge&quot;&gt;.state-&lt;/code&gt; part. You may think you need JS arrow functions because they save you from typing &lt;code class=&quot;highlighter-rouge&quot;&gt;function&lt;/code&gt; all the time. Or you may think you should name your variable &lt;code class=&quot;highlighter-rouge&quot;&gt;p&lt;/code&gt; instead of &lt;code class=&quot;highlighter-rouge&quot;&gt;people&lt;/code&gt; because you’ll avoid typing 5 letters again and again throughout the function.&lt;/p&gt;

&lt;p&gt;Even if we didn’t have autocomplete in our editors, the complaint is pretty silly. I think everybody knows this, in their hearts. It is a mistake to give up &lt;em&gt;any&lt;/em&gt; real benefit, any at all, for the apparent advantage of typing &lt;code class=&quot;highlighter-rouge&quot;&gt;p&lt;/code&gt; instead of &lt;code class=&quot;highlighter-rouge&quot;&gt;people&lt;/code&gt;.&lt;/p&gt;
</description>
        <pubDate>Sun, 10 Jul 2016 00:00:00 -0700</pubDate>
        <link>https://davidtheclark.com/lessons-i-may-have-learned/</link>
        <guid isPermaLink="true">https://davidtheclark.com/lessons-i-may-have-learned/</guid>
        
        
      </item>
    
      <item>
        <title>A Modular Approach to UI Problem Solving</title>
        <description>&lt;p&gt;At &lt;a href=&quot;https://www.reactrally.com/2015/&quot;&gt;React Rally&lt;/a&gt; last year, I watched an excellent talk by &lt;a href=&quot;https://twitter.com/linclark&quot;&gt;Lin Clark&lt;/a&gt; called &lt;a href=&quot;https://slides.com/linclark/webpack/#/&quot;&gt;“Making small modules actually work with webpack and npm”&lt;/a&gt;. At the beginning of the talk, Lin called out some participants in browserify vs. webpack arguments for their condescending remarks about &lt;em&gt;authentic&lt;/em&gt; “modularity,” who sucks and who doesn’t. It’s been suggested that webpack (and other mono-repos, like React) are not “modular” because they are not split across separately packaged repositories; in response, Lin pointed out that the core of modular programming is an approach to &lt;em&gt;problem solving&lt;/em&gt;, not necessarily an approach to &lt;em&gt;packaging&lt;/em&gt;. And it’s the problem-solving part that’s most important.&lt;/p&gt;

&lt;p&gt;She explained that modular problem solving is about dismantling bigger problems into smaller problems, then solving those smaller problems with individual modules that are &lt;em&gt;highly cohesive&lt;/em&gt; and &lt;em&gt;loosely coupled&lt;/em&gt;. Each module should contain everything necessary to solve a particular, focused problem, and then provide a public interface that makes it usable by an indefinite variety of other modules, in an indefinite variety of contexts.&lt;/p&gt;

&lt;p&gt;If Problem A can be broken down into Problems B, C, and D, then you might consider creating Modules B, C, and D, each solving their own particular problems, upon which Module A can depend. Module A then becomes pretty small. And when you run into Problem X, you might realize that it also involve Problems B and D, so you can reuse those modules.&lt;/p&gt;

&lt;p&gt;Everybody is their own little world, of course, so you may disagree with this statement — but &lt;strong&gt;in my experience, it seems like not much effort has gone into modular problem solving for user interface components&lt;/strong&gt;. Most UI libraries present themselves as &lt;em&gt;complete drop-in solutions&lt;/em&gt; — the keyword being “complete.” These complete drop-in solution can be as small as a tooltip or as large as jQuery UI or Bootstrap.&lt;/p&gt;

&lt;p&gt;I can think of many reasons why this pattern may have developed, and I’m sure more knowledgeable and experienced developers can think of even more. But &lt;strong&gt;the frontend realm has evolved in such a way that it’s high time we start experimenting with more modular approaches&lt;/strong&gt;.&lt;/p&gt;

&lt;h2 id=&quot;the-time-is-ripe&quot;&gt;The time is ripe&lt;/h2&gt;

&lt;h3 id=&quot;dependency-management-and-bundling&quot;&gt;Dependency management and bundling&lt;/h3&gt;

&lt;p&gt;Until recently, the more small scripts you loaded, the slower your site would be; and the more dependencies any given script relied upon, the harder it would be to use. &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;script&amp;gt;&lt;/code&gt; tags, global variables, and weak or non-existent build processes meant that we had good incentives to write and use big libraries that tackled as many problems as possible within a single codebase.&lt;/p&gt;

&lt;p&gt;But &lt;em&gt;dependency management and bundling are no longer significant impediments for developers versed in npm and bundlers like browserify and webpack&lt;/em&gt;. As everybody should be. (The rest of Lin Clark’s talk was about that.)&lt;/p&gt;

&lt;h3 id=&quot;lets-get-sophisticated&quot;&gt;Let’s get sophisticated&lt;/h3&gt;

&lt;p&gt;Because of the way that web development can blend into graphic design and Wordpress and super-fast site churn for small clients, etc., there are many consumers of UI components that do not &lt;em&gt;want&lt;/em&gt; to know what problems need solving or the best way to solve them: they just want a drop-in fancy-looking Thing.&lt;/p&gt;

&lt;p&gt;I can see how plenty of people would need complete drop-in solutions for the kind of work they do. But frontend development has become increasingly complex, and there are more and more frontend developers who &lt;em&gt;are&lt;/em&gt; technically minded, who &lt;em&gt;want&lt;/em&gt; to pick apart their problems and &lt;em&gt;want&lt;/em&gt; to consider a variety of possible solutions, and want or even &lt;em&gt;need&lt;/em&gt; to customize their approach to a unique context. For us, drop-in solutions are becoming less and less appealing, if not out of the question altogether, most of the time.&lt;/p&gt;

&lt;h2 id=&quot;my-attempts-at-modular-ui-libraries&quot;&gt;My attempts at modular UI libraries&lt;/h2&gt;

&lt;p&gt;I’ve been working on a different approach while building a few open-source React components. Instead of constructing complete, drop-in, strictly React-focused solutions, I’ve tried to isolate problems and extract small JS libraries (ideally vanilla JS) to solve those problems; then include them as dependencies in the React component.&lt;/p&gt;

&lt;p&gt;So while developing these three React components …&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/davidtheclark/react-aria-menubutton&quot;&gt;react-aria-menubutton&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/davidtheclark/react-aria-modal&quot;&gt;react-aria-modal&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/davidtheclark/react-aria-tabpanel&quot;&gt;react-aria-tabpanel&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;… I’ve published these lower-level vanilla JS libraries:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/davidtheclark/tabbable&quot;&gt;tabbable&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/davidtheclark/focus-trap&quot;&gt;focus-trap&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/davidtheclark/focus-group&quot;&gt;focus-group&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/davidtheclark/no-scroll&quot;&gt;no-scroll&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/davidtheclark/teeny-tap&quot;&gt;teeny-tap&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;… and these lower-level React libraries:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/davidtheclark/react-displace&quot;&gt;react-displace&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/davidtheclark/focus-trap-react&quot;&gt;focus-trap-react&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I wrote this little post to explain why I did that.&lt;/p&gt;

&lt;h2 id=&quot;modular-problem-solving-means-better-solutions&quot;&gt;Modular problem solving means better solutions&lt;/h2&gt;

&lt;p&gt;When you’ve opened a modal, you shouldn’t be able to scroll the main page until you’ve closed that modal. This is a basic virtue of any decent modal. So every drop-in modal library out there that aspires to decency must implement its own solution to that problem (scroll-stopping). And because each implementation will be different, some will be great, some will be good, and some will be bad.&lt;/p&gt;

&lt;p&gt;One of the important niceties of a scroll-stopper is that it prevents the awkward horizontal bounce that can happen when the scrollbar disappears and reappears as modals open and close. This is a small but somewhat tricky problem: you have to detect whether the scrollbar is present or not, and if it is determine its width (which varies among browsers and operating systems), then alter some CSS styles to compensate and un-compensate as the scrollbar disappears and reappears.&lt;/p&gt;

&lt;p&gt;Plenty of modal libraries don’t do this at all or don’t do it very well. It would be better, of course, if those bad implementations willingly gave up their claim to life, and their host libraries adopted a superior scroll-stopping solution shared by one of the other modal libraries. A scroll-stopping module.&lt;/p&gt;

&lt;p&gt;When building react-aria-modal, I wanted to stop the scroll the best way I could, and at the same time make my solution available to others, making it easy for them to do the same thing at least as well as I could. So instead of writing my scroll-stopping logic directly into my React component, I made &lt;a href=&quot;https://github.com/davidtheclark/no-scroll&quot;&gt;no-scroll&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Here are what I consider to be the benefits:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;no-scroll focuses on solving one problem the best way I know how, without getting tangled up in other modal requirements.&lt;/li&gt;
  &lt;li&gt;Other people building other modal libraries can use no-scroll instead of reimplementing the same (or worse) logic in their own codebase.&lt;/li&gt;
  &lt;li&gt;If other people know of &lt;em&gt;better&lt;/em&gt; ways to solve that one problem, they can help out Me And All The World by improving no-scroll, thereby improving react-aria-modal and anything else that uses no-scroll.&lt;/li&gt;
  &lt;li&gt;Use-cases other than modals might exist for scroll-stopping; so other libraries and components that stop scrolling can also depend on no-scroll and share the solution.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;modular-problem-solving-means-reusable-solutions&quot;&gt;Modular problem solving means reusable solutions&lt;/h2&gt;

&lt;p&gt;Another problem with modals is that they work best when they are dynamically appended to the end of the &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;body&amp;gt;&lt;/code&gt; tag, instead of living in the DOM tree right next to whatever trigger invokes them.&lt;/p&gt;

&lt;p&gt;This isn’t really a problem in vanilla JS, but &lt;em&gt;is&lt;/em&gt; a problem in React. So I didn’t want to separate the solution from React, but still wanted to separate it from the modal components, because I knew that it would be handy in &lt;em&gt;other&lt;/em&gt; components. (For example, a loading spinner that overlays the whole screen is also better off if appended to the end of the &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;body&amp;gt;&lt;/code&gt;.)&lt;/p&gt;

&lt;p&gt;So I made &lt;a href=&quot;https://github.com/davidtheclark/react-displace&quot;&gt;react-displace&lt;/a&gt;, which I use in react-aria-modal and also use in some project-specific code for loading spinners and other things.&lt;/p&gt;

&lt;p&gt;Modals also need to trap focus. That’s not a React-specific problem at all. So I made &lt;a href=&quot;https://github.com/davidtheclark/focus-trap&quot;&gt;focus-trap&lt;/a&gt; to handle that in vanilla JS, so it could be used by other modal libraries and also by other UI components that may want to trap focus, like popovers with forms in them or who knows what else.&lt;/p&gt;

&lt;p&gt;And so on. &lt;em&gt;By extracting a problem and generalizing its solution you can ensure that the same (hopefully good) solution is easily available for you when that problem rises again, maybe unexpectedly, in another context.&lt;/em&gt;&lt;/p&gt;

&lt;h2 id=&quot;the-diversity-of-js-frameworks-makes-low-level-libraries-especially-appealing&quot;&gt;The diversity of JS frameworks makes low-level libraries especially appealing&lt;/h2&gt;

&lt;p&gt;I’m using React a lot now, and liking it. But I do understand why other people are using Angular, Ember, web components, whatever else, no big weird framework at all, and so on. And I also know that I’ll probably be using some other component-creating-JS-tool in the future.&lt;/p&gt;

&lt;p&gt;A React menubutton, no matter how good, is not the only menubutton that the world or I myself will need. There is not and will never be a menubutton to end all menubuttons.&lt;/p&gt;

&lt;p&gt;Let’s say that I pull off a truly fabulous menubutton component in React, for users of React. That’s swell for Current React-Using Me, and swell for other React users; but &lt;em&gt;if all the important code is written in a React-specific manner, it sucks for non-React users and for Future Me&lt;/em&gt;. For them, my fabulous React menubutton is about as useless as a crappy React menubutton, or none at all. When a Non-React User or Future Me tries to create a new, equally fabulous menubutton component in the UI framework of the future — let’s call it KrazyKomponents — they’re going to have to reimplement whatever made my React widget fabulous. And then if they improve their KrazyKomponents menubutton with Rad Feature X, my formerly-fabulous React component falls behind — unless I put in the labor to write my own React-specific implementation of Rad Feature X.&lt;/p&gt;

&lt;p&gt;And so everybody loses, forever.&lt;/p&gt;

&lt;p&gt;With &lt;a href=&quot;https://github.com/davidtheclark/react-aria-menubutton&quot;&gt;react-aria-menubutton&lt;/a&gt;, I tried to isolate the hard problems into a vanilla JS library that I and others could re-use, even in non-React menubutton components (or, even further, in non-React, non-menubutton components). Hiding and showing stuff isn’t very hard. Neither is toggling ARIA attributes. Those problems are trivial in the React code, and probably will be in any other decent view library (current or future). But the keyboard interactions are more tricky. The up and down arrows should move focus through the items, wrapping from top to bottom and bottom to top; and it’s all so much better if your user can type letters to skip to specific menu items. So instead of solving those keyboard-interaction and focus-management problems with React-specific code, I wrote &lt;a href=&quot;https://github.com/davidtheclark/focus-group&quot;&gt;focus-group&lt;/a&gt; in vanilla JS, which provides an API enabling that kind of menu-y behavior.&lt;/p&gt;

&lt;p&gt;Somebody writing ember-menubutton, or angular-menubutton, or krazykomponents-menubutton can use focus-group to get the same sweet keyboard interactions that I worked hard on for react-aria-menubutton. With those keyboard interactions taken care of, the rest of their framework-specific menubutton code might be pretty trivial, really.&lt;/p&gt;

&lt;p&gt;Additionally, if those Ember, Angular, or KrazyKomponents authors figure out &lt;em&gt;better&lt;/em&gt; keyboard interactions, we can update the focus-group library and react-aria-menubutton wins, too.&lt;/p&gt;

&lt;p&gt;Everybody can win, at the same time.&lt;/p&gt;

&lt;p&gt;(By the way: The earlier versions of react-aria-menubutton had not extracted this solution into focus-group. So in &lt;a href=&quot;https://github.com/davidtheclark/react-aria-tabpanel&quot;&gt;react-aria-tabpanel&lt;/a&gt; I implemented very similar logic that also involved left and right arrows keys, but did not bother doing anything with letter keys, because that’s kind hard. Now, both UI components use focus-group, which means both have the same quality of keyboard interaction and I was able to add letter-key navigation to react-aria-tabpanel without any additional strain and suffering.)&lt;/p&gt;

&lt;h2 id=&quot;so-thats-something-worth-trying-right&quot;&gt;So that’s something worth trying, right?&lt;/h2&gt;

&lt;p&gt;I think so.&lt;/p&gt;
</description>
        <pubDate>Sat, 20 Feb 2016 00:00:00 -0700</pubDate>
        <link>https://davidtheclark.com/modular-approach-to-interface-components/</link>
        <guid isPermaLink="true">https://davidtheclark.com/modular-approach-to-interface-components/</guid>
        
        
      </item>
    
      <item>
        <title>It's Time for Everyone to Learn About PostCSS</title>
        <description>&lt;p&gt;A while ago, I wrote &lt;a href=&quot;/excited-about-postcss&quot;&gt;“I’m Excited About PostCSS, But I’m Scared to Leave Sass”&lt;/a&gt;. Since then, I have wholeheartedly embraced PostCSS (and left Sass, at least temporarily). I’ve been using PostCSS on large-scale projects, contributing to and authoring plugins, communicating with the maintainers to learn more about what’s possible; and that’s all gone swimmingly. Just swimmingly.&lt;/p&gt;

&lt;p&gt;Meanwhile, the buzz around PostCSS has increased, provoking all kinds of reactions — curiosity, excitement, suspicion, confusion, weariness, bitterness, defensiveness, vitriol, exhiliration, flippant indifference, smug disdain, cheerful fist-pumping, and so on.&lt;/p&gt;

&lt;p&gt;I have two points I’d like to contribute to the fuss:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;You don’t have to be afraid. The number of tools out there to &lt;em&gt;process&lt;/em&gt; style-code is actually pretty small (or so it seems to me, who also writes JavaScript). The addition of more possibilities won’t hurt anybody or anything.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Everybody who writes CSS should learn what PostCSS is&lt;/strong&gt; — what it &lt;em&gt;really&lt;/em&gt; is, and what it can be used for (not just what some choleric, reactive Tweeter said about it) — whether or not you end up using it right now. Because &lt;em&gt;if you think PostCSS is simply an alternative to Sass and Less, you’re misinformed&lt;/em&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;I don’t know what I can do to encourage #1 … Some blend of comforting words, coach-like encouragement, gentle prodding, and perspective? I’m probably not the best counselor in this case.&lt;/p&gt;

&lt;p&gt;So I’ll skip to #2, where I may be able to help. Having worked with PostCSS for a little while now, I think I’ve learned some things about it that are worth sharing.&lt;/p&gt;

&lt;h2 id=&quot;what-we-mean-when-we-say-postcss&quot;&gt;What we mean when we say “PostCSS”&lt;/h2&gt;

&lt;p&gt;With the word “PostCSS” we might alternately refer to two things:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;There is &lt;a href=&quot;https://github.com/postcss/postcss&quot;&gt;PostCSS&lt;/a&gt;, the tool itself — what you get when you run &lt;code class=&quot;highlighter-rouge&quot;&gt;npm install postcss&lt;/code&gt; — and&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/postcss/postcss#plugins&quot;&gt;The PostCSS plugin ecosystem&lt;/a&gt; powered by that tool.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The tool itself is a Node.js module that parses CSS into an &lt;a href=&quot;https://en.wikipedia.org/wiki/Abstract_syntax_tree&quot;&gt;abstract syntax tree (AST)&lt;/a&gt;; passes that AST through any number of “plugin” functions; and then converts that AST back into a string, which you can output to a file. Each function the AST passes through may or may not transform it; sourcemaps will be generated to keep track of any changes.&lt;/p&gt;

&lt;p&gt;The AST provides a straightforward API that developers can use to write plugins. For example, you can cycle through each rule set in a file with &lt;code class=&quot;highlighter-rouge&quot;&gt;css.eachRule()&lt;/code&gt;, or each declaration in a rule with &lt;code class=&quot;highlighter-rouge&quot;&gt;rule.eachDecl()&lt;/code&gt;. You can get the selector of a rule with &lt;code class=&quot;highlighter-rouge&quot;&gt;rule.selector&lt;/code&gt;, or the name of an at-rule with &lt;code class=&quot;highlighter-rouge&quot;&gt;atRule.name&lt;/code&gt;. From these few examples you can see that the PostCSS API makes it pretty easy to work with CSS source code (&lt;em&gt;much&lt;/em&gt; easier and more accurately than if you were to rely on regular expressions, like a chump).&lt;/p&gt;

&lt;p&gt;That’s all that PostCSS currently does: it does not alter your CSS. A &lt;em&gt;plugin&lt;/em&gt; might do that. Or maybe it won’t. &lt;em&gt;PostCSS plugins can do pretty much whatever they want with the parsed CSS.&lt;/em&gt; One plugin could enable variables, or some other useful language extension. Another could change all of your &lt;code class=&quot;highlighter-rouge&quot;&gt;a&lt;/code&gt;s to &lt;code class=&quot;highlighter-rouge&quot;&gt;k&lt;/code&gt;s. Another could log a warning whenever you use an ID selector. Another could add Masonic ASCII art to the top of your stylesheets. Another could count all the times you use &lt;code class=&quot;highlighter-rouge&quot;&gt;float&lt;/code&gt; declarations. And so on, forever.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;PostCSS can power an unlimited variety of plugins that read and manipulate your CSS. These plugins have no unifying agenda, except to solve problems.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Note that neither the tool itself nor the plugin ecosystem are &lt;em&gt;direct&lt;/em&gt; analogues to Sass and Less. However: bundle up a set of related plugins that transform author-friendly stylesheets into browser-friendly CSS, and &lt;em&gt;then&lt;/em&gt; you have something analogous to the good ol’ “preprocessors.”&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Buuuut&lt;/em&gt; keep in mind that these “plugin packs” are just additional members of the ecosystem, like all the non-packaged plugins. &lt;strong&gt;No given plugin or plugin pack &lt;em&gt;is&lt;/em&gt; or &lt;em&gt;represents&lt;/em&gt; “PostCSS”: instead, we have a growing ecosystem that consists of many &lt;em&gt;individual modules&lt;/em&gt; (powered by PostCSS).&lt;/strong&gt;&lt;/p&gt;

&lt;h2 id=&quot;a-few-implications-of-postcss-modularity&quot;&gt;A few implications of PostCSS modularity&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;☞ Attempts to maintain that PostCSS is (or should be) a “postprocessor”, as opposed to the “preprocessors” Sass and Less, are misguided.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Whatever your definitions of “postprocessor” and “preprocessor”, there will be PostCSS plugins that fall into both camps. According to most definitions, &lt;a href=&quot;https://github.com/postcss/autoprefixer&quot;&gt;Autoprefixer&lt;/a&gt; is the iconic “postprocessor”; but there’s also stuff like &lt;a href=&quot;https://github.com/outpunk/postcss-each&quot;&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;postcss-each&lt;/code&gt;&lt;/a&gt;, which is very “preprocessor”-y.&lt;/p&gt;

&lt;p&gt;There are also plugins that don’t transform your CSS at all, like &lt;a href=&quot;https://stylelint.io/&quot;&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;stylelint&lt;/code&gt;&lt;/a&gt; and &lt;a href=&quot;https://github.com/postcss/postcss-bem-linter&quot;&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;postcss-bem-linter&lt;/code&gt;&lt;/a&gt; and &lt;a href=&quot;https://github.com/davidtheclark/list-selectors&quot;&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;list-selectors&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;If you want to maintain some pure distinction in your own build process, &lt;em&gt;only&lt;/em&gt; using PostCSS plugins for what you think of as “postprocessing” — well that’s just fine: select your plugins with care.&lt;/p&gt;

&lt;p&gt;Building on that …&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;☞ Attempts to tie “PostCSS” to specific syntax extensions or transformations are misguided.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;PostCSS is a low-level module that facilitates the creation of other tools; and there are no limits on what those higher-level tools (the plugins) might do.&lt;/p&gt;

&lt;p&gt;So PostCSS is not “about” allowing you write CSS Of The Future (syntax and functions from spec drafts) any more that it is “about” providing loops and conditionals and other Sass-like features. There are individual plugins that do both, and plugin packs that do both (&lt;a href=&quot;http://cssnext.io/&quot;&gt;cssnext&lt;/a&gt; and &lt;a href=&quot;https://github.com/jonathantneal/precss&quot;&gt;precss&lt;/a&gt;); but none of these represents the extent of PostCSS.&lt;/p&gt;

&lt;p&gt;All this means that …&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;☞ When people think they are criticizing “PostCSS,” they are probably criticizing some particular plugin, or plugin pack, or particular way to use a particular plugin.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Which is fine — criticize away — but don’t trick yourself into dismissing &lt;em&gt;other&lt;/em&gt; PostCSS-based tools because &lt;em&gt;one&lt;/em&gt; of them rubs you the wrong way.&lt;/p&gt;

&lt;p&gt;Which leads to the next point …&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;☞ You can opt into or out of any PostCSS plugin at any time.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Each plugin is only part of your build process because you put it there. If a plugin displeases you, remove it. Nobody is going to stop you.&lt;/p&gt;

&lt;p&gt;Keep in mind, though, that some plugins can be used in a variety of ways, and maybe your displeasure can be appeased by &lt;em&gt;using&lt;/em&gt; the plugin differently.&lt;/p&gt;

&lt;p&gt;Maybe you, &lt;a href=&quot;https://twitter.com/chriseppstein/status/618515591582724096&quot;&gt;like Chris Eppstein&lt;/a&gt;, don’t like that with &lt;a href=&quot;https://github.com/daleeidd/postcss-define-property&quot;&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;postcss-define-property&lt;/code&gt;&lt;/a&gt; you can create new properties that look exactly like real, standard properties. Well, there’s a very simple solution for that: make new properties that &lt;em&gt;don’t&lt;/em&gt; look exactly like real, standard properties.&lt;/p&gt;

&lt;p&gt;And if you think a plugin needs better examples or new options, &lt;em&gt;you can contribute&lt;/em&gt;, because …&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;☞ Plugins are relatively small modules, so they should be responsive to feedback and easy to contribute to.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;And if you don’t see a plugin that does what you want, the way you want to do it …&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;☞ You can always build your own plugin to satisfy your own desires.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This is one of the most important points. It’s worth repeating …&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;☞ You can always build your own plugin to satisfy your own desires.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;That&lt;/em&gt; makes PostCSS new and fantastic — the ease with which you can try something totally different.&lt;/p&gt;

&lt;p&gt;Or you can slightly tweak what’s there. If some plugin uses syntax you like but functionality you hate, create a spin-off with the “right” functionality. If some other plugin provides functionality you love but syntax you abhor, create a spin-off with the “right” syntax. And when people see what you’ve wrought and complain about &lt;em&gt;your&lt;/em&gt; plugin, you can always suggest they write their own, their way.&lt;/p&gt;

&lt;p&gt;(At the risk of sounding ridiculous and grandiose …) I’d suggest that, for many designers and frontend developers, &lt;em&gt;really&lt;/em&gt; learning about PostCSS should &lt;em&gt;demystify&lt;/em&gt; the realm of CSS processing. All that functionality that Sass and Less provide — it’s not magic: it doesn’t &lt;em&gt;have&lt;/em&gt; to be that way: those are just people behind the curtain, and though they may be smart and hard-working, you don’t have to assume that they know better than you what’s best for &lt;em&gt;your&lt;/em&gt; situation.&lt;/p&gt;

&lt;h2 id=&quot;problem-solving-with-postcss&quot;&gt;Problem-Solving with PostCSS&lt;/h2&gt;

&lt;p&gt;Working with PostCSS has reminded me that CSS processing exists to solve problems; that pretty much all problems have multiple solutions; and that &lt;em&gt;I&lt;/em&gt; might be qualified to pick between alternative solutions, or even construct my own.&lt;/p&gt;

&lt;p&gt;Empowered by PostCSS, I’ve been tackling my CSS needs &lt;em&gt;problem-first&lt;/em&gt; — similarly to how I deal with JavaScript. Instead of jumping behind a massive library before I really know what’s going on, &lt;em&gt;first&lt;/em&gt; I think about the actual problem that needs solving; &lt;em&gt;then&lt;/em&gt; I consider existing solutions; and &lt;em&gt;then&lt;/em&gt; I either use an existing solution or start working on my own.&lt;/p&gt;

&lt;p&gt;I think that process is fun and interesting.&lt;/p&gt;

&lt;p&gt;Also, I think it’s helped me simplify my approach to CSS. Remember — though it may seem long ago — that many developers have at some point resisted adoption of Sass and Less because they were concerned that the “preprocessors” did not solve enough real problems to justify the complexity they might add to authoring. I’ve never really agreed (maybe because I’ve never minded a little complexity in my build process); but I do acknowledge the critique and concede that &lt;em&gt;if you don’t feel like problems are getting solved by a tool, you should not feel obliged to use that tool&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;I built (and continue to maintain) a &lt;a href=&quot;https://davidtheclark.github.io/scut/&quot;&gt;substantial Sass utility library&lt;/a&gt; because it solved important problems at my previous job, where I had to pound out a lot of CSS quickly. Now I have a new job, with different problems (e.g. &lt;a href=&quot;https://github.com/davidtheclark/scalable-css-reading-list&quot;&gt;scalability&lt;/a&gt;, and weird, unique theming requirements); and for my current needs I find myself preferring a minimalistic approach to CSS, involving at least as much &lt;em&gt;analysis&lt;/em&gt; as &lt;em&gt;processing&lt;/em&gt;. I also want to &lt;em&gt;limit&lt;/em&gt; the powers at my team’s disposal, only including non-standard features &lt;em&gt;selectively&lt;/em&gt;. PostCSS, the tool and the ecosystem, is an excellent fit for my current needs.&lt;/p&gt;

&lt;h2 id=&quot;thats-enough&quot;&gt;That’s enough&lt;/h2&gt;

&lt;p&gt;I was going to write another section called “Now, for kicks, let’s address some ill-conceived criticisms of PostCSS.” But I think this post is already long enough. And I think that the astute reader will already understand what most of my rebuttals would look like. If you do have an accusation that you’d like to hear rebutted, tweet me at &lt;a href=&quot;https://twitter.com/davidtheclark&quot;&gt;@davidtheclark&lt;/a&gt; and I’ll tweet you back.&lt;/p&gt;
</description>
        <pubDate>Tue, 14 Jul 2015 00:00:00 -0700</pubDate>
        <link>https://davidtheclark.com/its-time-for-everyone-to-learn-about-postcss/</link>
        <guid isPermaLink="true">https://davidtheclark.com/its-time-for-everyone-to-learn-about-postcss/</guid>
        
        
      </item>
    
      <item>
        <title>Building an Open Source React Component</title>
        <description>&lt;p&gt;Recently I set out to build a fully accessible dropdown menu-button with React. It would have been trivial to code an &lt;em&gt;inaccessible&lt;/em&gt; dropdown menu-button in React — just toss in a little state and a click handler to hide and show the menu. But adding accessibility, accommodating screen readers and keyboard users, was &lt;em&gt;not&lt;/em&gt; trivial; so I decided I’d try to bundle my solution into a module that could be open-sourced and shared, reused and improved by others — a self-contained React component that would be (ideally) complete, composable, flexible, and well-tested.&lt;/p&gt;

&lt;p&gt;The creation of &lt;a href=&quot;https://github.com/davidtheclark/react-aria-menubutton&quot;&gt;react-aria-menubutton&lt;/a&gt; (&lt;a href=&quot;https://davidtheclark.github.io/react-aria-menubutton/&quot;&gt;demo here&lt;/a&gt;) was an interesting process: I had to confront a few of the dilemmas that make UI components so hard to isolate, modularize, and share effectively. I thought it might be worth writing about those dilemmas and the solutions that I settled on.&lt;/p&gt;

&lt;h2 id=&quot;accessibility-is-hard&quot;&gt;Accessibility is Hard&lt;/h2&gt;

&lt;p&gt;Don’t let anybody tell you otherwise.&lt;/p&gt;

&lt;p&gt;Yes, yes, yes, I know, of course, you’re right, absolutely — there are indeed some very simple, elementary best-practices that do affect accessibility (e.g. use &lt;code class=&quot;highlighter-rouge&quot;&gt;alt&lt;/code&gt; text! semantic tags! colors with sufficient contrast!). And if that’s all you’re worried about, then yes, accessibility &lt;em&gt;is easy&lt;/em&gt;. But pass beyond those basics, &lt;em&gt;try to build something sufficiently complex with JS interactivity&lt;/em&gt;, and (unless your experience is dramatically different from mine) your research will lead you into a baffling hodgepodge of incomplete, contradictory, insufficiently exemplified, inadequately verified, usually outdated material.&lt;/p&gt;

&lt;p&gt;(If you care about accessibility, and have some knowledge in the area, you &lt;em&gt;could&lt;/em&gt; gain a few Twitter followers by delivering self-righteous one-sentence decrees that universal accessibility is a divine mandate and any web project falling short of it has failed because of the sloth, willful ignorance, concupiscence, and general immorality of its individual developers. &lt;em&gt;Or&lt;/em&gt; you could be honest about this bad situation — that &lt;em&gt;inadequate information and poor exemplification is thwarting the best intentions&lt;/em&gt; — and try to improve it.)&lt;/p&gt;

&lt;p&gt;What I needed to build my menu-button component was an authoritative resource telling me precisely what WAI-ARIA attributes, keyboard interactions, and focus management I would need to implement in order to achieve the best possible accessibility. I am not an expert myself and do not have the resources to run my own user tests, so I needed to rely on authority. Luckily, my intended component fit nicely into one of the WAI-ARIA Design Patterns: the &lt;a href=&quot;https://www.w3.org/TR/wai-aria-practices/#menubutton&quot;&gt;Menu Button widget&lt;/a&gt;. I decided to accept that spec and its clear requirements as my authority, without worrying further about other contradictory resources, and follow it to the letter. (It’s possible, given the apparent uncertainty and lack of consensus around these issues, that some people would disagree that this pattern is the “ideal”; but unless a higher authority provides a clear alternative, it’s the best I have to go on.)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The main reason I consider &lt;a href=&quot;https://github.com/davidtheclark/react-aria-menubutton&quot;&gt;react-aria-menubutton&lt;/a&gt; worth open-sourcing is that it carefully implements this ARIA Design Pattern.&lt;/strong&gt; With React (or jQuery, or just plain JS), showing and hiding some menu element with the click of a button is too simple to justify an open source component. However, I would not want to re-implement, again and again, the fine details of the &lt;em&gt;accessible&lt;/em&gt; interaction, especially the keyboard accommodations. I think this probably applies to many other frontend components, as well: the hardest &lt;em&gt;generalizable&lt;/em&gt; part to implement is often solid accessibility.&lt;/p&gt;

&lt;h2 id=&quot;css-is-unavoidable-and-problematic&quot;&gt;CSS is Unavoidable and Problematic&lt;/h2&gt;

&lt;p&gt;In the realm of UI, JS must always integrate with CSS and HTML. There is no escape from this. You cannot create a working component without DOM elements, and, in almost every case, that component will hardly look &lt;em&gt;or function&lt;/em&gt; as intended without some CSS.&lt;/p&gt;

&lt;p&gt;This is a major obstacle for &lt;em&gt;encapsulation&lt;/em&gt;, which is an essential feature of effective modules. Everything in CSS is global, everything cascades. It is &lt;em&gt;so&lt;/em&gt; easy to write CSS &lt;em&gt;so&lt;/em&gt; badly, littered with unintentional side-effects, uncontrolled consequences, quantum entanglements. And browsers come with inconsistent default styling that developers override in unique and unpredictable ways. All of this means that &lt;strong&gt;the module developer cannot really know whether or not her CSS will actually work in your context&lt;/strong&gt;. Nevertheless, almost all UI components &lt;em&gt;need&lt;/em&gt; to come with a stylesheet, at least something minimal. And on top of that, those styles &lt;em&gt;need&lt;/em&gt; to be easily customizable, because (outside of frontend toolkits) no two designs are exactly the same, and &lt;em&gt;an effective component needs to fit nicely into an indefinite multitude of stylistic contexts&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;(Maybe the advent of web components, with &lt;a href=&quot;https://dev.w3.org/csswg/css-scoping/#scope&quot;&gt;“scoped styles”&lt;/a&gt;, will eventually solve this problem; but who knows when that technology will practically affect day-to-day development.)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;In my opinion, it is more important for open source UI components to provide &lt;em&gt;the means for custom-styling&lt;/em&gt; than it is for them to include pre-written styles.&lt;/strong&gt; In my many attempts to use third-party components, I have spent more time fighting against built-in styles than embracing them, more time annoyed by the component’s CSS than grateful for it.&lt;/p&gt;

&lt;p&gt;With &lt;a href=&quot;https://github.com/davidtheclark/react-aria-menubutton&quot;&gt;react-aria-menubutton&lt;/a&gt;, my attempt to alleviate this problem has several parts:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;In the JS I inline a few styles — just a few that I think are inevitable — so that users don’t have to think about them at all.&lt;/li&gt;
  &lt;li&gt;Instead of working hard on a single impressive skin for the component, I provide &lt;em&gt;style suggestions&lt;/em&gt; in two forms:
    &lt;ol&gt;
      &lt;li&gt;A totally bare-bones stylesheet containing the minimum essential rules to “complete,” in a sense, the component’s functionality. I imagine that the user could copy-and-paste this code into her own stylesheets and then add more rules to make the thing match its context.&lt;/li&gt;
      &lt;li&gt;Several examples of polished skins (toggle between them in &lt;a href=&quot;https://davidtheclark.github.io/react-aria-menubutton/&quot;&gt;the demo&lt;/a&gt;). If a user wants to take one and run with it, that’s fine; but I imagine that most people will use them as sources of ideas, extracting and modifying the rules that blend with their different-but-similar design.&lt;/li&gt;
    &lt;/ol&gt;
  &lt;/li&gt;
  &lt;li&gt;In the markup I include a coherent set of classes that follows &lt;a href=&quot;https://github.com/suitcss/suit/blob/master/doc/README.md&quot;&gt;SUIT CSS conventions&lt;/a&gt;, and I document these classes in the Readme. I’m a true believer in BEM-style CSS, so &lt;em&gt;I think this may be the most important part of the CSS strategy&lt;/em&gt;.&lt;/li&gt;
  &lt;li&gt;Those class names are configurable (within the conventions of SUIT) through options. Users can change the component name (e.g. &lt;code class=&quot;highlighter-rouge&quot;&gt;.AriaMenuButton-menuWrapper&lt;/code&gt; becomes &lt;code class=&quot;highlighter-rouge&quot;&gt;.MyComponentName-menuWrapper&lt;/code&gt;) or add a namespace (e.g. &lt;code class=&quot;highlighter-rouge&quot;&gt;.AriaMenuButton-menuWrapper&lt;/code&gt; becomes &lt;code class=&quot;highlighter-rouge&quot;&gt;.myNamespace-AriaMenuButton-menuWrapper&lt;/code&gt;).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Notice that my solutions to the CSS conundrum depend upon ways that I “imagine” or “hope” users will put the component to work. So it goes. As far as I can tell there is no way to fully encapsulate a UI component that requires CSS: &lt;em&gt;the user will almost always have to be aware of implementation details in styles, and will almost always need to tweak those styles to some degree&lt;/em&gt;. That said, the approach that I’ve adopted might work for a lot of people, especially those people who have bought into some BEM-style methodology like SUIT.&lt;/p&gt;

&lt;h2 id=&quot;js-distribution-problems-receiving-dependencies-exposing-the-api-compiling&quot;&gt;JS Distribution Problems: Receiving Dependencies, Exposing the API, Compiling&lt;/h2&gt;

&lt;p&gt;The jQuery plugin has been the dominant form of open source UI components for some time. Probably one key to the success of that ecosystem has been its standardized format for receiving dependencies and exposing APIs: expect a global &lt;code class=&quot;highlighter-rouge&quot;&gt;$&lt;/code&gt; and connect your plugin to that in a specified way.&lt;/p&gt;

&lt;p&gt;That method worked for jQuery … but it’s outdated for many of us. More modern JS workflows avoid global variables, preferring modules — which in turn require a compilation process. And React, specifically, also requires compilation if the plugin code uses JSX.&lt;/p&gt;

&lt;p&gt;But an open source author cannot know whether the user will have global variables or modules, will configure a suitable compilation process or not. Therefore, I think, &lt;strong&gt;a React open source component needs to work with both globals and modules, and needs to be distributed in a pre-compiled form&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;I decided it would be justifiable to assume two user types: &lt;em&gt;global-users&lt;/em&gt; and &lt;em&gt;module-users&lt;/em&gt;. Global-users would have any of the library’s dependencies (just React, in this case) exposed as global variables and would expect the library to expose its own global variable. Module-users would import the library as needed and expect dependencies to resolve themselves.&lt;/p&gt;

&lt;p&gt;Because I want to author the code with modules and JSX (and ES6), obviously I need to compile to a &lt;code class=&quot;highlighter-rouge&quot;&gt;dist/&lt;/code&gt; directory. But I need to compile two formats: the global-user format and module-user format. Following a pattern I saw in &lt;a href=&quot;https://github.com/gaearon/react-dnd&quot;&gt;react-dnd&lt;/a&gt;, I setup the following build process:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Use Webpack to compile to a &lt;code class=&quot;highlighter-rouge&quot;&gt;dist/&lt;/code&gt; directory a bundle that expects dependencies as globals and exports the library as a global. Global-users should use this.&lt;/li&gt;
  &lt;li&gt;Use Babel to transpile JSX (and ES6) from &lt;code class=&quot;highlighter-rouge&quot;&gt;src/&lt;/code&gt; to a &lt;code class=&quot;highlighter-rouge&quot;&gt;dist-modules/&lt;/code&gt; directory, and &lt;em&gt;have &lt;code class=&quot;highlighter-rouge&quot;&gt;index.js&lt;/code&gt; reference files in that pre-compiled directory&lt;/em&gt;. Module-users will then import these pre-compiled files when they import the npm module; so the code should work regardless of the module-user’s build process (i.e. the user doesn’t have to deal with JSX or ES6).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;(There’s one aspect of this “distribution problem” that I do not think I have suitably resolved: how to &lt;em&gt;optionally&lt;/em&gt; utilize React’s &lt;code class=&quot;highlighter-rouge&quot;&gt;CSSTransitionGroup&lt;/code&gt;. The &lt;code class=&quot;highlighter-rouge&quot;&gt;CSSTransitionGroup&lt;/code&gt; component is only included in &lt;code class=&quot;highlighter-rouge&quot;&gt;React.addons&lt;/code&gt;, a larger   dependency than the base library — so I cannot assume that it will be available, and I do not want to surreptitiously import a larger dependency than the user expects. The solution I settled on is unusual, I think: provide a &lt;code class=&quot;highlighter-rouge&quot;&gt;transition&lt;/code&gt; option that expects users to &lt;em&gt;manually pass the &lt;code class=&quot;highlighter-rouge&quot;&gt;CSSTransitionGroup&lt;/code&gt; component&lt;/em&gt; if they want to use transitions (see &lt;a href=&quot;https://github.com/davidtheclark/react-aria-menubutton#options&quot;&gt;the docs&lt;/a&gt;). It’s not a familiar pattern; but it does solve the problem. I wonder if other people know of any alternate, better means to the same end?)&lt;/p&gt;

&lt;p&gt;(UPDATE: Looks like a future release of React will allow addons to be included individually, without pulling in the whole pack — so that might take care of this last confusing problem.)&lt;/p&gt;

&lt;h2 id=&quot;testing&quot;&gt;Testing&lt;/h2&gt;

&lt;p&gt;UI modules have always been inadequately tested — partly, I think, because with the DOM and CSS involved (as essential parts of the module’s “API”, really), the surface to test is large and confusing; also partly because UI developers aren’t always accustomed to the value of testing.&lt;/p&gt;

&lt;p&gt;React makes this easier, with its functional format (props go in, DOM comes out) and  handy &lt;a href=&quot;https://facebook.github.io/react/docs/test-utils.html&quot;&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;TestUtils&lt;/code&gt;&lt;/a&gt;. &lt;strong&gt;Testing with React is straightforward enough that it should be expected of any open source React component.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Testing is especially important for &lt;a href=&quot;https://github.com/davidtheclark/react-aria-menubutton&quot;&gt;react-aria-menubutton&lt;/a&gt; because it aspires to achieve accessibility by following an ARIA spec to the letter. The tests need to match the spec. To ensure that this happens, I’ve scattered quotations from the spec throughout the tests, illustrating which test-cases address which specific requirements.&lt;/p&gt;

&lt;h2 id=&quot;summary&quot;&gt;Summary&lt;/h2&gt;

&lt;p&gt;Here’s a little summary of the measures I took in my attempt to make &lt;a href=&quot;https://github.com/davidtheclark/react-aria-menubutton&quot;&gt;react-aria-menubutton&lt;/a&gt; a decent open source React component:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Follow a WAI-ARIA Design Pattern to ensure accessibility.&lt;/li&gt;
  &lt;li&gt;Provide users the means to write their own styles instead of pre-written styles they will have to override (via configurable classes, a base stylesheet, and fleshed-out examples).&lt;/li&gt;
  &lt;li&gt;Use strict SUIT CSS class naming conventions to make styling as smooth as possible — to ensure that the component follows best-practices, regardless of what the user’s CSS looks like.&lt;/li&gt;
  &lt;li&gt;Compile one version of the component, for global-users, to accept global dependencies and expose the component as a global variable (in &lt;code class=&quot;highlighter-rouge&quot;&gt;dist/&lt;/code&gt;).&lt;/li&gt;
  &lt;li&gt;Compile another version of the component for module-users, which should work regardless of the module-user’s build configuration (in &lt;code class=&quot;highlighter-rouge&quot;&gt;dist-modules/&lt;/code&gt;).&lt;/li&gt;
  &lt;li&gt;Provide a means for the user to manually pass in React’s &lt;code class=&quot;highlighter-rouge&quot;&gt;CSSTransitionGroup&lt;/code&gt; if she wants transitions (this is the one I’m uncertain about …).&lt;/li&gt;
  &lt;li&gt;Write lots and lots of tests.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I think it’s still an open question whether React will sprout an ecosystem of high quality open source components that add real value to applications (especially accessibility) while remaining flexible enough for reuse. The quandries mentioned above, though, are not specific to React: almost all of them will apply to any open source frontend component that has to deal the overlapping of HTML, CSS, and JS and diverse frontend build processes and workflows. A bigger question, maybe — one applicable to non-React users — is this: Can we raise the bar on open source UI components, figure out how to modularize them more effectively than is typically done with good old jQuery plugins? &lt;a href=&quot;https://github.com/davidtheclark/react-aria-menubutton&quot;&gt;react-aria-menubutton&lt;/a&gt; is my (first) attempt to experiment with that possibility and confront its challenges. I’m eager to see what other people come up with.&lt;/p&gt;
</description>
        <pubDate>Sat, 02 May 2015 00:00:00 -0700</pubDate>
        <link>https://davidtheclark.com/building-react-aria-menubutton/</link>
        <guid isPermaLink="true">https://davidtheclark.com/building-react-aria-menubutton/</guid>
        
        
      </item>
    
      <item>
        <title>I'm Excited About&amp;nbsp;PostCSS</title>
        <description>&lt;p&gt;I intend this to be a brief, spontaneous entry about one of my many tooling uncertainties. I hope it will provoke some others to weigh in on a choice that I consider &lt;em&gt;important&lt;/em&gt; (if you write stylesheet code) but &lt;em&gt;difficult&lt;/em&gt; (unless by &lt;a href=&quot;https://www.wordnik.com/words/haruspicy&quot;&gt;haruspicy&lt;/a&gt; or other means you know the future) and definitely &lt;em&gt;underdiscussed&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;To be mentioned:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/postcss/postcss&quot;&gt;PostCSS&lt;/a&gt; — but everywhere I say “PostCSS” you could also think about &lt;a href=&quot;https://github.com/reworkcss/rework&quot;&gt;Rework&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://sass-lang.com/&quot;&gt;Sass&lt;/a&gt; — but everywhere I say “Sass” you could substitute &lt;a href=&quot;http://lesscss.org/&quot;&gt;Less&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Please follow up with your own tweets or blog posts about “Why we should all switch to PostCSS” or “Why it would be foolhardy not to rely on the Venerable Sass”, etc. (I’m serious. Tell me what to do.)&lt;/p&gt;

&lt;h3 id=&quot;standard-syntax-and-the-future-css&quot;&gt;Standard Syntax and The Future CSS&lt;/h3&gt;

&lt;p&gt;There are (at least) two essential features that everybody wants (or should want) to add to their stylesheets: variables and calculations. So every CSS processor must provide (at least) those. Sass has its own &lt;code class=&quot;highlighter-rouge&quot;&gt;$variables&lt;/code&gt; and operations; Less has its own &lt;code class=&quot;highlighter-rouge&quot;&gt;@variables&lt;/code&gt; and operations; but a set of PostCSS plugins offer the synax of &lt;a href=&quot;https://www.w3.org/TR/css-variables/&quot;&gt;custom-properties&lt;/a&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;calc()&lt;/code&gt; — &lt;em&gt;the syntax of present and future CSS standards&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;The deeper, wiser parts of me don’t care whether I type &lt;code class=&quot;highlighter-rouge&quot;&gt;$llama&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;@llama&lt;/code&gt;, or &lt;code class=&quot;highlighter-rouge&quot;&gt;var(--llama)&lt;/code&gt; as I work. But one thing they &lt;em&gt;do&lt;/em&gt; care about (besides the acquisition of desserts) is &lt;em&gt;the interoperability of my syntax&lt;/em&gt;, and the only way to achieve &lt;em&gt;that&lt;/em&gt; is to establish some kind of standard … which, it turns out, is what standards bodies are for. Because PostCSS plugins enable me to use standard CSS syntax, it’s not out of the question that &lt;em&gt;the code I write using PostCSS might make sense to other CSS processors&lt;/em&gt; — not to mention &lt;em&gt;real browsers&lt;/em&gt;. Even now, I could switch between &lt;a href=&quot;https://github.com/postcss/postcss-custom-properties&quot;&gt;postcss-custom-properties&lt;/a&gt; and &lt;a href=&quot;https://github.com/reworkcss/rework-vars&quot;&gt;rework-vars&lt;/a&gt;, &lt;a href=&quot;https://github.com/postcss/postcss-calc&quot;&gt;postcss-calc&lt;/a&gt; and &lt;a href=&quot;https://github.com/reworkcss/rework-calc&quot;&gt;rework-calc&lt;/a&gt; without having to modify my stylesheets. The same should go for all the other PostCSS and Rework plugins that mimic standard syntax. &lt;em&gt;That&lt;/em&gt; is fantastic —— that alone is almost enough to convert me.&lt;/p&gt;

&lt;p&gt;Sass is definitely “the most mature, stable, and powerful professional grade CSS extension language in the world” (as its website boasts). Like jQuery, Sass is one of the few frontend tools that we can confidently predict will remain popular for years to come. But does that make it &lt;em&gt;more&lt;/em&gt; secure and reliable than using standard CSS syntax and PostCSS? Even if PostCSS never takes off or fizzles out (which is unlikely, because of Autoprefixer’s popularity), some other tool could step in and carry the torch, simply by allowing us to use the standardized syntax that browsers will eventually understand.&lt;/p&gt;

&lt;p&gt;(See &lt;a href=&quot;https://github.com/cssnext/cssnext&quot;&gt;cssnext&lt;/a&gt; if you’re interested in PostCSS plugins that “allow you to use tomorrow’s CSS syntax today.” It is packaged as a standalone tool, but all of its pieces are modular.)&lt;/p&gt;

&lt;h3 id=&quot;mature-and-monolithic-or-fresh-and-modular&quot;&gt;Mature and Monolithic or Fresh and Modular?&lt;/h3&gt;

&lt;p&gt;While Sass delivers a monolithic extension language with a fixed feature-set, PostCSS is a framework with which to build focused plugins. A PostCSS user can &lt;em&gt;selectively&lt;/em&gt; include plugins for features that fit her needs, preferences, and whims, or write her own if nothing exists.&lt;/p&gt;

&lt;p&gt;Why does this appeal to me?&lt;/p&gt;

&lt;p&gt;Sass provides more than I want. Not just more than I want &lt;em&gt;to use&lt;/em&gt;; but more than I want myself or the other people working on the same stylesheets &lt;em&gt;to be able to use&lt;/em&gt;. I think it offers too much — more than enough to become cleverly counterproductive. (Which specific parts of Sass I’d leave out is a topic for another post.) But I cannot pick and choose pieces of Sass; so if I include Sass at all, I must welcome into my work-home an API that is larger and more complex than I need or want. Through internal conventions and code review, I might be able to enforce a whitelist of Sass features; but I would prefer to be more selective upfront, by &lt;em&gt;limiting what’s possible&lt;/em&gt; to what (I think) would be most productive.&lt;/p&gt;

&lt;p&gt;&lt;small&gt;(Is there a life lesson in the paragraph above? I’m not sure …)&lt;/small&gt;&lt;/p&gt;

&lt;p&gt;Also, with compact modules &lt;em&gt;the barrier to creation is low&lt;/em&gt;. I am confident that I could contribute to whatever existing plugins I choose to use, and also confident that I could write my own plugins as needed. (Nicolas Gallagher published &lt;a href=&quot;http://nicolasgallagher.com/custom-css-preprocessing/&quot;&gt;a good post about doing that with Rework&lt;/a&gt;.)&lt;/p&gt;

&lt;p&gt;It’s tempting to relate this discussion to ongoing debates about modularity in the JavaScript community, which pit the ideals of small, focused, UNIX-y packages against feature-rich behemoths like jQuery and Ember. In many respects, I think, the analogy makes sense. But if you read through the code of some PostCSS modules (probably short, relatively straightforward code), you’ll see that the &lt;em&gt;simplicity&lt;/em&gt; of CSS — both the language itself and the things that we do with the language — seriously alters the premises of comparison, &lt;em&gt;in favor of modularity&lt;/em&gt;. Because some of the strongest arguments in favor of large projects like jQuery and Ember hinge on the premise that those projects do things you could never do on your own, solve problems you could never solve on your own, tackle all kinds of complexity that would overwhelm you on your own, etc.; but &lt;em&gt;your CSS problems aren’t really that complex, and with PostCSS you probably have more than enough power to solve them&lt;/em&gt;.&lt;/p&gt;

&lt;h3 id=&quot;a-word-about-using-both&quot;&gt;A Word About Using Both&lt;/h3&gt;

&lt;p&gt;You &lt;em&gt;could&lt;/em&gt; use both: write SCSS files, compiled them to CSS, post-process those with PostCSS — go hog wild. That’s one way to aggregate power in this world. And if your primary reason for adding PostCSS to your Sass-based process is to have &lt;em&gt;more power&lt;/em&gt; at your disposal, that would work. Harness them both. (Use &lt;code class=&quot;highlighter-rouge&quot;&gt;@for&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;@if&lt;/code&gt; and Sass maps and then write crazy PostCSS plugins and make your fantasies reality.)&lt;/p&gt;

&lt;p&gt;However: This would not &lt;em&gt;simplify&lt;/em&gt; your dev stack — which is one of &lt;em&gt;my&lt;/em&gt; goals in considering the switch. This would not &lt;em&gt;selectively limit&lt;/em&gt; your powers — another of my goals. And you would still have to choose which process gets to take care of the key features — variables, calculations, imports.&lt;/p&gt;

&lt;h3 id=&quot;conclusion&quot;&gt;Conclusion?&lt;/h3&gt;

&lt;p&gt;How do I conclude this?&lt;/p&gt;

&lt;p&gt;The more that I write, the more I feel like I should switch from Sass to PostCSS. I seem to be convincing myself — am I convincing anybody else?&lt;/p&gt;

&lt;p&gt;But I can’t shake the concern that dropping a tool as proven and popular as Sass — one of the few frontend tools we can truly, indefinitely rely on — is like setting myself adrift, and maybe unnecessarily.&lt;/p&gt;

&lt;p&gt;… I should probably write a poem about it.&lt;/p&gt;

&lt;p&gt;What do you think?&lt;/p&gt;
</description>
        <pubDate>Tue, 13 Jan 2015 00:00:00 -0700</pubDate>
        <link>https://davidtheclark.com/excited-about-postcss/</link>
        <guid isPermaLink="true">https://davidtheclark.com/excited-about-postcss/</guid>
        
        
      </item>
    
      <item>
        <title>An Auto-Enforceable SCSS Styleguide</title>
        <description>&lt;h2 id=&quot;automated-enforcement-matters-and-scss-lint-is-excellent&quot;&gt;Automated Enforcement Matters, and SCSS-Lint is Excellent&lt;/h2&gt;

&lt;p&gt;&lt;small&gt;(If you don’t really want to read, just look at &lt;a href=&quot;https://github.com/causes/scss-lint&quot;&gt;SCSS-Lint&lt;/a&gt; and seriously consider using it.)&lt;/small&gt;&lt;/p&gt;

&lt;p&gt;There seems to be universal agreement that consistent, predictable code style is important, in any language (at least if more than one fallible human is working on the project for more than one week); and that thorough code styleguides, which enable that consistency and predictability, are well worth writing, using, and sharing.&lt;/p&gt;

&lt;p&gt;In the world of stylesheet-code (CSS and its relatives), plenty of individuals and organizations have published their approaches, with suggested guidelines spanning the gamut of from minute finetuning (whether there should be a leading zero in &lt;code class=&quot;highlighter-rouge&quot;&gt;0.5&lt;/code&gt;) to big-picture architecture (how modules should be named and organized). I’ve been trying to keep track of them in a &lt;a href=&quot;https://github.com/davidtheclark/scalable-css-reading-list&quot;&gt;Scalable CSS Reading List&lt;/a&gt;: take a look.&lt;/p&gt;

&lt;p&gt;So I don’t think I have to advocate for &lt;em&gt;the sharing of guidelines&lt;/em&gt;, generally. But I do think there could be more advocacy for &lt;em&gt;the sharing of &lt;strong&gt;automatable&lt;/strong&gt; guidelines&lt;/em&gt;. In that area, the CSS-cluster is far behind other languages.&lt;/p&gt;

&lt;p&gt;There’s a vital distinction between discussions of best practices and the actual codification of clear, specific, concrete conventions and standards that will constitute, for some group of developers, enforced expectations. All the discussions are valuable, of course; but it’s easy to forget that they must be realized in day-to-day practice, in keyboard-tapping and mouse-clicking and glazed-eyeball-flickering. The “good ideas” should aspire to become “the way things are done”. That’s why I think that &lt;strong&gt;among the most important properties of any guideline, whatever its scale or focus, is &lt;em&gt;if and how it will be enforced&lt;/em&gt;.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Sometimes, for some guidelines, manual code review with human senses and human brains is the only viable means of enforcement. Machine minds probably can’t determine meaningful rule categories, when to use utility classes, whether &lt;code class=&quot;highlighter-rouge&quot;&gt;@mixin&lt;/code&gt;s and &lt;code class=&quot;highlighter-rouge&quot;&gt;@extend&lt;/code&gt;s are helpful or harmful in a given situation — and so on.&lt;/p&gt;

&lt;p&gt;But sometimes automation &lt;em&gt;will&lt;/em&gt; work — maybe all on its own, or maybe as a first step that eases the burden on human brains, pointing out suspicious characters that deserve a second look. &lt;small&gt;(… thank god I’m just talking about code and not &lt;em&gt;law&lt;/em&gt; enforcement … machines are so much easier … I swear I’m not authoritarian …)&lt;/small&gt; And those times are the best times. Just &lt;em&gt;trying&lt;/em&gt; to make a guideline auto-enforceable will probably improve it, since you’ll have to shake off all vagueries, honing what might be loose recommendations into well-defined and delineated, observable patterns.&lt;/p&gt;

&lt;p&gt;Here’s a sentence for you: Any styleguide will become more valuable as more of its enforcement becomes more automated.&lt;/p&gt;

&lt;p&gt;All that’s to say that the world of stylesheet-code would benefit from more and better tools for code style enforcement. &lt;strong&gt;Currently, &lt;a href=&quot;https://github.com/causes/scss-lint&quot;&gt;SCSS-Lint&lt;/a&gt; stands alone, in my opinion. Thankfully, SCSS-Lint is &lt;em&gt;excellent&lt;/em&gt;.&lt;/strong&gt; Full-featured, configurable, exceptionally well-maintained. One of the major reasons I stick with SCSS is &lt;em&gt;because of SCSS-Lint&lt;/em&gt;. There should be more tools like it. (I’m not fully warmed up yet to CSSComb — maybe I need to give it some more attention — and that seems to be just about the only other option right now (?).)&lt;/p&gt;

&lt;p&gt;If you write SCSS, give SCSS-Lint some serious thought. Furrow your brow. Work it into your build system. Plug it in to your text editor. Talk with your family about it over the holidays. If you do not write SCSS, look into SCSS-Lint anyway, and if you have some extra energy maybe do one of the following: (1) convince me that CSSComb is as good as SCSS-Lint (or make it as good); or (2) make or contribute to a LESS or PostCSS/Rework linter that might one day match it (like maybe &lt;a href=&quot;https://github.com/stylelint/stylelint&quot;&gt;this one&lt;/a&gt;?).&lt;/p&gt;

&lt;p&gt;Enough jabber. I share this little guide below for a few reasons:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;As an introduction to the powers of SCSS-Lint, for those who don’t yet use it.&lt;/li&gt;
  &lt;li&gt;As an example of the &lt;em&gt;auto-enforceable&lt;/em&gt; section that should be part of &lt;em&gt;every&lt;/em&gt; real-world stylesheet-code styleguide. (Note that this does not aspire to be a &lt;em&gt;complete&lt;/em&gt; styleguide, like Harry Robert’s &lt;a href=&quot;https://cssguidelin.es/&quot;&gt;cssguidlin.es&lt;/a&gt;: it’s just the auto-enforceable part. Note that.)&lt;/li&gt;
  &lt;li&gt;Just to share.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;my-scss-lint-powered-scss-styleguide&quot;&gt;My SCSS-Lint-Powered SCSS Styleguide&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Please keep in mind that this is just &lt;em&gt;my&lt;/em&gt; styleguide, not The Styleguide, and SCSS-Lint is completely configurable to fit your own weird, sometimes misguided preferences.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If any terminology below is unfamiliar, search it out in &lt;a href=&quot;https://www.w3.org/TR/CSS2/&quot;&gt;the CSS 2 spec&lt;/a&gt; or the &lt;a href=&quot;https://sass-lang.com/documentation/file.SASS_REFERENCE.html&quot;&gt;Sass docs&lt;/a&gt;. (I’m &lt;em&gt;trying&lt;/em&gt; to use these words correctly: please send a tweet if I’m mistaken. Educate me.)&lt;/p&gt;

&lt;p&gt;After each guideline I’ve included the name(s) of the SCSS-Lint linter(s) that will enforce it. And I’ve posted &lt;a href=&quot;https://gist.github.com/davidtheclark/d764f70ffaca09892293&quot;&gt;the configuration file that would make SCSS-Lint enforce all of this&lt;/a&gt;, so you can inspect or copy-paste it.&lt;/p&gt;

&lt;p&gt;Last point: I am not going to spend a lot of time below &lt;em&gt;justifying&lt;/em&gt; each guideline. A few are really just personal preference, but for most I do have some rational justification. But there are enough words on this page already. (Also, the SCSS-Lint documentation offers a little justification for each linter, which I usually, though not always, agree with.) And when I do justify, I may be facetiously dogmatic. If you think I should take the time to further explain something, send a tweet.&lt;/p&gt;

&lt;h3 id=&quot;whitespace-and-punctuation&quot;&gt;Whitespace and Punctuation&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;» Indent using 2 spaces.&lt;/strong&gt; Or tabs. Whatever. Fight about it. [&lt;em&gt;Indentation&lt;/em&gt;]&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;» End each file with an empty newline.&lt;/strong&gt; After all, you’re not an animal, are you? [&lt;em&gt;FinalNewline&lt;/em&gt;]&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;» Include an empty line between all statements&lt;/strong&gt; — i.e. rule sets, at-rules, and Sass directives. Even the nested ones. Spaces help people, and people are important. (Variable definitions are like properties and don’t need to be spaced.) [&lt;em&gt;EmptyLineBetweenBlocks&lt;/em&gt;]&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;» Include spaces after commas and colons, not before.&lt;/strong&gt; As in written English. [&lt;em&gt;SpaceAfterComma&lt;/em&gt;, &lt;em&gt;SpaceAfterPropertyColon&lt;/em&gt;, &lt;em&gt;SpaceAfterPropertyName&lt;/em&gt;]&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;» Do not include spaces between parentheses and parenthesized.&lt;/strong&gt; As in written English. [&lt;em&gt;SpaceBetweenParens&lt;/em&gt;]&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;» Use single quotation marks.&lt;/strong&gt; That way you don’t have to push &lt;code class=&quot;highlighter-rouge&quot;&gt;shift&lt;/code&gt;, everyone’s least favorite key. [&lt;em&gt;StringQuotes&lt;/em&gt;]&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;» End every declaration with a semicolon, with no whitespace in front of it.&lt;/strong&gt; (Did you even &lt;em&gt;think&lt;/em&gt; about putting a space in front of it? What is wrong with you?) [&lt;em&gt;TrailingSemicolon&lt;/em&gt;]&lt;/p&gt;

&lt;h3 id=&quot;selectors-and-names&quot;&gt;Selectors and Names&lt;/h3&gt;

&lt;p&gt;All of the guidelines in this section reflect my preference for class selectors and class selectors &lt;em&gt;only&lt;/em&gt; (except in highly unusual and probably unhappy circumstances), and keeping the hierarchy as flat as possible.&lt;/p&gt;

&lt;p&gt;Excuse me: I say “my preference” but I mean “God’s law”.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;» Never use ID selectors.&lt;/strong&gt; Find another way. (No, this is not “throwing out the baby with  the bath water”. This is “not pooping in the bath”.) [&lt;em&gt;IdSelector&lt;/em&gt;]&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;» Do not qualify type selectors.&lt;/strong&gt; Better yet, don’t use them at all, if possible: just use classes. [&lt;em&gt;QualifyingElement&lt;/em&gt;]&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-scss&quot; data-lang=&quot;scss&quot;&gt;&lt;span class=&quot;c1&quot;&gt;// bad&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;href&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'horse'&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;...&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// maybe fine&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;href&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'horse'&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;...&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// good&lt;/span&gt;
&lt;span class=&quot;nc&quot;&gt;.horse&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;...&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;&lt;strong&gt;» Provide lowercase, hyphen-delimited names for Sass variables and directives.&lt;/strong&gt; That way they fit in with standard CSS keywords and property names. [&lt;em&gt;NameFormat&lt;/em&gt;]&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;» Do not chain more than three simple selectors.&lt;/strong&gt; In almost every case, &lt;em&gt;one&lt;/em&gt; simple selector is best (that is, &lt;em&gt;no&lt;/em&gt; chaining, flat specificity, world peace). Some situations call for two. But let’s just draw the line at three. Find another way. [&lt;em&gt;SelectorDepth&lt;/em&gt;]&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-scss&quot; data-lang=&quot;scss&quot;&gt;&lt;span class=&quot;c1&quot;&gt;// good&lt;/span&gt;
&lt;span class=&quot;nc&quot;&gt;.horse&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;...&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// often useful&lt;/span&gt;
&lt;span class=&quot;nc&quot;&gt;.horse&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;.horse&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;...&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// suspicious&lt;/span&gt;
&lt;span class=&quot;nc&quot;&gt;.horse&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;.donkey&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;...&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// uh ...&lt;/span&gt;
&lt;span class=&quot;nc&quot;&gt;.horse&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;.donkey&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;.mule&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;...&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// call the police&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;ul&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;li&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;span&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;...&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;&lt;strong&gt;» Include a line break between each selector in a group.&lt;/strong&gt; [&lt;em&gt;SingleLinePerSelector&lt;/em&gt;]&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-scss&quot; data-lang=&quot;scss&quot;&gt;&lt;span class=&quot;c1&quot;&gt;// bad&lt;/span&gt;
&lt;span class=&quot;nc&quot;&gt;.horse&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;.donkey&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;.mule&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;...&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// good&lt;/span&gt;
&lt;span class=&quot;nc&quot;&gt;.horse&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;nc&quot;&gt;.donkey&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;nc&quot;&gt;.mule&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;...&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;&lt;strong&gt;» Include a single space between selectors and the curly braces that begin declaration blocks.&lt;/strong&gt; [&lt;em&gt;SpaceBeforeBrace&lt;/em&gt;]&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;» Use a consistent selector naming convention.&lt;/strong&gt; This is probably the least globally portable guideline in my guide, because it’s hard to achieve consensus on naming conventions. But maybe you’ll like mine? I try to be strict (not because I’m a fascist but because I’ve seen strange horrors in stylesheets).&lt;/p&gt;

&lt;p&gt;Here’s what I do: Class names should match the following regular expression:&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;/^(?:u|is|has)\-[a-z][a-zA-Z0-9]*$|^(?!u|is|has)[a-zA-Z][a-zA-Z0-9]*(?:\-[a-z][a-zA-Z0-9]*)?(?:\-\-[a-z][a-zA-Z0-9]*)?$/&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This regular expression enforces &lt;a href=&quot;https://github.com/suitcss/suit/blob/master/doc/README.md&quot;&gt;the SUIT CSS naming convention&lt;/a&gt; (but without requiring the initial capital letter on a component). The possibilities can be summed up like this:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-scss&quot; data-lang=&quot;scss&quot;&gt;&lt;span class=&quot;nc&quot;&gt;.u-utilityClass&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{}&lt;/span&gt;
&lt;span class=&quot;nc&quot;&gt;.is-stateClass&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{}&lt;/span&gt;
&lt;span class=&quot;nc&quot;&gt;.has-stateClass&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{}&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// And maybe I'll add other verbs for state classes, as needed&lt;/span&gt;
&lt;span class=&quot;nc&quot;&gt;.ComponentName&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{}&lt;/span&gt;
&lt;span class=&quot;nc&quot;&gt;.ComponentName-descendentName&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{}&lt;/span&gt;
&lt;span class=&quot;nc&quot;&gt;.ComponentName--modifierName&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{}&lt;/span&gt;
&lt;span class=&quot;nc&quot;&gt;.ComponentName-descendentName--modifierName&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{}&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// All of the above could also use `componentName`, without the initial capital&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;(All non-class-name selectors should be straightforward hyphenated lowercase — but I’d try not to use them anyway.) [&lt;em&gt;SelectorFormat&lt;/em&gt;]&lt;/p&gt;

&lt;h3 id=&quot;rule-set-innards&quot;&gt;Rule Set Innards&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;» Include a line break between each declaration.&lt;/strong&gt; Keep declarations isolated: that way you can monitor and discipline them more easily. [&lt;em&gt;SingleLinePerProperty&lt;/em&gt;]&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;» Include quotation marks around URLs.&lt;/strong&gt; SCSS-Lint documentation &lt;a href=&quot;https://github.com/causes/scss-lint/blob/master/lib/scss_lint/linter/README.md#urlquotes&quot;&gt;explains why&lt;/a&gt;. [&lt;em&gt;UrlQuotes&lt;/em&gt;]&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;» Use shorthand properties when you can.&lt;/strong&gt; As &lt;a href=&quot;http://www.bbc.com/news/magazine-29464446&quot;&gt;is the case with husbands&lt;/a&gt;: shorter is better. [&lt;em&gt;Shorthand&lt;/em&gt;]&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;» Do not include units on zero values.&lt;/strong&gt; It could be zero of anything: we don’t have to know. [&lt;em&gt;ZeroUnit&lt;/em&gt;]&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;» Use &lt;code class=&quot;highlighter-rouge&quot;&gt;border: 0&lt;/code&gt; instead of &lt;code class=&quot;highlighter-rouge&quot;&gt;border: none&lt;/code&gt;.&lt;/strong&gt; Cuz. [&lt;em&gt;BorderZero&lt;/em&gt;]&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;» Do not nest selectors more than 3 levels deep.&lt;/strong&gt; Zero levels of nesting is best, of course. One level is often handy, and arguably helps organize some common code (especially pseudo-classes and pseudo-elements). Two or three raises eyebrows and specificity. More invites (induces?) disaster. [&lt;em&gt;NestingDepth&lt;/em&gt;]&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-scss&quot; data-lang=&quot;scss&quot;&gt;&lt;span class=&quot;c1&quot;&gt;// best&lt;/span&gt;
&lt;span class=&quot;nc&quot;&gt;.horse&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;nl&quot;&gt;font-size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;10em&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;// handy&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;nd&quot;&gt;:hover&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nl&quot;&gt;font-size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;20em&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;// eyebrow-raising&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;.donkey&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nl&quot;&gt;color&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;#eee&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

      &lt;span class=&quot;c1&quot;&gt;// disaster-inviting(-inducing?)&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;.mule&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;nl&quot;&gt;margin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;100em&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

        &lt;span class=&quot;c1&quot;&gt;// disaster-in-progress&lt;/span&gt;
        &lt;span class=&quot;nt&quot;&gt;ul&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;li&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;span&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
          &lt;span class=&quot;nl&quot;&gt;color&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$death&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
      &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;&lt;strong&gt;» Do not type vendor prefixes.&lt;/strong&gt; You have too much life to live. Use &lt;a href=&quot;https://github.com/postcss/autoprefixer&quot;&gt;Autoprefixer&lt;/a&gt; — or, at the very least, Bourbon or Compass mixins. [&lt;em&gt;VendorPrefixes&lt;/em&gt;]&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;» Order declarations in a logical manner&lt;/strong&gt;. Here’s one logical manner that SCSS-Lint can enforce: (A) &lt;code class=&quot;highlighter-rouge&quot;&gt;@extend&lt;/code&gt; directives first; then (B) &lt;code class=&quot;highlighter-rouge&quot;&gt;@include&lt;/code&gt; directives &lt;em&gt;without&lt;/em&gt; inner &lt;code class=&quot;highlighter-rouge&quot;&gt;@content&lt;/code&gt;; then (C) vanilla CSS declarations, with their properties and values; then (D) &lt;code class=&quot;highlighter-rouge&quot;&gt;@include&lt;/code&gt; directives &lt;em&gt;with&lt;/em&gt; inner &lt;code class=&quot;highlighter-rouge&quot;&gt;@content&lt;/code&gt;; then (E) nested statements (rule sets and at-rules). [&lt;em&gt;DeclarationOrder&lt;/em&gt;]&lt;/p&gt;

&lt;p&gt;&lt;small&gt;Why? (This is going to get weird.) A and B belong at the top so that C and E can override them as needed — whereas A and B should never &lt;em&gt;need&lt;/em&gt; to override C or E, I think, or something has gone sour and refactoring is preferable. E is at the end so that it can override anything. And D is where it is because God only knows what it’s going to do. (I’m not sure this logic is 100% foolproof, but I’m giving it a try. We can’t simply be spectators to chance, can we? We must make choices and live with the consequences. There’s a movie about that, right?)&lt;/small&gt;&lt;/p&gt;

&lt;h3 id=&quot;colors&quot;&gt;Colors&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;» Do not use color keywords&lt;/strong&gt; — such as &lt;code class=&quot;highlighter-rouge&quot;&gt;green&lt;/code&gt; instead of &lt;code class=&quot;highlighter-rouge&quot;&gt;#008000&lt;/code&gt;. Color keywords are for children, aging parents, and pranks, not the robot-communication we all aspire to. [&lt;em&gt;ColorKeyword&lt;/em&gt;]&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;» Use the shortest possible hex value.&lt;/strong&gt; &lt;code class=&quot;highlighter-rouge&quot;&gt;#fff&lt;/code&gt; instead of &lt;code class=&quot;highlighter-rouge&quot;&gt;#ffffff&lt;/code&gt;. (See the statement about husbands, above.) [&lt;em&gt;HexLength&lt;/em&gt;]&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;» Use lowercase letters inside hex values.&lt;/strong&gt; &lt;code class=&quot;highlighter-rouge&quot;&gt;#fff&lt;/code&gt; instead of &lt;code class=&quot;highlighter-rouge&quot;&gt;#FFF&lt;/code&gt;. Unnecessary capital letters are obnoxious to the sensitive soul. And, again, why use &lt;code class=&quot;highlighter-rouge&quot;&gt;shift&lt;/code&gt; when you don’t have to? Last reason: lowercase letters are, I think, a little bit easier to distinguish from numbers. I’m not making that up. [&lt;em&gt;HexNotation&lt;/em&gt;]&lt;/p&gt;

&lt;h3 id=&quot;numbers&quot;&gt;Numbers&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;» Do &lt;em&gt;not&lt;/em&gt; include unnecessary trailing zeros after a decimal point.&lt;/strong&gt; &lt;code class=&quot;highlighter-rouge&quot;&gt;2em&lt;/code&gt; instead of &lt;code class=&quot;highlighter-rouge&quot;&gt;2.0em&lt;/code&gt;. [&lt;em&gt;TrailingZero&lt;/em&gt;, &lt;em&gt;UnnecessaryMantissa&lt;/em&gt;]&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;» &lt;em&gt;Do&lt;/em&gt; include an unnecessary leading zero before decimal values less than 1.&lt;/strong&gt; &lt;code class=&quot;highlighter-rouge&quot;&gt;0.5em&lt;/code&gt; instead of &lt;code class=&quot;highlighter-rouge&quot;&gt;.5em&lt;/code&gt;. Don’t you think it improves readability? I do. [&lt;em&gt;LeadingZero&lt;/em&gt;]&lt;/p&gt;

&lt;h3 id=&quot;sassy-things&quot;&gt;Sassy Things&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;» &lt;code class=&quot;highlighter-rouge&quot;&gt;@extend&lt;/code&gt; placeholders only.&lt;/strong&gt; This practice will help prevent some common &lt;code class=&quot;highlighter-rouge&quot;&gt;@extend&lt;/code&gt;-related mistakes and misunderstandings. [&lt;em&gt;PlaceholderInExtend&lt;/em&gt;]&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;» Include one space before any &lt;code class=&quot;highlighter-rouge&quot;&gt;!&lt;/code&gt; and no spaces after it&lt;/strong&gt;, with &lt;code class=&quot;highlighter-rouge&quot;&gt;!important&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;!default&lt;/code&gt; declarations. That’s just how it’s done. [&lt;em&gt;BangFormat&lt;/em&gt;]&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;» Put &lt;code class=&quot;highlighter-rouge&quot;&gt;@else&lt;/code&gt; on the same line as the curly brace it follows.&lt;/strong&gt; [&lt;em&gt;ElsePlacement&lt;/em&gt;]&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-scss&quot; data-lang=&quot;scss&quot;&gt;&lt;span class=&quot;c1&quot;&gt;// bad&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;@if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// ...&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;@else&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// ...&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// good&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;@if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// ...&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;@else&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// ...&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;&lt;strong&gt;» Do not include leading underscores or filename extensions in the basenames of SCSS files that you &lt;code class=&quot;highlighter-rouge&quot;&gt;@import&lt;/code&gt;.&lt;/strong&gt; &lt;code class=&quot;highlighter-rouge&quot;&gt;@import 'horse/donkey'&lt;/code&gt; instead of &lt;code class=&quot;highlighter-rouge&quot;&gt;@import 'horse/_donkey.scss';&lt;/code&gt;. [&lt;em&gt;ImportPath&lt;/em&gt;]&lt;/p&gt;

&lt;h3 id=&quot;avoiding-mistakes&quot;&gt;Avoiding Mistakes&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;» Do not define any property more than once within a single rule set.&lt;/strong&gt; You probably did it by accident, anyway. [&lt;em&gt;DuplicateProperty&lt;/em&gt;]&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;» Do not leave &lt;code class=&quot;highlighter-rouge&quot;&gt;@debug&lt;/code&gt; statements in your code.&lt;/strong&gt; Clean up after yourself. [&lt;em&gt;DebugStatement&lt;/em&gt;]&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;» Do not leave empty rule sets in your code.&lt;/strong&gt; Pay attention. [&lt;em&gt;EmptyRule&lt;/em&gt;]&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;» Only use three or six hexidecimal characters.&lt;/strong&gt; Nothing else means anything, right? [&lt;em&gt;HexValidation&lt;/em&gt;]&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;» Do not misspell properties.&lt;/strong&gt; In fact, do not misspell &lt;em&gt;anything&lt;/em&gt;, ever. [&lt;em&gt;PropertySpelling&lt;/em&gt;]&lt;/p&gt;
</description>
        <pubDate>Sat, 13 Dec 2014 00:00:00 -0700</pubDate>
        <link>https://davidtheclark.com/scss-lint-styleguide/</link>
        <guid isPermaLink="true">https://davidtheclark.com/scss-lint-styleguide/</guid>
        
        
      </item>
    
      <item>
        <title>The Role of Utility Classes in Scalable CSS</title>
        <description>&lt;p&gt;Lately, I’ve been thinking about how to write &lt;em&gt;scalable&lt;/em&gt; CSS. Not so much about how to pull off snazzy effects, or take advantage of the latest modules, or play clever tricks with preprocessors — but how to create stylesheets that will remain coherent, maintainable, consistent, and extendable as projects grow and change — and how to do this &lt;em&gt;systematically&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;I’ve been reading everything I can find on the subject — which includes some very good stuff. (I’ve put together, and plan to maintain, a &lt;a href=&quot;https://github.com/davidtheclark/scalable-css-reading-list&quot;&gt;Scalable CSS Reading List&lt;/a&gt; to share what I consider the most important resources.) But there’s one particular question that keeps pestering me, a question that is rarely addressed, and never at length: &lt;strong&gt;Where exactly do “utility classes” fit in? — why, when, and how should they be used?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Every frontend framework includes some utility classes, but none offer substantial advice about when or why to use them. &lt;em&gt;Considering that any given CSS declaration could be transformed into a utility class, is it possible that every declaration should be? If you can build a component, a page, or a whole site with utility classes alone, should you?&lt;/em&gt; (In &lt;a href=&quot;https://www.smashingmagazine.com/2013/10/21/challenging-css-best-practices-atomic-approach/&quot;&gt;“Challenging CSS Best Practices”&lt;/a&gt;, Thierry Koblentz argues for such an approach: “Atomic CSS,” he calls it.) If not, where do we draw the line; how do we determine if we’ve gone too far? Can reliance on utility classes hurt maintainability? The mind swims.&lt;/p&gt;

&lt;h2 id=&quot;two-css-paradigms-component-and-utilityclasses&quot;&gt;Two CSS Paradigms: Component and Utility Classes&lt;/h2&gt;

&lt;p&gt;All the scalable CSS approaches I’ve learned about endorse two distinct paradigms of classes: I’ll call them &lt;em&gt;component&lt;/em&gt; and &lt;em&gt;utility&lt;/em&gt; classes.&lt;/p&gt;

&lt;p&gt;These two paradigms represent fundamentally different methods of styling; taken to extremes, they can result in dramatically different code and dramatically different workflows.&lt;/p&gt;

&lt;p&gt;(I consider the older dichotomy between “semantic” and “presentational” classes outdated: I fully agree &lt;a href=&quot;http://nicolasgallagher.com/about-html-semantics-front-end-architecture/&quot;&gt;with Nicolas Gallagher&lt;/a&gt; that “the primary purpose of a class name is to be a hook for CSS and JavaScript” and “class names should communicate &lt;em&gt;useful&lt;/em&gt; information to &lt;em&gt;developers&lt;/em&gt;.” Either component or utility classes could be considered “presentational,” by the old logic, and I wouldn’t consider that to be a problem.)&lt;/p&gt;

&lt;h3 id=&quot;component-classes&quot;&gt;Component Classes&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;A &lt;em&gt;component class&lt;/em&gt; encapsulates the declarations required to style an element within the context of a component.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The HTML simply references that component’s classes, and maybe some modifiers, to implement the pattern. Bootstrap’s &lt;code class=&quot;highlighter-rouge&quot;&gt;.btn&lt;/code&gt; exemplifies the single-element component. Other, multi-element Bootstrap components rely on namespaced classes or particular HTML structures. &lt;em&gt;Systematic naming conventions like &lt;a href=&quot;https://csswizardry.com/2013/01/mindbemding-getting-your-head-round-bem-syntax/&quot;&gt;BEM’s&lt;/a&gt; and &lt;a href=&quot;https://github.com/suitcss/suit/blob/master/doc/naming-conventions.md&quot;&gt;SUIT’s&lt;/a&gt; help to conceptualize, implement, and scale the component paradigm.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Using component classes (and SUIT naming), some navigation markup might look like this:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-html&quot; data-lang=&quot;html&quot;&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;nav&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;class=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Nav Nav--light&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;&amp;lt;ul&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;class=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Nav-list&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;li&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;class=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Nav-item&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;nt&quot;&gt;&amp;lt;a&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;class=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Nav-link&quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;href=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;...&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;...&lt;span class=&quot;nt&quot;&gt;&amp;lt;/a&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;li&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;class=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Nav-item&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;nt&quot;&gt;&amp;lt;a&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;class=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Nav-link is-current&quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;href=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;...&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;...&lt;span class=&quot;nt&quot;&gt;&amp;lt;/a&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;&amp;lt;/ul&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/nav&amp;gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;The class &lt;code class=&quot;highlighter-rouge&quot;&gt;.Nav-link&lt;/code&gt; will apply all the rules necessary to style that &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;a&amp;gt;&lt;/code&gt; in that context. The &lt;code class=&quot;highlighter-rouge&quot;&gt;.Nav&lt;/code&gt; component as a whole is reusable, but the &lt;code class=&quot;highlighter-rouge&quot;&gt;.Nav-link&lt;/code&gt; class will never be used outside of its context — outside of a &lt;code class=&quot;highlighter-rouge&quot;&gt;.Nav&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Keep in mind that what I’m calling “components” can be as granular as the situation demands, and “components” can contain other “components.” They are simply coherent, reusable patterns, and might address any level of a design — atoms, molecules, organisms, templates, or pages, in the terminology of &lt;a href=&quot;http://bradfrostweb.com/blog/post/atomic-web-design/&quot;&gt;Atomic Design&lt;/a&gt;. Also (with a nod to &lt;a href=&quot;https://smacss.com/&quot;&gt;SMACSS&lt;/a&gt;), components might be organized into categories targeting different responsibilities — for instance, typography (like a &lt;code class=&quot;highlighter-rouge&quot;&gt;.Heading&lt;/code&gt;) distinguished from layout (like a &lt;code class=&quot;highlighter-rouge&quot;&gt;.Grid&lt;/code&gt;) and interface (like a &lt;code class=&quot;highlighter-rouge&quot;&gt;.Button&lt;/code&gt; or a &lt;code class=&quot;highlighter-rouge&quot;&gt;.Nav&lt;/code&gt;). OOCSS fans might call them “objects.” The point is that &lt;em&gt;component classes represent named units, explicitly related to a reusable pattern, whose styles are defined and modified in a stylesheet.&lt;/em&gt;&lt;/p&gt;

&lt;h3 id=&quot;utility-classes&quot;&gt;Utility Classes&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;A &lt;em&gt;utility class&lt;/em&gt; applies a single rule or a very simple, universal pattern.&lt;/strong&gt; Think &lt;code class=&quot;highlighter-rouge&quot;&gt;.float-right&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;.align-center&lt;/code&gt;, or &lt;code class=&quot;highlighter-rouge&quot;&gt;.font-small&lt;/code&gt; for single rules; &lt;code class=&quot;highlighter-rouge&quot;&gt;.list-unstyled&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;.clearfix&lt;/code&gt;, and &lt;code class=&quot;highlighter-rouge&quot;&gt;.visually-hidden&lt;/code&gt; for patterns. (Sometimes they’re called “helpers,” as in Bootstrap.)&lt;/p&gt;

&lt;p&gt;Utility classes are as simple, composable, and reusable as possible; so they will (and are intended to) end up scattered throughout your markup, performing their little role in diverse and sundry contexts. They &lt;em&gt;must&lt;/em&gt; be strictly categorized and tightly focused, or they lose their … utility — their &lt;i&gt;raison d’être&lt;/i&gt;. They must epitomize &lt;a href=&quot;https://csswizardry.com/2012/04/the-single-responsibility-principle-applied-to-css/&quot;&gt;the single responsibility principle&lt;/a&gt;. Unlike a component class like &lt;code class=&quot;highlighter-rouge&quot;&gt;.Nav-link&lt;/code&gt;, a utility class like &lt;code class=&quot;highlighter-rouge&quot;&gt;.float-left&lt;/code&gt; could show up anywhere, on any element, in any context, so we need to know that it will &lt;em&gt;always&lt;/em&gt; do &lt;em&gt;exactly&lt;/em&gt; the same thing. That’s why The Authorities agree that &lt;em&gt;utility classes must never change&lt;/em&gt;, and why they are a rare case when &lt;a href=&quot;https://css-tricks.com/when-using-important-is-the-right-choice/&quot;&gt;using &lt;code class=&quot;highlighter-rouge&quot;&gt;!important&lt;/code&gt; might be the right choice&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;(If you’re at all uncertain what I mean by “utility class,” have a look at some collections: &lt;a href=&quot;https://foundation.zurb.com/docs/utility-classes.html&quot;&gt;Foundation’s&lt;/a&gt;, &lt;a href=&quot;https://getbootstrap.com/css/#helper-classes&quot;&gt;Bootstrap’s&lt;/a&gt;, &lt;a href=&quot;https://getuikit.com/docs/utility.html&quot;&gt;UIKit’s&lt;/a&gt;, &lt;a href=&quot;http://groundworkcss.github.io/groundwork/docs/helpers.html&quot;&gt;GroundworkCSS’s&lt;/a&gt;, and &lt;a href=&quot;https://github.com/suitcss/utils&quot;&gt;SUIT’s&lt;/a&gt;.)&lt;/p&gt;

&lt;p&gt;More so than component classes, &lt;em&gt;utility classes bring styling directly into the markup&lt;/em&gt;, without even the mediation of a name. Design patterns can be anonymous aggregates of globally shared styles. So if you want to change the appearance of an element styled with utility classes, you’ll have to change the markup — the class list, specifically — &lt;em&gt;not&lt;/em&gt; the stylesheet.&lt;/p&gt;

&lt;p&gt;Typically, it’s suggested or assumed that God made utility classes for straightforward situations calling for very few rules. You just want a button floated right, a centered heading, a different color, invisible text. Why give an element its own name, its own selector, its own ruleset in some stylesheet — why conceptualize a “component” for it — when all you want is to apply a very common, probably very basic pattern?&lt;/p&gt;

&lt;p&gt;Theoretically, though, entire components and entire sites can be constructed by piecing together utility classes. Relying on utility classes alone, the navigation markup from above might look like this:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-html&quot; data-lang=&quot;html&quot;&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;nav&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;class=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;pad-2 bg-light text-med text-upper&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;&amp;lt;ul&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;class=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;list-unstyled clearfix&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;li&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;class=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;float-left&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;nt&quot;&gt;&amp;lt;a&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;class=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;link-unstyled pad-1x2 bg-button&quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;href=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;...&quot;&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;...&lt;span class=&quot;nt&quot;&gt;&amp;lt;/a&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;li&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;class=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;float-left margin-left-1&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;nt&quot;&gt;&amp;lt;a&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;class=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;link-unstyled pad-1x2 bg-accent&quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;href=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;...&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;...&lt;span class=&quot;nt&quot;&gt;&amp;lt;/a&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;&amp;lt;/ul&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/nav&amp;gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;h3 id=&quot;more-comparing-and-contrasting&quot;&gt;More Comparing and Contrasting&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;With component classes, implementation of a new design pattern will probably involve the creation of new classes and more lines of CSS. With utility classes, new design patterns can often be implemented with existing classes, no new CSS.&lt;/li&gt;
  &lt;li&gt;With component classes, maintenance and modification of styles happens in the stylesheets. With utility classes, it happens in the HTML, in class attributes.&lt;/li&gt;
  &lt;li&gt;Component classes entail repetition in stylesheets. Utility classes entail repetition in HTML.&lt;/li&gt;
  &lt;li&gt;Component classes are scoped to their component and should be built to prevent leakage; so one component can change without affecting others. Utility classes are global dependencies; so any change to a utility class will affect any number of elements.&lt;/li&gt;
  &lt;li&gt;Component class names primarily represent the relationships of component parts, usually without reference to style. Utility class names exclusively and transparently represent the styling applied.&lt;/li&gt;
  &lt;li&gt;Component classes are flexible: they can be attached to arbitrary, variable styles, different themes, etc. Utility classes are rigid and unchangeable — ideally, they are just as reliable as a declaration in CSS.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;some-alleged-benefits-of-utilityclasses&quot;&gt;Some Alleged Benefits of Utility Classes&lt;/h2&gt;

&lt;p&gt;The component-class paradigm is a more straightforward, traditional utilization of CSS, I think. Apply a distinct class and tie that class to some styles. Add, modify, and remove declarations in the stylesheet. And so on. Though you may never achieve the fabled “Separation of Concerns” — and may not even care to — you will be closer to it and reap some of its rewards if you use component classes more, utility classes less.&lt;/p&gt;

&lt;p&gt;So it’s worth articulating and assessing what people consider to be the benefits of utility classes. Why do we use them at all? Why are they included in every frontend framework? And why might some people consider using them exclusively?&lt;/p&gt;

&lt;h3 id=&quot;1-dryness-and-consistency&quot;&gt;1. DRYness and Consistency&lt;/h3&gt;

&lt;p&gt;Apply &lt;code class=&quot;highlighter-rouge&quot;&gt;.float-left&lt;/code&gt; whenever you need something floated left, and you won’t end up filling stylesheets with repeated &lt;code class=&quot;highlighter-rouge&quot;&gt;float: left;&lt;/code&gt; declarations. (&lt;a href=&quot;http://csslint.net/&quot;&gt;CSSLint&lt;/a&gt; will be proud of you.) Reuse color utilities (e.g. &lt;code class=&quot;highlighter-rouge&quot;&gt;.bg-dark&lt;/code&gt;), and you won’t have to copy and paste hex values; additionally, when the design changes, you can alter the value in one place instead of many. Rather than throwing &lt;code class=&quot;highlighter-rouge&quot;&gt;3px&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;6px&lt;/code&gt;, and &lt;code class=&quot;highlighter-rouge&quot;&gt;7px&lt;/code&gt; of padding here and there — whatever seems right at the time — rely on a simple &lt;code class=&quot;highlighter-rouge&quot;&gt;.pad-sm&lt;/code&gt; for consistent spacing. Utility classes can help you reduce repetition in your CSS and inadvertant irregularity in your style.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;This benefit, however, is negligible &lt;em&gt;if you use a preprocessor&lt;/em&gt;&lt;/strong&gt;. The reuse of single-rule classes like &lt;code class=&quot;highlighter-rouge&quot;&gt;.float-left&lt;/code&gt; merely shifts repetition from CSS to HTML. It’s still repetition: you still tell every individual element you want floated left to float left, in one way or another. And for more complex patterns and particular values, a preprocessor — with variables, mixins, and extends — enables the same degree of consistency by other, arguably more author-friendly, means.&lt;/p&gt;

&lt;h3 id=&quot;2-filesize&quot;&gt;2. Filesize&lt;/h3&gt;

&lt;p&gt;By taking repetition out of the CSS, utility classes reduce CSS filesize.&lt;/p&gt;

&lt;p&gt;Preprocessors can’t match this benefit. Using mixins and variables to repeat patterns and values does result in DRYness and consistency but does not cut back on the size of the delivered CSS: every repeated mixin and variable generates repeated declarations.&lt;/p&gt;

&lt;p&gt;Sass &lt;code class=&quot;highlighter-rouge&quot;&gt;@extend&lt;/code&gt;s can be used to reduce &lt;em&gt;some&lt;/em&gt; repetition — but they are problematic in other, key ways. And rather than advocating &lt;code class=&quot;highlighter-rouge&quot;&gt;@extend&lt;/code&gt;s, I’d argue that &lt;strong&gt;this benefit is questionable&lt;/strong&gt;. CSS filesize should be one of your last optimization concerns; and once Gzip gets involved, it’s possible that the savings won’t make a substantial difference. In any situation where the &lt;em&gt;scalability&lt;/em&gt; of your CSS is at issue, where there’s any danger of things getting messy and confusing and out-of-control, &lt;em&gt;authoring and maintenance considerations should trump kilobyte savings&lt;/em&gt;.&lt;/p&gt;

&lt;h3 id=&quot;3-expedience&quot;&gt;3. Expedience&lt;/h3&gt;

&lt;p&gt;There is no question that utility classes are handy, allowing HTML authors to apply styles more directly. Especially for those elements that require only one or two rules and don’t play an obvious role in some component structure — why add an extra layer of abstraction?&lt;/p&gt;

&lt;p&gt;If you move quickly from project to project, a full set of utility classes — or a method of generating them as needed — could speed up your work. And it’s possible that utility classes could help backend engineers, or other collaborators, get things done without modifying any stylesheets.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;There’s no denying this benefit; however, it needs to be considered in the context of your workflow&lt;/strong&gt;, because often expedience is much less important than long-term maintainability — though sometimes it is the top priority.&lt;/p&gt;

&lt;h2 id=&quot;some-dangers-of-utility-classes&quot;&gt;Some Dangers of Utility Classes&lt;/h2&gt;

&lt;p&gt;Each of the dangers below would be minor, even trivial, if you use utility classes sparingly. But the more you rely on utility classes, the more significant these dangers become.&lt;/p&gt;

&lt;h3 id=&quot;1-inconstant-appearances&quot;&gt;1. Inconstant Appearances&lt;/h3&gt;

&lt;p&gt;When an element’s appearance won’t stay constant, reliance on utility classes can become troublesome. Responsive design is one such circumstance: For example, if things float right at some widths but not others, different utility classes will have to be made to target different widths — something like &lt;code class=&quot;highlighter-rouge&quot;&gt;float-right-sm&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;float-right-md&lt;/code&gt;, and &lt;code class=&quot;highlighter-rouge&quot;&gt;float-right-lg&lt;/code&gt;. Or else you might need to develop a more complicated system that uses JavaScript to enact style changes (&lt;a href=&quot;https://www.smashingmagazine.com/2013/10/21/challenging-css-best-practices-atomic-approach/#comment-208370&quot;&gt;Thierry Koblentz does something like that&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;You know things are going downhill if you ever end up with elements bearing the utility class &lt;code class=&quot;highlighter-rouge&quot;&gt;.float-right&lt;/code&gt; that are not, in fact, floating right, for one reason or another.&lt;/p&gt;

&lt;p&gt;Similar issues will arise if your project involves themed variations that rely on the same markup. In such cases the abstraction of component classes can allow for flexibility that the directness of utility classes precludes.&lt;/p&gt;

&lt;h3 id=&quot;2-readability-and-reusability-of-component-relationships&quot;&gt;2. Readability and Reusability of Component-Relationships&lt;/h3&gt;

&lt;p&gt;The more you rely on utility classes, the fewer of your design elements will be explicitly associated with components, with class names that reflect their relationships to one another (like &lt;code class=&quot;highlighter-rouge&quot;&gt;.Nav&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;.Nav-item&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;.Nav-link&lt;/code&gt;). Although the &lt;em&gt;styles&lt;/em&gt; will be transparently communicated by the utility class names, the &lt;em&gt;conceptual structure&lt;/em&gt; of the components and the page will not; and that ambiguity might result in some confusion down the line, when somebody is trying to read and understand your code, maybe trying to reuse a particular pattern.&lt;/p&gt;

&lt;h3 id=&quot;3-encouragement-of-one-off-styles&quot;&gt;3. Encouragement of One-Off Styles&lt;/h3&gt;

&lt;p&gt;Without component classes, any repeat occurrence of a pattern might feel more like a re-implementation than reuse — which could lead to slight tweaks here and there, variations that seem right in the heat of the moment but add up to inconsistency. At some point you may realize that your broad set of utility classes gave HTML authors too much freedom to diverge, to favor expedience over architecture, and the maintainability of your code suffered.&lt;/p&gt;

&lt;h3 id=&quot;4-too-many-decisions&quot;&gt;4. Too Many Decisions&lt;/h3&gt;

&lt;p&gt;With utility classes, it’s up to HTML authors to apply the right cluster of styles to the right elements. A good component class system, on the other hand, might allow authors to create HTML without knowing or caring much about why it ends up looking the way it ends up looking.&lt;/p&gt;

&lt;p&gt;An important goal of any systematic methodology is to &lt;em&gt;reduce on-the-spot decision-making&lt;/em&gt; — and component classes do that better than utility classes.&lt;/p&gt;

&lt;h2 id=&quot;conclusions-prescriptions-guesses&quot;&gt;Conclusions, Prescriptions, Guesses&lt;/h2&gt;

&lt;p&gt;Light use of limited utility classes, for patterns that you know will never change, should not cause any problems that can’t be mended easily — by removing the utility class as needed, shifting the style-burden back into the stylesheets.&lt;/p&gt;

&lt;p&gt;And there &lt;em&gt;are&lt;/em&gt; some particular circumstances when &lt;em&gt;heavy&lt;/em&gt; reliance on utility classes — building complete components or pages solely with them — might be ok:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;If you are not using a preprocessor to facilitate DRYness and consistency.&lt;/li&gt;
  &lt;li&gt;If your workflow makes it just as easy or easier to adjust styles by modifying class lists in templates than by modifying rule sets in stylesheets.&lt;/li&gt;
  &lt;li&gt;If your responsive design hinges on very reliable breakpoints, and that’s all the inconstancy you anticipate.&lt;/li&gt;
  &lt;li&gt;If you are building a unique pattern, something you don’t plan on rebuilding the exact same way in other places.&lt;/li&gt;
  &lt;li&gt;If filesize is at an absolute premium.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;In most situations, though — especially for large-scale projects and reusable components — it seems to me that the benefits of systematic component classes will prove more valuable than the expedience of utility classes.&lt;/em&gt; By using component classes, you get the communicative value of naming element groups and relationships; you encourage the creation of coherent, relatively self-contained components intended for reuse; and you allow for the flexibility to vary styling, to some degree, without changing markup.&lt;/p&gt;

&lt;p&gt;I hope that’s enough words for you. That’s enough for me.&lt;/p&gt;
</description>
        <pubDate>Sat, 20 Sep 2014 00:00:00 -0700</pubDate>
        <link>https://davidtheclark.com/on-utility-classes/</link>
        <guid isPermaLink="true">https://davidtheclark.com/on-utility-classes/</guid>
        
        
      </item>
    
  </channel>
</rss>
