<?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: Sameer Trimade</title>
    <description>The latest articles on DEV Community by Sameer Trimade (@sameer_trimade).</description>
    <link>https://dev.to/sameer_trimade</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%2F714158%2F678588a5-9c07-40fd-b0d5-fd1b7a0cfbdd.png</url>
      <title>DEV Community: Sameer Trimade</title>
      <link>https://dev.to/sameer_trimade</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sameer_trimade"/>
    <language>en</language>
    <item>
      <title>Best Practices for Optimizing Frontend Performance</title>
      <dc:creator>Sameer Trimade</dc:creator>
      <pubDate>Fri, 17 Mar 2023 03:12:23 +0000</pubDate>
      <link>https://dev.to/sameer_trimade/best-practices-for-optimizing-frontend-performance-2c8i</link>
      <guid>https://dev.to/sameer_trimade/best-practices-for-optimizing-frontend-performance-2c8i</guid>
      <description>&lt;p&gt;Optimizing web performance from the frontend is essential because it can have a significant impact on the user experience of a website. The frontend of a website consists of the code that runs in the user's web browser and is responsible for displaying the content and functionality of the website. If the frontend code is inefficient or poorly optimized, it can result in slow page load times, unresponsive user interfaces, and other performance issues.&lt;/p&gt;

&lt;p&gt;The main reason to optimize web performance from the frontend is:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Faster page load times: When a website loads quickly, it can improve the user experience and reduce bounce rates(bounce rates means the percentage of visitors to a particular website who navigate away from the site after viewing only one page). Optimizing frontend code can reduce the amount of time it takes for a website to load.&lt;/li&gt;
&lt;li&gt;Better user engagement: A website that responds quickly and smoothly to user interactions can improve user engagement and increase the time spent on the site.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;So, in this article, I will discuss best practices that can be used to optimize the frontend performance-&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Minification of resources:&lt;/strong&gt; It refers to the process of removing unnecessary, redundant data from your HTML, CSS, and JavaScript that are not required to load. This includes eliminating code comments and formatting, white space characters, unused codes, newline characters, and so on. Minification of HTML, CSS, and JavaScript will speeds up the frontend load times as it reduces the amount of code that needs to be requested from the server.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Reduce the Number of Server Calls:&lt;/strong&gt; It is a crucial technique used to optimize web performance from the frontend. A server call occurs when the user's browser requests data from the server, such as images, stylesheets, or JavaScript files. Each server call adds additional overhead to the page load time and can impact the user experience.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Optimizing images:&lt;/strong&gt; It is a crucial technique used to optimize web performance from the frontend. Images can be a significant contributor to the size of a web page, and larger image sizes can lead to slower page load times, which can negatively impact the user experience.&lt;br&gt;
Using SVG (Scalable Vector Graphics) images is a popular technique used to optimize web performance from the frontend.&lt;br&gt;
Here are some reasons why using SVG images can be beneficial for optimizing web performance:&lt;/p&gt;

&lt;p&gt;(a) &lt;strong&gt;Smaller file size:&lt;/strong&gt; Compared to images, such as JPEG or PNG, SVG images have a significantly smaller file size.&lt;/p&gt;

&lt;p&gt;(b) &lt;strong&gt;Scalability:&lt;/strong&gt; SVG images are highly scalable and can be resized without losing quality. This means that you can use the same image on multiple devices, such as desktops, tablets, and smartphones, without having to create different versions of the image.&lt;/p&gt;

&lt;p&gt;(c) &lt;strong&gt;Faster loading times:&lt;/strong&gt; Because SVG images have a smaller file size, they can be loaded more quickly than larger images such as JPEG or PNG, resulting in faster page load times.&lt;/p&gt;

&lt;p&gt;Also using new image formats like &lt;strong&gt;WebP&lt;/strong&gt; and &lt;strong&gt;AVIF&lt;/strong&gt; gives a better performance than older formats like &lt;strong&gt;JPEG&lt;/strong&gt; and &lt;strong&gt;PNG&lt;/strong&gt;. &lt;strong&gt;WebP&lt;/strong&gt; is 26% smaller compared to &lt;strong&gt;PNG&lt;/strong&gt; and 25–35% smaller compared to &lt;strong&gt;JPEG&lt;/strong&gt;. &lt;strong&gt;AVIF&lt;/strong&gt; is 50% smaller compared to &lt;strong&gt;JPEG&lt;/strong&gt; and 20% smaller compared to &lt;strong&gt;WebP&lt;/strong&gt;. However, the downside is with the browser support. &lt;strong&gt;WebP&lt;/strong&gt; gained browser support recently, so older versions might not support it. On the other hand, &lt;strong&gt;AVIF&lt;/strong&gt; is only supported in Chrome and Opera.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Apply Lazy Loading:&lt;/strong&gt; It helps to improve frontend loading time. With lazy loading, the web page loads only the required content first and loads the remaining content when the user needs it. Lazy loading is a technique that is widely used in real-world scenarios to improve web performance. Here are some examples of how lazy loading is implemented in different scenarios:&lt;br&gt;
(a) &lt;strong&gt;Image-heavy websites:&lt;/strong&gt; Websites that rely heavily on images, such as online stores or photography websites, can benefit greatly from lazy loading. In this scenario, images are loaded only when they are in the user's viewport, allowing the website to load faster and reducing the amount of data that needs to be transferred.&lt;/p&gt;

&lt;p&gt;(b) &lt;strong&gt;Infinite scroll:&lt;/strong&gt; Websites that implement infinite scroll, such as social media platforms, can also benefit from lazy loading. In this scenario, new content is loaded as the user scrolls down the page. By lazy loading the content, the initial page load time is reduced, and the user can continue to scroll without experiencing a lag in performance.&lt;/p&gt;

&lt;p&gt;(c) &lt;strong&gt;Videos:&lt;/strong&gt; Lazy loading is also commonly used for videos, especially on mobile devices, where data usage is a concern. In this scenario, a thumbnail image or preview is loaded initially, and the video is loaded only when the user clicks on the play button.&lt;/p&gt;

