How to love Tailwind
Tags: css, tailwindcss, react, nextjs
Published: 2021-08-03 13:53:15

<!-- wp:paragraph -->
<p>In recent years there's been a rise in what some people call utility-first CSS frameworks: Taking an atomic approach to CSS by combining single-purpose classes straight onto your HTML. It's easy to see why this format has taken off: you can build layouts more quickly when you don't need to write custom CSS, and no need to rebuild the files every time you make a change. Utility-first frameworks can also be configured to follow a strict design system, which feels lovely when you're building a design that follows the same design systems - designers often use consistent measures of space and width, so it feels great for your CSS to be so in-line with their vision straight out of the box.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>One popular framework has been rising to dominance, with many agencies and software houses making use of the fast prototyping capabilities it has to offer. It's called Tailwind, and it looks like it could be a major player in the world of frontend development in the future. </p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>So what's the problem?</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Tailwind CSS tends to be a very divisive topic between developers: a bit like Marmite, you either love it or you hate it. And I think that's a crying shame, because most of the arguments against it could be addressed with a change in mindset. It's important to keep in mind that, as with anything in life, you should always pick the right tool for the job. I won't sit and pretend Tailwind solves everything: it's only useful in the right situation. </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>The thing is, Tailwind and other utility-first frameworks aren't at all like traditional CSS. If you look at a methodology we're all pretty familiar with, such as BEM, there is a massive difference in the <em>source of truth</em> of styles.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>A new source of truth</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>With a methodology like BEM, there's a focus on maintaining a separation of concerns between your HTML, CSS and JavaScript. The CSS is generally considered to be the source of truth when it comes to styling, whereas HTML should only concern content. This works really well for monolithic sites such as Wordpress or static HTML sites, where you would be writing HTML that may repeat itself. For example, here's a simple <a href="http://www.stubbornella.org/content/2010/06/25/the-media-object-saves-hundreds-of-lines-of-code/">media object</a> structure:</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>&lt;div class="media-object"&gt;
  &lt;div class="media-object__media"&gt;
    &lt;img src="avatar.jpg" /&gt;
  &lt;/div&gt;
  &lt;div class="media-object__content"&gt;
    Hello world! Here's some content.
  &lt;/div&gt;
&lt;/div&gt;</code></pre>
<!-- /wp:code -->

<!-- wp:code -->
<pre class="wp-block-code"><code>$module: 'media-object';

.#{$module} {
  display: flex;
  flex-direction: row;
  
  &amp;__media {
    flex-basis: 48px;
  }
  
  &amp;__content {
    flex: 1 0 auto;
  }
}</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>The HTML for this object can be copy &amp; pasted ad infinium, as would be the case if you were building a basic HTML page. Since the CSS is stored away in its source of truth, it doesn't matter too much if we repeat the HTML so long as the structure remains the same. It's not perfect and doesn't always feel right to copy and paste in this way, but by working in this way we can keep the styling pretty consistent even if it changes later down the line. If we change <code>.media-object</code> later on by adding padding, the change will be reflected wherever the class is used. This is where the confusion starts when moving over to Tailwind.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>The problem is that many developers will move over to Tailwind and use it in exactly the same way as they used BEM: By copy-pasting HTML structures wherever required. Since Tailwind uses class composition to create styles, your source of truth no longer lies in the CSS files. The HTML itself becomes the source of truth for the way it looks. Here's the same component built using Tailwind's utility classes:</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>&lt;div class="flex flex-row"&gt;
  &lt;div class="w-7"&gt;
    &lt;img src="avatar.jpg" /&gt;
  &lt;/div&gt;
  &lt;div class="flex-grow w-auto"&gt;
    Hello world! Here's some content.
  &lt;/div&gt;
&lt;/div&gt;</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>Imagine we have this media object copy-pasted all over a website; If we want to add padding to the top element, we would have to go through the whole site and manually add the padding class to each instance. Sure, you could use a find-and-replace tool to help, but this technique could lead to mistakes if you're not careful, and will become difficult to manage as the site grows.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>This is why <strong>I wouldn't recommend Tailwind if you're not using component-based framework.</strong> This is why so many people grow to hate Tailwind: because they're using the wrong tool for the job, and it's working against them. It's just not designed to be used in the traditional sense.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>Component-based architecture</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>The place where Tailwind really shines is in modern frameworks: Be it JavaScript frameworks such as React and Vue, or template systems like Twig, this approach to CSS thrives when combined with a <em>component-based </em>architecture.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>In such systems, the source of truth for the styles can be merged with the structure of the site. In these systems, developers are encouraged to build reusable, composable components. For example here's the same media object built using React:</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>// MediaObject.js
export default function MediaObject({ children, img}) {
  return (
    &lt;div class="flex flex-row"&gt;
      &lt;div class="w-7"&gt;
        &lt;img src={ img } /&gt;
      &lt;/div&gt;
      &lt;div class="flex-grow w-auto"&gt;
        { children }
      &lt;/div&gt;
    &lt;/div&gt;
  )
}</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>This file, <code>MediaObject.js</code>, is now the absolute source of truth for the way the component looks and feels: There's no CSS file off in the distance being relied on, no HTML that needs to be copy-pasted a gazillion times. Everything's here in this one file.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>As you can see, this component doesn't care about the content it contains: both the media and the text content are props passed down when component is used anywhere. Here's an example of how <code>MediaObject</code> would be called on a page:</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>&lt;MediaObject media="avatar.jpg"&gt;
  &lt;h3&gt;Person Personson&lt;/h3&gt;
  &lt;p&gt;Hello world!&lt;/p&gt;
