<?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: sabrymuhamad</title>
    <description>The latest articles on DEV Community by sabrymuhamad (@sabrymuhamad).</description>
    <link>https://dev.to/sabrymuhamad</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%2F1347930%2F7e87e3d0-9a01-4196-adef-597c5458cf59.jpeg</url>
      <title>DEV Community: sabrymuhamad</title>
      <link>https://dev.to/sabrymuhamad</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sabrymuhamad"/>
    <language>en</language>
    <item>
      <title>Customizable and re-usable embedded views using NgTemplateOutlet in Angular (Declarative way)</title>
      <dc:creator>sabrymuhamad</dc:creator>
      <pubDate>Fri, 12 Jul 2024 12:12:02 +0000</pubDate>
      <link>https://dev.to/sabrymuhamad/customizable-and-re-usable-embedded-views-using-ngtemplateoutlet-in-angular-declarative-way-1kg</link>
      <guid>https://dev.to/sabrymuhamad/customizable-and-re-usable-embedded-views-using-ngtemplateoutlet-in-angular-declarative-way-1kg</guid>
      <description>&lt;p&gt;There are different ways to customize the view of angular component, one of the most famous ways is using content projection "ng-content" &lt;br&gt;
but actually I find "ng-content" won't be very flexible for the upcoming example and I really doubt if it would work at all.&lt;/p&gt;

&lt;p&gt;Before we jump into a real life example I want to emphasis that we could use the "imperative" way to solve this example as well but I'm afraid that it would be more complicated&lt;br&gt;
than using &lt;code&gt;NgTemplateOutlet&lt;/code&gt; directive "declarative"...  &lt;/p&gt;

&lt;h3&gt;
  
  
  and now to our first example:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt; Using Angular material stepper I need to make a customizable stepper header in some cases so I want to conditionally show and hide the default header and this an easy part using
