<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Cheaven</title>
    <description>The latest articles on DEV Community by Cheaven (@cheaven88).</description>
    <link>https://dev.to/cheaven88</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3807159%2F8a63d3be-1657-4c50-8281-9b4c0088b2dd.png</url>
      <title>DEV Community: Cheaven</title>
      <link>https://dev.to/cheaven88</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/cheaven88"/>
    <language>en</language>
    <item>
      <title>Day 1 of Building a build-shop:Understanding the cn() Utility Function</title>
      <dc:creator>Cheaven</dc:creator>
      <pubDate>Thu, 05 Mar 2026 06:31:50 +0000</pubDate>
      <link>https://dev.to/cheaven88/day-1-of-building-a-build-shopunderstanding-the-cn-utility-function-1328</link>
      <guid>https://dev.to/cheaven88/day-1-of-building-a-build-shopunderstanding-the-cn-utility-function-1328</guid>
      <description>&lt;h1&gt;
  
  
  Day 1 – Learning a Utility Function for Tailwind Classes
&lt;/h1&gt;

&lt;p&gt;Today I started working on the &lt;strong&gt;Build Shop project&lt;/strong&gt;, full-stack web marketplace platform designed for the construction industry&lt;/p&gt;

&lt;p&gt;Project repository:&lt;br&gt;&lt;br&gt;
&lt;a href="https://github.com/cheaven8/build-shop" rel="noopener noreferrer"&gt;https://github.com/cheaven8/build-shop&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;While exploring the project, I came across a small utility function that helps manage CSS classes when using Tailwind. At first it looked simple, but it actually solves a common problem in frontend development.&lt;/p&gt;

&lt;p&gt;Here is the code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;clsx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;ClassValue&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;clsx&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;twMerge&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;tailwind-merge&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;cn&lt;/span&gt;&lt;span class="p"&gt;(...&lt;/span&gt;&lt;span class="nx"&gt;inputs&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;ClassValue&lt;/span&gt;&lt;span class="p"&gt;[])&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;twMerge&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;clsx&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;inputs&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  What Problem Does This Solve?
&lt;/h2&gt;

&lt;p&gt;When building UI components, especially with Tailwind CSS, we often need to combine multiple class names dynamically.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Some classes depend on conditions&lt;/li&gt;
&lt;li&gt;Some come from component props&lt;/li&gt;
&lt;li&gt;Some might override others&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If we just concatenate strings, the code becomes messy and sometimes conflicting Tailwind classes appear.&lt;/p&gt;

&lt;p&gt;Example problem:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"p-2 p-4 text-red-500"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here both &lt;code&gt;p-2&lt;/code&gt; and &lt;code&gt;p-4&lt;/code&gt; exist, but Tailwind should only apply the last one.&lt;/p&gt;

&lt;p&gt;This is where &lt;code&gt;clsx&lt;/code&gt; and &lt;code&gt;tailwind-merge&lt;/code&gt; help.&lt;/p&gt;

&lt;h2&gt;
  
  
  Understanding Each Part
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. clsx
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;clsx&lt;/code&gt; is a small utility for conditionally joining class names.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nf"&gt;clsx&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;btn&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;active&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;hidden&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Result:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"btn active"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It ignores false values and merges valid class names.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. tailwind-merge
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;tailwind-merge&lt;/code&gt; solves Tailwind conflicts.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;twMerge("p-2 p-4")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Result:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"p-4"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It removes conflicting Tailwind classes and keeps the correct one.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. The cn() Function
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;cn&lt;/code&gt; function combines both tools:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;clsx()&lt;/code&gt; merges class names
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;twMerge()&lt;/code&gt; resolves Tailwind conflicts&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nf"&gt;cn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;p-2&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;p-4&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;text-red-500&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Result:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"p-4 text-red-500"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This makes it perfect for React or component-based projects.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why This Pattern Is Popular
&lt;/h2&gt;

&lt;p&gt;Many modern React projects use this helper because it:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Keeps components clean&lt;/li&gt;
&lt;li&gt;Prevents Tailwind class conflicts&lt;/li&gt;
&lt;li&gt;Makes conditional styling easier&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You will often see this function in projects built with &lt;strong&gt;Next.js&lt;/strong&gt;, &lt;strong&gt;ShadCN UI&lt;/strong&gt;, or modern Tailwind setups.&lt;/p&gt;

&lt;h2&gt;
  
  
  What I Learned Today
&lt;/h2&gt;

&lt;p&gt;From this small utility function, I learned:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;How to manage dynamic class names&lt;/li&gt;
&lt;li&gt;Why Tailwind classes sometimes conflict&lt;/li&gt;
&lt;li&gt;How libraries like &lt;code&gt;clsx&lt;/code&gt; and &lt;code&gt;tailwind-merge&lt;/code&gt; simplify UI development&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Even though the function is only a few lines long, it improves developer experience a lot.&lt;/p&gt;

&lt;p&gt;Small utilities like this make large projects easier to maintain.&lt;/p&gt;

&lt;p&gt;Looking forward to learning more tomorrow.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>react</category>
      <category>tailwindcss</category>
      <category>frontend</category>
    </item>
  </channel>
</rss>
