<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/"
    xmlns:atom="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
    <channel>
        
        <title>
            <![CDATA[ Israel Oyetunji - freeCodeCamp.org ]]>
        </title>
        <description>
            <![CDATA[ Browse thousands of programming tutorials written by experts. Learn Web Development, Data Science, DevOps, Security, and get developer career advice. ]]>
        </description>
        <link>https://www.freecodecamp.org/news/</link>
        <image>
            <url>https://cdn.freecodecamp.org/universal/favicons/favicon.png</url>
            <title>
                <![CDATA[ Israel Oyetunji - freeCodeCamp.org ]]>
            </title>
            <link>https://www.freecodecamp.org/news/</link>
        </image>
        <generator>Eleventy</generator>
        <lastBuildDate>Wed, 27 May 2026 20:46:31 +0000</lastBuildDate>
        <atom:link href="https://www.freecodecamp.org/news/author/IsraelMitolu/rss.xml" rel="self" type="application/rss+xml" />
        <ttl>60</ttl>
        
            <item>
                <title>
                    <![CDATA[ JavaScript Geolocation API Tutorial – How to Get a User's Location in JS ]]>
                </title>
                <description>
                    <![CDATA[ Some applications require you to know your user's location, like food delivery or eCommerce apps. So you'll need an efficient way to get this info. The Geolocation API, which we will look at today, is a simple solution. You can use it to determine yo... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-to-get-user-location-with-javascript-geolocation-api/</link>
                <guid isPermaLink="false">66d45f33052ad259f07e4ae2</guid>
                
                    <category>
                        <![CDATA[ api ]]>
                    </category>
                
                    <category>
                        <![CDATA[ JavaScript ]]>
                    </category>
                
                    <category>
                        <![CDATA[ General Programming ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Web Development ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Israel Oyetunji ]]>
                </dc:creator>
                <pubDate>Wed, 07 Sep 2022 18:29:36 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2022/09/Blog-article-cover-images--4---1-.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>Some applications require you to know your user's location, like food delivery or eCommerce apps. So you'll need an efficient way to get this info.</p>
<p>The Geolocation API, which we will look at today, is a simple solution. You can use it to determine your users' location, local currency, language, and other useful information. You can then use this to provide them with the most relevant content based on their location.</p>
<p>In this article, we'll go over what the Geolocation API is, why it's useful, and how to use it in your apps.</p>
<h2 id="heading-what-is-the-geolocation-api">What Is the Geolocation API?</h2>
<p>The JavaScript Geolocation API provides access to geographical location data associated with a user's device. This can be determined using GPS, WIFI, IP Geolocation and so on.</p>
<p>To protect the user's privacy, it requests permission to locate the device. If the user grants permission, you will gain access to location data such as latitude, longitude, altitude, and speed. You'll also get the accuracy of the acquired location data and the approximate time when the position was acquired.</p>
<p>Here are a few uses for geolocation:</p>
<ul>
<li><p>Show user's position on a map</p>
</li>
<li><p>Get up-to-date local information</p>
</li>
<li><p>Show local Points-of-interest (POI) near the user</p>
</li>
<li><p>Enable Turn-by-turn navigation (GPS)</p>
</li>
<li><p>Track a fleet or delivery vehicle</p>
</li>
<li><p>Tag photographs with a location</p>
</li>
</ul>
<h2 id="heading-how-to-use-the-geolocation-api">How to Use the Geolocation API</h2>
<p>You can access the Geolocation API by calling the <code>navigator.geolocation</code> object. It grants the app access to the device's location.</p>
<p>This object provides the methods listed below for working with the device's position:</p>
<ol>
<li><p>getCurrentPosition: Returns the current location of the device.</p>
</li>
<li><p>watchPosition: A handler function that is automatically invoked when the device's location changes.</p>
</li>
</ol>
<p>There are three possible arguments with these methods:</p>
<ul>
<li><p>A success callback (required)</p>
</li>
<li><p>An error callback (optional)</p>
</li>
<li><p>An options object (optional)</p>
</li>
</ul>
<h3 id="heading-how-to-get-user-location-with-getcurrentposition">How to Get User Location with <code>getCurrentPosition()</code></h3>
<p>You can use the <code>getCurrentPosition</code> method to get the user's current location. It sends an asynchronous request to the browser, asking for consent to share their location.</p>
<p>Here is the syntax for getting a user's location:</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> successCallback = <span class="hljs-function">(<span class="hljs-params">position</span>) =&gt;</span> {
  <span class="hljs-built_in">console</span>.log(position);
};

<span class="hljs-keyword">const</span> errorCallback = <span class="hljs-function">(<span class="hljs-params">error</span>) =&gt;</span> {
  <span class="hljs-built_in">console</span>.log(error);
};

navigator.geolocation.getCurrentPosition(successCallback, errorCallback);
</code></pre>
<p>When you run this, you'll get a popup in the browser requesting permission:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/09/popup.PNG" alt="Image" width="600" height="400" loading="lazy"></p>
<p>Click <strong>Allow</strong>, and open up the developer console. You'll see that the successful call returns two things:</p>
<ol>
<li><p><code>GeolocationPosition.coords</code> object: represents the position, altitude and the accuracy at which the device calculates these properties.</p>
</li>
<li><p><code>timestamp</code>: represents the time at which the location was gotten.</p>
</li>
</ol>
<p>You should see something like this in your console:</p>
<pre><code class="lang-plaintext">GeolocationPosition {coords: GeolocationCoordinates, timestamp: 1662499816712}
    coords: GeolocationCoordinates
        accuracy: 7173.528443511279
        altitude: null
        altitudeAccuracy: null
        heading: null
        latitude: 6.5568768
        longitude: 3.3488896
        speed: null
        [[Prototype]]: GeolocationCoordinates
timestamp: 1662499816712
</code></pre>
<p>With this simple request, we've successfully gotten the location. But that's not all. We can also track the user by watching their location.</p>
<h3 id="heading-how-to-track-user-location-with-watchposition">How to Track User Location with <code>watchPosition()</code></h3>
<p>The <code>watchPosition()</code> method allows the app to continually track the user, and get updated as their position changes. It does this by installing a handler function that will be called automatically whenever the user's device position changes.</p>
<p>Here is the syntax below, where <code>id</code> is basically used to manage or reference the method:</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> id = navigator.geolocation.watchPosition(successCallback, errorCallback);
</code></pre>
<h3 id="heading-how-to-stop-tracking-position-with-clearwatch">How to Stop Tracking Position with <code>clearWatch()</code></h3>
<p>We use the <code>clearWatch()</code> method to cancel the handler functions that were previously installed using the <code>watchPosition</code>.</p>
<pre><code class="lang-js">navigator.geolocation.clearWatch(id);
</code></pre>
<h3 id="heading-how-to-use-the-options-object">How to Use the <code>options</code> Object</h3>
<p>Although the <a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/API/Geolocation/getCurrentPosition#parameters"><code>options</code></a> object is optional, it provides parameters that can help you get more accurate results, for example:</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> options = {
  <span class="hljs-attr">enableHighAccuracy</span>: <span class="hljs-literal">true</span>,
  <span class="hljs-attr">timeout</span>: <span class="hljs-number">10000</span>,
};

navigator.geolocation.getCurrentPosition(
  successCallback,
  errorCallback,
  options
);
</code></pre>
<p>In the code above, we specified in out options object that:</p>
<ul>
<li><p>The response should provide a more accurate position, by setting enableHighAccuracy to true.</p>
</li>
<li><p>The maximum length of time (in milliseconds) the device is allowed to take in order to return a position. In this case, 10 seconds.</p>
</li>
</ul>
<h2 id="heading-wrapping-up">Wrapping Up</h2>
<p>In this article, we learned about the JavaScript Geolocation API, how to use it to get a user's location and also track the user using the watchPosition() method.</p>
<p>You can go further to explore this API by building a Weather app, Search App or Map app. Thanks for reading!</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to Import SVGs in a React and Vite app ]]>
                </title>
                <description>
                    <![CDATA[ Are you having difficulties importing SVGs into your React app? This is a problem that many developers face, especially when setting up a new React app with a module bundler. In this article, I will share with you the different ways of importing SVGs... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-to-import-svgs-in-react-and-vite/</link>
                <guid isPermaLink="false">66d45f35052ad259f07e4ae6</guid>
                
                    <category>
                        <![CDATA[ General Programming ]]>
                    </category>
                
                    <category>
                        <![CDATA[ React ]]>
                    </category>
                
                    <category>
                        <![CDATA[ SVG ]]>
                    </category>
                
                    <category>
                        <![CDATA[ vite ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Web Development ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Israel Oyetunji ]]>
                </dc:creator>
                <pubDate>Fri, 01 Jul 2022 22:15:02 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2022/07/Blog-article-cover-images--3-.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>Are you having difficulties importing SVGs into your React app? This is a problem that many developers face, especially when setting up a new React app with a module bundler.</p>
<p>In this article, I will share with you the different ways of importing SVGs in React, as well as how the process works under the hood.</p>
<p>Let's get started.</p>
<h2 id="heading-what-is-an-svg">What Is an SVG?</h2>
<p>SVG, short for Scalable Vector Graphic, is an image format used for rendering two-dimensional (2D) graphics on the internet.</p>
<p>The SVG format stores images as <strong>vectors</strong> which are graphics made up of points, lines, and curves based on geometry and mathematical formulas.</p>
<p>Because they are based on numbers and values rather than a grid of pixels like <a target="_blank" href="https://en.wikipedia.org/wiki/Raster_graphics">raster images</a>(.png and.jpg), they do not lose quality when zoomed or resized.</p>
<p>They're also great for creating responsive websites that need to look good and function well across a variety of screen sizes.</p>
<p>Overall, SVGs are great as they are scalable, lightweight, customizable, and can be animated using CSS when used <a class="post-section-overview" href="#2usingsvgsbyaddingdirectlyasjsx">inline</a>.</p>
<h2 id="heading-how-to-import-svgs-in-react-apps">How to Import SVGs in React Apps</h2>
<p>Let's go through some of the most used methods when importing SVGs into React Apps.</p>
<h3 id="heading-1-how-to-import-svgs-using-the-image-tag">1. How to Import SVGs Using the Image Tag</h3>
<p>Importing SVGs using the image tag is one of the easiest ways to use an SVG. If you initialize your app using CRA (Create React App), you can import the SVG file in the image source attribute, as it supports it off the bat.</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">import</span> YourSvg <span class="hljs-keyword">from</span> <span class="hljs-string">"/path/to/image.svg"</span>;

<span class="hljs-keyword">const</span> App = <span class="hljs-function">() =&gt;</span> {
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"App"</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">img</span> <span class="hljs-attr">src</span>=<span class="hljs-string">{YourSvg}</span> <span class="hljs-attr">alt</span>=<span class="hljs-string">"Your SVG"</span> /&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
  );
};
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> App;
</code></pre>
<p>But if you are not using CRA, you have to set up a file loader system in the bundler you're using (Webpack, Parcel, Rollup, and so on).</p>
<p>Webpack, for instance, has a loader for handling SVGs called <a target="_blank" href="https://v4.webpack.js.org/loaders/file-loader/">file-loader</a>.</p>
<p>To install the file-loader, add the following command:</p>
<pre><code class="lang-bash">npm install file-loader --save-dev
</code></pre>
<p>Next, add the loader to the <code>webpack.config.js</code> file:</p>
<pre><code class="lang-js"><span class="hljs-built_in">module</span>.exports = {
  <span class="hljs-attr">module</span>: {
    <span class="hljs-attr">rules</span>: [
      {
        <span class="hljs-attr">test</span>: <span class="hljs-regexp">/\.(png|jpe?g|gif)$/i</span>,
        use: [
          {
            <span class="hljs-attr">loader</span>: <span class="hljs-string">"file-loader"</span>,
          },
        ],
      },
    ],
  },
};
</code></pre>
<p>Now, you can import your SVG files and use them:</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">import</span> YourSvg <span class="hljs-keyword">from</span> <span class="hljs-string">"/path/to/image.svg"</span>;

<span class="hljs-keyword">const</span> App = <span class="hljs-function">() =&gt;</span> {
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"App"</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">img</span> <span class="hljs-attr">src</span>=<span class="hljs-string">{YourSvg}</span> <span class="hljs-attr">alt</span>=<span class="hljs-string">"Your SVG"</span> /&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
  );
};
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> App;
</code></pre>
<p>NOTE: While this approach is straightforward, it does have one disadvantage: unlike the other methods for importing, you cannot style the SVG imported in a <code>img</code> element. As a result, it will be suitable for an SVG that does not need customization, like logos.</p>
<h3 id="heading-2-how-to-import-svgs-by-adding-them-directly-as-jsx">2. How to Import SVGs by Adding them Directly as JSX</h3>
<p>JSX supports the <code>svg</code> tag, so we can copy-paste the SVG directly into our React components. This method is straightforward as it helps you take full advantage of SVGs without using a bundler.</p>
<p>The approach is possible because SVGs are in XML format, just like HTML. So, we can convert it to JSX syntax. You can also use a <a target="_blank" href="https://transform.tools/html-to-jsx">compiler</a> instead of manually converting.</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">const</span> App = <span class="hljs-function">() =&gt;</span> {
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"App"</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">svg</span>
        <span class="hljs-attr">xmlns</span>=<span class="hljs-string">"http://www.w3.org/2000/svg"</span>
        <span class="hljs-attr">className</span>=<span class="hljs-string">"ionicon"</span>
        <span class="hljs-attr">viewBox</span>=<span class="hljs-string">"0 0 512 512"</span>
      &gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">path</span>
          <span class="hljs-attr">d</span>=<span class="hljs-string">"M160 136c0-30.62 4.51-61.61 16-88C99.57 81.27 48 159.32 48 248c0 119.29 96.71 216 216 216 88.68 0 166.73-51.57 200-128-26.39 11.49-57.38 16-88 16-119.29 0-216-96.71-216-216z"</span>
          <span class="hljs-attr">fill</span>=<span class="hljs-string">"none"</span>
          <span class="hljs-attr">stroke</span>=<span class="hljs-string">"currentColor"</span>
          <span class="hljs-attr">strokeLinecap</span>=<span class="hljs-string">"round"</span>
          <span class="hljs-attr">strokeLinejoin</span>=<span class="hljs-string">"round"</span>
          <span class="hljs-attr">strokeWidth</span>=<span class="hljs-string">{32}</span>
        /&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">svg</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
  );
};
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> App;
</code></pre>
<p>The advantage of including SVGs inline is that we have access to their different properties, which allows us to style and customize them as we see fit.</p>
<p>One thing to keep in mind is that if your SVG file size is large, your code may become complex, reducing readability and productivity. If this is the case, use a png or jpeg file..</p>
<h3 id="heading-3-how-to-import-svgs-as-react-components">3. How to Import SVGs as React Components</h3>
<p>If you use CRA, there's a chance you have imported and used SVGs directly as a React component at one point in time.</p>
<p>This method, which is possible with the help of a file loader, works by loading the image alongside the HTML rather than as a separate file.</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">import</span> { ReactComponent <span class="hljs-keyword">as</span> Logo } <span class="hljs-keyword">from</span> <span class="hljs-string">"./logo.svg"</span>;

