<?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: Bibhu Padhy</title>
    <description>The latest articles on DEV Community by Bibhu Padhy (@bibhupadhy).</description>
    <link>https://dev.to/bibhupadhy</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%2F224117%2Fa4750867-fad9-4238-b895-ebffa74de893.jpeg</url>
      <title>DEV Community: Bibhu Padhy</title>
      <link>https://dev.to/bibhupadhy</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/bibhupadhy"/>
    <language>en</language>
    <item>
      <title>solidity 101</title>
      <dc:creator>Bibhu Padhy</dc:creator>
      <pubDate>Tue, 06 Sep 2022 13:23:06 +0000</pubDate>
      <link>https://dev.to/bibhupadhy/solidity-101-12jk</link>
      <guid>https://dev.to/bibhupadhy/solidity-101-12jk</guid>
      <description>&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Data types&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Variables&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Constants&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Immutables&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Data types
&lt;/h2&gt;

&lt;p&gt;Mainly we have 4 types of data types in solidity language.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;boolean&lt;/li&gt;
&lt;li&gt;uint&lt;/li&gt;
&lt;li&gt;int&lt;/li&gt;
&lt;li&gt;address 
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  boolean:
&lt;/h3&gt;

&lt;p&gt;As we know we can specify a variable is true or false.&lt;/p&gt;

&lt;h3&gt;
  
  
  uint:
&lt;/h3&gt;

&lt;p&gt;Its stands for unsigned integers. we can specify only a non negative integers. &lt;br&gt;&lt;br&gt;
uint can have a range of sizes like uint8, uint16 ... uint256&lt;br&gt;
uint8 = 0 to 2**8-1. same goes with others.&lt;/p&gt;
&lt;h3&gt;
  
  
  int:
&lt;/h3&gt;

&lt;p&gt;As we know we can specify an integer and negative numbers are allowed for int type. like uint here also we can specify a range of different sizes.&lt;/p&gt;
&lt;h3&gt;
  
  
  address:
&lt;/h3&gt;

&lt;p&gt;address holds a 20 byte value which is the size of a ethereum address. additionally we can specify as &lt;code&gt;address payable&lt;/code&gt; which can transfer or send fund to the address.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// SPDX-License-Identifier: MIT
pragma solidity ^0.8.9;

contract ExampleDataType {

 bool public boo = true;
 uint public age = 27;
 int public num = -3;
 address public addr = 0x8CA434018A6d.....;

}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Variables
&lt;/h2&gt;

&lt;p&gt;Variables are containers where we store some data. we have three type of variables.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;state&lt;/li&gt;
&lt;li&gt;local&lt;/li&gt;
&lt;li&gt;global 
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  state:
&lt;/h3&gt;

&lt;p&gt;State variables are declared inside a function and not stored in the blockchain.&lt;/p&gt;

&lt;h3&gt;
  
  
  local:
&lt;/h3&gt;

&lt;p&gt;Local variables are declared outside of the function and stored in the blockchain.&lt;/p&gt;

&lt;h3&gt;
  
  
  global:
&lt;/h3&gt;

&lt;p&gt;Global variables provides information about the blockchain.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// SPDX-License-Identifier: MIT
pragma solidity ^0.8.9;

