<?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: Denis Severin</title>
    <description>The latest articles on DEV Community by Denis Severin (@n1xus).</description>
    <link>https://dev.to/n1xus</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%2F1006831%2F1ab28c2e-229e-458e-9a99-72978865fd7e.png</url>
      <title>DEV Community: Denis Severin</title>
      <link>https://dev.to/n1xus</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/n1xus"/>
    <language>en</language>
    <item>
      <title>Leveraging @angular 15 host directives</title>
      <dc:creator>Denis Severin</dc:creator>
      <pubDate>Thu, 26 Jan 2023 10:27:33 +0000</pubDate>
      <link>https://dev.to/valorsoftware/leveraging-angular-15-host-directives-52en</link>
      <guid>https://dev.to/valorsoftware/leveraging-angular-15-host-directives-52en</guid>
      <description>&lt;p&gt;Not while ago, Angular team released a stable 15 version with few neat features, such as host directives.&lt;br&gt;
In this article I will try to explain how to leverage the directive composition approach and move from old class-inheritance to a composition approach.&lt;/p&gt;
&lt;h2&gt;
  
  
  What are host directives?
&lt;/h2&gt;

&lt;p&gt;Host directives are a &lt;strong&gt;standalone&lt;/strong&gt; directives that can be added to a component via &lt;code&gt;@Component&lt;/code&gt; decorator, thus avoiding the need to apply the directive through the markup. Developers can expose its inputs and outputs. Additionally, they can also map their names to avoid the confusion between components and directives properties.&lt;/p&gt;
&lt;h2&gt;
  
  
  Why would you want to use host directives?
&lt;/h2&gt;

&lt;p&gt;With a complex component comes complex business logic inside its class. Typescript has &lt;a href="https://www.typescriptlang.org/docs/handbook/mixins.html" rel="noopener noreferrer"&gt;mixins&lt;/a&gt; support to divide logic between multiple classes and then join it into one giant class.&lt;/p&gt;

&lt;p&gt;Mixins are widely used in @angular/material project. For example, &lt;a href="https://github.com/angular/components/blob/9f0071dc4c4848ea440907e016a12a5cba48a1a9/src/material/button/button-base.ts#L81" rel="noopener noreferrer"&gt;Material Button Component&lt;/a&gt;.&lt;br&gt;
But, as you can see, it itself requires complex structures to actually use it. Not mentioning setting inputs/outputs properties as an array for the decorator itself.&lt;br&gt;
In short, developers can start struggling with input/output properties and class dependencies if they use lots of mixins.&lt;/p&gt;

&lt;p&gt;Another way is to use long chains of &lt;strong&gt;class inheritance&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;In the end, the final component would have a huge constructor (prior to &lt;code&gt;inject&lt;/code&gt; function) and supporting this constructor sometimes becomes too painful.&lt;/p&gt;

