<?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 Joelvis on Medium]]></title>
        <description><![CDATA[Stories by Joelvis on Medium]]></description>
        <link>https://medium.com/@joep2198?source=rss-863c20bda25c------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*mzQNi-Lxc7FwpoNa6XBt_Q.jpeg</url>
            <title>Stories by Joelvis on Medium</title>
            <link>https://medium.com/@joep2198?source=rss-863c20bda25c------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 05 Dec 2026 06:09:23 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@joep2198/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[Enhancing Angular Styling with @HostBinding]]></title>
            <link>https://medium.com/@joep2198/enhancing-angular-styling-with-hostbinding-803f838a766e?source=rss-863c20bda25c------2</link>
            <guid isPermaLink="false">https://medium.com/p/803f838a766e</guid>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[styling]]></category>
            <category><![CDATA[tailwind-css]]></category>
            <category><![CDATA[tips]]></category>
            <category><![CDATA[improvement]]></category>
            <dc:creator><![CDATA[Joelvis]]></dc:creator>
            <pubDate>Thu, 07 Nov 2024 23:30:47 GMT</pubDate>
            <atom:updated>2024-11-07T23:58:58.377Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*08P2w78h5EVk7sNdvqhsmA.png" /></figure><p>In Angular, a popular way to apply dynamic styles based on component state is through @HostBinding. While effective, a standard approach to using @HostBinding for conditional styling can quickly become cluttered, and as projects grow, this clutter can lead to cognitive complexity. Let’s take a look at a traditional method for styling with @HostBinding before diving into a cleaner, more efficient way to handle conditional styles.</p><h3>The Standard @HostBinding Approach: Effective but Messy</h3><p>Typically, you might start with a @HostBinding decorator to dynamically set the CSS class for a component based on its state. Here’s an example:</p><pre>@HostBinding(&#39;class&#39;) get hostClass() {<br>  let classes = &#39;flex h-40 items-center p-16&#39;;<br>  <br>  // Conditional classes<br>  if (this.disabled()) {<br>    tclasses += &#39; bg-gray text-muted&#39;;<br>  } else {<br>    classes += &#39; bg-primary text-bold&#39;;<br>  }<br>  <br>  if (this.isActive()) {<br>    classes += &#39; border border-active&#39;;<br>  } else {<br>    classes += &#39; border-none&#39;;<br>  }<br>  <br>  return classes;<br>}</pre><p>At first glance, this seems fine. However, as the conditions multiply, this method can turn into a cluttered mess that’s hard to read and maintain. Each new condition requires adding or removing classes in a string format, which isn’t inherently intuitive for developers coming back to this code. Let’s break down some of the main issues with this approach:</p><ul><li><strong>String Concatenation</strong>: The constant concatenation of strings makes it cumbersome and error-prone.</li><li><strong>Limited Scalability</strong>: For each new conditional class, we add lines of code, increasing cognitive load.</li><li><strong>Reduced Readability</strong>: The @HostBinding decorator becomes harder to parse visually, especially for complex components.</li></ul><p>To tackle these issues, I’ve created a utility function called evalNgClass to streamline how classes are assigned.</p><h3>The Solution: evalNgClass Utility Function</h3><p>The evalNgClass function simplifies dynamic styling by allowing you to pass an object where keys are class names and values are booleans representing whether or not the class should be applied. Here’s the function:</p><pre>export function evalNgClass(<br>  classConfig: Record&lt;string, boolean&gt;,<br>): string {<br>  return Object.keys(classConfig)<br>    .reduce((acc, curr) =&gt; {<br>      if (classConfig[curr] === true) {<br>        acc.push(curr);<br>      }<br>      return acc;<br>    }, [] as string[])<br>    .join(&#39; &#39;);<br>}</pre><p>This utility function takes a configuration object (classConfig) where each key represents a class name, and each value is a boolean indicating if the class should be applied. It iterates over the keys of the classConfig object, collecting only those classes with true values. This eliminates the need for manual string concatenation and makes the code much cleaner.</p><h3>A Cleaner, More Readable @HostBinding</h3><p>Now, using evalNgClass, the @HostBinding decorator can be simplified to:</p><pre>@HostBinding(&#39;class&#39;) get hostClass() {<br>  return evalNgClass({<br>    &#39;flex h-40 items-center p-16&#39;: true,<br>    &#39;bg-gray text-muted&#39;: this.disabled(),<br>    &#39;bg-primary text-bold&#39;: !this.disabled(),<br>    &#39;border border-active&#39;: this.isActive(),<br>    &#39;border-none&#39;: !this.isActive(),<br>  });<br>}</pre><h3>Wrapping Up</h3><p>Styling in Angular doesn’t have to be difficult. By using a utility function like evalNgClass, we can streamline the @HostBinding decorator to make it both more readable and more maintainable. This approach helps developers keep their code clean and organized, letting them focus on functionality rather than wrestling with complex string concatenations.</p><p>Give evalNgClass a try in your next project to see how it can improve your Angular codebase!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=803f838a766e" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>