&lt;p&gt;Overall, lazy loading is a powerful technique that can significantly improve web performance by reducing the initial page load time and improving the user experience.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Use a Content Delivery Network (CDN):&lt;/strong&gt; A CDN can improve web performance by caching content closer to the user, reducing latency, and speeding up content delivery. When the user is far from the server geographically, latency increases. Moreover, request load could also impact the content serving time. The loading speed of frontend content, including HTML pages, stylesheets, JavaScript files, and images, can be optimized using a CDN. A content delivery network (CDN) is a group of servers distributed across several geographical locations that store a cached version of the content to deliver fast to the end-user. CDN redirects user requests to the nearest server.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;By following these best practices, you can significantly improve web performance from the frontend, providing a better user experience for your website visitors.&lt;/p&gt;

</description>
      <category>frontend</category>
      <category>javascript</category>
      <category>programming</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Route protection in Angular</title>
      <dc:creator>Sameer Trimade</dc:creator>
      <pubDate>Fri, 03 Mar 2023 04:58:38 +0000</pubDate>
      <link>https://dev.to/sameer_trimade/route-protection-in-angular-396m</link>
      <guid>https://dev.to/sameer_trimade/route-protection-in-angular-396m</guid>
      <description>&lt;p&gt;In any complex web application, it is necessary to have some sort of authentication to restrict certain users from accessing specific pages. &lt;br&gt;
For example- &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Checking if a user has logged in&lt;/strong&gt;. Suppose, if they’re not logged in, thus the guard will redirect to the register/login page.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Checking if a user has permission&lt;/strong&gt;. It checks if the user have rights to access that page or not.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Route protection helps us in front-end frameworks to validate a route before it’s activated. Angular supports route protection via &lt;strong&gt;route guards.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A route guard validates a route and activates it only if authorized.&lt;/p&gt;

&lt;p&gt;Angular allows you to authorize parent routes and child routes separately.&lt;/p&gt;

&lt;p&gt;🌟 &lt;strong&gt;Implementing Route Protection in Angular using CanActivate&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Angular supports the &lt;strong&gt;CanActivate&lt;/strong&gt; interface to authorize &lt;strong&gt;parent&lt;/strong&gt; routes. This interface is implemented by a class that determines whether the router can activate a specific route or not.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;CanActivate&lt;/strong&gt; Interface is defined in the &lt;code&gt;@angular/router&lt;/code&gt; package. This Interface has one method i.e. &lt;code&gt;canActivate&lt;/code&gt;. We need to implement it in our Service. The details of the &lt;code&gt;CanActivate&lt;/code&gt; interface is as shown below.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--4c_A0KYq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cq2k2agarmh3r6vdt08s.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--4c_A0KYq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cq2k2agarmh3r6vdt08s.png" alt="Image description" width="880" height="189"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;strong&gt;canActivate&lt;/strong&gt; method gets the instance of the &lt;code&gt;ActivatedRouteSnapshot&lt;/code&gt; and &lt;code&gt;RouterStateSnapshot&lt;/code&gt; which we can use to get access to the route parameter, query parameter etc.&lt;/li&gt;
&lt;li&gt;The guard must return true/false or a &lt;a href="https://angular.io/api/router/UrlTree"&gt;UrlTree&lt;/a&gt;. The return value can be in the form of observable or a promise or a simple &lt;code&gt;boolean&lt;/code&gt; value.&lt;/li&gt;
&lt;li&gt;A route can have more than one &lt;code&gt;canActivate&lt;/code&gt; guard.&lt;/li&gt;
&lt;li&gt;If &lt;strong&gt;all guards&lt;/strong&gt; returns &lt;code&gt;true&lt;/code&gt;, navigation to the route will continue.&lt;/li&gt;
&lt;li&gt;If &lt;strong&gt;any one of the guard&lt;/strong&gt; returns &lt;code&gt;false&lt;/code&gt;, navigation will be cancelled and we may write some logic to navigate to some other route in this case.&lt;/li&gt;
&lt;li&gt;The example of canActivate guard is as follows-
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { Injectable } from '@angular/core';
import {
  Router,
  CanActivate,
  ActivatedRouteSnapshot,
  RouterStateSnapshot,
} from '@angular/router';

@Injectable()
export class AuthGuardService implements CanActivate {
  constructor(private _router: Router) {}