<span class="hljs-keyword">const</span> App = <span class="hljs-function">() =&gt;</span> {
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"App"</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">Logo</span> /&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
  );
};

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> App;
</code></pre>
<h3 id="heading-4-how-to-convert-svgs-to-react-components">4. How to Convert SVGs to React Components</h3>
<p>This approach is similar to the one previously mentioned. Here, we can convert an SVG to a React component by returning it from inside a class or functional component.</p>
<p>To do this, open up the SVG file in a text editor, and copy-paste the code into a new component:</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> ArrowUndo = <span class="hljs-function">() =&gt;</span> {
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">svg</span>
      <span class="hljs-attr">xmlns</span>=<span class="hljs-string">"http://www.w3.org/2000/svg"</span>
      <span class="hljs-attr">className</span>=<span class="hljs-string">"ionicon"</span>
      <span class="hljs-attr">viewBox</span>=<span class="hljs-string">"0 0 512 512"</span>
    &gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">path</span> <span class="hljs-attr">d</span>=<span class="hljs-string">"M245.09 327.74v-37.32c57.07 0 84.51 13.47 108.58 38.68 5.4 5.65 15 1.32 14.29-6.43-5.45-61.45-34.14-117.09-122.87-117.09v-37.32a8.32 8.32 0 00-14.05-6L146.58 242a8.2 8.2 0 000 11.94L231 333.71a8.32 8.32 0 0014.09-5.97z"</span> /&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">path</span>
        <span class="hljs-attr">d</span>=<span class="hljs-string">"M256 64C150 64 64 150 64 256s86 192 192 192 192-86 192-192S362 64 256 64z"</span>
        <span class="hljs-attr">fill</span>=<span class="hljs-string">"none"</span>
        <span class="hljs-attr">stroke</span>=<span class="hljs-string">"currentColor"</span>
        <span class="hljs-attr">strokeMiterlimit</span>=<span class="hljs-string">{10}</span>
        <span class="hljs-attr">strokeWidth</span>=<span class="hljs-string">{32}</span>
      /&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">svg</span>&gt;</span></span>
  );
};
</code></pre>
<p>Now, you can import and render the SVG component in another component like this:</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">import</span> { ArrowUndo } <span class="hljs-keyword">from</span> <span class="hljs-string">"./path/to/ArrowUndo.jsx"</span>;

<span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> Button = <span class="hljs-function">() =&gt;</span> {
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">button</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">ArrowUndo</span> /&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span></span>
  );
};
</code></pre>
<p>Again, this approach is only possible if your React app has a loader like SVGR's <a target="_blank" href="https://www.npmjs.com/package/@svgr/webpack">Webpack loader</a> included.</p>
<h3 id="heading-5-how-to-import-svgs-using-svgr">5. How to Import SVGs Using SVGR</h3>
<p><a target="_blank" href="https://react-svgr.com/">SVGR</a> is a tool that takes raw SVG files and transforms them into React components. It also has a large ecosystem with support for Create React App, Gatsby, Parcel, Rollup, and other technologies.</p>
<p>So, how do we set it up?</p>
<p>First, install the package by running the code below:</p>
<pre><code class="lang-bash"><span class="hljs-comment"># with npm</span>
npm install --save-dev @svgr/webpack

