<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Thomas Dargon on Medium]]></title>
        <description><![CDATA[Stories by Thomas Dargon on Medium]]></description>
        <link>https://medium.com/@thomasdargon?source=rss-8309eccc1d18------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*BV6tlj6kZntzcJrGBhcszA.png</url>
            <title>Stories by Thomas Dargon on Medium</title>
            <link>https://medium.com/@thomasdargon?source=rss-8309eccc1d18------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 21:06:14 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@thomasdargon/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[How do we approach theming for white-label applications?]]></title>
            <link>https://medium.com/koodoo/how-do-we-approach-theming-for-white-label-applications-672163019731?source=rss-8309eccc1d18------2</link>
            <guid isPermaLink="false">https://medium.com/p/672163019731</guid>
            <category><![CDATA[white-label-app]]></category>
            <category><![CDATA[tailwind]]></category>
            <category><![CDATA[mortgage]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[component-libraries]]></category>
            <dc:creator><![CDATA[Thomas Dargon]]></dc:creator>
            <pubDate>Mon, 31 Jan 2022 11:29:12 GMT</pubDate>
            <atom:updated>2022-01-31T11:29:12.009Z</atom:updated>
            <content:encoded><![CDATA[<h3>How do we approach styling for white-label applications?</h3><figure><img alt="Gif of a FE web application under different theme" src="https://cdn-images-1.medium.com/max/500/1*jiyimUUXbANLH3H20a9C0g.gif" /><figcaption>One application under different themes.</figcaption></figure><h4>TL;DR</h4><p>At build time our frontend application dynamically imports a tailwind config file and a style-overrides file based on the value of a “THEME” environment variable. This allows us to apply different styles to every single component in our UI, effectively creating a customisable application.</p><h4>1) Introduction 💭</h4><h4>2) A Component’s World🌏</h4><ul><li>2.1 — Creating theme-able components 🌈</li><li>2.2 — Using custom CSS classes🖌</li><li>2.3 — Using default Tailwind CSS classes 🎨</li></ul><h4>3) Applying a theme to a site 🖥</h4><ul><li>3.1 — Injecting a theme prop ⚙️</li><li>3.2 — Applying a Tailwind configuration file 🏋️‍♀️</li></ul><h4>4) Conclusion 🔮</h4><h3>1 — Introduction</h3><p>At Koodoo, we are building a mortgage distribution platform with the end goal to match people with mortgages that suit them, hassle free. To achieve our mission we want to make our applications available to the widest audience. A key element in our distribution strategy is to partner up with price-comparison websites such as <a href="https://mortgages.thetimes.co.uk/">The Times</a>, <a href="https://www.confused.com/mortgages">Confused.com</a>, and <a href="https://www.nerdwallet.com/uk/mortgages/">Nerdwallet</a> and power their mortgage services.</p><p>This is a win-win: our partners can focus on what they are good at, which is gathering wide audiences onto their website and promote services via their own channels while we can focus on what we are good at — building applications to connect people with mortgages. A key element to a successful integration with a partner is styling: to be attractive we need to provide applications that integrate seamlessly with our partner’s website and brand theme.</p><p>To achieve this, we develop “white label”<strong> </strong>applications. This means that when you visit any of our partners websites, you will have access to a seamless mortgage journey designed and developed by Koodoo but each have their own unique theming and content. This approach comes with additional complexity with regards to hosting, journey design (content customization) and styling but the benefits are enormous, we can focus our development efforts on a single code base and avoid the risk of divergence that would come from maintaining multiple applications.</p><p>In this article we will focus on the styling challenges and answer the question: How do we approach styling for white label applications. Let’s dive in! 🐇</p><h3>2) A Component’s World 🌍</h3><p>Modern web applications often use frameworks that promote UI componentization. At Koodoo, we typically use Next.js and Tailwind CSS as our frontend frameworks which means that our UIs are made of components.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7_CCTBZ6-KnfEM1jrCroZw.png" /><figcaption>Breakdown of a User Interface into multiple components</figcaption></figure><p>We can define a “theme” as simply being a group of CSS rules which when put together represents the identity and the brand image of one of our partner. Theming is style customisation, so it is tightly coupled with how our components are written and styled. The first step in creating a customisable UI is creating theme-able components.</p><p>As we maintain multiple “white-label” applications across different teams we have our own React component library, that provides our applications with theme-able components.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xuCYsUwBI_UszPz-7AQXOQ.png" /><figcaption>FE application using components from our shared central component library.</figcaption></figure><p><em>Note: Having a component library is not necessary to achieve theming for white label applications, (we could also have all our components in the same code repository as our application), it just happens that as we develop multiple white label applications, having our own component library in a separate repository avoids duplications and facilitates maintainability in the long run.</em></p><h4>2.1) Creating theme-able components 🧩 🌍</h4><p>The code snippet below is for a <strong>&lt;Button /&gt; </strong>component and shows how our components are written to be theme-able. If we overlook the imports and the propTypes definition, our component is a simple function which does 3 things:</p><ol><li>Defines three string variables (<em>primaryClasses</em>, <em>secondaryClasses</em> and <em>criticalClasses</em>) holding CSS utility classes. Each of these strings define a design variant ( “primary”, “secondary” and “critical” respectively).</li><li>Defines one <em>attributes</em> object which can contain any of the classes defined above.</li><li>Returns a JSX expression: <em>&lt;Component &gt;</em> which receives each of these classes defined as a value for its className property.</li></ol><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/45c5a612583c79c0a96238b39a26a6d4/href">https://medium.com/media/45c5a612583c79c0a96238b39a26a6d4/href</a></iframe><h4>Enabling design variants</h4><p>This component can be rendered under different <strong>design variants </strong>depending on the “primary”, “secondary” and “critical” props it receives.</p><p><em>Our button component also supports “small” and “base” design variants, but I removed them from the code snippet for simplicity.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OxLaSVz3qXXvXV1RRj3fYA.png" /><figcaption><em>&lt;Button/&gt; component rendered in our Component Library explorer under the “Koodoo” theme.</em></figcaption></figure><p>⚠️ <strong>Design Variants of a component are different from the Theme</strong>: each of these &lt;Button /&gt; design variants should be available under a given theme:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*sTK-Tcua6_pGnnnI7hRwdg.png" /><figcaption><em>&lt;Button/&gt; component rendered in our Component Library explorer under the four different themes.</em></figcaption></figure><h4>2.2) Using custom CSS utility classes 🎨</h4><p>To make components theme-able, we use custom css utility classes which are defined differently in different tailwind config files.</p><p>To see how this works we will focus on the theme-able “primary” button:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*p3wJZjJaQqVWDtafSmzCow.png" /><figcaption>&lt;Button/&gt; component in its “primary” design variant and default state (not being hovered or activated) under four different themes.</figcaption></figure><p>If we zoom on the primaryClasses definition, We have styles defined for each state our &lt;Button /&gt; can be in (<em>default</em>, <em>hover</em>, <em>focus-visible</em> and <em>pressed</em>).</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/17c75a1252c378e09a6adb052629229c/href">https://medium.com/media/17c75a1252c378e09a6adb052629229c/href</a></iframe><p>Zooming in further on the 3 utility classes used for the primary &lt;Button/&gt; in a “default” state, we see that these utility classes simply apply the following styles:</p><ol><li>background-colour</li><li>box-shadow</li><li>text-colour</li></ol><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/66f8e6e8a0bb9a657c2003ea6bfb4b40/href">https://medium.com/media/66f8e6e8a0bb9a657c2003ea6bfb4b40/href</a></iframe><p>If you are familiar with Tailwind, you would have noticed that these 3 utility classes are not the default tailwind utility classes, and you are right my friend ! 😉 Indeed, we are leveraging <a href="https://tailwindcss.com/docs/theme#customizing-the-default-theme">tailwind extendibility</a> to extend it with our custom classes on top of the default classes Tailwind offers out of the box. This works particularly well for custom values such as “colour palettes” aka our own theme.</p><p>In our component library repository we define a tailwind.config.jsfile where we “tell” tailwind what each utility class (i.e. ‘bg-buttons/primary/fill/default’ etc.. ) should mean, and they each have a different meaning for each theme: one class multiple flavours 🍦.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a205b14768923e5d780545974b9c0688/href">https://medium.com/media/a205b14768923e5d780545974b9c0688/href</a></iframe><p>Thanks to this file we are able to use the notation bg-[customColorClass] , text-[customColorClass]and shadow-[customBoxShadowClass]to apply each style defined in the tailwind.config.jsfile to the component. Then at build time, Tailwind generates the CSS file containing the styles defined in this tailwind.config.js file. So when our component renders, the browser will match the .bg-buttons/primary/fill/defaultclass with the CSS rule {background-color:#495ce9;} and render the following &lt;Button /&gt;:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/400/1*6E3K4hxfoqhiaSsUTrPG_g.png" /><figcaption>✨ Primary &lt;Button /&gt; under Theme1 ✨</figcaption></figure><p>For another theme, we have another tailwind.config.js where the same custom classes are pointing to different values:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ddb532087bc857be3d60af50cb9121ee/href">https://medium.com/media/ddb532087bc857be3d60af50cb9121ee/href</a></iframe><p>My primary button under the theme3 has a different background colour and text colour:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/404/1*DAE8WoMd0Br-91Ibt5Jkvg.png" /><figcaption>✨ Primary &lt;Button /&gt; in default state under Theme3✨</figcaption></figure><p>Each time we want to support an additional theme, we just need to add a new tailwind.config.jsfile for that theme.</p><p>💡 We have one component, using the same className value under each theme but we simply “pair” it with a different theme file (at build time) in order to render the &lt;Button/&gt; under the theme we want: <strong>one class, multiple flavours! 🍻</strong></p><h4>2.3) Utilizing default Tailwind CSS classes</h4><p>There are scenarios where we actually want to use the default utility classes that Tailwind offers out of the box. Imagine you have a <strong>&lt;Button /&gt;</strong> component<em> (hello again </em>👋 😁<em>)</em> that you would like to render with let’s say different border-radius between two themes. My theme1 &lt;Button /&gt; should have slightly rounded corners and I want my theme2 &lt;Button /&gt; to be rounder and maybe be uppercased and bold:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/400/1*6E3K4hxfoqhiaSsUTrPG_g.png" /><figcaption>✨ &lt;Button /&gt; <strong>Theme1</strong> ✨</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/392/1*Lw5wuKjdEUq5-A56y_Cb_w.png" /><figcaption>✨ &lt;Button /&gt; <strong>Theme2</strong> ✨</figcaption></figure><p>You wouldn’t want to have to write a custom class for each border radius and font weight the buttons should have under each theme. Some styles such as border-radius, font-weight, text-transform are so common that most websites only use a small variation of values these properties can have. Tailwind already comes equipped with the default classes to support these properties:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/602/1*e9XRMpFilv17pXKrO6EmKA.png" /><figcaption><em>Tailwind default </em><strong><em>border-radius</em></strong><em> styling utility classes</em></figcaption></figure><ul><li><a href="https://tailwindcss.com/docs/text-transform">Text Transform - Tailwind CSS</a></li><li><a href="https://tailwindcss.com/docs/font-weight#setting-the-font-weight">Font Weight - Tailwind CSS</a></li></ul><p>Instead of passing custom classes taking different meanings for each theme, we would like to simply inject different <em>already defined</em> classes for each theme that my &lt;Button/&gt; component should support.</p><p>Let’s take a look at the code again:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0f67f61d0921522c7b98a166336fffc5/href">https://medium.com/media/0f67f61d0921522c7b98a166336fffc5/href</a></iframe><p>Notice that our &lt;Button /&gt; component can receive a <strong>theme</strong> property and whatever is held inside <strong>theme?.button</strong> will be applied to the Button <strong>className</strong>, effectively changing its style based on the<strong> theme props. 💡</strong></p><p>🙋‍♂️ “ Excuse me“ …BUT, where is this prop coming from? does the application has to define a theme props and pass it into <strong>every</strong> single components it is using ? ? ? 🙀 🙀 🙀</p><p>The <strong>theme</strong> prop can still be defined in the theme files inside the component library so it can be maintained in one place an re-used in different frontend applications!</p><p>Let’s introduce some nuance: 💡 each of the themes we support is defined by not one but <strong>two files</strong>: one <strong>tailwind-config</strong> file (that we previously discussed) and one <strong>style-overrides</strong> file.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*lf8VDiF9KX8ZNZL898zvLw.png" /><figcaption>A more accurate representation of the /themes folder inside our Component Library repository.</figcaption></figure><p>Let’s take a look at the style-overrides file for the theme1:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/93495b902bc0000fdeab32163dc7d331/href">https://medium.com/media/93495b902bc0000fdeab32163dc7d331/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/400/1*6E3K4hxfoqhiaSsUTrPG_g.png" /><figcaption>✨ &lt;Button/&gt; with border-radius value of rounded-md =&gt; theme1✨</figcaption></figure><p>The same file for the theme2:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/69c73ce6f5d524247eafa6f37bf638c4/href">https://medium.com/media/69c73ce6f5d524247eafa6f37bf638c4/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/392/1*Lw5wuKjdEUq5-A56y_Cb_w.png" /><figcaption>✨ &lt;Button/&gt; with border-radius value of rounded-full (uppercase and bold text) =&gt; theme2✨</figcaption></figure><p>Multiple classes, multiple flavours 🌈 🍦🍦🍦</p><h3>3) Applying a theme to a site:</h3><p>So we have two files that fully define the style rules to achieve a specific “theme”:</p><ul><li>style-overrides.js which contains default Tailwind classes</li><li>tailwind.config.js which contains custom utility classes</li></ul><p>When we deploy a new instance of the front end application we specify the “theme” it should use as an environment variable and use dynamic import to require the appropriate files with a few dynamic imports:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2b6ece78ea0195c6bbba7bbae285f4c7/href">https://medium.com/media/2b6ece78ea0195c6bbba7bbae285f4c7/href</a></iframe><p>💡 Using Next.js we can easily<a href="https://nextjs.org/docs/basic-features/environment-variables#exposing-environment-variables-to-the-browser"> expose environment variables</a> to the browser.</p><h4>3.1) Injecting a theme prop</h4><p>Based on the value of the NEXT_PUBLIC_THEME environment variable we can inject different `theme` objects inside our components at import time by wrapping each components we export from our component library with an injectTheme function.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d1f7f1c360dcfbdc4409196e4236db3a/href">https://medium.com/media/d1f7f1c360dcfbdc4409196e4236db3a/href</a></iframe><p>The injectTheme function simply relies on dynamic import to apply the desired styleOverrides :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/285bcf6d3ecbcbf8c7ba17d72c303153/href">https://medium.com/media/285bcf6d3ecbcbf8c7ba17d72c303153/href</a></iframe><p>So every classes defined inside my theme1/style-overrides.js will be applied to my component via the “theme” prop effectively achieving conditional styling based on a “theme” required by the app.</p><h4>3.2) Applying a Tailwind configuration file</h4><p>We also have a “master” tailwind.config.js files in our frontend app which directly points out to the right config file defined in the component library:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ae7370dc09b4357bba3cf4197e164aa1/href">https://medium.com/media/ae7370dc09b4357bba3cf4197e164aa1/href</a></iframe><p>When building the application, Tailwind will automatically generate the CSS files needed to apply the styles defined in the imported tailwind.config.js . The same custom class in our component will point to different styles.</p><p>Let’s visualize what has been said so far:</p><ul><li>Each theme folder contains one tailwind.config.js file and one style-overried.js file with classes name and classes value specific the the theme it defines.</li><li>Our components are exported through one index.js file which injects a theme prop in each of our theme-able components.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4Jx-YJIVUviOo32M6tuTyQ.png" /><figcaption>Structure of the Component Library. (which could be a folder in the FE app if you don’t need an external component library)</figcaption></figure><ol><li>We define a component once, in <strong>one</strong> place (can be in app or in external repo if the use case is justified for your needs). This component uses custom utility classes which are defined differently in different tailwind config files.</li><li>For each theme we have a tailwind.config.js which defines custom classes differently (one class which associated with different values achieve different styles)</li><li>For each theme we create a style-overrides.js which contains Tailwind default classes to be applied to our components (different classes achieving different styles).</li><li>We specify a theme value in our frontend application environment variables: NEXT_PUBLIC_THEME=theme1</li><li>At build time, our frontend application which is configured has tailwind installed will generate the appropriates styles based (defined in tailwind.config.js ) and inject the appropriate theme prop (defined in style-overrides.js) in the components it imports.</li></ol><figure><img alt="full system view" src="https://cdn-images-1.medium.com/max/1024/1*Js01po21iKT8bxAj40z37w.png" /><figcaption>Full system view</figcaption></figure><p>And voilà 🪄 🎩 🐇: 👏</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vytKob2lrIJo6X-47URB3A.png" /><figcaption>If we set our environment variable “NEXT_PUBLIC_THEME=theme1” the app will have the theme1.</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TGGFhU7nISAsVUvnKidsMA.png" /><figcaption>If we set our environment variable “NEXT_PUBLIC_THEME=theme3” the app will have the theme3.</figcaption></figure><p>The system is so flexible that we could have multiple application picking and choosing components and a theme from out component library:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RO5gU6GNDnYTQfZS2pkuEQ.png" /><figcaption>Multiple white-label apps using themes defined in one central place.</figcaption></figure><h3>4) Conclusion 🎬</h3><h4>Drawbacks ⚖️ :</h4><ul><li>As you can imagine our theme files are way bigger than the small extract I have shared. Their length vary depending on how complex and specific the UI design is for a given theme but they are generally <strong>MASSIVE. </strong>Which means, the creation of a new theme to integrate with a new partner still requires intensive work driven by the Design team.</li><li>Also the dynamic imports that the system is heavily based on and the way we integrate Tailwind with Next.JS means we have to <a href="https://nextjs.org/docs/deployment#nextjs-build-api">re-build</a> our application every time we want to switch between themes which adds an extra step in the release process.</li><li>Currently, the complexity of the system and the size of theme files, are the biggest drawbacks we can honestly share: complex system means a lot of overhead for a new joiners to implement a new theme and lots of time spent implementing a new design (manual and error prone). Which can impact the speed at which we can integrate with new partners.</li></ul><h4>Advantages ✨:</h4><ul><li>The system is incredibly flexible: we can achieve every single design requirements, style variations to meet the demands of our partners without having to develop a different application.</li><li>It is predictable and reusable<strong>, </strong>the themes and components created are reusable across different Frontend apps and app instances, no matter how the application evolves.</li><li>The main advantage of the current system, something that we often overlook in our quest for improvement: it works! We have a system that allows us to develop theme-able white label applications, yes there is room for improvements but currently this system powers every single Frontend app instances for each white-label app we develop. 👊</li></ul><h4>Future thoughts 🔮</h4><p>🎬 As I write these lines, our Component Library is undergoing a massive update, we are moving away from JavaScript and Tailwind CSS in favour of TypeScript and Styled Components with <a href="http://bit.dev/">Bit.dev</a> as a library manager which will allow huge simplification of the current system, theming granularity at a component level and at runtime! Stay tune for more tech updates from Koodoo. 🤘</p><p><em>Let’s give back to Caesar what is Caesar’s: I am not the creator of the system presented here, merely the user and messenger</em>. <em>A massive thank you to all the people at Koodoo who contributed and are contributing to building great things.</em></p><p>I hope that sharing our experience as a development team will help you and your team succeed whatever your mission might be. 🚀 🌕</p><p>Take Care and Stay Awesome ✌️!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=672163019731" width="1" height="1" alt=""><hr><p><a href="https://medium.com/koodoo/how-do-we-approach-theming-for-white-label-applications-672163019731">How do we approach theming for white-label applications?</a> was originally published in <a href="https://medium.com/koodoo">Koodoo</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[ Moving from vanillaCSS to TailwindCSS in an Express app using Pug templates ]]></title>
            <link>https://thomasdargon.medium.com/moving-from-vanillacss-to-tailwindcss-in-an-express-app-using-pug-templates-1dee445566eb?source=rss-8309eccc1d18------2</link>
            <guid isPermaLink="false">https://medium.com/p/1dee445566eb</guid>
            <category><![CDATA[pug]]></category>
            <category><![CDATA[express]]></category>
            <category><![CDATA[css]]></category>
            <category><![CDATA[tailwind]]></category>
            <category><![CDATA[tailwind-css]]></category>
            <dc:creator><![CDATA[Thomas Dargon]]></dc:creator>
            <pubDate>Wed, 14 Jul 2021 15:57:19 GMT</pubDate>
            <atom:updated>2021-07-26T08:41:44.135Z</atom:updated>
            <content:encoded><![CDATA[<p><strong><em>Disclaimer:</em></strong></p><p><em>If you are only just starting with CSS and are tempted to take a shortcut with CSS frameworks, my personal opinion is: </em><strong><em>take your time.</em></strong><em> Styling is a BIG part of web app development and taking the time to learn and experiment with vanilla CSS will not only make you a more confident web developer but also save time in the future. It can seem overwhelming at first due to all the “rules” you’ve got to learn and you will have to do a lot of reading but it will payoff 💯 %.</em></p><p><em>I think it is easier / more enjoyable to start playing with a CSS framework like Tailwind once you have a good level of understanding and confidence with CSS.</em></p><p><em>As always one of the best place to learn web development technologies is:🔥🦊 </em><a href="https://developer.mozilla.org/en-US/docs/Learn/CSS"><em>https://developer.mozilla.org/en-US/docs/Learn/CSS</em></a><em> .</em></p><h3>Introduction</h3><p>I recently had an impulse through work to start working with a CSS framework: <strong>TailwindCSS.<br></strong> I have been writing <strong>vanilla CSS</strong> for a while and to be honest I quite like it. It is a very pleasant activity which in my opinion uses both the programming and graphic design areas of one’s brain. So I would often take a few days to fully focus on the styling of an app, this would give me the time to dig into the implementation details of a design, experiment with fonts / colors / responsive layouts. Sometimes getting stuck and spending 3 hours or more just switching between different colors or padding values. (We have all been there..)</p><p>This is fine if you like it and for small scale projects where one or maybe two devs are responsible for the whole project 👍. Now, in a bigger company setup where you typically have a separate design department the implementation work won’t be as straightforward. 📋</p><p>Especially when you consider an app which will be developed and maintained by different people through many years / version implementations — using custom CSS utility classes written and implemented by different devs at different time will create lots of frictions.</p><p>If on top of that you now consider a company which builds not only one app but a bunch of apps then it becomes even harder to follow and maintain. 🤯</p><p>You have two options: build a set of company-wide utility classes (building your own CSS framework) 🚀 or use a publicly available CSS framework ❤️.</p><p>Using a publicly available CSS framework will save some time but it would be great if that CSS framework was fully customisable so you could add your own color palettes, custom fonts and implements all those lovely designs from your Design department ? 👨‍🎨</p><p>Tailwind offers just that: 🖼 <a href="https://tailwindcss.com/">https://tailwindcss.com</a></p><p><strong><em>“Because Tailwind is a framework for building bespoke user interfaces, it has been designed from the ground up with customization in mind.”</em></strong></p><p><strong><em>“ Extend it, tweak it, change it.”</em></strong></p><p><strong><em>“Tailwind includes an expertly crafted set of defaults out-of-the-box, but literally everything can be customized — from the color palette to the spacing scale to the box shadows to the mouse cursor. “</em></strong></p><p>That sounds awesome ! I was quite convinced till my eyes fell upon that scary statement: <strong><em>“Rapidly build modern websites without ever leaving your HTML.”</em></strong> What ? No more CSS files… How come ? I love CSS files and I love writing them, they allow you to separate the style from the substance of you app, I don’t want to have cluttered HTML files… Right ?</p><p>But maybe I am being a bit change adverse and stuck in my ways, I have been working with vanilla CSS for a while, it is time to try something new!</p><p><strong>Let’s dive in!</strong></p><p>I gave myself the following exercise:</p><p>Converting an existing App built with vanilla CSS (and custom CSS utility classes) to only using Tailwind for styling.</p><p>The starting point for the project can be found here: <a href="https://github.com/argonathmos/joyeux">https://github.com/argonathmos/joyeux</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MeXetEd3cs7M3kp1TXGToA.png" /><figcaption><em>“Joyeux”: A simple Express app using Pug templates. 🦦</em></figcaption></figure><p>👾 <a href="https://expressjs.com/">https://expressjs.com/</a></p><p>🐶 <a href="https://pugjs.org/api/getting-started.html">https://pugjs.org/api/getting-started.html</a></p><h3>Part 1 — Install TailwindCSS</h3><p>Our goal for part one is to install Tailwind in our app and go through the steps to make it usable directly in our Pug templates.</p><p>Simply clone the git repo and follow the steps detailed below to get started.</p><h3>1.a — Install Tailwind via npm (without PostCSS and autoprefixer):</h3><pre>npm install -D tailwindcss@latest</pre><p>Output :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/406/1*LUsT8weFpthAUUiMx8aP1g.png" /></figure><h3>1.b — Manually build your CSS file:</h3><pre>npx tailwindcss-cli@latest build -o public/styles/tailwind.css</pre><p>(This will create the file tailwind.css — you are free to name it however you want).</p><p>Output :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/482/1*8On3WznxpQGYYMjfbtiYZg.png" /></figure><h3>1.c — Replace stylish.css by tailwind.css in the index.pug file:</h3><pre>link(rel=”stylesheet” type=”text/css” href=”styles/stylish.css”)</pre><p>Now becomes:</p><pre>link(rel=”stylesheet” type=”text/css” href=”styles/tailwind.css”)</pre><p>(This is the stylesheet that the app will use)</p><h3>1.d — Run the app locally</h3><pre>npm run dev</pre><p>It should now look pretty basic as only the default tailwind styles (preflight) are applied — <a href="https://tailwindcss.com/docs/preflight">https://tailwindcss.com/docs/preflight</a></p><p>The stylish.css file is not loaded so the CSS classes in the pug file are not applied anymore (we can delete them from the index.pug )</p><p>👉 Don’t delete the stylish.css file yet, we will keep going back to our previously defined styles in order to translate them to Tailwind’s classes.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AicfkpdESegt_Dt3AOn80g.png" /></figure><p>👉 If you want to start from step 1.d, use the code from this starting point: <a href="https://github.com/argonathmos/joyeux/commit/76d965972ab9fc972b8fade3eaf4d1d81b1cef51">https://github.com/argonathmos/joyeux/commit/76d965972ab9fc972b8fade3eaf4d1d81b1cef51</a></p><p>It includes the tailwind install, the generated tailwind.css file and index.pug cleaned up.</p><h3>Part 2 — Adding styles to HTML attributes via Classes</h3><p><em>Our goal is to match our previous 🍦 vanillaCSS styling using Tailwind’s default utility classes only.</em></p><p>At this step we will “merge” stylish.css into index.pug , going through each properties in the CSS file and adding them as classes onto our html element in the pug file.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wLF70IS5eit_mU8uetgNcA.png" /></figure><p>But how do I know the class names Tailwind uses ? There is so many ? You are right there is no point learning them. The official website is very well made and make it simple to find classnames by searching for a regular CSS properties. You will find what you are looking for plus a short and insightful description.</p><p>So we will simply search for classes matching our CSS properties in stylish.css and apply them to our HMTL elements in index.pug in order to style the app.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Z8aviNR9gdxiGgFVd7ieCQ.png" /><figcaption><em>Tailwind’s splendid website: </em><a href="https://tailwindcss.com/"><em>https://tailwindcss.com</em></a></figcaption></figure><p>Properties applied to the body are now becoming classes onto the body element.</p><pre>/* stylish.css */</pre><pre>body {</pre><pre>  font-family: monospace;</pre><pre>  font-size: 15px;</pre><pre>  color: rgb(205, 217, 229);</pre><pre>  background-color: rgb(34, 39, 46);</pre><pre>  width: 75%;</pre><pre>  max-width: 640px;</pre><pre>  margin: 10px auto;</pre><pre>  display: flex;</pre><pre>  flex-direction: column;</pre><pre>  justify-content: center;</pre><pre>}</pre><p>Our previous styles applied via CSS properties inside a CSS file become a class attribute value on and HTML element in our Pug template.</p><pre>//- index.pug</pre><pre>body(class=”font-mono text-base text-gray-50 bg-gray-800 w-3/4 max-w-screen-sm my-10 mx-auto flex flex-col justify-center”)</pre><p>text-gray-50 is not exactly matching {color:rgb(205, 217; 229);} but is close enough for now.</p><p>max-w-screen-smmatches exactly our css property{max-width: 640px;}</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/658/1*BgqwHdcmwJJCIIBzk-7_BA.png" /><figcaption><em>Search results for max-width property — </em><a href="https://tailwindcss.com/docs/max-width"><em>https://tailwindcss.com/docs/max-width</em></a></figcaption></figure><p>Refresh the browser, the app should look like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ea6IH7kvjYYSTDDIt1dIAg.png" /><figcaption><em>UI after styling the body element only</em></figcaption></figure><p>We are getting close to our our <a href="https://github.com/argonathmos/joyeux/blob/vanillaCSS/public/styles/stylish.css">original vanillaCSS file</a> 💪, let’s keep adding Tailwind utility classes to our HTML elements to get as close as possible to</p><p>You can stop at any point, if you have gained enough understanding of the process or can take a few shortcuts by either:</p><ul><li>Using pre-existing combination of classes from Tailwind components: <a href="https://v1.tailwindcss.com/components/buttons#outline">https://v1.tailwindcss.com/components/buttons#outline</a> 👍</li><li>Pulling the code from this commit to compare your code to mine after this step: <a href="https://github.com/argonathmos/joyeux/commit/dff90dd591b533c0ad99a0ce1a443a325c010de4">https://github.com/argonathmos/joyeux/commit/dff90dd591b533c0ad99a0ce1a443a325c010de4</a></li></ul><h3>Part 3 (Optional) — Customising Tailwind:</h3><p>Now we have moved from styles implemented through vanilla CSS (using our own utility classes) to styles implemented using Tailwind’s default utility classes.</p><p>The eagle eyed 🦅 reader will have noticed that some of the default styles provided by Tailwind don’t exactly match our previous styles from stylish.css .</p><p>We will now have a look at how to tweak Tailwind’s config files to implement our own custom styles.</p><h3>3.a — Add webfonts</h3><p>At the moment we are using Tailwind’s class font-mono instead of our CSS property: body{font-family:monospace} . I personally can’t tell the difference between 🧐 the two but let’s pretend we want to use another font: <a href="https://fonts.google.com/specimen/Space+Mono?query=Mono">Space Mono</a> by <a href="http://colophon-foundry.org/">colophon-foundry.org</a> ✒️👌.</p><p><strong>i — The first step is to load the web font into the head section of the pug template:</strong></p><pre>link(href=”https://fonts.gstatic.com&quot; rel=”preconnect”)</pre><pre>link(href=”https://fonts.googleapis.com/css2?family=Space+Mono&amp;display=swap&quot; rel=”stylesheet”)</pre><p><strong>ii — Then create a tailwind.config.js file and replace the default font utility classes with our own:</strong></p><pre>module.exports = {</pre><pre>  theme: {</pre><pre>    fontFamily: {</pre><pre>      thatCoolColophonFont: [‘“Space Mono”’, “monospace”],</pre><pre>    },</pre><pre>  },</pre><pre>};</pre><pre>=&gt; ❤️ Ensure fonts with spaces are wrapped by <strong>“</strong> <strong>“</strong> .</pre><p><strong>iii — Rebuild our CSS file to overwrite tailwind default fontFamily utility classes:</strong></p><pre>npx tailwindcss-cli@latest build -o public/styles/tailwind.css</pre><p>It will generate the following lines in our tailwind.css file:</p><pre>.font-thatCoolColophonFont {</pre><pre>  font-family: “Space Mono”, monospace;</pre><pre>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/989/1*9u6ULfsqjzlNbRvIxh5Nhg.png" /><figcaption><em>New utility class generated in our CSS file from tailwind.config.js</em></figcaption></figure><p><strong>iv — Update index.pug to use our new utility class: font-thatCoolColophonFont</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jcKoI2wo7b2Q8xyV2GezEQ.png" /></figure><p><strong>v — Restart the server npm run dev and reload the page:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*plrN63kzpLoKiWmTUW8yYA.png" /><figcaption><em>Taadaa 🐇 🎩 !! The UI is now using “Space Mono” which will default to monospace if the browser can’t load it.</em></figcaption></figure><h3>3.b — Add custom colors</h3><p>We had 6 different colors in our stylish.css file which we moved away from to use exclusively colors from Tailwind’s default color palette. The default palette is very rich, great for rapid prototyping but what if you would like to use a custome color palette / scheme you took ages to come up with ? 🤔</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wXoZXoRknfxPEGLzuAT1tQ.png" /></figure><h3>By default, Tailwind color classes point to a default color palette:</h3><p>gray-50 points to #F9FAFB</p><p>gray-800 points to #1F2937</p><p>pink-600 points to #DB2777 etc…</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pyCUiJMEOittr9VkXuDhyA.png" /><figcaption><em>Full list here: </em><a href="https://tailwindcss.com/docs/customizing-colors"><em>https://tailwindcss.com/docs/customizing-colors</em></a></figcaption></figure><p>To change this and use our own colors, we need to add to tailwind.config.js file again to override the default color palette.</p><p>Our stylish.css file, we have used a mix of rgb() functional notation, css named color notation and RGB hex notation-&gt; 🤫 … let’s clean up and convert them all to RGB Hex notation. 🧹</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7OHqs8K_uD355tjh7ubjqQ.png" /></figure><p><strong>i — Add `colors`field to tailwind.config.js to override the default color utility classes with our own color palette.</strong></p><p><a href="https://tailwindcss.com/docs/customizing-colors#custom-colors">https://tailwindcss.com/docs/customizing-colors#custom-colors</a></p><pre>module.exports = {</pre><pre> theme: {</pre><pre>  fontFamily:{…},</pre><pre>  colors: {</pre><pre>    gray: {</pre><pre>      50: “#cdd9e5”,</pre><pre>      600: “#495057”,</pre><pre>      800: “#22272e”,</pre><pre>    },</pre><pre>    blue: {</pre><pre>      400: “#539bf5”,</pre><pre>    },</pre><pre>    pink: {</pre><pre>      600: “#DB277”,</pre><pre>    },</pre><pre>    transparent: “transparent”,</pre><pre>    white: “#fff”,</pre><pre>  },</pre><pre>},</pre><pre>};</pre><p>=&gt; ❤️ We still have to define <strong>white</strong> and <strong>transparent</strong> if we want to use them as the config file will override all the tailwind predefined colors.</p><p><strong>ii — Rebuild or css style to override Tailwind default colors with our own colors:</strong></p><pre>npx tailwindcss-cli@latest build -o public/styles/tailwind.css</pre><p><strong>iii — Restart the server and reload the page:</strong></p><p>Our own colors are now being applied, without needing to update our classes in index.pug as the color classes now points to our own values. 👏👏</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GvWvkG6klYakGx6KEYL8Kg.png" /><figcaption><em>blue-400 now points to our own custom color #539bf5 -&gt; rgb(83, 155, 245) 💙🫐</em></figcaption></figure><h3>3.c — Add custom utility class (optional)</h3><p>If you followed carefully 🦉 you might have noticed that we had the property {height: 40vh;} for our form element defined in stylish.css but I have used h-96 Tailwind’s class on that element as Tailwind doesn’t offer the equivalent in its pre-defined utility classes.</p><p>At the time of writing Tailwind only offer h-screen class which will make the height of the element: 100vh.</p><pre>&lt;div class=”h-screen”&gt;&lt;/div&gt;</pre><p>⬇️ ⬇️ ⬇️</p><pre>div {</pre><pre>  height: 100vh;</pre><pre>}</pre><p><a href="https://tailwindcss.com/docs/height#screen-height"><em>https://tailwindcss.com/docs/height#screen-height</em></a></p><p>There might be a good reason behind it: <a href="https://stackoverflow.com/questions/21624014/css-are-view-height-vh-and-view-width-vw-units-widely-supported">https://stackoverflow.com/questions/21624014/css-are-view-height-vh-and-view-width-vw-units-widely-supported</a>. 🤷‍♂️</p><p>But let’s say I really want to add a height of 40 viewport-height to my form element, what are my options 🤓 ?</p><p>I could simply add a style attribute to my form element in the pug template (CSS inline styles) which will take precedence and override the h-96 class but that would mean mixing inline styling with Tailwind classes… I would like to stick to one approach.</p><p>Let’s create our own utility class: <a href="https://tailwindcss.com/docs/adding-new-utilities#using-css">https://tailwindcss.com/docs/adding-new-utilities#using-css</a> 🪄</p><p><strong>i — Create a custom.css file in the folder of your choice (mine is /build at the root of the project)</strong></p><pre>// custom.css</pre><pre>@tailwind base;</pre><pre>@tailwind components;</pre><pre>@tailwind utilities;</pre><pre>@layer utilities {</pre><pre> .h-screen-40 {</pre><pre>   height: 40vh;</pre><pre> }</pre><pre>}</pre><p><strong>ii — Manually build our tailwind.css file taking into account our custom.css file:</strong></p><pre>npx tailwindcss-cli@latest build build/custom.css -o public/styles/tailwind.css</pre><p>The output of the command is</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/420/1*PRA-6JgWbK5jutHBSn2bsg.png" /></figure><p><strong>iii — Add our custom `h-screen-40` class to our form element in the pug template.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*UsPEzxOGYQHwUD7OYorLcw.png" /><figcaption><em>Our custom utility class `h-screen-40` is applied</em></figcaption></figure><h3>Part 4 (recommended) — Purge the generated CSS file</h3><p>Removing unused CSS styles from our tailwind.css file — <a href="https://tailwindcss.com/docs/optimizing-for-production#basic-usage">https://tailwindcss.com/docs/optimizing-for-production#basic-usage</a></p><p>By default Tailwind will generate a file containing all the CSS code their default utility classes will point to, but in a single project we use only a fraction those styles made available to us. Tailwind offers a way to remove this unnecessary styling properties from the generated style files.</p><h3>4.a — Add the purge key to our tailwind.config.js file, and make sure to target all the files in which we are using tailwind classes (here only index.pug):</h3><pre>// tailwind.config.js</pre><pre>module.exports = {</pre><pre>  purge: [</pre><pre>    ‘./views/*.pug’,</pre><pre>  ],</pre><pre>  theme: { … },</pre><pre>}</pre><h3>4.b Manually build the tailwind.css file by running:</h3><pre>NODE_ENV=production npx tailwindcss-cli@latest build -o public/styles/tailwind.css</pre><p>By setting NODE_ENV=production, Tailwind will automatically “purge” the generated CSS.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/968/1*_Vv6AU8Hy18VmRIj_iTf7w.png" /><figcaption><em>The generated CSS file contains only the necessary styles and is way smaller.</em></figcaption></figure><p><strong>If you followed the step 3.c, you will have to run:</strong></p><pre>NODE_ENV=production npx tailwindcss-cli@latest build build/custom.css -o public/styles/tailwind.css</pre><p>To include our custom utility class.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/976/1*B9y-K4yB3QcmFMi8XwNNdA.png" /><figcaption><em>Generating the small CSS file containing our utility classes defined in build/custom.css</em></figcaption></figure><h3>Conclusion</h3><p>We moved from vanilla CSS with pre-defiend custom CSS utility classes such as (.btn .form-control-field .form-control-label) in our <a href="https://github.com/argonathmos/joyeux/blob/vanillaCSS/public/styles/stylish.css">stylish.css</a> filed to using Tailwind’s utility classes directly in our Pug template, we didn’t need to write any CSS code in order to style our app (apart from adding custom configurations), though knowing CSS makes the process way easier as we can browse Tailwind’s website through CSS properties.</p><p>At this stage, I have no opinion on which approach is better, using Tailwind in a project could implement a sort of de facto standard for styling. So a big plus ✌️ if you work within a team and will help with maintenance in the future (the class names will stay the same and mean the same to everyone who knows Tailwind / can search the docs), plus you don’t have to write CSS files (we ended up adding a few classes to your HTML elements instead).</p><p>On the other hand, if you are working on a smaller project or if you want to create your own standard (and you like vanilla CSS and want to stick with it) by all means go for it 🏎 🏁 and create your own utility classes.</p><p>Regarding this particular exercise facts are: I got rid of my stylish.css file (replaced by the automatically generated tailwind.css file), HTML elements and their associated styles are all in one place: index.pug.</p><p>We also ended up with a “bloated” index.pug file with lots of CSS classes duplication: the classes in &lt;p&gt; elements, &lt;input&gt; elements and &lt;label&gt; elements needs to be written on each element. (This could be avoided by creating “Pug component” or “template partial” files and referencing them from the index.pug file <a href="https://pugjs.org/language/inheritance.html">https://pugjs.org/language/inheritance.html</a> . But this will create extra work especially for a small project like this one).</p><p>I believe Tailwind is a great tool that offers lots of power and flexibility if you decide to implement it to force consistency between your projects.</p><p>In my opinion, using Tailwind would be even more interesting when building SPAs with <strong>components based frameworks </strong>such as <a href="https://vuejs.org/"><em>Vue</em></a> or <a href="https://reactjs.org/"><em>React</em></a> as you won’t have to worry about duplicating classes to style element as your App’s UI would likely be split in components already. Bonus: you won’t have to worry about styles scope anymore as using tailwind classes from within your component makes the styles de facto scoped to that component ! 👑 👑</p><p>Will try this in a future article: update a ⚛️ React App where style is implemented via “CSS modules” to use TailwindCSS.</p><p>✌️ Stay Awesome 😘</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1dee445566eb" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>