  public canActivate(
    route: ActivatedRouteSnapshot,
    state: RouterStateSnapshot
  ): boolean {
    //check for some validation condition
    if (validation_Fails) {
      alert('You are not allowed to view this page');
      //redirect to login/home page etc
      //return false to cancel the navigation
      return false;
    }
    return true;
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;In the route configuration file, update the route definition with the canActivate guard as shown below. You can also apply more than one guard to a route.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;p&gt;🌟 &lt;strong&gt;Implementing Route Protection in Angular using CanActivateChild&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Angular supports the &lt;strong&gt;CanActivateChild&lt;/strong&gt; interface to authorize &lt;strong&gt;child&lt;/strong&gt; routes.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;CanActivateChild&lt;/strong&gt; Interface is defined in the &lt;code&gt;@angular/router&lt;/code&gt; package. This Interface has one method i.e. &lt;code&gt;canActivateChild&lt;/code&gt;. We need to implement it in our Service. The details of the &lt;code&gt;CanActivateChild&lt;/code&gt; interface is as shown below.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s---wyUHcr---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3zhohy47rrlcuosf4bs6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s---wyUHcr---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3zhohy47rrlcuosf4bs6.png" alt="Image description" width="880" height="181"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;strong&gt;canActivateChild&lt;/strong&gt; method gets the instance of the &lt;code&gt;ActivatedRouteSnapshot&lt;/code&gt; and &lt;code&gt;RouterStateSnapshot&lt;/code&gt; which we can use to get access to the route parameter, query parameter etc.&lt;/li&gt;
&lt;li&gt;The guard must return true/false or a &lt;a href="https://angular.io/api/router/UrlTree"&gt;UrlTree&lt;/a&gt;. The return value can be in the form of observable or a promise or a simple &lt;code&gt;boolean&lt;/code&gt; value.&lt;/li&gt;
&lt;li&gt;A route can have more than one &lt;code&gt;canActivateChild&lt;/code&gt; guard.&lt;/li&gt;
&lt;li&gt;If &lt;strong&gt;all guards&lt;/strong&gt; returns &lt;code&gt;true&lt;/code&gt;, navigation to the route will continue.&lt;/li&gt;
&lt;li&gt;If &lt;strong&gt;any one of the guard&lt;/strong&gt; returns &lt;code&gt;false&lt;/code&gt;, navigation will be cancelled and we may write some logic to navigate to some other route in this case.&lt;/li&gt;
&lt;li&gt;We apply &lt;strong&gt;CanActivateChild&lt;/strong&gt; guard to the parent route. The Angular invokes this guard whenever the user tries to navigate to any of its child route.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;🌟 &lt;strong&gt;Controlling navigation using CanDeactivate&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We use the &lt;strong&gt;CanDeactivate&lt;/strong&gt; guard when we want to prevent the user from accidentally navigating away. The best use case for &lt;code&gt;CanDectivate&lt;/code&gt; guard is the data entry component. The user may have filled the data entry and tries to leave that component without saving his work. The &lt;code&gt;CanDeactivate&lt;/code&gt; guard gives us a chance to warn the user that he has not saved his work and give him a chance to cancel the navigation.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion-&lt;/strong&gt;&lt;br&gt;
Overall, route protection is an essential part of building secure and user-friendly Angular applications. By using route guards and other protective measures, you can ensure that your application is accessible only to authorized users and that sensitive data is kept safe from unauthorized access.&lt;/p&gt;

</description>
      <category>angular</category>
    </item>
    <item>
      <title>Using Renderer2 in Angular</title>
      <dc:creator>Sameer Trimade</dc:creator>
      <pubDate>Tue, 21 Feb 2023 03:57:57 +0000</pubDate>
      <link>https://dev.to/sameer_trimade/using-renderer2-in-angular-498c</link>
      <guid>https://dev.to/sameer_trimade/using-renderer2-in-angular-498c</guid>
      <description>&lt;p&gt;In Angular, &lt;strong&gt;&lt;code&gt;Renderer2&lt;/code&gt;&lt;/strong&gt; is a class that provides a way to manipulate the DOM (Document Object Model) in a way that is safe and efficient.&lt;/p&gt;

&lt;p&gt;In Angular, &lt;strong&gt;&lt;code&gt;Renderer2&lt;/code&gt;&lt;/strong&gt; is a class that provides a way to manipulate the DOM (Document Object Model) in a way that is safe and efficient. It's a low-level API that abstracts away the differences between browsers and provides a platform-independent way to manipulate the DOM.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;Renderer2&lt;/code&gt;&lt;/strong&gt; provides a set of methods that you can use to create, modify, and remove DOM elements, as well as to set and remove attributes, styles, and event listeners. By using &lt;strong&gt;&lt;code&gt;Renderer2&lt;/code&gt;&lt;/strong&gt;, you can manipulate the DOM in a way that is safe and efficient, and that works seamlessly with Angular's change detection mechanism.&lt;/p&gt;

&lt;p&gt;It's important to use &lt;strong&gt;&lt;code&gt;Renderer2&lt;/code&gt;&lt;/strong&gt; methods to manipulate the DOM in Angular, rather than using the native DOM API directly.&lt;/p&gt;

&lt;p&gt;Here's a brief explanation of how to use &lt;strong&gt;&lt;code&gt;Renderer2&lt;/code&gt;&lt;/strong&gt;:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1) Inject Renderer2:&lt;/strong&gt; To use &lt;strong&gt;&lt;code&gt;Renderer2&lt;/code&gt;&lt;/strong&gt;, you need to inject it into your component or service. You can do this by adding it as a parameter in the constructor:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { Component, Renderer2 } from '@angular/core';

@Component({
  selector: 'my-component',
  template: '&amp;lt;div #myDiv&amp;gt;&amp;lt;/div&amp;gt;',
})
export class MyComponent {
  constructor(private renderer: Renderer2) {}
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;2) Use Renderer2 methods:&lt;/strong&gt; Once you have the &lt;strong&gt;&lt;code&gt;Renderer2&lt;/code&gt;&lt;/strong&gt; instance, you can use its methods to manipulate the DOM. For example, you can create a new element and append it to an existing element:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const newElement = this.renderer.createElement('div');
const existingElement = this.renderer.selectRootElement('#myDiv');
this.renderer.appendChild(existingElement, newElement);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;In above example, we're creating a new &lt;strong&gt;&lt;code&gt;div&lt;/code&gt;&lt;/strong&gt; element using the &lt;strong&gt;&lt;code&gt;createElement&lt;/code&gt;&lt;/strong&gt; method. Then, we're selecting the existing &lt;strong&gt;&lt;code&gt;div&lt;/code&gt;&lt;/strong&gt; element with the &lt;strong&gt;&lt;code&gt;selectRootElement&lt;/code&gt;&lt;/strong&gt; method, and appending the new element to it using the &lt;strong&gt;&lt;code&gt;appendChild&lt;/code&gt;&lt;/strong&gt; method.&lt;/p&gt;

&lt;p&gt;You can also use other methods of &lt;strong&gt;&lt;code&gt;Renderer2&lt;/code&gt;&lt;/strong&gt; to manipulate the DOM, such as &lt;strong&gt;&lt;code&gt;setAttribute&lt;/code&gt;&lt;/strong&gt;, &lt;strong&gt;&lt;code&gt;removeClass&lt;/code&gt;&lt;/strong&gt;, &lt;strong&gt;&lt;code&gt;setStyle&lt;/code&gt;&lt;/strong&gt;, and so on.&lt;/p&gt;

&lt;p&gt;Methods provided by the &lt;strong&gt;&lt;code&gt;Renderer2&lt;/code&gt;&lt;/strong&gt; class in Angular&lt;/p&gt;

&lt;p&gt;Here are some of the methods provided by the &lt;strong&gt;&lt;code&gt;Renderer2&lt;/code&gt;&lt;/strong&gt; class in Angular:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;code&gt;createElement(name: string, namespace?: string): any&lt;/code&gt;&lt;/strong&gt;: This method creates a new element with the given tag name and namespace (giving namespace to createElement method is optional).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;code&gt;createText(text: string): any&lt;/code&gt;&lt;/strong&gt;: This method will create a new text node with the given text content.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;code&gt;appendChild(parent: any, newChild: any): void&lt;/code&gt;&lt;/strong&gt;: This method will append a child node to the end of the parent node.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;code&gt;removeChild(parent: any, oldChild: any): void&lt;/code&gt;&lt;/strong&gt;: This method will remove a child node from the parent node.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;code&gt;selectRootElement(selectorOrNode: string | any): any&lt;/code&gt;&lt;/strong&gt;: This method will find the root element of a view using a selector string or a node reference.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;code&gt;setAttribute(el: any, name: string, value: string, namespace?: string): void&lt;/code&gt;&lt;/strong&gt;: This method will set an attribute value for an element.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;code&gt;removeAttribute(el: any, name: string, namespace?: string): void&lt;/code&gt;&lt;/strong&gt;: This method will remove an attribute from an element.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;code&gt;addClass(el: any, name: string): void&lt;/code&gt;&lt;/strong&gt;: This method will add a CSS class to an element.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;code&gt;removeClass(el: any, name: string): void&lt;/code&gt;&lt;/strong&gt;: This method will remove a CSS class from an element.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;code&gt;setStyle(el: any, style: string, value: any, flags?: RendererStyleFlags2): void&lt;/code&gt;&lt;/strong&gt;: This method will set a CSS style property for an element.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;code&gt;removeStyle(el: any, style: string, flags?: RendererStyleFlags2): void&lt;/code&gt;&lt;/strong&gt;: This method will remove a CSS style property from an element.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;code&gt;setProperty(el: any, name: string, value: any): void&lt;/code&gt;&lt;/strong&gt;: This method will set a property value for an element.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;code&gt;listen(target: any, eventName: string, callback: (event: any) =&amp;gt; boolean | void): () =&amp;gt; void&lt;/code&gt;&lt;/strong&gt;: This method will register an event listener for a DOM element.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;These are just some of the methods provided by &lt;strong&gt;&lt;code&gt;Renderer2&lt;/code&gt;&lt;/strong&gt;. There are many more methods that you can use to manipulate the DOM in a safe and efficient way in your Angular application.&lt;/p&gt;

</description>
      <category>css</category>
      <category>pixelart</category>
      <category>officeculture</category>
    </item>
    <item>
      <title>Observables and RxJs operators in Angular.</title>
      <dc:creator>Sameer Trimade</dc:creator>
      <pubDate>Mon, 13 Feb 2023 18:31:09 +0000</pubDate>
      <link>https://dev.to/sameer_trimade/observables-and-rxjs-operators-in-angular-4bi6</link>
      <guid>https://dev.to/sameer_trimade/observables-and-rxjs-operators-in-angular-4bi6</guid>
      <description>&lt;h2&gt;
  
  
  What are Observables in Angular?
&lt;/h2&gt;

&lt;p&gt;An Observable can be thought of as a data source. In Angular project, an Observable is basically an Object we import from a third party package called RxJs. The Observable is implemented in such a way that it follows the observable pattern that means we have an &lt;strong&gt;Observable&lt;/strong&gt;, then we have an &lt;strong&gt;Observer&lt;/strong&gt; and in between we have a stream(or timeline) and on this timeline we can have multiple events emitted by the Observable. So, an Observable could emit data because you trigger it to do so, for example-&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Suppose, an Observable is connected to a button so whenever the button is clicked an event in a data package is emitted automatically.&lt;/li&gt;
&lt;li&gt;Suppose, an Observable is connected to HTTP request so when the response returns, the response is emitted as a data package.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Observer- is actually our own code meaning it is the &lt;strong&gt;.subscribe()&lt;/strong&gt; function where we have 3 ways of handling data packages-&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;handle data of the Observable&lt;/li&gt;
&lt;li&gt;handle error of the Observable&lt;/li&gt;
&lt;li&gt;handle completion of the Observable&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;We use Observables to handle Asynchronous tasks (because all the data sources like User events, Http request etc. are Asynchronous tasks, meaning you don’t know when these tasks will happen and how long they will take to finish. So when you execute your normal application code, you don’t want to wait for these events or you don’t want to wait for completion of such HTTP request because that would block execution of your normal application code).&lt;/p&gt;

&lt;p&gt;In the below piece of code 👇, we are using &lt;strong&gt;params&lt;/strong&gt; as an Observable and here we do &lt;strong&gt;subscribe&lt;/strong&gt; to the Observable to be informed about changes in the data.&lt;br&gt;
&lt;strong&gt;params&lt;/strong&gt; is a stream of route parameters and this stream gives us a new route parameter whenever we go to a new page, whenever that parameter in the URL changes and then in the .&lt;strong&gt;subscribe&lt;/strong&gt; function we get new route parameter.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="nf"&gt;ngOnInit&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;route&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;params&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;subscribe&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;paramsData&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Params&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="nx"&gt;paramsData&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;;&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;p&gt;You don’t need to unsubscribe to the Observables that are provided by Angular (It is internally  done by Angular). For example- the &lt;strong&gt;params&lt;/strong&gt; Observable is provided by Angular so we don’t need to unsubscribe it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;NOTE&lt;/strong&gt;: Observable is a feature which is not provided by JavaScript or Typescript, instead it is provided by RxJs which is a 3rd party package.&lt;/p&gt;

&lt;p&gt;In the below piece of code 👇,  we are using a function called &lt;strong&gt;interval()&lt;/strong&gt; provided by the &lt;strong&gt;RxJs&lt;/strong&gt; package. The &lt;strong&gt;interval()&lt;/strong&gt; function &lt;strong&gt;returns an Observable&lt;/strong&gt; that emits sequential numbers after every specified interval of time(in milliseconds).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&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;Component&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;OnDestroy&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;OnInit&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="s1"&gt;@angular/core&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;interval&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Subscription&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="s1"&gt;rxjs&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Component&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;selector&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;app-home&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;templateUrl&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./home.component.html&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;styleUrls&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./home.component.css&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&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;class&lt;/span&gt; &lt;span class="nc"&gt;HomeComponent&lt;/span&gt; &lt;span class="k"&gt;implements&lt;/span&gt; &lt;span class="nx"&gt;OnInit&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;OnDestroy&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="nx"&gt;observableSubscription$&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Subscription&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;

  &lt;span class="nf"&gt;ngOnInit&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;observableSubscription$&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;interval&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;subscribe&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// ---&amp;gt; Output: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 …………so on&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nf"&gt;ngOnDestroy&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;observableSubscription$&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;unsubscribe&lt;/span&gt;&lt;span class="p"&gt;();&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;p&gt;&lt;strong&gt;interval()&lt;/strong&gt; function returns an Observable therefore we need to subscribe to this Observable to receive the emitted value. Looking at the above implementation, the &lt;strong&gt;interval()&lt;/strong&gt; function after every 1 second will  print the emitted value on the browser console.&lt;/p&gt;

&lt;p&gt;(With respect to above piece of code) ——&amp;gt; If the HomeComponent is destroyed, maybe via navigating away from the component or using the &lt;code&gt;destroy(...)&lt;/code&gt;method, we will still be seeing the console log on the browser. This is because the AppComponent has been destroyed but the subscription still lives on, it hasn't been canceled.&lt;/p&gt;

&lt;p&gt;If a subscription is not closed the function callback attached to it will be continuously called, this poses a huge memory leak and will slow down the overall app performance.&lt;/p&gt;

&lt;p&gt;To prevent this memory leaks we have to unsubscribe from the subscriptions, we do so by calling the &lt;code&gt;unsubscribe&lt;/code&gt; method in the Observable.&lt;/p&gt;

&lt;p&gt;In Angular, we have to unsubscribe from the Observable when the component is being destroyed. Luckily, Angular has a &lt;code&gt;ngOnDestroy&lt;/code&gt; hook that is called before a component is destroyed, this enables developer to call the &lt;code&gt;unsubscribe&lt;/code&gt; method which will avoid hanging subscriptions, open portals, and all that may come in the future to bite us back.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;NOTE-&lt;/strong&gt;&lt;br&gt;
Whenever we use Observables in a component in Angular, we should set up the &lt;strong&gt;ngOnDestroy&lt;/strong&gt; method, and call the &lt;strong&gt;unsubscribe&lt;/strong&gt; method on all of the Observables.&lt;/p&gt;
&lt;h2&gt;
  
  
  RxJs Operators in Observable-
&lt;/h2&gt;

&lt;p&gt;Sometimes we don’t want the raw data from the &lt;strong&gt;.subscription()&lt;/strong&gt; method. We might want to transform this raw data or filter some data points. We can do this inside the &lt;strong&gt;.subscription()&lt;/strong&gt; method but there is another better approach. We can make use of &lt;strong&gt;Operators&lt;/strong&gt; between an Observable and a &lt;strong&gt;.subscription()&lt;/strong&gt; method, this means that the data points will first reach the &lt;strong&gt;Operators&lt;/strong&gt; that manipulates the data and then we use .&lt;strong&gt;subscription()&lt;/strong&gt; method to get the result of these operators.&lt;br&gt;
We can use Operators on any Observable by calling a method called .&lt;strong&gt;pipe()&lt;/strong&gt; which is provided by the RxJs package. &lt;/p&gt;

&lt;p&gt;Here are some commonly used RxJS operators in Angular:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1) map operator-&lt;/strong&gt;&lt;br&gt;
The map operator takes an Observable as its input and returns a new Observable as its output. The new Observable emits the values from the original Observable, but transformed by the function you provide to the map operator. The function takes each value from the original Observable and applies a transformation to it, returning a new value that is emitted by the new Observable.&lt;br&gt;
Here's a simple example of using the map operator to double the values of an Observable:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { of } from 'rxjs';
import { map } from 'rxjs/operators';

const numbers = of(1, 2, 3);
const doubledNumbers = numbers.pipe(map(x =&amp;gt; x * 2));

doubledNumbers.subscribe(x =&amp;gt; console.log(x));
// Output: 2, 4, 6
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, the numbers Observable emits the values 1, 2, and 3. The map operator takes this Observable as input and returns a new Observable, doubledNumbers, which emits the values 2, 4, and 6, which are the original values doubled.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2) filter operator-&lt;/strong&gt;&lt;br&gt;
The filter operator takes an Observable as its input and returns a new Observable as its output. The new Observable emits only the values from the original Observable that pass a test specified by the function you provide to the filter operator.&lt;/p&gt;