<span class="hljs-comment"># with yarn</span>
yarn add --dev @svgr/webpack
</code></pre>
<p>Next, update your <code>webpack.config.js</code>:</p>
<pre><code class="lang-js"><span class="hljs-built_in">module</span>.exports = {
  <span class="hljs-attr">module</span>: {
    <span class="hljs-attr">rules</span>: [
      {
        <span class="hljs-attr">test</span>: <span class="hljs-regexp">/\.svg$/i</span>,
        issuer: <span class="hljs-regexp">/\.[jt]sx?$/</span>,
        use: [<span class="hljs-string">"@svgr/webpack"</span>],
      },
    ],
  },
};
</code></pre>
<p>Now, you can import an SVG file as a React component:</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">import</span> Logo <span class="hljs-keyword">from</span> <span class="hljs-string">"./logo.svg"</span>;

<span class="hljs-keyword">const</span> App = <span class="hljs-function">() =&gt;</span> {
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"App"</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">Logo</span> /&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
  );
};

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> App;
</code></pre>
<h3 id="heading-6-how-to-import-svgs-using-the-vite-plugin-for-svgr">6. How to Import SVGs Using the Vite Plugin for SVGR</h3>
<p><a target="_blank" href="https://www.npmjs.com/package/vite-plugin-svgr"><code>vite-plugin-svgr</code></a> is a plugin for Vite that uses svgr under the hood to transform SVGs into React components.</p>
<p>You can install it by running the following command:</p>
<pre><code class="lang-bash"><span class="hljs-comment"># with npm</span>
npm i vite-plugin-svgr

<span class="hljs-comment"># with yarn</span>
yarn add vite-plugin-svgr
</code></pre>
<p>Next, add the plugin inside your app's <code>vite.config.js</code>:</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> { defineConfig } <span class="hljs-keyword">from</span> <span class="hljs-string">"vite"</span>;
<span class="hljs-keyword">import</span> react <span class="hljs-keyword">from</span> <span class="hljs-string">"@vitejs/plugin-react"</span>;
<span class="hljs-keyword">import</span> svgr <span class="hljs-keyword">from</span> <span class="hljs-string">"vite-plugin-svgr"</span>;