&lt;/MediaObject&gt;</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>"But what about modifiers?", I hear you ask. Well, component-based frameworks can handle that easily too, and do much cooler things while they're at it.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>For example, let's say we also have a dark variant of the media object with a dark grey background. Not only does the background colour need to change, but the colour of the text inside needs to change to contract with the darker background.</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
`{% raw %} - please excuse these tags, I'm not used to dev.to and the page won't render without them. But if anyone knows how to hide them, please let me know!
<pre class="wp-block-code"><code>// MediaObject.js
export default function MediaObject({ children, img, modifiers }) {
  const bgC = modifiers.isDarkBG ? 'bg-dark-grey' : 'bg-transparent';
  const textC = modifiers.isDarkBG ? 'text-white' : 'text-dark-grey';
  
  return (
    &lt;div class={`flex flex-row ${ bgC }`}&gt;
      &lt;div class="w-7"&gt;
        &lt;img src={ img }
      &lt;/div&gt;
      &lt;div class={`flex-grow w-auto ${ textC }`}&gt;
        { children }
      &lt;/div&gt;
    &lt;/div&gt;
  )
}</code></pre>
{% endraw %}`
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>Now we can make use of ordinary JavaScript to control look at feel using a 'modifiers' object, which gives us far more powerful tools for building conditional styles. There's so many more ways to use this, and once you get used to working in this way it starts to feel really natural and intuitive. You can define conditions inside the component itself, or pass a string of class names directly through for extra control.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>It's recommended when building components to take advantage of abstraction: You can move different levels of logic into different files for the same component. The best example of this in React is <a href="https://medium.com/@yassimortensen/container-vs-presentational-components-in-react-8eea956e1cea">container components vs presentational components.</a></p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>Abstract business logic to keep it tidy</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>By wrapping the view of the component (the way it looks) inside a container where the business logic (the way it works) is stored, you can isolate different concerns into different files. I'll often use a folder structure where the folder name is the name of the component, and there are two files, <code>index.js</code> and <code>View.js</code>. <code>index.js</code> is the container component, while <code>View.js</code> is the presentational component.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":146,"width":262,"height":115,"sizeSlug":"large","linkDestination":"none"} -->
<figure class="wp-block-image size-large is-resized"><img src="https://npmrundev.files.wordpress.com/2021/08/file-structure.png?w=262" alt="" class="wp-image-146" width="262" height="115"/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>By keeping all my presentational logic inside <code>View.js</code>, including any conditional styles based on the <code>modifier</code> prop, I can make sure any logic that doesn't concern the way the component is styles is kept in the container component, <code>index.js</code>. This really helps with tidiness and staying sane, as everything has a logical place to go.</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
`{% raw %}
<pre class="wp-block-code"><code>// View.js
export default function MediaObjectView(&#123; children, img, modifiers &#125;) &#123;
  const bgC = modifiers.isDarkBG ? 'bg-dark-grey' : 'bg-transparent';
  const textC = modifiers.isDarkBG ? 'text-white' : 'text-dark-grey';
  
  return (
    &lt;div class=&#123;`flex flex-row $&#123; bgC &#125;`&#125;&gt;
      &lt;div class="w-7"&gt;
        &lt;img src=&#123; img &#125;
      &lt;/div&gt;
      &lt;div class=&#123;`flex-grow w-auto $&#123; textC &#125;`&#125;&gt;
        &#123; children &#125;
      &lt;/div&gt;
    &lt;/div&gt;
  )
&#125;</code></pre>
{% endraw %}`
<!-- /wp:code -->

<!-- wp:code -->
<pre class="wp-block-code"><code>// index.js
export default function MediaObject({ children, img, modifiers }) {
  // any business logic can go here and be passed to MediaObjectView using props
  
  return &lt;MediaObjectView {...children, img, modifiers} /&gt;
}
</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>In theory, you could keep on abstracting components as many times as you like. It's generally recommended to stick with a max of 2, but I'm sure there's some cases where separating logic even further would be beneficial.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>Is Tailwind right for me?</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Well, maybe. If you're building static HTML sites, probably not. If you're building Wordpress sites with PHP, you'd be best off using some kind of templating engine to maintain your source of truth. But if you're using a modern JavaScript framework like React or Vue, I highly recommend giving it a try: It's very different and comes with its own challenges, but can be a joy to use and extremely powerful if used in the right way. Just don't expect to be able to use the same concepts you learned with BEM - it's a completely different box of frogs.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Learn more about Tailwind on the <a href="https://tailwindcss.com/">official website</a>, and <a href="https://www.youtube.com/watch?v=J_7_mnFSLDg">watch this great presentation</a> for more details on how to use Tailwind in place of traditional CSS.</p>
<!-- /wp:paragraph -->