&lt;p&gt;Another way would be to use &lt;strong&gt;services&lt;/strong&gt; injected into your component, but this creates an additional headache with keeping the config up-to-date (triggering some configuration update for the service when some component's input property was changed, etc.).&lt;/p&gt;

&lt;p&gt;Directive composition approach works similarly to the Typescript's mixins: you have multiple classes that contain their own logic, and in the end they all end up used for one final class (component). The difference is that mixins are combined into one class, and for directive composition you need to inject your directive instances into your component class.&lt;/p&gt;
&lt;h2&gt;
  
  
  Leveraging the host directives in your application
&lt;/h2&gt;

&lt;p&gt;First, I'll &lt;a href="https://github.com/N1XUS/host-directives-app" rel="noopener noreferrer"&gt;leave a link to an example app&lt;/a&gt; built with nx to split the app and its libs.&lt;/p&gt;

&lt;p&gt;Let's take an example of simple form control component:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;This component implements &lt;code&gt;ControlValueAccessor&lt;/code&gt; and implements its methods such as &lt;code&gt;writeValue&lt;/code&gt;, &lt;code&gt;registerOnChange&lt;/code&gt;, &lt;code&gt;registerOnTouched&lt;/code&gt;.&lt;br&gt;
And this stuff is commonly repeated across multiple components in your app.&lt;br&gt;
Previously, to simplify the logic you could extract those methods into base abstract class. But this class might require it's dependencies, which needed to be passed via &lt;code&gt;super&lt;/code&gt; call in constructor. This complicates things.&lt;/p&gt;

&lt;p&gt;Let's simplify the code, and first, create a standalone directive called &lt;code&gt;CvaDirective&lt;/code&gt;.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Let me explain what's going on inside this directive.&lt;br&gt;
First, we are declaring it as a standalone, which means we can apply it to a component via &lt;code&gt;hostDirectives&lt;/code&gt; property of the &lt;code&gt;@Component&lt;/code&gt;.&lt;br&gt;
Next, since we need to support template-driven and reactive forms, lets' inject necessary dependencies such as &lt;code&gt;NgControl&lt;/code&gt;, &lt;code&gt;NgForm&lt;/code&gt; and &lt;code&gt;ControlContainer&lt;/code&gt;. We will need these properties later.&lt;/p&gt;

&lt;p&gt;You may see that we also injected &lt;code&gt;ChangeDetectorRef&lt;/code&gt; from the &lt;code&gt;host&lt;/code&gt;. This is needed to get the components change detector and call it when the state of the control being changed (valid/invalid).&lt;/p&gt;

&lt;p&gt;Next, we implement all members of &lt;code&gt;ControlValueAccessor&lt;/code&gt; interface for further usage in the component.&lt;br&gt;
We also have support for a disabled state of the form control, which may be handy in real-case scenarios. This input property is optional and you can ignore it during input exposing.&lt;/p&gt;

&lt;p&gt;We also have &lt;code&gt;updateErrorState&lt;/code&gt; method which automatically checks whether the control is valid and checks whether the user interacted with the control itself or submitted the form.&lt;/p&gt;

&lt;p&gt;That's all for the directive itself, now let's update our combobox component to use this directive instead direct &lt;code&gt;ControlValueAccessor&lt;/code&gt; implementation:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;So, we've removed all form-related stuff to the directive. This gives us a more clear and readable component.&lt;/p&gt;

&lt;p&gt;You see that we've injected &lt;code&gt;CvaDirective&lt;/code&gt; into the component to call its members such as &lt;code&gt;setValue&lt;/code&gt; and use the initial value of the form control to set it for the input field.&lt;/p&gt;

&lt;h2&gt;
  
  
  Bonus: Advanced Directives composition
&lt;/h2&gt;

&lt;p&gt;With the example above I've shown how to simplify your component and move all background logic into a separate class without the need of inheritance.&lt;/p&gt;

&lt;p&gt;Now, let's say we want it to accept not only &lt;code&gt;string[]&lt;/code&gt;, but also &lt;code&gt;Observable&amp;lt;string[]&amp;gt;&lt;/code&gt;, or even custom data source class which retrieves the data from some backend.&lt;/p&gt;

&lt;p&gt;And again, host directives to the rescue!&lt;/p&gt;

&lt;p&gt;Before we start with the directive itself, let's define what our directive should support:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Automatically subscribe/unsubscribe from the dataSource;&lt;/li&gt;
&lt;li&gt;When datasource's data changes, or new dataSource instance being passed, notify parent component of the changes in data;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In this example, we will create a simple data source class which will convert passed data into an observable and simply return it.&lt;/p&gt;

&lt;p&gt;First, let's generate abstract data source provider class which our components would implement in own way:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;So, here we're declaring that our &lt;code&gt;DataSource&lt;/code&gt; can accept three types of data: Class instance, Observable of an array and a plain array.&lt;/p&gt;

&lt;p&gt;With &lt;code&gt;AbstractDataSourceProvider&lt;/code&gt; we can now actually create our directive called &lt;code&gt;DataSourceDirective&lt;/code&gt;:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
&lt;br&gt;
Quick explanation of what's going on here:&lt;br&gt;
We have &lt;code&gt;T&lt;/code&gt; and &lt;code&gt;P&lt;/code&gt; generic types which are responsible for providing awareness of the data types we are working with in our components, so IDE also knows it, and provides suggestions.

&lt;p&gt;Next, we have a &lt;code&gt;dataSource&lt;/code&gt; input property which accepts our &lt;code&gt;DataSource&lt;/code&gt; type.&lt;/p&gt;

&lt;p&gt;When the data is set, we call &lt;code&gt;_initializeDataSource&lt;/code&gt; method which does couple of things:&lt;br&gt;
First, it closes the stream of the previous data source.&lt;br&gt;
Then, it transforms our data into acceptable data source provider with the help of our &lt;code&gt;DataSourceParser&lt;/code&gt; which is injected with a &lt;code&gt;DATA_SOURCE_TRANSFORMER&lt;/code&gt; injection token.&lt;br&gt;
Lastly, it subscribes to the events of the data source provider and passes them to the component it applied to.&lt;/p&gt;

&lt;p&gt;That's all for the directive itself and its dependencies.&lt;/p&gt;

&lt;p&gt;Now, let's go back to our combobox component, and update it in order to accept multiple types of data.&lt;/p&gt;

&lt;p&gt;First, we need to implement our &lt;code&gt;AbstractDataSourceProvider&lt;/code&gt; class:&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;As you can see, we defined an additional interface for the combobox item in case we want to render the label different than its value.&lt;br&gt;
And for the data source provider, we are just checking whether the data is observable or a plain array. If it's an array, we wrap it into Observable and return it.&lt;/p&gt;

&lt;p&gt;Additionally, we are implementing &lt;code&gt;DataSourceParser&lt;/code&gt; for combobox to be able to apply the necessary data source class for the data passed to it.&lt;/p&gt;

&lt;p&gt;Now, let's update our component to work with the data source directive:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;So, what's changed?&lt;/p&gt;

&lt;p&gt;First, we added our DataSourceDirective to hostDirectives and exposed its &lt;code&gt;dataSource&lt;/code&gt; input property as &lt;code&gt;options&lt;/code&gt; input property which we previously had directly in the component.&lt;/p&gt;

&lt;p&gt;Next, instead of relying directly on &lt;code&gt;options&lt;/code&gt; input property, we're subscribing to DataSourceDirective's &lt;code&gt;dataChanged$&lt;/code&gt; &lt;code&gt;BehaviourSubject&lt;/code&gt; and waiting for new data to come.&lt;br&gt;
When the data is emitted, we update the inner &lt;code&gt;options&lt;/code&gt; property with the data received from the DataSourceProvider.&lt;/p&gt;

&lt;p&gt;And that's pretty much it!&lt;/p&gt;

&lt;p&gt;In conclusion: Even though Host Directives at the early stage of its development, and has some childish issues, such as explicit definition of the host directive, it already provides huge benefit of simplifying the codebase of your existing components and libraries by splitting the logic between multiple independent classes and reducing the amount of inheritance chains.&lt;/p&gt;

&lt;p&gt;As I was mentioning at the beginning of the article, here's a complete &lt;a href="https://github.com/N1XUS/host-directives-app" rel="noopener noreferrer"&gt;example application&lt;/a&gt; used in this article.&lt;/p&gt;

</description>
      <category>web3</category>
      <category>security</category>
      <category>blockchain</category>
      <category>discuss</category>
    </item>
  </channel>
</rss>