&lt;p&gt;Here's a simple example of using the filter operator to only emit even numbers from an Observable:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { of } from 'rxjs';
import { filter } from 'rxjs/operators';

const numbers = of(1, 2, 3, 4, 5);
const evenNumbers = numbers.pipe(filter(x =&amp;gt; x % 2 === 0));

evenNumbers.subscribe(x =&amp;gt; console.log(x));
// Output: 2, 4
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, the numbers Observable emits the values 1, 2, 3, 4, and 5. The filter operator takes this Observable as input and returns a new Observable, evenNumbers, which emits only the values that are even numbers, in this case 2 and 4.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3) tap operator-&lt;/strong&gt;&lt;br&gt;
The tap operator takes an Observable as its input and returns the same Observable as its output. The difference is that it allows you to perform a side effect, such as logging or performing a state update, for each value emitted by the Observable.&lt;/p&gt;

&lt;p&gt;Here's a simple example of using the tap operator to log each value emitted by an Observable:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { of } from 'rxjs';
import { tap } from 'rxjs/operators';

const numbers = of(1, 2, 3);
const loggedNumbers = numbers.pipe(tap(x =&amp;gt; console.log(x)));

loggedNumbers.subscribe();
// Output: 1, 2, 3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, the numbers Observable emits the values 1, 2, and 3. The tap operator takes this Observable as input and returns the same Observable, but with a side effect that logs each value. When the loggedNumbers Observable is subscribed to, it logs the values 1, 2, and 3.&lt;/p&gt;