some CSS so I created a class called &lt;code&gt;hide-stepper-header&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;        &lt;span class="nc"&gt;.hide-stepper-header&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="err"&gt;.mat-horizontal-stepper-header-container&lt;/span&gt; &lt;span class="err"&gt;{&lt;/span&gt;
                &lt;span class="nl"&gt;display&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;none&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="err"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"tw-mt-10 tw-w-1/2 tw-mx-auto"&lt;/span&gt; &lt;span class="na"&gt;[ngClass]=&lt;/span&gt;&lt;span class="s"&gt;"{'hide-stepper-header': stepperHeader()}"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"tw-flex tw-mb-4"&lt;/span&gt; &lt;span class="na"&gt;*ngIf=&lt;/span&gt;&lt;span class="s"&gt;"stepperHeader()"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
                &lt;span class="nt"&gt;&amp;lt;ng-container&lt;/span&gt; &lt;span class="na"&gt;[ngTemplateOutlet]=&lt;/span&gt;&lt;span class="s"&gt;"stepperHeader()!"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/ng-container&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;

            &lt;span class="nt"&gt;&amp;lt;mat-stepper&lt;/span&gt; &lt;span class="na"&gt;#stepper&lt;/span&gt; &lt;span class="na"&gt;[(selectedIndex)]=&lt;/span&gt;&lt;span class="s"&gt;"selectedIndex"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
                &lt;span class="nt"&gt;&amp;lt;mat-step&amp;gt;&lt;/span&gt;
                    &lt;span class="nt"&gt;&amp;lt;ng-template&lt;/span&gt; &lt;span class="na"&gt;matStepLabel&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Fill out your name&lt;span class="nt"&gt;&amp;lt;/ng-template&amp;gt;&lt;/span&gt;
                    &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;Step 1 body&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
                    &lt;span class="nt"&gt;&amp;lt;div&amp;gt;&lt;/span&gt;
                        &lt;span class="nt"&gt;&amp;lt;button&lt;/span&gt; &lt;span class="na"&gt;mat-button&lt;/span&gt; &lt;span class="na"&gt;matStepperNext&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Next&lt;span class="nt"&gt;&amp;lt;/button&amp;gt;&lt;/span&gt;
                    &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
                &lt;span class="nt"&gt;&amp;lt;/mat-step&amp;gt;&lt;/span&gt;

                &lt;span class="nt"&gt;&amp;lt;mat-step&amp;gt;&lt;/span&gt;
                    &lt;span class="nt"&gt;&amp;lt;ng-template&lt;/span&gt; &lt;span class="na"&gt;matStepLabel&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Done&lt;span class="nt"&gt;&amp;lt;/ng-template&amp;gt;&lt;/span&gt;
                    &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;You are now done.&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
                    &lt;span class="nt"&gt;&amp;lt;div&amp;gt;&lt;/span&gt;
                        &lt;span class="nt"&gt;&amp;lt;button&lt;/span&gt; &lt;span class="na"&gt;mat-button&lt;/span&gt; &lt;span class="na"&gt;matStepperPrevious&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Back&lt;span class="nt"&gt;&amp;lt;/button&amp;gt;&lt;/span&gt;
                        &lt;span class="nt"&gt;&amp;lt;button&lt;/span&gt; &lt;span class="na"&gt;mat-button&lt;/span&gt; &lt;span class="na"&gt;(click)=&lt;/span&gt;&lt;span class="s"&gt;"stepper.reset()"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Reset&lt;span class="nt"&gt;&amp;lt;/button&amp;gt;&lt;/span&gt;
                    &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
                &lt;span class="nt"&gt;&amp;lt;/mat-step&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;/mat-stepper&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;        &lt;span class="nx"&gt;stepperHeader&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;input&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;TemplateRef&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;any&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="nx"&gt;selectedIndex&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;model&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;this class would be triggered dynamically depends on if there is a custom header provided to the stepper or not.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;in the above code snippet I created a stepper component which takes a model signal that works like an Input and Output to listen to the active step changes.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;and this is the parent component re-using the stepper component in customizable view&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;    &lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;EmbeddedViewsComponent&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;steps&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
            &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;stepTitle&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Step title 1&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;isActive&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;
            &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;stepTitle&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Step title 2&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;isActive&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;];&lt;/span&gt;
        &lt;span class="nx"&gt;activeIndex&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&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;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;app-stepper&lt;/span&gt; &lt;span class="na"&gt;[stepperHeader]=&lt;/span&gt;&lt;span class="s"&gt;"stepperHeaderRef"&lt;/span&gt; &lt;span class="na"&gt;[(selectedIndex)]=&lt;/span&gt;&lt;span class="s"&gt;"activeIndex"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/app-stepper&amp;gt;&lt;/span&gt;

    &lt;span class="nt"&gt;&amp;lt;ng-template&lt;/span&gt; &lt;span class="na"&gt;#stepperHeaderRef&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;ng-container&lt;/span&gt; &lt;span class="na"&gt;*ngFor=&lt;/span&gt;&lt;span class="s"&gt;"let step of steps;let i = index;let last = last"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"step"&lt;/span&gt;
                &lt;span class="na"&gt;[ngClass]=&lt;/span&gt;&lt;span class="s"&gt;"{'active':activeIndex === i, 'completed':activeIndex &amp;gt; i, 'pending':activeIndex &amp;lt; i}"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
                &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"bullet"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
                    &lt;span class="nt"&gt;&amp;lt;mat-icon&lt;/span&gt; &lt;span class="na"&gt;*ngIf=&lt;/span&gt;&lt;span class="s"&gt;"activeIndex &amp;gt; i"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;check&lt;span class="nt"&gt;&amp;lt;/mat-icon&amp;gt;&lt;/span&gt;
                    &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;[ngClass]=&lt;/span&gt;&lt;span class="s"&gt;"{'active':activeIndex === i}"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
                &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
                &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"number"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;span&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"tw-uppercase"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;{{step.stepTitle}}&lt;span class="nt"&gt;&amp;lt;/span&amp;gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
                &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"tw-w-max"&lt;/span&gt; &lt;span class="na"&gt;[ngClass]=&lt;/span&gt;&lt;span class="s"&gt;"{'active':activeIndex === i}"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
                    {{activeIndex === i ? 'In Progress' :
                    activeIndex &lt;span class="nt"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nt"&gt;i&lt;/span&gt; &lt;span class="err"&gt;?&lt;/span&gt; &lt;span class="err"&gt;'&lt;/span&gt;&lt;span class="na"&gt;Pending&lt;/span&gt;&lt;span class="err"&gt;'&lt;/span&gt; &lt;span class="na"&gt;:&lt;/span&gt; &lt;span class="err"&gt;'&lt;/span&gt;&lt;span class="na"&gt;Completed&lt;/span&gt;&lt;span class="err"&gt;'&lt;/span&gt; &lt;span class="err"&gt;}}&lt;/span&gt; &lt;span class="err"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="na"&gt;div&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
                &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
                &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;*ngIf=&lt;/span&gt;&lt;span class="s"&gt;"!last"&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"step-link"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;/ng-container&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/ng-template&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nc"&gt;.step-link&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="err"&gt;@apply&lt;/span&gt; &lt;span class="err"&gt;tw-h-[1px]&lt;/span&gt; &lt;span class="err"&gt;tw-bg-neutral-300&lt;/span&gt; &lt;span class="err"&gt;tw-w-full&lt;/span&gt; &lt;span class="err"&gt;tw-mt-4;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="nc"&gt;.step&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="err"&gt;.bullet&lt;/span&gt; &lt;span class="err"&gt;{&lt;/span&gt;
            &lt;span class="err"&gt;@apply&lt;/span&gt; &lt;span class="err"&gt;tw-mb-4&lt;/span&gt; &lt;span class="err"&gt;tw-flex&lt;/span&gt; &lt;span class="err"&gt;tw-items-center&lt;/span&gt; &lt;span class="err"&gt;tw-justify-center&lt;/span&gt; &lt;span class="err"&gt;tw-bg-green-100&lt;/span&gt; &lt;span class="err"&gt;tw-w-[32px]&lt;/span&gt; &lt;span class="err"&gt;tw-h-[32px]&lt;/span&gt; &lt;span class="err"&gt;tw-rounded-full;&lt;/span&gt;

            &lt;span class="err"&gt;mat-icon&lt;/span&gt; &lt;span class="err"&gt;{&lt;/span&gt;
                &lt;span class="err"&gt;@apply&lt;/span&gt; &lt;span class="err"&gt;tw-text-white;&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;

            &lt;span class="nc"&gt;.active&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="err"&gt;@apply&lt;/span&gt; &lt;span class="err"&gt;tw-bg-green-400&lt;/span&gt; &lt;span class="err"&gt;tw-w-[12px]&lt;/span&gt; &lt;span class="err"&gt;tw-h-[12px]&lt;/span&gt; &lt;span class="err"&gt;tw-rounded-full;&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="err"&gt;}&lt;/span&gt;

        &lt;span class="nc"&gt;.number&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="err"&gt;@apply&lt;/span&gt; &lt;span class="err"&gt;tw-text-neutral-600&lt;/span&gt; &lt;span class="err"&gt;tw-tracking-widest&lt;/span&gt; &lt;span class="err"&gt;tw-w-32;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="o"&gt;&amp;amp;&amp;gt;&lt;/span&gt;&lt;span class="nc"&gt;.active&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="err"&gt;@apply&lt;/span&gt; &lt;span class="err"&gt;tw-text-green-500;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nc"&gt;.pending&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="err"&gt;@apply&lt;/span&gt; &lt;span class="err"&gt;tw-opacity-60;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nc"&gt;.completed&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="err"&gt;.bullet&lt;/span&gt; &lt;span class="err"&gt;{&lt;/span&gt;
                &lt;span class="err"&gt;@apply&lt;/span&gt; &lt;span class="err"&gt;tw-bg-green-600;&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="err"&gt;}&lt;/span&gt;
    &lt;span class="err"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;here in the parent component I provide number of steps for the header to show and actually I could also provide the steps content to show in our re-usable component &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You could also check the src code in my &lt;a href="https://github.com/sabrymuhamad/dev-best-practice" rel="noopener noreferrer"&gt;github repo&lt;/a&gt;.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Reusable blocks using Sass @mixin with angular example</title>
      <dc:creator>sabrymuhamad</dc:creator>
      <pubDate>Mon, 18 Mar 2024 18:44:11 +0000</pubDate>
      <link>https://dev.to/sabrymuhamad/reusable-blocks-using-sass-mixin-with-angular-example-49o9</link>
      <guid>https://dev.to/sabrymuhamad/reusable-blocks-using-sass-mixin-with-angular-example-49o9</guid>
      <description>&lt;p&gt;One of the biggest features of CSS Preprocessors like Sass is the resuable blocks in another meaning "@mixin"... &lt;br&gt;
The very common use for using "mixins" is to store some css properties to be later included "&lt;a class="mentioned-user" href="https://dev.to/include"&gt;@include&lt;/a&gt;" into another class and this is very useful already but one of rare usage of mixin is to act like a function which takes some arguments and act accordingly.&lt;/p&gt;

&lt;p&gt;of course Sass has @function decorator as well but it is different than @mixin since function in sass should return a value and it is commonly used to do operations not to create css code.&lt;/p&gt;

&lt;p&gt;so lets jump straight into an example of "@mixin" &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;use case:&lt;/strong&gt;  &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;For this example I'm using Angular with scss.However, this is still valid with any other framework or pure html/sass.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Imagine you have multiple status in your app and according to each status returned from http request, you need to change the color of text or background of certain UI block.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;div [ngClass]="status"&amp;gt;Current Status&amp;lt;div&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;We have 3 different status returned from the backend: active/freezed/cancelled&lt;/p&gt;

&lt;p&gt;so with every status returned I want to apply this css code&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.active {
  border: 5px solid green;
  box-shadow: 0 0 35px green;
}

.freezed {
  border: 5px solid blue;
  box-shadow: 0 0 35px blue;
}

.cancelled {
  border: 5px solid red;
  box-shadow: 0 0 35px red;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;this might seem fine to you since it is not a lot of code but imagin that use need to add some kind of animation like so&lt;br&gt;
&lt;code&gt;animation: fadeInOut 2s infinite;&lt;/code&gt;&lt;br&gt;
so the code should look like somthing similar&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.active{
  border: 5px solid blue;
  box-shadow: 0 0 35px green;
  animation: fadeInOut-green2s infinite;
}
  @keyframes fadeInOut-green{

    0%,
    100% {
      box-shadow: 1px 0px 30px 4px green, inset 0px 0px 10px rgba(255, 255, 255, 0.5);
    }

    50% {
      box-shadow: 0px 0px 0px 0px rgba(255, 0, 0, 0), inset 0px 0px 0px rgba(255, 255, 255, 0);
    }
  }
}
.freezed{
  border: 5px solid blue;
  box-shadow: 0 0 35px blue;
  animation: fadeInOut-blue 2s infinite;
}
  @keyframes fadeInOut-blue {

    0%,
    100% {
      box-shadow: 1px 0px 30px 4px blue, inset 0px 0px 10px rgba(255, 255, 255, 0.5);
    }

    50% {
      box-shadow: 0px 0px 0px 0px rgba(255, 0, 0, 0), inset 0px 0px 0px rgba(255, 255, 255, 0);
    }
  }
}
.cancelled{
  border: 5px solid red;
  box-shadow: 0 0 35px red;
  animation: fadeInOut-red 2s infinite;
}
  @keyframes fadeInOut-red {

    0%,
    100% {
      box-shadow: 1px 0px 30px 4px red, inset 0px 0px 10px rgba(255, 255, 255, 0.5);
    }

    50% {
      box-shadow: 0px 0px 0px 0px rgba(255, 0, 0, 0), inset 0px 0px 0px rgba(255, 255, 255, 0);
    }
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;so these keyframes are changing the shadow of the element according to its color and in our case we will have to create 3 keyframes for the 3 statuses... suddenly you would feel there is something wrong if you didn't feel it already earlier.&lt;/p&gt;

&lt;p&gt;here comes the power of Sass and mixin...&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@mixin _fadeAnimation($ColorVar) {
  border: 5px solid $ColorVar;
  box-shadow: 0 0 35px $ColorVar;
  animation: fadeInOut-#{$ColorVar} 2s infinite;

  @keyframes fadeInOut-#{$ColorVar} {

    0%,
    100% {
      box-shadow: 1px 0px 30px 4px $ColorVar, inset 0px 0px 10px rgba(255, 255, 255, 0.5);
    }

    50% {
      box-shadow: 0px 0px 0px 0px rgba(255, 0, 0, 0), inset 0px 0px 0px rgba(255, 255, 255, 0);
    }
  }
}

.active {
  @include _fadeAnimation(green);
}

.freezed {
  @include _fadeAnimation(blue);
}

.cancelled{
  @include _fadeAnimation(red);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I think that last piece of code is a please to see for most of developers since it is very close to what we use on daily basis on any programming language... mixin here is the function that takes some arguments and we excute that function using &lt;a class="mentioned-user" href="https://dev.to/include"&gt;@include&lt;/a&gt; with function name with the desired parameters.&lt;/p&gt;

</description>
      <category>sass</category>
      <category>mixin</category>
      <category>include</category>
      <category>angular</category>
    </item>
  </channel>
</rss>