contract ExampleVariable {

  string public name = "jon doe"; // state variable.

  function changeName() public {
     uint pin = 500082;  // local variable
     address sender = msg.sender; // global variable
  }

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  constants
&lt;/h2&gt;

&lt;p&gt;constants are variables that can not be modified. Their value is hard coded and using constants we can save gas.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// SPDX-License-Identifier: MIT
pragma solidity ^0.8.9;

contract ExampleConstants {

   address public constant ADDR = 0xee3445e...;
   uint256 public constant MAX_NFT = 1000

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Immutable
&lt;/h2&gt;

&lt;p&gt;immutable variables are like constants. Value of immutable variables can be set inside the constructor but can not be modified afterwords.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// SPDX-License-Identifier: MIT
pragma solidity ^0.8.9;

contract ExampleConstants {

   address public immutable MY_ADDRESS;

   constructor() {
       MY_ADDRESS = msg.sender;
   }

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://github.com/bibhu-padhy/learn-web3"&gt;https://github.com/bibhu-padhy/learn-web3&lt;/a&gt;&lt;/p&gt;

</description>
      <category>web3</category>
      <category>solidity</category>
      <category>blockchain</category>
    </item>
    <item>
      <title>How to deploy Angular project using firebase</title>
      <dc:creator>Bibhu Padhy</dc:creator>
      <pubDate>Sun, 12 Sep 2021 11:50:24 +0000</pubDate>
      <link>https://dev.to/bibhupadhy/how-to-deploy-angular-project-using-firebase-42oi</link>
      <guid>https://dev.to/bibhupadhy/how-to-deploy-angular-project-using-firebase-42oi</guid>
      <description>&lt;p&gt;If you are going through this article then I assume you have a javascript-based Awesome project with (Angular, React, etc..) which you want to deploy to any cloud providers like (AWS, Azure, or GCP).&lt;br&gt;
In this article, you will get to know how to deploy your project on firebase, cause i don't know how to do it using the above ones :( .&lt;/p&gt;

&lt;p&gt;Step-1:npm i -g firebase-tools .&lt;/p&gt;

&lt;p&gt;step-2:run firebase login(if you haven't logged in yet then it will take you to the browser from where you can log in to your firebase account ).&lt;/p&gt;

&lt;p&gt;step-3:run firebase init. you will be going to get some options like the below example from which you can select which one do you want to go with. in my case I have selected hosting.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--2p4uvsHx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j5q42t98ankov8ycia99.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--2p4uvsHx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j5q42t98ankov8ycia99.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;step-4:After you select hosting you need to select the project from firebase. if you don't have it then you need to create a new one&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--clXaXVCa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lvxicd549i0l1b9rn0wd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--clXaXVCa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lvxicd549i0l1b9rn0wd.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;step-5: Hosting setup, in this step you need to select in which folder you have your hosting assets.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--hMkAFThr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6p0azwrp4aeaqy3dtckg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--hMkAFThr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6p0azwrp4aeaqy3dtckg.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After this, you will get an option to set up CI/CD pipeline with Github which I am not doing right now will do it in the later articles.&lt;/p&gt;

&lt;p&gt;Step-6:run firebase deploy. After this firebase will deploy your application and will give you a site link like(abc.web.app) where you can interact with your project.&lt;/p&gt;

&lt;p&gt;Hope it will be helpful to you.&lt;/p&gt;

</description>
      <category>angular</category>
      <category>firebase</category>
      <category>devops</category>
      <category>typescript</category>
    </item>
    <item>
      <title>js swap two numbers</title>
      <dc:creator>Bibhu Padhy</dc:creator>
      <pubDate>Sun, 21 Feb 2021 06:09:25 +0000</pubDate>
      <link>https://dev.to/bibhupadhy/js-swap-two-numbers-70</link>
      <guid>https://dev.to/bibhupadhy/js-swap-two-numbers-70</guid>
      <description>&lt;p&gt;Swap two numbers, a common problem-solving interview question.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Using a Variable&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;function swapTwoNumbers(a, b) {&lt;br&gt;
    let temp = a;&lt;br&gt;
    a = b;&lt;br&gt;
    b = temp&lt;br&gt;
    return [a, b];&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;console.log(swapTwoNumbers(10, 5))&lt;br&gt;
// output a = 5, b = 10&lt;/p&gt;




&lt;ol&gt;
&lt;li&gt;Using arithmetic operators&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;function swapTwoNumbers(a, b) {&lt;br&gt;
    a = a + b; // 15&lt;br&gt;
    b = a - b; // 15 - 5 = 10&lt;br&gt;
    a = a - b; // 15 - 10 = 5&lt;br&gt;
    return [a, b];&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;console.log(swapTwoNumbers(10, 5))&lt;/p&gt;

&lt;p&gt;// output a = 5, b = 10&lt;/p&gt;




&lt;ol&gt;
&lt;li&gt;Using Destructuring&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;function swapTwoNumbers(a, b) {&lt;br&gt;
    return [a, b] = [b, a]&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;console.log(swapTwoNumbers(10, 5))&lt;/p&gt;

</description>
      <category>node</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Reactive forms in angular</title>
      <dc:creator>Bibhu Padhy</dc:creator>
      <pubDate>Tue, 24 Nov 2020 11:43:06 +0000</pubDate>
      <link>https://dev.to/bibhupadhy/reactive-forms-in-angular-5879</link>
      <guid>https://dev.to/bibhupadhy/reactive-forms-in-angular-5879</guid>
      <description>&lt;p&gt;This is going to be a short introduction to how we can handle form logic using angular reactive forms.&lt;/p&gt;

&lt;p&gt;The requirement is like we are going to have 3 fields (Name, Email, Password) and on submitting it will send the user data through an API, mostly we will not concentrate on the send data part.&lt;/p&gt;

&lt;p&gt;so in Angular, we have a class called FormControl according to Angular it Tracks the value and validation status of an individual form control. There are around 20-30 controls we have in this class you can check all of them by this &lt;a href="https://angular.io/api/forms/FormControl"&gt;link&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;step-1: Declare Reactive forms in the module file in the imports section.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@NgModule({
  imports: [BrowserModule, FormsModule, ReactiveFormsModule],
  declarations: [AppComponent],
  bootstrap: [AppComponent]
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Step-2:Take form controls classes which we want to use in the form.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  name: FormControl = new FormControl("", Validators.required);
  email: FormControl = new FormControl("", [
    Validators.required,
    Validators.email
  ]);
  password: FormControl = new FormControl("", Validators.required);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;at the name field, as we don't want any value to be in the name field so we are not assigning any value to it and this field is required. The same goes for the others.&lt;/p&gt;

&lt;p&gt;step-3: Declare these above form controls in the HTML.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    &amp;lt;form&amp;gt;
        &amp;lt;div class="form_group"&amp;gt;
            &amp;lt;label for="name"&amp;gt;Name&amp;lt;/label&amp;gt;
            &amp;lt;input [formControl]="name" type="text" id="name"&amp;gt;
    &amp;lt;/div&amp;gt;
           &amp;lt;div class="form_group"&amp;gt;
            &amp;lt;label for="email"&amp;gt;email&amp;lt;/label&amp;gt;
            &amp;lt;input [formControl]="email" type="email" id="email"&amp;gt;
    &amp;lt;/div&amp;gt;
            &amp;lt;div class="form_group"&amp;gt;
                &amp;lt;label for="password"&amp;gt;password&amp;lt;/label&amp;gt;
                &amp;lt;input [formControl]="password" type="password" id="password"&amp;gt;
    &amp;lt;/div&amp;gt;
    &amp;lt;/form&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;[formControl]="name" indicates that we are binding a form control to the input element.&lt;/p&gt;

&lt;p&gt;yes! Here we have a functional Angular reactive form&lt;/p&gt;

&lt;p&gt;WAIT! but for most of the time, we are not going to have only 3 inputs. In requirement, we may have 6 or 8 to 10 fields if we take the example of an address field.&lt;/p&gt;

&lt;p&gt;In this case, we have to take 8 to 10 from controls and need to maintain their validations, state, and a lot more. It's doable but it will become a bit clumsy.&lt;/p&gt;

&lt;p&gt;In Angular, we have a class called FormGroup. According to the documentations FromGroup Tracks the value and validity state of a group of FormControl instances.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  form: FormGroup = new FormGroup({
    name: new FormControl("", Validators.required),
    email: new FormControl("", [Validators.required, Validators.email]),
    phone: new FormControl("", Validators.required)
  });
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Inside the form group, we need to declare FormControls and their state and validation.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    &amp;lt;form [formGroup]="form"&amp;gt;
        &amp;lt;div class="form_group"&amp;gt;
            &amp;lt;label for="name"&amp;gt;Name&amp;lt;/label&amp;gt;
            &amp;lt;input formControlName="name" type="text" id="name"&amp;gt;
    &amp;lt;/div&amp;gt;
            &amp;lt;div class="form_group"&amp;gt;
                &amp;lt;label for="email"&amp;gt;email&amp;lt;/label&amp;gt;
                &amp;lt;input formControlName="email" type="email" id="email"&amp;gt;
    &amp;lt;/div&amp;gt;
                &amp;lt;div class="form_group"&amp;gt;
                    &amp;lt;label for="password"&amp;gt;password&amp;lt;/label&amp;gt;
                    &amp;lt;input formControlName="password" type="password" id="password"&amp;gt;
    &amp;lt;/div&amp;gt;
    &amp;lt;/form&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;we need to declare like this formControlName="password" in the input element.&lt;/p&gt;

</description>
      <category>angular</category>
      <category>typescript</category>
      <category>html</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Tab animation using Angular</title>
      <dc:creator>Bibhu Padhy</dc:creator>
      <pubDate>Fri, 13 Nov 2020 06:07:16 +0000</pubDate>
      <link>https://dev.to/bibhupadhy/tab-animation-using-angular-g6n</link>
      <guid>https://dev.to/bibhupadhy/tab-animation-using-angular-g6n</guid>
      <description>&lt;p&gt;I assume if you are going through this post then you have some idea about angular Framework basic HTML and CSS.&lt;/p&gt;

&lt;p&gt;still, let's start from the beginning, open your preferred terminal.&lt;/p&gt;

&lt;p&gt;npm install -g @angular/cli&lt;br&gt;
ng new my-app&lt;br&gt;
cd my-app&lt;br&gt;
remove all the boilerplate content from app.component.html and at the app TS file I have taken an array of tabs.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div class="main_container"&amp;gt;
  &amp;lt;div class="tabs_container"&amp;gt;
    &amp;lt;div
      class="tab"
      [class.active]="tab.tabId === selectedTabId"
      (click)="handelTabChange(tabRef.getBoundingClientRect());selectedTabId = tab.tabId"
      #tabRef
      *ngFor=" let tab of tabs"
    &amp;gt;
      {{tab.tabName}}
    &amp;lt;/div&amp;gt;
  &amp;lt;/div&amp;gt;
  &amp;lt;span #panelRef class="active_panel"&amp;gt;&amp;lt;/span&amp;gt;
&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;here I am iterating the tabs array and showing the tabs name .active_panel class is the one which should show under the active tab.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#tabRef takes the reference for each tab.
#panelRef reference of the active panel
(click)="handelTabChange(tabRef.getBoundingClientRect())
handelTabChange function gives the width,height and position of the clicked tab. 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That all we need for the HTML let's move to TS now.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  @ViewChild("panelRef", { read: ElementRef })
  panelRef: ElementRef; // panel reference 
  @ViewChildren("tabRef", { read: ElementRef }) 
  tabRef: QueryList&amp;lt;ElementRef&amp;gt;; // tabs reference Query List
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  ngAfterViewInit() {
    const firstChild = this.tabRef.toArray()[0];
   // I want to show the first child of the tab as selected
   // so 0th index is going to be the first one
    const firstChildPosition = 
    firstChild.nativeElement.getBoundingClientRect();
   // here I am storing the position of the first child.
    this.renderer.setStyle(
      this.panelRef.nativeElement,
      "width",
      `${firstChildPosition.width}px`
    );
   // giving same width as tab label to the active panel
    this.renderer.setStyle(
      this.panelRef.nativeElement,
      "left",
      `${firstChildPosition.left}px`
    );
   // setting same left position as the first child to panel
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now when the page will load it will look for the first tab and the active panel will take the same width and left position.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  handelTabChange(tabRef: DOMRect) {
    this.renderer.setStyle(
      this.panelRef.nativeElement,
      "left",
      `${tabRef.left}px`
    );
    this.renderer.setStyle(
      this.panelRef.nativeElement,
      "width",
      `${tabRef.width}px`
    );
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Kinda doing the same thing as explained above but now when the user clicks on the tabs.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.main_container {
  width: 100%;
  display: flex;
  flex-direction: column;
  justify-content: center;
}
.tabs_container {
  width: 100%;
  display: flex;
  justify-content: space-around;
}
.tab {
  font-size: 18px;
  cursor: pointer;
  margin-right: 10px;
  text-align: center;
  margin: 5px;
  transform: scale(0.95);
}
.active {
  color: gray;
  transform: scale(1);
}
.active_panel {
  position: relative;
  height: 5px;
  background-color: cyan;
  transition: all 400ms ease-in-out;
  border-radius: 10px;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Required CSS for this one&lt;/p&gt;

&lt;p&gt;live link for the demo &lt;a href="https://angular-tab-animations-u6421j.stackblitz.io"&gt;https://angular-tab-animations-u6421j.stackblitz.io&lt;/a&gt;&lt;/p&gt;

</description>
      <category>angular</category>
      <category>html</category>
      <category>css</category>
      <category>typescript</category>
    </item>
    <item>
      <title>How to show a tooltip in angular</title>
      <dc:creator>Bibhu Padhy</dc:creator>
      <pubDate>Tue, 10 Nov 2020 07:00:33 +0000</pubDate>
      <link>https://dev.to/bibhupadhy/how-to-show-a-tooltip-in-angular-5bj</link>
      <guid>https://dev.to/bibhupadhy/how-to-show-a-tooltip-in-angular-5bj</guid>
      <description>&lt;p&gt;I assume if you are going through this post then you have some idea about angular Framework.&lt;/p&gt;

&lt;p&gt;still, let's start from the beginning, open your preferred terminal.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;npm install -g @angular/cli&lt;/li&gt;
&lt;li&gt;ng new my-app&lt;/li&gt;
&lt;li&gt;cd my-app&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;remove all the boilerplate content from app.component.html and just add a simple h1 tag or a button (basically where you want to show a tooltip).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;h1 tooltip="This is a tooltip"&amp;gt;Hey there &amp;lt;/h1&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;back to the terminal type &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;ng generate directive tooltip (CLI will create a directive class)&lt;/li&gt;
&lt;li&gt;Go to the created directive class and copy the class name (TooltipDirective)&lt;/li&gt;
&lt;li&gt;open app.module.ts and declare it in the declarations
(declarations: [TooltipDirective])
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@NgModule({
  imports:      [ BrowserModule, FormsModule ],
  declarations: [ AppComponent, HelloComponent, TooltipDirective ],
  bootstrap:    [ AppComponent ]
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;open TooltipDirective and add
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  tooltip: HTMLElement;
  @Input("tooltip") tooltipTitle: string;
  delay = 500;
  constructor(private el: ElementRef, private renderer: Renderer2) {}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;tooltip is the element where we will show the tooltip message.&lt;/li&gt;
&lt;li&gt;tooltipTitle by this input we will get the tooltip message from the component
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
  @HostListener("mouseover") onMouseEnter() {
    showTooltip();
  }

  @HostListener("mouseleave") onMouseLeave() {
    hidetooltip();
  }

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;onMouseEnter and onMouseLeave functions triggers by their respective event listeners.lets continue with the showToolTip function
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  showTooltip() {
    this.tooltip = this.renderer.createElement("span"); 
    // creating a span
    this.tooltip.appendChild(this.renderer.createElement("span")); 
    // appending a span to the tooltip

    this.renderer.appendChild(
      this.tooltip,
      this.renderer.createText(this.tooltipTitle) 
      // adding the tooltip text into the tooltip span
    );
    const hostPos = this.el.nativeElement.getBoundingClientRect(); 
    // getting the hight width and positions of the target element
    let top, left;

    top = hostPos.bottom;
    left = hostPos.left + hostPos.width / 2;
    this.renderer.setStyle(this.tooltip, "top", `${top}px`); 
    //adding a top positions value for the tooltip
    this.renderer.setStyle(this.tooltip, "left", `${left}px`); 
    // adding the left value
    this.renderer.appendChild(document.body, this.tooltip); 
   // appending to the document
    this.renderer.addClass(this.tooltip, "tooltip"); 
   // adding the tooltip styles
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;required CSS (you can add the below CSS in the root style file of your project)
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.tooltip {
  position: absolute;
  max-width: 90%;
  font-size: 14px;
  text-align: center;
  color: #f8f8f2;
  padding: 5px;
  background: #1e1e1f;
  z-index: 1000;
  opacity: 0;
}

.tooltip_show {
  opacity: 1;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;to remove the tooltip on mouse leave
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  @HostListener("mouseleave") onMouseLeave() {
    this.renderer.removeClass(this.tooltip, "tooltip_show");
     // on mouse over it will remove the opacity
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I have used this directive in a mobile app, There I am looking for a click event instead of mouseleave I guess you know why.&lt;/p&gt;


&lt;h2&gt;Done

&lt;/h2&gt;
&lt;p&gt;&lt;iframe src="https://stackblitz.com/edit/angular-ivy-gnbyp4?" width="100%" height="500"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

</description>
      <category>angular</category>
      <category>html</category>
      <category>css</category>
      <category>typescript</category>
    </item>
  </channel>
</rss>