&lt;p&gt;Note that the tap operator doesn't modify the values emitted by the Observable. It's only used to perform side effects. If you need to modify the values, you can use other operators like map or filter in combination with tap.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4) switchMap operator-&lt;/strong&gt;&lt;br&gt;
The switchMap operator takes an Observable as its input and returns a new Observable as its output. The new Observable emits values from the most recent inner Observable created by the function you provide to the switchMap operator. The function takes each value from the original Observable and maps it to a new inner Observable.&lt;/p&gt;

&lt;p&gt;Here's a simple example of using the switchMap operator to fetch data from an API based on user input:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { of, fromEvent } from 'rxjs';
import { switchMap } from 'rxjs/operators';

const input = document.querySelector('input');
const input$ = fromEvent(input, 'input');

const apiCall = query =&amp;gt; of({ query, data: `Result for ${query}` });

const data$ = input$.pipe(
  switchMap(event =&amp;gt; apiCall(event.target.value))
);

data$.subscribe(data =&amp;gt; console.log(data));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, the input$ Observable emits events whenever the user types in the input field. The switchMap operator takes this Observable as input and returns a new Observable, data$, which emits values from the API call. The API call is made using the apiCall function, which takes the current input value and returns an Observable with the API response.&lt;/p&gt;

&lt;p&gt;Each time a new value is emitted by the input$ Observable, the switchMap operator cancels any previous inner Observables and creates a new one based on the latest value. This allows for the most recent API request to be used, and any previous requests to be discarded.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5) finalize operator-&lt;/strong&gt;&lt;br&gt;
The finalize operator takes an Observable as its input and returns the same Observable as its output. The difference is that it allows you to perform a side effect, such as cleanup or resource disposal, when the Observable completes or errors.&lt;/p&gt;