<span class="hljs-comment">// https://vitejs.dev/config/</span>
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> defineConfig({
  <span class="hljs-attr">plugins</span>: [svgr(), react()],
});
</code></pre>
<p>Now, you can import the SVG files as <a class="post-section-overview" href="#3importingsvgsasreactcomponents">React components</a>:</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">import</span> { ReactComponent <span class="hljs-keyword">as</span> Logo } <span class="hljs-keyword">from</span> <span class="hljs-string">"./logo.svg"</span>;
</code></pre>
<h2 id="heading-conclusion">Conclusion</h2>
<p>And it's a wrap! In this article, we've covered how to import SVGs in a React App using custom configuration from specific packages, how importing React components works, and how to use them in a Vite setup.</p>
<p>When working with Vite, I use the vite svgr plugin, which works flawlessly. You can also experiment with the other ways discussed in this article.</p>
<p>I hope you found this article insightful. If you do have any questions, feel free to send a message on <a target="_blank" href="https://twitter.com/israelmitolu">Twitter</a> or <a target="_blank" href="https://www.linkedin.com/in/israeloyetunji/">LinkedIn</a>.</p>
<p>Thanks for reading, and happy coding!</p>
<p>Before you go, check out these resources:</p>
<ul>
<li><p><a target="_blank" href="https://israelmitolu.hashnode.dev/why-you-should-ditch-create-react-app-for-vite">Why You Should Ditch Create-React-App for Vite</a></p>
</li>
<li><p><a target="_blank" href="https://rossbulat.medium.com/working-with-svgs-in-react-d09d1602a219">Working with SVGs in React</a></p>
</li>
<li><p><a target="_blank" href="https://twitter.com/i/communities/1532313139810906114">Twitter Community for Devs</a></p>
</li>
</ul>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ Skeleton Loader Example – How to Build a Skeleton Screen with CSS for Better UX ]]>
                </title>
                <description>
                    <![CDATA[ Content loaders, skeleton screens, ghost elements, and content placeholders. These are the names given to the effect we'll be exploring today. Many companies, such as Linkedin, Facebook, Youtube and Slack, use this effect in their apps and websites, ... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-to-build-skeleton-screens-using-css-for-better-user-experience/</link>
                <guid isPermaLink="false">66d45f313dce891ac3a96804</guid>
                
                    <category>
                        <![CDATA[ CSS ]]>
                    </category>
                
                    <category>
                        <![CDATA[ JavaScript ]]>
                    </category>
                
                    <category>
                        <![CDATA[ General Programming ]]>
                    </category>
                
                    <category>
                        <![CDATA[ user experience ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Web Development ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Israel Oyetunji ]]>
                </dc:creator>
                <pubDate>Mon, 25 Apr 2022 13:49:34 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2022/04/Build-Skeleton-Screens-for-Better-UX.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>Content loaders, skeleton screens, ghost elements, and content placeholders. These are the names given to the effect we'll be exploring today.</p>
<p>Many companies, such as Linkedin, Facebook, Youtube and Slack, use this effect in their apps and websites, as you may have noticed.</p>
<p>As much as we developers want our websites to load as quickly as possible, there are times when a lot of data needs to be rendered on the page, so Skeleton screens are a great option.</p>
<p>In this article, we'll cover:</p>
<ul>
<li><p><a class="post-section-overview" href="#heading-what-is-a-skeleton-screen">What Skeleton Screens are</a></p>
</li>
<li><p><a class="post-section-overview" href="#differenttypesofskeletonscreens">Different types of Skeleton screens</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-why-use-skeleton-screens">Why use Skeleton Screens</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-when-to-use-them">When to use them</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-things-to-keep-in-mind">Things to keep in mind</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-building-a-daily-dev-skeleton-loading-ui">Building A Daily Dev Skeleton Loading UI</a></p>
<ul>
<li><p><a class="post-section-overview" href="#heading-step-1-set-up-the-project">Set up the project</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-step-2-design-the-skeleton-elements">Design the Skeleton elements</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-step-3-clone-the-card-template">Clone the card template</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-step-4-create-json-file">Create JSON file</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-step-5-populate-html-elements-with-appropriate-content">Populate HTML elements</a></p>
</li>
</ul>
</li>
</ul>
<h2 id="heading-prerequisites">Prerequisites</h2>
<p>This article assumes that you have:</p>
<ul>
<li><p>Knowledge of HTML and CSS (SASS)</p>
</li>
<li><p>Knowledge of JavaScript (ES6)</p>
</li>
</ul>
<p>We will use HTML and SASS for this project. If you would like to get started with SASS, check out this <a target="_blank" href="https://freecodecamp.org/news/beginners-guide-to-sass">Beginner's Guide.</a></p>
<h2 id="heading-what-is-a-skeleton-screen">What is a Skeleton Screen?</h2>
<p>A skeleton screen is an animated placeholder that simulates the layout of a website while data is being loaded.</p>
<p>They let the user know that some content is loading and, more importantly, provide an indication of what is loading, whether it's an image, text, card, and so on.</p>
<p>This gives the user the impression that the website is faster because they already know what type of content is loading before it appears. This is referred to as <strong>perceived performance</strong>.</p>
<p>Here are some examples of skeleton screens from Facebook and LinkedIn:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/04/1-2.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>LinkedIn home feed loading state</em></p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/04/2-2.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>Facebook home feed loading state</em></p>
<h2 id="heading-different-types-of-skeleton-screens">Different types of Skeleton Screens</h2>
<p>There are 2 main types of skeleton screens:</p>
<ul>
<li><p>Content Placeholders</p>
</li>
<li><p>Color Placeholders</p>
</li>
</ul>
<p>Content Placeholders are typically light grey boxes and circles that simulate the appearance of the page, as shown in the images above for Facebook and LinkedIn.</p>
<p>Color Placeholders are more difficult to create because they simulate not only the UI layout but also the dominant color. It is most commonly found on image-focused websites such as Pinterest and Unsplash.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/04/9.gif" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>Color Placeholder example from Pinterest</em></p>
<h2 id="heading-why-use-skeleton-screens">Why Use Skeleton Screens?</h2>
<ul>
<li><p>They appear to be faster and are more user friendly. Improved perceived performance provides both good UX and helps in increasing conversion rate.</p>
</li>
<li><p>The issue with spinners/loaders is that we have no idea what is loading or how long it will take.</p>
</li>
<li><p>The use of spinners/loaders creates a period of uncertainty for the user since the load time is unknown.</p>
</li>
<li><p>Skeleton screens draw the user's attention to progress rather than waiting time.</p>
</li>
<li><p>It creates an illusion of speed and short load time</p>
</li>
</ul>
<h2 id="heading-when-to-use-them">When to use them</h2>
<ul>
<li><p>Use to notify the user that something is loading when more than one element is loading at the same time.</p>
</li>
<li><p>Use when loading data takes more than 3 seconds.</p>
</li>
<li><p>Use on websites with a lot of traffic.</p>
</li>
<li><p>Use for a background or long-running process.</p>
</li>
</ul>
<h2 id="heading-things-to-keep-in-mind">Things to keep in mind</h2>
<p>While implementing skeleton screens, we should keep in mind the goal we are trying achieve with the website or app, and prioritize loading the content.</p>
<p>Use of skeleton loading screens is no excuse to skip actual performance optimization, and if you can cache meaningful content and display that, that'll be good.</p>
<h2 id="heading-building-a-daily-dev-skeleton-loading-ui">Building A Daily Dev Skeleton Loading UI</h2>
<p>In this section, we will dive into the implementation of the skeleton loading screen following a step-by-step process so it's easier to understand.</p>
<p>We'll build one like daily.dev's feed section.</p>
<h3 id="heading-step-1-set-up-the-project">Step 1: Set up the project</h3>
<p>First, to code along with me, clone or download the starter code for the layout <a target="_blank" href="https://github.com/israelmitolu/Skeleton-Loading-using-CSS/tree/master/starter">here</a>. You can download the files by using <a target="_blank" href="https://minhaskamal.github.io/DownGit/#/home">DownGit</a>.</p>
<p>The code contains the card layout, so we will continue from here in the next steps.</p>
<p>To begin, start the development server in the IDE and open up your browser.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/04/screencapture-codepen-io-israelmitolu-full-wvpOaQd-2022-04-21-17_16_47.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>Starter Card Layout</em></p>
<h3 id="heading-step-2-design-the-skeleton-elements">Step 2: Design the Skeleton elements</h3>
<p>There are 5 elements that we want to build for the skeleton loading: the logo image, title, details, cover image and footer section.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/04/4-3.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>Daily Dev's Skeleton Elements</em></p>
<p>Now, we will add <code>skeleton</code> classes to the locations of the above elements.</p>
<p>For the logo,</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">img</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"card__header header__img skeleton"</span> /&gt;</span>
</code></pre>
<p>For the title, there will be 2 divs to represent the two lines that we have in the picture above.</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"skeleton skeleton-text"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"skeleton skeleton-text"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
</code></pre>
<p>For the details, add the following code inside the div of class <code>body__text</code>:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"skeleton skeleton-text skeleton-text__body"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
</code></pre>
<p>Inside the <code>body__img</code> div, add the following code:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">img</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"skeleton"</span> <span class="hljs-attr">alt</span>=<span class="hljs-string">""</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"cover-img"</span> /&gt;</span>
</code></pre>
<p>For the footer, add this code:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"skeleton skeleton-text skeleton-footer"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
</code></pre>
<p>Now, the complete HTML code for the card:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"card"</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"card-link"</span> <span class="hljs-attr">target</span>=<span class="hljs-string">"_blank"</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"card__header"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">img</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"card__header header__img skeleton"</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"logo-img"</span> <span class="hljs-attr">alt</span>=<span class="hljs-string">""</span> /&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">h3</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"card__header header__title"</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"card-title"</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"skeleton skeleton-text"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"skeleton skeleton-text"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">h3</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>

  <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"card__body"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"card__body body__text"</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"card-details"</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"skeleton skeleton-text skeleton-text__body"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>

    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"card__body body__img"</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">img</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"skeleton"</span> <span class="hljs-attr">alt</span>=<span class="hljs-string">""</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"cover-img"</span> /&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>

  <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"card__footer"</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"card-footer"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"skeleton skeleton-text skeleton-footer"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span>
</code></pre>
<p>Now, let's add some styling to make the skeleton components:</p>
<pre><code class="lang-scss"><span class="hljs-selector-class">.skeleton</span> {
  <span class="hljs-attribute">animation</span>: skeleton-loading <span class="hljs-number">1s</span> linear infinite alternate;
}

<span class="hljs-keyword">@keyframes</span> skeleton-loading {
  0% {
    <span class="hljs-attribute">background-color</span>: hsl(<span class="hljs-number">200</span>, <span class="hljs-number">20%</span>, <span class="hljs-number">80%</span>);
  }
  100% {
    <span class="hljs-attribute">background-color</span>: hsl(<span class="hljs-number">200</span>, <span class="hljs-number">20%</span>, <span class="hljs-number">95%</span>);
  }
}

<span class="hljs-selector-class">.skeleton-text</span> {
  <span class="hljs-attribute">width</span>: <span class="hljs-number">100%</span>;
  <span class="hljs-attribute">height</span>: <span class="hljs-number">0.7rem</span>;
  <span class="hljs-attribute">margin-bottom</span>: <span class="hljs-number">0.5rem</span>;
  <span class="hljs-attribute">border-radius</span>: <span class="hljs-number">0.25rem</span>;
}

<span class="hljs-selector-class">.skeleton-text__body</span> {
  <span class="hljs-attribute">width</span>: <span class="hljs-number">75%</span>;
}

<span class="hljs-selector-class">.skeleton-footer</span> {
  <span class="hljs-attribute">width</span>: <span class="hljs-number">30%</span>;
}
</code></pre>
<p>This is the resulting layout:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/04/5.gif" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>Card component loading</em></p>
<h3 id="heading-step-3-clone-the-card-template">Step 3: Clone the card template</h3>
<p>Insert a <code>template</code> tag between the <code>container</code> and the <code>card</code> element in the <code>index.html</code> file.</p>
<p>In the image above there's a <code>template</code> tag that I commented out, and yes, its a <a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element/template">valid HTML element</a> ;). It is used to declare fragments of HTML that can be cloned and inserted in the document by script.</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">template</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"card-template"</span>&gt;</span>
</code></pre>
<p>As a result, make sure to add the closing tag <code>&lt;/template&gt;</code> after the closing tag of the <code>card</code> div.</p>
<p>Now let's look at the JavasScript code that we'll use to clone the card template.</p>
<p>Create a <code>script</code> tag just before the end of the <code>body</code> tag, and add the following code:</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> container = <span class="hljs-built_in">document</span>.querySelector(<span class="hljs-string">".container"</span>);
<span class="hljs-keyword">const</span> cardTemplate = <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">"card-template"</span>);
<span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i &lt; <span class="hljs-number">10</span>; i++) {
  container.append(cardTemplate.content.cloneNode(<span class="hljs-literal">true</span>));
}
</code></pre>
<p>The code above grabs the page container and the card template, and then creates 9 clones/copies of the card (making 10 in total). Then it appends/inserts the cards into the container.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/04/6.gif" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>Cloned card skeleton UI</em></p>
<h3 id="heading-step-4-create-json-file">Step 4: Create JSON file</h3>
<p>We need some data before we can add content to the page. Normally, you would need to get data with an external website, but we'll be using one that I've set up specifically for this project.</p>
<p>To begin, create a file called <code>data.json</code> in the project folder.</p>
<p>Add the following code to the JSON file.</p>
<pre><code class="lang-json">[
  {
    <span class="hljs-attr">"id"</span>: <span class="hljs-number">1</span>,
    <span class="hljs-attr">"logoImage"</span>: <span class="hljs-string">"https://res.cloudinary.com/daily-now/image/upload/t_logo,f_auto/v1/logos/4a287b2e7cb5499bae863f8e7137cdb4"</span>,
    <span class="hljs-attr">"title"</span>: <span class="hljs-string">"Writing Cleaner CSS Using BEM "</span>,
    <span class="hljs-attr">"details"</span>: <span class="hljs-string">"Mar 12, 2022 · 4m read time"</span>,
    <span class="hljs-attr">"coverImage"</span>: <span class="hljs-string">"https://res.cloudinary.com/daily-now/image/upload/f_auto,q_auto/v1/posts/dd19e7a56475f39ab1c38167c02c7b58"</span>,
    <span class="hljs-attr">"link"</span>: <span class="hljs-string">"https://israelmitolu.hashnode.dev/writing-cleaner-css-using-bem-methodology"</span>
  },
  {
    <span class="hljs-attr">"id"</span>: <span class="hljs-number">2</span>,
    <span class="hljs-attr">"logoImage"</span>: <span class="hljs-string">"https://daily-now-res.cloudinary.com/image/upload/t_logo,f_auto/v1628412854/logos/freecodecamp"</span>,
    <span class="hljs-attr">"title"</span>: <span class="hljs-string">"The Beginner's Guide to Sass"</span>,
    <span class="hljs-attr">"details"</span>: <span class="hljs-string">"Apr 05, 2022 · 8m read time"</span>,
    <span class="hljs-attr">"coverImage"</span>: <span class="hljs-string">"https://res.cloudinary.com/daily-now/image/upload/f_auto,q_auto/v1/posts/bec6719be210973098293a32dc732d1e"</span>,
    <span class="hljs-attr">"link"</span>: <span class="hljs-string">"https://www.freecodecamp.org/news/the-beginners-guide-to-sass/"</span>
  },
  {
    <span class="hljs-attr">"id"</span>: <span class="hljs-number">3</span>,
    <span class="hljs-attr">"logoImage"</span>: <span class="hljs-string">"https://res.cloudinary.com/daily-now/image/upload/t_logo,f_auto/v1/logos/devto"</span>,
    <span class="hljs-attr">"title"</span>: <span class="hljs-string">"I made Squid Game with Javascript"</span>,
    <span class="hljs-attr">"details"</span>: <span class="hljs-string">"Oct 25, 2021 · 3m read time"</span>,
    <span class="hljs-attr">"coverImage"</span>: <span class="hljs-string">"https://res.cloudinary.com/daily-now/image/upload/f_auto,q_auto/v1/posts/1f947033365381cbe322ddf294ad7169"</span>,
    <span class="hljs-attr">"link"</span>: <span class="hljs-string">"https://dev.to/0shuvo0/i-made-squid-game-with-javascript-10j9"</span>
  },
  {
    <span class="hljs-attr">"id"</span>: <span class="hljs-number">4</span>,
    <span class="hljs-attr">"logoImage"</span>: <span class="hljs-string">"https://res.cloudinary.com/daily-now/image/upload/t_logo,f_auto/v1/logos/4a287b2e7cb5499bae863f8e7137cdb4"</span>,
    <span class="hljs-attr">"title"</span>: <span class="hljs-string">"Using Custom Cursors with Javascript for a Better User Experience"</span>,
    <span class="hljs-attr">"details"</span>: <span class="hljs-string">"Feb 12, 2022 · 9m read time"</span>,
    <span class="hljs-attr">"coverImage"</span>: <span class="hljs-string">"https://res.cloudinary.com/daily-now/image/upload/f_auto,q_auto/v1/posts/3d056b99c95b37cd35ae5cfc6a8b38be"</span>,
    <span class="hljs-attr">"link"</span>: <span class="hljs-string">"https://israelmitolu.hashnode.dev/using-custom-cursors-with-javascript-for-a-better-user-experience"</span>
  },
  {
    <span class="hljs-attr">"id"</span>: <span class="hljs-number">5</span>,
    <span class="hljs-attr">"logoImage"</span>: <span class="hljs-string">"https://daily-now-res.cloudinary.com/image/upload/t_logo,f_auto/v1628412854/logos/freecodecamp"</span>,
    <span class="hljs-attr">"title"</span>: <span class="hljs-string">"React Best Practices - Tips for Writing Better React Code in 2022"</span>,
    <span class="hljs-attr">"details"</span>: <span class="hljs-string">"Feb 03, 2022 · 31m read time"</span>,
    <span class="hljs-attr">"coverImage"</span>: <span class="hljs-string">"https://res.cloudinary.com/daily-now/image/upload/f_auto,q_auto/v1/posts/5a629fff5583f9ab5f0931d14736b299"</span>,
    <span class="hljs-attr">"link"</span>: <span class="hljs-string">"https://www.freecodecamp.org/news/best-practices-for-react/"</span>
  },
  {
    <span class="hljs-attr">"id"</span>: <span class="hljs-number">6</span>,
    <span class="hljs-attr">"logoImage"</span>: <span class="hljs-string">"https://res.cloudinary.com/daily-now/image/upload/t_logo,f_auto/v1/logos/tnw"</span>,
    <span class="hljs-attr">"title"</span>: <span class="hljs-string">"You suck at Googling: 5 tips to improve your search skills"</span>,
    <span class="hljs-attr">"details"</span>: <span class="hljs-string">"Mar 31, 2022 · 4m read time"</span>,
    <span class="hljs-attr">"coverImage"</span>: <span class="hljs-string">"https://res.cloudinary.com/daily-now/image/upload/f_auto,q_auto/v1/posts/e318150ae67c2083ff3585a96f366f7b"</span>,
    <span class="hljs-attr">"link"</span>: <span class="hljs-string">"https://thenextweb.com/news/5-tips-to-improve-your-google-search-skills"</span>
  },
  {
    <span class="hljs-attr">"id"</span>: <span class="hljs-number">7</span>,
    <span class="hljs-attr">"logoImage"</span>: <span class="hljs-string">"https://res.cloudinary.com/daily-now/image/upload/t_logo,f_auto/v1/logos/logrocket"</span>,
    <span class="hljs-attr">"title"</span>: <span class="hljs-string">"A better way of solving prop drilling in React apps"</span>,
    <span class="hljs-attr">"details"</span>: <span class="hljs-string">"Jan 14, 2022 · 13m read time"</span>,
    <span class="hljs-attr">"coverImage"</span>: <span class="hljs-string">"https://res.cloudinary.com/daily-now/image/upload/f_auto,q_auto/v1/posts/6fe4c4060bca638b419d8b2c63d8eaf7"</span>,
    <span class="hljs-attr">"link"</span>: <span class="hljs-string">"https://blog.logrocket.com/solving-prop-drilling-react-apps/"</span>
  },
  {
    <span class="hljs-attr">"id"</span>: <span class="hljs-number">8</span>,
    <span class="hljs-attr">"logoImage"</span>: <span class="hljs-string">"https://res.cloudinary.com/daily-now/image/upload/t_logo,f_auto/v1/logos/dz"</span>,
    <span class="hljs-attr">"title"</span>: <span class="hljs-string">"Golang and Event-Driven Architecture"</span>,
    <span class="hljs-attr">"details"</span>: <span class="hljs-string">"Apr 18, 2022 · 6m read time"</span>,
    <span class="hljs-attr">"coverImage"</span>: <span class="hljs-string">"https://res.cloudinary.com/daily-now/image/upload/f_auto,q_auto/v1/posts/d06eddd82c62288df6e2600bcda61579"</span>,
    <span class="hljs-attr">"link"</span>: <span class="hljs-string">"https://dzone.com/articles/golang-and-event-driven-architecture"</span>
  },
  {
    <span class="hljs-attr">"id"</span>: <span class="hljs-number">9</span>,
    <span class="hljs-attr">"logoImage"</span>: <span class="hljs-string">"https://res.cloudinary.com/daily-now/image/upload/t_logo,f_auto/v1/logos/4a287b2e7cb5499bae863f8e7137cdb4"</span>,
    <span class="hljs-attr">"title"</span>: <span class="hljs-string">"Introduction to Git In 16 Minutes"</span>,
    <span class="hljs-attr">"details"</span>: <span class="hljs-string">"Mar 18, 2021 · 8m read time"</span>,
    <span class="hljs-attr">"coverImage"</span>: <span class="hljs-string">"https://res.cloudinary.com/daily-now/image/upload/f_auto,q_auto/v1/posts/3c02111a8f242f607551500432e17a78"</span>,
    <span class="hljs-attr">"link"</span>: <span class="hljs-string">"https://vickyikechukwu.hashnode.dev/introduction-to-git-in-16-minutes"</span>
  },
  {
    <span class="hljs-attr">"id"</span>: <span class="hljs-number">10</span>,
    <span class="hljs-attr">"logoImage"</span>: <span class="hljs-string">"https://res.cloudinary.com/daily-now/image/upload/t_logo,f_auto/v1/logos/4a287b2e7cb5499bae863f8e7137cdb4"</span>,
    <span class="hljs-attr">"title"</span>: <span class="hljs-string">"How to Create a Sleek Preloader Animation Using GSAP Timeline"</span>,
    <span class="hljs-attr">"details"</span>: <span class="hljs-string">"Jan 25, 2022 · 7m read time"</span>,
    <span class="hljs-attr">"coverImage"</span>: <span class="hljs-string">"https://res.cloudinary.com/daily-now/image/upload/f_auto,q_auto/v1/posts/e238c35cb9d41dd9a5475602aef00119"</span>,
    <span class="hljs-attr">"link"</span>: <span class="hljs-string">"https://israelmitolu.hashnode.dev/how-to-create-a-sleek-preloader-animation-using-gsap-timeline"</span>
  }
]
</code></pre>
<p>To replicate Daily Dev's feed section, we have created some data that has an array of objects with properties such as id, logo image, title, details and cover image.</p>
<h3 id="heading-step-5-populate-html-elements-with-appropriate-content">Step 5: Populate HTML Elements with appropriate content</h3>
<p>Add the following code to the script tag that houses your JavaScript:</p>
<pre><code class="lang-javascript">fetch(<span class="hljs-string">"data.json"</span>)
  .then(<span class="hljs-function">(<span class="hljs-params">response</span>) =&gt;</span> response.json())
  .then(<span class="hljs-function">(<span class="hljs-params">posts</span>) =&gt;</span> {
    container.innerHTML = <span class="hljs-string">""</span>;
    posts.forEach(<span class="hljs-function">(<span class="hljs-params">post</span>) =&gt;</span> {
      <span class="hljs-keyword">const</span> div = cardTemplate.content.cloneNode(<span class="hljs-literal">true</span>);
      div.getElementById(<span class="hljs-string">"card-link"</span>).href = post.link;
      div.getElementById(<span class="hljs-string">"logo-img"</span>).src = post.logoImage;
      div.getElementById(<span class="hljs-string">"card-title"</span>).textContent = post.title;
      div.getElementById(<span class="hljs-string">"card-details"</span>).textContent = post.details;
      div.getElementById(<span class="hljs-string">"cover-img"</span>).src = post.coverImage;
      div.getElementById(
        <span class="hljs-string">"card-footer"</span>
      ).innerHTML = <span class="hljs-string">` &lt;ion-icon name="arrow-up"&gt;&lt;/ion-icon&gt;
          &lt;ion-icon name="chatbox-ellipses"&gt;&lt;/ion-icon&gt;
          &lt;ion-icon name="bookmark"&gt;&lt;/ion-icon&gt;`</span>;
      container.append(div);
    });
  });
</code></pre>
<p>The code above is what we will use to add content to the cards once they are done loading.</p>
<p>Now, let me explain the code bit by bit:</p>
<pre><code class="lang-json">fetch(<span class="hljs-string">"data.json"</span>)
  .then((response) =&gt; response.json())
</code></pre>
<p>Here, we have a basic fetch request, where we set the path to the resource. In this case, the <code>data.json</code> file. If it were an external API, you would use the endpoint URL as the argument:</p>
<p>The <code>fetch()</code> method does not directly return the JSON response body but instead returns a promise that resolves with a Response object.</p>
<p>To learn more, check out the <a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch">MDN docs</a>.</p>
<pre><code class="lang-json">.then((posts) =&gt; {
    container.innerHTML = <span class="hljs-attr">""</span>;
    posts.forEach((post) =&gt; {
      const div = cardTemplate.content.cloneNode(true);
      div.getElementById(<span class="hljs-attr">"logo-img"</span>).src = post.logoImage;
      div.getElementById(<span class="hljs-attr">"card-title"</span>).textContent = post.title;
      div.getElementById(<span class="hljs-attr">"card-details"</span>).textContent = post.details;
      div.getElementById(<span class="hljs-attr">"cover-img"</span>).src = post.coverImage;
      div.getElementById(
        <span class="hljs-attr">"card-footer"</span>
      ).innerHTML = `&lt;ion-icon name=<span class="hljs-attr">"arrow-up"</span>&gt;&lt;/ion-icon&gt;
          &lt;ion-icon name=<span class="hljs-attr">"chatbox-ellipses"</span>&gt;&lt;/ion-icon&gt;
          &lt;ion-icon name=<span class="hljs-attr">"bookmark"</span>&gt;&lt;/ion-icon&gt;`;
      container.append(div);
    });
  });
</code></pre>
<p>Here, we define what should happen after fetching the data.</p>
<p>The code first clears the page, and then runs a <code>forEach()</code> method which extracts the properties from the JSON file, and then inputs it into the card elements (logo image, card title,...) using <code>.textContent</code> property.</p>
<p>Finally, for the footer, we used <code>.innerHTML</code> to insert the icons as HTML content.</p>
<p>If you added everything correctly, there shouldn't be any errors, and this is our fully functional skeleton loading UI.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/04/7-1.gif" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>Our finished Daily Dev skeleton UI</em></p>
<p>Check out the <a target="_blank" href="https://daily-dev-ui.netlify.app/">live demo</a> and the <a target="_blank" href="https://github.com/israelmitolu/Skeleton-Loading-using-CSS">source code repository</a> on Github.</p>
<h3 id="heading-network-throttling-in-chrome-devtools">Network Throttling in Chrome DevTools</h3>
<p>It's important to note that we didn't set a timeout because this skeleton screen is dependent on the user's network speed.</p>
<p>If you want to simulate it at different network speeds, go into the network tab in your browser Devtools.</p>
<p>Here's how to do it in Chrome v100:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/04/8.gif" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>Throttle Network in Chrome DevTools</em></p>
<ol>
<li><p>Open DevTools (Ctrl+Shift+i).</p>
</li>
<li><p>Navigate to the "Network" tab.</p>
</li>
<li><p>Select the type of connection you want</p>
</li>
<li><p>Reload the page to see assets downloading at the specified connection speed.</p>
</li>
</ol>
<p>If the default options don't suit you, you can create a custom Network Throttling Profile by selecting the option at the very top of the dropdown menu.</p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>You made it all the way to the end! You've learned about skeleton loading, and how it contributes to user experience by creating the illusion of speed when loading data, and you've implemented your own.</p>
<p>I hope you found this tutorial useful and that it serves as a good starting point for creating various skeleton loading screens.</p>
<p>If you found this article insightful, do share it with your friends and network. Also, feel free to connect with me on <a target="_blank" href="https://twitter.com/israelmitolu">Twitter</a> and my <a target="_blank" href="https://israelmitolu.hashnode.dev">blog</a> where I share resources and articles to make you a better dev.</p>
<p>Thanks for reading, and happy coding!</p>
<p>Before you go, here are some skeleton loading packages for <a target="_blank" href="https://blog.openreplay.com/3-ways-to-implement-skeleton-components-in-react#heading-what-is-a-skeleton-component">React</a>, <a target="_blank" href="https://openbase.com/categories/js/best-angular-loading-skeleton-libraries">Angular</a> and <a target="_blank" href="https://openbase.com/categories/js/best-vue-loading-skeleton-libraries">Vue</a>.</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ The Beginner's Guide to Sass ]]>
                </title>
                <description>
                    <![CDATA[ Have you ever wondered what SASS stands for? Or perhaps you already know what it is but haven't taken the time to study and use it. Whether you're learning about it for the first time, or want to brush up on your knowledge of the subject, this is the... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/the-beginners-guide-to-sass/</link>
                <guid isPermaLink="false">66d45f37264384a65d5a9544</guid>
                
                    <category>
                        <![CDATA[ CSS ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Sass ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Web Design ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Web Development ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Israel Oyetunji ]]>
                </dc:creator>
                <pubDate>Mon, 04 Apr 2022 23:39:31 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2022/04/The-Beginner-s-Guide-to-SASS.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>Have you ever wondered what SASS stands for? Or perhaps you already know what it is but haven't taken the time to study and use it.</p>
<p>Whether you're learning about it for the first time, or want to brush up on your knowledge of the subject, this is the article for you.</p>
<p>In this post, you'll learn the fundamentals of Sass, what it is, and how to use Sass's awesome features to speed up the process of writing styles.</p>
<h2 id="heading-prerequisites">Prerequisites</h2>
<p>This article assumes that you have:</p>
<ul>
<li><p>Basic understanding of HTML &amp; CSS</p>
</li>
<li><p>A code editor (VS Code recommended). If you don't have it installed, download it <a target="_blank" href="http://code.visualstudio.com/">here</a>.</p>
</li>
<li><p>And a browser (Chrome or Firefox recommended)</p>
</li>
</ul>
<h2 id="heading-what-exactly-is-sass">What exactly is Sass?</h2>
<p>Sass (Syntactically Awesome Style Sheets) is a CSS preprocessor that gives your CSS superpowers.</p>
<p>Let's face it: writing CSS can be difficult at times, especially in today's world of increasingly complex user interfaces.</p>
<p>And many times, you'll find that you're repeating yourself often.</p>
<p>Sass comes to the rescue in this situation. It helps you stick to the DRY (Do Not Repeat Yourself) philosophy when writing CSS.</p>
<p>Sass provides a compiler that allows us to write stylesheets in two different syntaxes, indented and SCSS. Let's look at each now.</p>
<h3 id="heading-indented-syntax">Indented syntax</h3>
<p>This is the older syntax that is indented, and gets rid of the curly braces and semi-colons. It has a file extension of <code>.sass</code>.</p>
<pre><code class="lang-plaintext">nav
  ul
    margin: 0
    padding: 0
    list-style: none

  li
    display: inline-block

  a
    display: block
    text-decoration: none
</code></pre>
<h3 id="heading-scss-syntax">SCSS syntax</h3>
<p>This is the newer and more popular syntax. It is essentially a subset of the CSS3 syntax. This means that you can write regular CSS with some additional functionalities.</p>
<p>Due to its advanced features it is often termed as <em>Sassy CSS</em>. It has a file extension of <code>.scss</code>.</p>
<pre><code class="lang-scss"><span class="hljs-selector-tag">nav</span> {
  <span class="hljs-selector-tag">ul</span> {
    <span class="hljs-attribute">margin</span>: <span class="hljs-number">0</span>;
    <span class="hljs-attribute">padding</span>: <span class="hljs-number">0</span>;
    <span class="hljs-attribute">list-style</span>: none;
  }
  <span class="hljs-selector-tag">li</span> {
    <span class="hljs-attribute">display</span>: inline-block;
  }

  <span class="hljs-selector-tag">a</span> {
    <span class="hljs-attribute">display</span>: block;
    <span class="hljs-attribute">text-decoration</span>: none;
  }
}
</code></pre>
<blockquote>
<p>Quick Disclaimer: This article uses the SCSS syntax because it's more widely used.</p>
</blockquote>
<h2 id="heading-how-does-sass-work">How Does Sass Work?</h2>
<p>Sass works in such a way that when you write your styles in a <code>.scss</code> file, it gets compiled into a regular CSS file. The CSS code is then loaded into the browser.</p>
<p>That is why it's called a Preprocessor.</p>
<h2 id="heading-why-should-you-use-sass">Why should you use Sass?</h2>
<ul>
<li><p><strong>Easy to learn</strong>: If you are familiar with CSS already, then you'll be glad to know that Sass actually has a similar syntax, and so you can start using it, even after this tutorial ;)</p>
</li>
<li><p><strong>Compatibility</strong>: It is compatible with all versions of CSS. So, you can use any available CSS libraries.</p>
</li>
<li><p><strong>Saves time</strong>: It helps reduce the repetition of CSS, because of its powerful features.</p>
</li>
<li><p><strong>Reusable code</strong>: Sass allows for variables and chunks of code (mixins) that can be reused over and over again. This helps you save time and makes you able to code faster.</p>
</li>
<li><p><strong>Organized Code</strong>: Sass helps keep your code organized by using partials.</p>
</li>
<li><p><strong>Cross Browser Compatibility</strong>: Sass gets compiled into CSS and adds all the necessary vendor prefixes so you don't have to worry about manually writing them out.</p>
</li>
</ul>
<h2 id="heading-features-of-sass">Features of Sass</h2>
<p>Here are some of the features that make Sass truly CSS with Superpowers:</p>
<h3 id="heading-variables-in-sass">Variables in Sass</h3>
<p>You can declare variables in Sass. This is one of Sass's strengths since we can define variables for various properties and use them in any file.</p>
<p>The benefit here is that if that value changes, you simply need to update a single line of code.</p>
<p>This is done by naming a variable with a dollar symbol <code>$</code> and then referencing it elsewhere in your code.</p>
<pre><code class="lang-scss"><span class="hljs-variable">$primary-color</span>: <span class="hljs-number">#24a0ed</span>;

<span class="hljs-selector-class">.text</span> {
  <span class="hljs-attribute">color</span>: <span class="hljs-variable">$primary-color</span>;
}
<span class="hljs-selector-tag">button</span> {
  <span class="hljs-attribute">color</span>: <span class="hljs-variable">$primary-color</span>;
  <span class="hljs-attribute">border</span>: <span class="hljs-number">2px</span> solid <span class="hljs-variable">$primary-color</span>;
}
</code></pre>
<h3 id="heading-nesting-in-sass">Nesting in Sass</h3>
<p>Most of the time, while writing CSS, classes are often duplicated. We can avoid this duplication by nesting styles inside the parent element.</p>
<p>In CSS,</p>
<pre><code class="lang-css"><span class="hljs-selector-tag">nav</span> {
  <span class="hljs-attribute">height</span>: <span class="hljs-number">10vh</span>;
  <span class="hljs-attribute">width</span>: <span class="hljs-number">100%</span>;
  <span class="hljs-attribute">display</span>: flex;
}

<span class="hljs-selector-tag">nav</span> <span class="hljs-selector-tag">ul</span> {
  <span class="hljs-attribute">list-style</span>: none;
  <span class="hljs-attribute">display</span>: flex;
}

<span class="hljs-selector-tag">nav</span> <span class="hljs-selector-tag">li</span> {
  <span class="hljs-attribute">margin-right</span>: <span class="hljs-number">2.5rem</span>;
}

<span class="hljs-selector-tag">nav</span> <span class="hljs-selector-tag">li</span> <span class="hljs-selector-tag">a</span> {
  <span class="hljs-attribute">text-decoration</span>: none;
  <span class="hljs-attribute">color</span>: <span class="hljs-number">#707070</span>;
}

<span class="hljs-selector-tag">nav</span> <span class="hljs-selector-tag">li</span> <span class="hljs-selector-tag">a</span><span class="hljs-selector-pseudo">:hover</span> {
  <span class="hljs-attribute">color</span>: <span class="hljs-number">#069c54</span>;
}
</code></pre>
<p>With Sass, the above code can be written like this:</p>
<pre><code class="lang-scss"><span class="hljs-selector-tag">nav</span> {
  <span class="hljs-attribute">height</span>: <span class="hljs-number">10vh</span>;
  <span class="hljs-attribute">width</span>: <span class="hljs-number">100%</span>;
  <span class="hljs-attribute">display</span>: flex;

  <span class="hljs-selector-tag">ul</span> {
    <span class="hljs-attribute">list-style</span>: none;
    <span class="hljs-attribute">display</span>: flex;
  }

  <span class="hljs-selector-tag">li</span> {
    <span class="hljs-attribute">margin-right</span>: <span class="hljs-number">2.5rem</span>;

    <span class="hljs-selector-tag">a</span> {
      <span class="hljs-attribute">text-decoration</span>: none;
      <span class="hljs-attribute">color</span>: <span class="hljs-number">#707070</span>;

      &amp;<span class="hljs-selector-pseudo">:hover</span> {
        <span class="hljs-attribute">color</span>: <span class="hljs-number">#069c54</span>;
      }
    }
  }
}
</code></pre>
<h3 id="heading-parent-selector">Parent Selector</h3>
<p>In the Sass code above, you might notice the ampersand symbol <code>&amp;</code> used with the hover pseudo-class. This is called a Parent Selector.</p>
<blockquote>
<p>The parent selector, <code>&amp;</code>, is a special selector invented by Sass that's used in nested selectors to refer to the outer selector. Source – <a target="_blank" href="https://sass-lang.com/documentation/style-rules/parent-selector">Sass Documentation</a></p>
</blockquote>
<p>So, in the case of the code above, <code>&amp;</code> will refer to the parent which is the anchor tag <code>a</code>.</p>
<blockquote>
<p>You can check out my <a target="_blank" href="https://israelmitolu.hashnode.dev/writing-cleaner-css-using-bem-methodology">article</a> on how to implement Sass using BEM methodology.</p>
</blockquote>
<h3 id="heading-partials-in-sass">Partials in Sass</h3>
<p>This is one of the many awesome features of Sass that gives you an advantage.</p>
<p>As stylesheets grow large over time, it gets difficult to maintain them. Because of this, it just makes sense to break your stylesheets into smaller chunks. In other words, Partials help you organize and structure your code.</p>
<p>To declare a partial, we will start the file name with an underscore <code>_</code>, and add it in another Sass file using the <code>@import</code> directive.</p>
<p>For example, if we have a <code>_globals.scss</code>, <code>_variables.scss</code>, and <code>_buttons.scss</code>, we could import them into the main SCSS file <code>main.scss</code>.</p>
<pre><code class="lang-scss"><span class="hljs-keyword">@import</span> <span class="hljs-string">"globals"</span>;
<span class="hljs-keyword">@import</span> <span class="hljs-string">"variables"</span>;
<span class="hljs-keyword">@import</span> <span class="hljs-string">"buttons"</span>;
</code></pre>
<p>You'll notice that the underscore and the <code>.scss</code> are not added. That is because Sass automatically assumes that you are referring to the <code>.sass</code> or <code>.scss</code> file.</p>
<h3 id="heading-mixins-in-sass">Mixins in Sass</h3>
<p>Another major issue with CSS is that you'll often use a similar group of styles. Mixins allow you to encapsulate a group of styles, and apply those styles anywhere in your code using the <code>@include</code> keyword.</p>
<p>An example of when you'd use mixins is when using Flexbox.</p>
<pre><code class="lang-scss"><span class="hljs-keyword">@mixin</span> flex-container {
  <span class="hljs-attribute">display</span>: flex;
  <span class="hljs-attribute">justify-content</span>: space-around;
  <span class="hljs-attribute">align-items</span>: center;
  <span class="hljs-attribute">flex-direction</span>: column;
  <span class="hljs-attribute">background</span>: <span class="hljs-number">#ccc</span>;
}

<span class="hljs-selector-class">.card</span> {
  <span class="hljs-keyword">@include</span> flex-container;
}

<span class="hljs-selector-class">.aside</span> {
  <span class="hljs-keyword">@include</span> flex-container;
}
</code></pre>
<h3 id="heading-sass-functions-and-operators">Sass Functions and Operators</h3>
<p>Sass provides a suite of tools to help write more programmatic code.</p>
<p>Sass offers built-in functions that enable us to do calculations and operations that return a specific value.</p>
<p>They range from color calculations to math operations like getting random numbers and calculation of sizes, and even conditionals.</p>
<p>It also provides support for mathematical operators like <code>+</code>, <code>-</code>, <code>\</code>, <code>*</code>, <code>/</code>, and <code>%</code>, which we can use with the <code>calc</code> function.</p>
<p>Here is an example using a pixel to rem conversion function:</p>
<pre><code class="lang-scss"><span class="hljs-keyword">@function</span> pxToRem(<span class="hljs-variable">$pxValue</span>) {
  <span class="hljs-variable">$remValue</span>: (<span class="hljs-variable">$pxValue</span> / <span class="hljs-number">16</span>) + rem;
  <span class="hljs-keyword">@return</span> <span class="hljs-variable">$remValue</span>;
}

<span class="hljs-selector-tag">div</span> {
  <span class="hljs-attribute">width</span>: pxToRem(<span class="hljs-number">480</span>);
}
</code></pre>
<blockquote>
<p>However, it's important to note that the <code>/</code> operator for division is deprecated, and will be removed in Dart Sass 2.0.0. You can read about it in the <a target="_blank" href="https://sass-lang.com/documentation/breaking-changes/slash-div">Docs</a>.</p>
</blockquote>
<p>So, this is how it should be written:</p>
<pre><code class="lang-scss"><span class="hljs-keyword">@use</span> <span class="hljs-string">"sass:math"</span>;

<span class="hljs-keyword">@function</span> pxToRem(<span class="hljs-variable">$pxValue</span>) {
  <span class="hljs-keyword">@return</span> math.div(<span class="hljs-variable">$pxValue</span>, <span class="hljs-number">16px</span>) * <span class="hljs-number">1rem</span>;
}

<span class="hljs-selector-tag">div</span> {
  <span class="hljs-attribute">width</span>: pxToRem(<span class="hljs-number">480px</span>); <span class="hljs-comment">// gives 30rem</span>
}
</code></pre>
<p>Here is an example of conditional logic in a mixin:</p>
<pre><code class="lang-scss"><span class="hljs-keyword">@mixin</span> body-theme(<span class="hljs-variable">$theme</span>) {
  <span class="hljs-keyword">@if</span> <span class="hljs-variable">$theme</span> == <span class="hljs-string">"light"</span> {
    <span class="hljs-attribute">background-color</span>: <span class="hljs-variable">$light-bg</span>;
  } <span class="hljs-keyword">@else</span> {
    <span class="hljs-attribute">background-color</span>: <span class="hljs-variable">$dark-bg</span>;
  }
}
</code></pre>
<p>Sass also provides the <code>lighten</code> and <code>darken</code> functions to adjust a color by a certain percentage.</p>
<p>For example:</p>
<pre><code class="lang-scss"><span class="hljs-variable">$red</span>: <span class="hljs-number">#ff0000</span>;

<span class="hljs-selector-tag">a</span><span class="hljs-selector-pseudo">:visited</span> {
  <span class="hljs-attribute">color</span>: darken(<span class="hljs-variable">$red</span>, <span class="hljs-number">25%</span>);
}
</code></pre>
<h2 id="heading-how-to-set-up-sass-for-local-development">How to Set Up Sass for Local Development</h2>
<p>Great! Now that we have learned about the "theoretical" aspects of Sass, let's get into the code to better understand how it works.</p>
<p>In this section, you will learn how to set up a local development environment, and also go through a simple landing page I have prepared.</p>
<p>Check out the demo on <a target="_blank" href="https://codesandbox.io/s/currying-river-44d7zr?file=/index.html">Codesandbox</a> and code repository on <a target="_blank" href="https://github.com/israelmitolu/Getting-Started-with-SASS">GitHub</a>.</p>
<h3 id="heading-ways-to-compile-sass">Ways to compile Sass</h3>
<p>There are different ways of compiling Sass files which are:</p>
<ul>
<li><p>VS Code Extension</p>
</li>
<li><p>Install using NPM globally</p>
</li>
<li><p>Install using open source apps such as Compass.app, Live Reload, and Koala.</p>
</li>
<li><p>Install using Homebrew (for MacOS)</p>
</li>
</ul>
<p>In this tutorial, we will be using the VS code Extension option because it is the easiest to get started with.</p>
<h3 id="heading-how-to-set-up-sass-for-vs-code">How to Set Up Sass for VS Code</h3>
<h4 id="heading-step-1-install-live-sass-compiler">Step 1: Install Live Sass Compiler</h4>
<p>First, launch Visual Studio Code. Once it's loaded, go to the side panel on the left and select the extensions tab.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/04/1.PNG" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>Extensions tab in VS Code</em></p>
<p>In the search bar, search for "Live Sass Compiler" and install it. This extension helps us to compile the Sass files — <code>.scss</code> (or <code>.sass</code>) – into <code>.css</code> files.</p>
<h4 id="heading-step-2-set-the-save-location">Step 2: Set the Save Location</h4>
<p>Now change the file path so that Sass gets compiled into the <code>styles</code> folder.</p>
<p>To do this, you will make changes to the <code>settings.json</code> file.</p>
<p>In VS Code, go to File &gt; Preferences &gt; Settings. Now search for <code>live sass compile</code> to change the global settings.</p>
<p>Click on <code>Edit settings.json</code>.</p>
<p>Now, on the first few lines, where you see this code:</p>
<pre><code class="lang-json">{
  <span class="hljs-attr">"liveSassCompile.settings.formats"</span>: [
    {
      <span class="hljs-attr">"format"</span>: <span class="hljs-string">"expanded"</span>,
      <span class="hljs-attr">"extensionName"</span>: <span class="hljs-string">".css"</span>,
      <span class="hljs-attr">"savePath"</span>: <span class="hljs-string">"/"</span>
    }
  ],
</code></pre>
<p>Change <code>"savePath": "/"</code> to <code>"savePath": "/styles"</code>, so it now looks like this:</p>
<pre><code class="lang-json">{
  <span class="hljs-attr">"liveSassCompile.settings.formats"</span>:[
    {
      <span class="hljs-attr">"format"</span>: <span class="hljs-string">"expanded"</span>,
      <span class="hljs-attr">"extensionName"</span>:<span class="hljs-string">".css"</span>,
      <span class="hljs-attr">"savePath"</span>:<span class="hljs-string">"/styles"</span>,
    },

    <span class="hljs-comment">// You can also use this minified extension for production, as it reduces the file size</span>

    {
      <span class="hljs-attr">"format"</span>: <span class="hljs-string">"compressed"</span>,
      <span class="hljs-attr">"extensionName"</span>:<span class="hljs-string">".min.css"</span>,
      <span class="hljs-attr">"savePath"</span>:<span class="hljs-string">"/styles"</span>,
    }
  ],
</code></pre>
<h4 id="heading-step-3-compile-sass">Step 3: Compile Sass</h4>
<p>Now, after saving the settings, go back to the Sass file, and click on the button that says "Watch Sass" at the very bottom of the window.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/04/2.PNG" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>Click on "Watch Sass"</em></p>
<p>After you click the button, two files get created: <code>.css</code> and a <code>.css.map</code> in the <code>styles</code> folder.</p>
<p>You should not, however, change any of them. Because it already helps you compile the Sass into CSS every time you save new stylings.</p>
<h4 id="heading-step-4-link-the-css-file">Step 4: Link the CSS file</h4>
<p>Then, link the CSS file in your <code>index.html</code>. In our case:</p>
<pre><code class="lang-html">    <span class="hljs-tag">&lt;<span class="hljs-name">link</span> <span class="hljs-attr">rel</span>=<span class="hljs-string">"stylesheet"</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"/styles/main.css"</span> /&gt;</span>
</code></pre>
<p>Now run the file in your browser. This should be the resulting layout in CodeSandbox below:</p>
<div class="embed-wrapper">
        <iframe width="100%" height="350" src="https://codesandbox.io/embed/currying-river-44d7zr?autoresize=1&amp;fontsize=14&amp;hidenavigation=1&amp;moduleview=1&amp;theme=dark&amp;view=preview" style="aspect-ratio: 16 / 9; width: 100%; height: auto;" title="CodeSandbox embed" allow="geolocation; microphone; camera; midi; vr; accelerometer; gyroscope; payment; ambient-light-sensor; encrypted-media; usb" sandbox="allow-modals allow-forms allow-popups allow-scripts allow-same-origin" loading="lazy"></iframe></div>
<p> </p>
<h2 id="heading-walking-through-the-code">Walking through the code</h2>
<p>Here's an explanation of the code from the previous section:</p>
<ul>
<li><p>We have a basic markup in the <code>index.html</code> file which contains a header and home/hero section.</p>
<ul>
<li><p>It contains a link to the CSS file which the extension compiled for us.</p>
</li>
<li><p>And some JavaScript for the responsive menu toggle.</p>
</li>
</ul>
</li>
<li><p>The <code>main.scss</code> gets compiled, and the resulting CSS file <code>main.css</code> is what is imported in the <code>index.html</code>:</p>
<pre><code class="lang-html">  <span class="hljs-tag">&lt;<span class="hljs-name">link</span> <span class="hljs-attr">rel</span>=<span class="hljs-string">"stylesheet"</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"/styles/main.css"</span> /&gt;</span>
</code></pre>
</li>
<li><p>The Main Scss file <code>main.scss</code> imports all of the partials: <code>_base.scss</code>, <code>_components.scss</code>, <code>_home.scss</code>, <code>_layout.scss</code> <code>_responsive.scss</code>, <code>_variables.scss</code>.</p>
<pre><code class="lang-scss">  <span class="hljs-keyword">@import</span> <span class="hljs-string">"variables"</span>;
  <span class="hljs-keyword">@import</span> <span class="hljs-string">"base"</span>;
  <span class="hljs-keyword">@import</span> <span class="hljs-string">"layout"</span>;
  <span class="hljs-keyword">@import</span> <span class="hljs-string">"components"</span>;
  <span class="hljs-keyword">@import</span> <span class="hljs-string">"home"</span>;
  <span class="hljs-keyword">@import</span> <span class="hljs-string">"responsive"</span>;
</code></pre>
</li>
<li><p>The base partial contains the mixins of <code>flex</code> and <code>grid</code> which are included in the places where we need them.</p>
</li>
</ul>
<h2 id="heading-conclusion">Conclusion</h2>
<p>Congrats! If you made it to the end, that means you have learned about how Sass works, its cool features, and hopefully you start using it soon.</p>
<p>If you want to learn more about Sass, I recommend checking out <a target="_blank" href="https://www.youtube.com/watch?v=aoQ6S1a32j8&amp;t=3323s">freeCodeCamp's course</a>.</p>
<p>If you found this article useful (which I'm sure you did 😉), do share it with your friends and network, and feel free to connect with me on <a target="_blank" href="https://twitter.com/israelmitolu">Twitter</a> and my <a target="_blank" href="https://israelmitolu.hashnode.dev">blog</a> where I share resources and articles to make you a better dev.</p>
<p>Thanks for reading, and happy coding!</p>
 ]]>
                </content:encoded>
            </item>
        
    </channel>
</rss>