&lt;p&gt;Here's a simple example of using the finalize operator to log a message when an Observable completes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { of } from 'rxjs';
import { finalize } from 'rxjs/operators';

const numbers = of(1, 2, 3);
const loggedNumbers = numbers.pipe(finalize(() =&amp;gt; console.log('complete')));

loggedNumbers.subscribe();
// Output: 1, 2, 3, complete
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, the numbers Observable emits the values 1, 2, and 3. The finalize operator takes this Observable as input and returns the same Observable, but with a side effect that logs a message when the Observable completes. When the loggedNumbers Observable is subscribed to, it logs the values 1, 2, and 3, and then logs the message "complete".&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;To summarize&lt;/strong&gt;, these are just a few of the many operators available in RxJS. Operators can be combined to create complex data processing pipelines, allowing you to handle and transform data in a variety of ways.&lt;/p&gt;

</description>
      <category>productivity</category>
    </item>
    <item>
      <title>CSS Methodologies</title>
      <dc:creator>Sameer Trimade</dc:creator>
      <pubDate>Wed, 12 Jan 2022 14:18:10 +0000</pubDate>
      <link>https://dev.to/sameer_trimade/css-methodologies-2cl0</link>
      <guid>https://dev.to/sameer_trimade/css-methodologies-2cl0</guid>
      <description>&lt;p&gt;&lt;strong&gt;Writing CSS is easy. Making it scalable and maintainable is not.&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You must have experienced the scenario when you update the CSS of your application and you broke something else?&lt;/li&gt;
&lt;li&gt;did you wonder where the CSS you have to change is coming from?&lt;/li&gt;
&lt;li&gt;did you update some HTML and it broke your design?&lt;/li&gt;
&lt;li&gt;did you write some CSS and wonder why it wasn’t applied to the browser and then ultimately discovered that it was overridden by some other CSS?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is when you decide there is a better way and come across some CSS methodologies, which seems like a good solution to all those headaches.&lt;/p&gt;

&lt;p&gt;There are multiple CSS methodologies , but at the end of the day, it’s all about what fits your projects.&lt;/p&gt;

&lt;p&gt;Also, you may think you’re perfectly fine without them and you may be right. But you might be missing out on big improvements too. You should at least have an idea on what’s out there and why you're or you’re not using it.&lt;/p&gt;

&lt;p&gt;⭐ It’s all about maintaining your CSS code and reusability ⭐ &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Big Reveal !!!&lt;/strong&gt;&lt;br&gt;
There is no best CSS methodology. It depends on you and the type of your project.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Top 3 CSS methodologies:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;OOCSS (Object Oriented CSS)&lt;/li&gt;
&lt;li&gt;BEM (Block Element Modifier)&lt;/li&gt;
&lt;li&gt;SMACSS (Scalable and Modular Architecture CSS)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Applying a modular approach to your CSS will save you hours.&lt;br&gt;
Web components are the future of the web and starting now will make your life easier.&lt;br&gt;
Each piece of your application helps you build the final results and you should be able to move or replace it without breaking anything else. This is the goal most CSS methodologies aim for.&lt;/p&gt;
&lt;h2&gt;
  
  
  OOCSS Methodology
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;OOCSS stands for Object Oriented CSS.&lt;/li&gt;
&lt;li&gt;OOCSS is concerned with converting your regular CSS styles to reusable classes.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;OOCSS has two major principles&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;1. Separate structure and skin:&lt;/strong&gt;&lt;br&gt;
The structure refers to invisible styles applied to elements (width, height, margin, padding) while the skin is the visible styles (colors, fonts, shadows).&lt;br&gt;
OOCSS defines these 2 separately. For example, Below there is a random snippet of CSS:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.button-1 {
    width: 100px;
    height: 50px;
    background: #000;
    color: #fff;
 }

 .button-2 {
    width: 100px;
    height: 50px;
    background: #fff;
    color: #333;
 }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can see in the previous example that both classes have the same structure but differs in the skin properties. OOCSS deals with this situation and separate them as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.button-1 {
    background: #000;
    color: #fff;
 }

 .button-2 {
    background: #fff;
    color: #333;
 }

 .btn-structure {
    width: 100px;
    height: 50px;
 }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we don't have redundant code and the .btn-structure class can be used on any button having the same structure.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Separate container and content:&lt;/strong&gt;&lt;br&gt;
Content refers to the elements such as images, paragraphs, divs which are nested inside other elements that serve as Containers. CSS Styles used for Content elements should be independent of the Container class so that it can be used without restrictions on their parent element. For example, this is a random piece of code dealing with the styling for a sidebar component.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#sidebar {
    left: 0;
    margin: 3px;
    position: absolute;
    width: 140px;
}

#sidebar .list {
    margin: 3px;
}
#sidebar .list .list-header {
    font-size: 16px;
    color: red;
} 

#sidebar .list .list-body {
    font-size: 12px;
    color: #FFF;
    background-color: red;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But if we separate content from container, it will something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#sidebar {
  padding: 2px;
  left: 0;
  margin: 3px;
  position: absolute;
  width: 140px;
}

.list {
  margin: 3px;
}

.list-header {
  font-size: 16px;
  color: red
}

.list-body {
  font-size: 12px;
  color: #FFF;
  background-color: red;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now these classes can be used in many situations without the restriction of having a parent with an id of &lt;strong&gt;#sidebar&lt;/strong&gt; or &lt;strong&gt;.list&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Advantages of OOCSS:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Improved Scalability and Reusability.&lt;/li&gt;
&lt;li&gt;Better Readability.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Disadvantages of OOCSS:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It is not suitable for small projects.&lt;/li&gt;
&lt;li&gt;It increases number of classes.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  BEM Methodology
&lt;/h2&gt;

&lt;p&gt;BEM stands for Block-Element-Modifier.&lt;br&gt;
When the use of CSS is no longer to just style the personal websites or small projects and moves to large, scalable and complex projects, there is a need to organize and think about the architecture for the CSS. Simple things like class naming can become a huge problem in future maintenance.&lt;br&gt;
To correct problems like these, some organizational architectures and methodologies were created and one of the most popular is BEMCSS.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What is BEM?&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The acronym comes from &lt;strong&gt;Block-Element-Modifier&lt;/strong&gt;, which is basically a method to create names for the CSS classes. &lt;/li&gt;
&lt;li&gt;The idea is to create a rigid standard for class naming, making it easy to read and understand what the class does and what component it targets.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;⭐ The class names follow the line&lt;br&gt;
&lt;strong&gt;.block__element--modifier {}&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;One point to Note here is that there are Two underlines that separate the block from the element and two lines that separate the element from the modifier.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Block:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A block is an independent, modular UI component. &lt;/li&gt;
&lt;li&gt;A block may be composed of multiple HTML elements, or even multiple blocks. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So basically a Block is the element or component that a class is assigned to. You don't need to respect the name of the element, the idea is that it describes what the component is doing in the scope of the project. For example, if you create a class for a login form, the name would not be .form, even though in HTML you do create a form.&lt;br&gt;
In the scope of the project the class could be &lt;strong&gt;.Login&lt;/strong&gt;, which would make it clear to everyone what it is about. A general naming rule is to separate compound names with a single dash for example,  &lt;strong&gt;.select-box-container&lt;/strong&gt; would be the correct way to write a name with three words.&lt;/p&gt;

&lt;p&gt;For example, &lt;strong&gt;.login&lt;/strong&gt;, &lt;strong&gt;.select-box-container&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Element:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;An element is a component of a block. Each element serves a singular purpose.&lt;/li&gt;
&lt;li&gt;For example, if you have a navigation menu block, then elements of it might be your navigation menu’s links, which in turn might be in the form of list items (li elements) and anchor tag’s (a elements).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The Element are internal parts of the component. In the example shown in previous slide, inside the login block there would be multiple inputs for the username, email, password and few action button's.&lt;br&gt;
So the CSS class naming convention for block-element will be like,&lt;br&gt;
&lt;strong&gt;.login__username&lt;/strong&gt;, &lt;strong&gt;.login__email&lt;/strong&gt;, &lt;strong&gt;.login__password&lt;/strong&gt;, &lt;strong&gt;.login__button&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Modifier:&lt;/strong&gt;&lt;br&gt;
A modifier is a CSS class that changes the default presentation of a block or element.&lt;br&gt;
For example, in the login form when the user put something that does not match the password or email in the inputs and there was a check to confirm this, the visual feedback of the error, for example, could be done by changing the colors of the edges of the incorrect inputs to red. The modifier class that would be used to create this case will be in this way:&lt;br&gt;
 &lt;strong&gt;.login__email--error {}&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
The modifiers can also be used for the block as a whole, and can be applied directly over it in the class, ignoring the existence of the elements. For example, &lt;strong&gt;.login--error {}&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Point to remember:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We have to remember that each element has its class in an isolated way. For example, if an element has another element within it, it is not correct to join the elements into a single class, such as:
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.block__class1__class2__class3 {}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;In these cases each element has its own class, ignoring the order of the HTML structure, such as:
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.block {}
.block__class1 {}
.block__class2 {}
.block__class3 {}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;Advantages of using BEM:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;BEM is a very robust class-naming convention. &lt;/li&gt;
&lt;li&gt;It successfully distinguishes the different concerns that classes are used for.&lt;/li&gt;
&lt;li&gt;It is easy to see in the markup which classes are related to one another.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Disadvantages of using BEM:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The class names can end up being long and ugly.&lt;/li&gt;
&lt;li&gt;The naming convention is not intuitive to inexperienced developers.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  SMACSS Methodology
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;SMACSS stands for Scalable and Modular Architecture for CSS.&lt;/li&gt;
&lt;li&gt;SMACSS offers a simpler naming convention than BEM.&lt;/li&gt;
&lt;li&gt;There are no class names for base styles because only type selectors like { h1, p, a, etc. } are used for those.&lt;/li&gt;
&lt;li&gt; Modules are given their own unique class names. Also, in SMACSS the Sub-components and variations are prefixed with the name of their parent module.&lt;/li&gt;
&lt;li&gt;SMACSS is a style guide rather than a framework.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;SMACSS is all about organising your CSS in &lt;strong&gt;5 Categories&lt;/strong&gt;:&lt;br&gt;
1) &lt;strong&gt;Base:&lt;/strong&gt; Base rule is applied to an element using an element selector, a descendent selector, or a child selector, along with any pseudo-classes. It is defining the default styling for how that element should look in all occurrences on the page.&lt;br&gt;
This category include applying CSS to the selector. No CSS is applied to classes or id here. This is to reset browser rules and set a base style for elements which are going to be consistent and reused. &lt;br&gt;
In this category you are defining the default style for your elements. This can include html, body, h1, h2, h3, h4, h5, h6, a, img.&lt;/p&gt;

&lt;p&gt;An example for the BASE rule is given here,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;body, form {
  margin: 0;
  padding: 0;
}

a {
  color: #039;
}

a:hover {
  color: #03F;    
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;2) &lt;strong&gt;Layout:&lt;/strong&gt; &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;This is where the style used to lay out your pages will sit. It should be separated from your module style for flexibility. &lt;/li&gt;
&lt;li&gt;Layout styles can also be divided into major and minor styles based on reuse. &lt;/li&gt;
&lt;li&gt;Major layout styles such as header and footer are traditionally styled using ID selectors but it takes time to think about the elements that are common across all components of the page and use class selectors where appropriate. &lt;/li&gt;
&lt;li&gt;Therefore, Usually layout modules are not many so id selector can be used.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;An example for the LAYOUT rule is given here,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#header, #article, #footer {
  width: 960px;
  margin: auto;
}

#article {
  border: solid #CCC;
  border-width: 1px 0 0;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;3) &lt;strong&gt;Module:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A module is a part or a component of your page. Your menu, dialog box, download list or any widget you have on your page can be a module.&lt;/li&gt;
&lt;li&gt;A module is independent from your layout so it can live anywhere in you application.&lt;/li&gt;
&lt;li&gt;You should be able to copy/paste the html and move somewhere else, and it will look and behave the same.&lt;/li&gt;
&lt;li&gt;Each Module should be designed to exist as a standalone component. In doing so, the page will be more flexible. If done right, Modules can easily be moved to different parts of the layout without breaking.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;An example for the MODULE rule is given here,&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;ul class="tabnav"&amp;gt;
    &amp;lt;li class="" tabnav__item"&amp;gt;&amp;lt;a href="" class="tabnav__link"&amp;gt;1&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;
    &amp;lt;li&amp;gt;&amp;lt;a href=""&amp;gt;2&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;
    &amp;lt;li&amp;gt;&amp;lt;a href=""&amp;gt;3&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;
    &amp;lt;li&amp;gt;&amp;lt;a href=""&amp;gt;4&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;
&amp;lt;/ul&amp;gt;
&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;.tabnav {
  display: flex;
}

.tabnav li {
  border-top: 1px solid black;
  border-right: 1px solid black;
  border-bottom: 1px solid black;
}

.tabnav li:first-child {
  border-left: 1px solid black;
}

.tabnav li a {
  display: block;
  padding: 10px 30px;
  text-decoration: none;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;4) &lt;strong&gt;State:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A state will be a style which modifies or overrides other rules. &lt;/li&gt;
&lt;li&gt;Good practise is to prefix or add a namespace to the classes which fall under this category like &lt;strong&gt;is-&lt;/strong&gt; or &lt;strong&gt;has-&lt;/strong&gt; --&amp;gt; is-hidden, is-displayed, is-collapsed, has-children, etc.&lt;/li&gt;
&lt;li&gt;A great example is accordion when collapsing or expanding elements. Using a is-collapsed class will make sure your element is collapsed. &lt;/li&gt;
&lt;li&gt;This is a good place to use !important (and probably the only one) as you want this state to be applied no matter what.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.is-active {
    background-color: red;
}

.is-hidden {
    pointer-events: none;
    color: black;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;5) &lt;strong&gt;Theme:&lt;/strong&gt;&lt;br&gt;
In Theme rule, the idea is to have a file called theme.css where you can define all the theme rules. For example,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// in module-name.css
.mod {
    border: 1px solid;
}

// in theme.css
.mod {
    border-color: blue;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Advantages of using SMACSS:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Modular.&lt;/li&gt;
&lt;li&gt;Flexible.&lt;/li&gt;
&lt;li&gt;Better file organisation.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Disadvantages of using SMACSS:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;No specific naming convention to write a class.&lt;/li&gt;
&lt;li&gt;Modules and submodules can be hard to identify.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;*&lt;em&gt;Thank you for reading this blog post!!! 🙂 *&lt;/em&gt;&lt;/p&gt;

</description>
      <category>css</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
