<?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: Bishal Pahari</title>
    <description>The latest articles on DEV Community by Bishal Pahari (@bishalpahari).</description>
    <link>https://dev.to/bishalpahari</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%2F1036061%2Fce3f5d1f-e9c7-420b-9162-340b3da9ddbd.jpeg</url>
      <title>DEV Community: Bishal Pahari</title>
      <link>https://dev.to/bishalpahari</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/bishalpahari"/>
    <language>en</language>
    <item>
      <title>Recreating Ambient Mode from Youtube Videos</title>
      <dc:creator>Bishal Pahari</dc:creator>
      <pubDate>Sat, 01 Mar 2025 04:16:38 +0000</pubDate>
      <link>https://dev.to/bishalpahari/recreating-ambient-mode-from-youtube-videos-5f75</link>
      <guid>https://dev.to/bishalpahari/recreating-ambient-mode-from-youtube-videos-5f75</guid>
      <description>&lt;h2&gt;
  
  
  Briefly About Ambient Mode
&lt;/h2&gt;

&lt;p&gt;In October 2022, YouTube introduced Ambient Mode, an immersive feature that enhances the viewing experience by extending the video's colors into the interface background. This effect, available exclusively in Dark Mode, reduces eye strain and adds a cinematic touch to video playback. &lt;/p&gt;

&lt;p&gt;In this blog, we'll create a dynamic ambient mode similar to YouTube's Ambient Mode.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step by Step Guide
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;It's essentially two different stacked videos playing in sync with effects.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F34db9i39rpa05q1p5psr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F34db9i39rpa05q1p5psr.png" alt=" " width="290" height="555"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;First, wrap two &lt;code&gt;video&lt;/code&gt; elements within a &lt;code&gt;div.container&lt;/code&gt;. Here, we'll use the first &lt;code&gt;video&lt;/code&gt; element for the ambient mode and the second as our main video. Ensure you mute and remove the controls of the ambient &lt;code&gt;video&lt;/code&gt; element.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fb7qfp3c1yr9j7q0qp9gm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fb7qfp3c1yr9j7q0qp9gm.png" alt=" " width="299" height="575"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzgml9mwr0htacgj5sday.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzgml9mwr0htacgj5sday.png" alt=" " width="362" height="505"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Next, set the container's position to &lt;code&gt;relative&lt;/code&gt; and the second video's position to &lt;code&gt;absolute&lt;/code&gt;, positioning the main video over the ambient one.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsowkrztcbesetc5qn9jo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsowkrztcbesetc5qn9jo.png" alt=" " width="354" height="232"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;At this stage, your setup should look similar to the picture above.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjeeuvks4zxa8yhwgvjhn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjeeuvks4zxa8yhwgvjhn.png" alt=" " width="338" height="234"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, scale up the ambient mode video element so it expands and becomes visible.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvz4khc6bqjmj9m830qyp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvz4khc6bqjmj9m830qyp.png" alt=" " width="338" height="234"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Add some blur to the ambient mode &lt;code&gt;video&lt;/code&gt; element.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Filvan649eb29tq8cwyu4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Filvan649eb29tq8cwyu4.png" alt=" " width="332" height="244"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Finally, reduce the opacity of the ambient mode video element to enhance the effect.&lt;/p&gt;

&lt;p&gt;If you want to create a dynamic ambient mode that syncs with the main video, you can achieve this using JavaScript. Configure actions so that the main video's controls also affect the ambient mode video.&lt;/p&gt;

&lt;p&gt;Thanks for following along! I hope you found this guide helpful and easy to understand. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Youtube blog:&lt;/strong&gt; &lt;a href="https://support.google.com/youtube/answer/12827017?hl=en&amp;amp;co=GENIE.Platform%3DAndroid" rel="noopener noreferrer"&gt;Open (Watch YouTube in ambient mode)&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Demo Site:&lt;/strong&gt; &lt;a href="https://youtube-ambient-demo.netlify.app/" rel="noopener noreferrer"&gt;Youtube-ambient-mode&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Source Code:&lt;/strong&gt; &lt;a href="https://github.com/Bishal-Pahari/Youtube-ambient-mode" rel="noopener noreferrer"&gt;Github&lt;/a&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>css</category>
      <category>javascript</category>
      <category>frontend</category>
    </item>
    <item>
      <title>The Magical effect Behind the KPR Verse Footer Effect</title>
      <dc:creator>Bishal Pahari</dc:creator>
      <pubDate>Wed, 09 Oct 2024 03:48:12 +0000</pubDate>
      <link>https://dev.to/bishalpahari/unraveling-the-magical-effect-behind-the-kpr-verse-footer-effect-42bm</link>
      <guid>https://dev.to/bishalpahari/unraveling-the-magical-effect-behind-the-kpr-verse-footer-effect-42bm</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhnccc4u2rflvu1nd0h64.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhnccc4u2rflvu1nd0h64.gif" alt="KPR Verse" width="600" height="287"&gt;&lt;/a&gt;&lt;br&gt;
I was scrolling through the KPR-verse website when I noticed this cool effect at the bottom. Whenever I see something like this, I always want to recreate it. My process is simple: I start by trying to replicate the effect, then make a blog to explain how it’s done, aiming to break down complex designs into easy-to-understand steps.&lt;/p&gt;

&lt;p&gt;At first, I often doubt whether I can pull it off, but after some deep thought, I usually find a way to break the design into pieces. By connecting those pieces, I manage to achieve the effect. Enough talk — let’s dive into how you can make it yourself!&lt;/p&gt;

&lt;h2&gt;
  
  
  Breaking Down the KPR Verse Footer Effect
&lt;/h2&gt;

&lt;p&gt;At the end of the site, you’ll find the KPR banner in the background. At first glance, it might seem a bit complex, but it’s not that complicated.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu5pr9dq6lqh4k5idfkkb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu5pr9dq6lqh4k5idfkkb.png" alt="Step 1" width="357" height="597"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;First, create a &lt;code&gt;div&lt;/code&gt; with the class &lt;code&gt;.container&lt;/code&gt;. This will be the root element for our tutorial.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr8699ztgp5nf78vab6vq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr8699ztgp5nf78vab6vq.png" alt="Step 2" width="386" height="582"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Next, divide the .container into two parts: &lt;code&gt;.subcontainer-wrapper&lt;/code&gt; and &lt;code&gt;footer&lt;/code&gt;. The &lt;code&gt;footer&lt;/code&gt; element will hold the KPR banner.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fj05u7ujiis9i64upbwht.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fj05u7ujiis9i64upbwht.png" alt="Step 3" width="800" height="593"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, further divide the &lt;code&gt;.subcontainer-wrapper&lt;/code&gt; into two parts. The upper part contains all the content for our website, while the lower part is left blank with no background—essentially invisible, but it still has the width and height of the viewport. The lower half is crucial because it allows us to see the footer.&lt;/p&gt;

&lt;p&gt;Up to this point, all the elements are in the normal HTML flow, i.e., top-to-bottom. Now, we need to break this order by placing the footer behind and bringing the &lt;code&gt;.subcontainer-wrapper&lt;/code&gt; in front. To do this, we'll set the &lt;code&gt;.subcontainer-wrapper&lt;/code&gt; to position: absolute relative to its parent .container. This will remove the &lt;code&gt;.subcontainer-wrapper&lt;/code&gt; from the normal DOM flow.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fplh0d63s5o9yty5j34hf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fplh0d63s5o9yty5j34hf.png" alt="Step 4" width="517" height="549"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;That’s all we need to set up the effect. Now, fill the upper part of the &lt;code&gt;.subcontainer-wrapper&lt;/code&gt; with content, but leave the lower part—the invisible div—empty. Also, add content to the footer.&lt;/p&gt;

&lt;p&gt;Once you’ve added the content, it should look something like this.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8tfx9sdp6stxe40nlhy9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8tfx9sdp6stxe40nlhy9.png" alt="Step 5" width="656" height="596"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The upper part hides the background, and as you scroll down, when the visible content ends, the invisible section reveals the footer. Voilà! There you have it — simple and easy.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fl1ti5kh57c2msx0cv3ee.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fl1ti5kh57c2msx0cv3ee.png" alt="Step 6" width="626" height="638"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;_Thanks for following along! I hope you found this guide helpful and easy to understand. Stay curious and keep exploring!&lt;/p&gt;

&lt;p&gt;original: &lt;a href="https://kprverse.com/" rel="noopener noreferrer"&gt;KPR-verse&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Demo site: &lt;a href="https://kprverse-demo.netlify.app/" rel="noopener noreferrer"&gt;Demo Link&lt;/a&gt; , For this demonstration, I used the Google I/O site (&lt;a href="https://io.google/2024/" rel="noopener noreferrer"&gt;https://io.google/2024/&lt;/a&gt;) and adapted it to showcase a similar concept.&lt;/p&gt;

&lt;p&gt;Source code: &lt;a href="https://github.com/Bishal-Pahari/KPR-verse-Footer" rel="noopener noreferrer"&gt;Github Link&lt;/a&gt;_&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>css</category>
      <category>frontendchallenge</category>
    </item>
    <item>
      <title>iOS-Style Switch Button: A Simple Step-by-Step Guide</title>
      <dc:creator>Bishal Pahari</dc:creator>
      <pubDate>Sat, 16 Mar 2024 07:37:50 +0000</pubDate>
      <link>https://dev.to/bishalpahari/crafting-an-ios-style-switch-button-a-simple-step-by-step-guide-5ii</link>
      <guid>https://dev.to/bishalpahari/crafting-an-ios-style-switch-button-a-simple-step-by-step-guide-5ii</guid>
      <description>&lt;p&gt;Have you ever wanted to create an iOS-style switch button for your web apps? No need to be an expert! Let’s create one together using HTML, CSS, and JavaScript.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxm6w04djqt32yulrlf7t.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxm6w04djqt32yulrlf7t.png" alt="iOS switch Button" width="313" height="471"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Pre-requisites
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Basic knowledge of HTML, CSS, and JavaScript.&lt;/li&gt;
&lt;li&gt;A text editor of your choice.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Let’s deep dive into the code&lt;/strong&gt;&lt;br&gt;
Create a new file named &lt;code&gt;index.html&lt;/code&gt; within your project folder and add the following HTML code:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;index.html&lt;/em&gt;&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;!DOCTYPE html&amp;gt;
&amp;lt;html lang="en"&amp;gt;
  &amp;lt;head&amp;gt;
    &amp;lt;meta charset="UTF-8" /&amp;gt;
    &amp;lt;meta name="viewport" content="width=device-width, initial-scale=1.0" /&amp;gt;
    &amp;lt;title&amp;gt;IOS Button&amp;lt;/title&amp;gt;

    &amp;lt;!-- Link to the external CSS file for styling --&amp;gt;
    &amp;lt;link rel="stylesheet" href="./styles.css" /&amp;gt;

    &amp;lt;!-- Link to the external JavaScript file for functionality --&amp;gt;
    &amp;lt;script defer src="./main.js"&amp;gt;&amp;lt;/script&amp;gt;
  &amp;lt;/head&amp;gt;
  &amp;lt;body&amp;gt;
    &amp;lt;!-- Container for the switch button --&amp;gt;
    &amp;lt;div class="switch-container"&amp;gt;

      &amp;lt;!-- Box containing the image and text --&amp;gt;
      &amp;lt;div class="pack-box"&amp;gt;
        &amp;lt;!-- Image for the switch button --&amp;gt;
        &amp;lt;img id="svg-icon" src="./active.svg" /&amp;gt;
        &amp;lt;!-- Text for the switch button --&amp;gt;
        &amp;lt;p&amp;gt;Notification Alert&amp;lt;/p&amp;gt;
      &amp;lt;/div&amp;gt;

      &amp;lt;!-- Label associating the input with the switch functionality --&amp;gt;
      &amp;lt;label for="switch-1" class="switch"&amp;gt;
        &amp;lt;!-- Input checkbox acting as the switch button --&amp;gt;
        &amp;lt;input type="checkbox" id="switch-1" onClick="handleChange()" /&amp;gt;
        &amp;lt;!-- Span element creating the appearance of the slider button --&amp;gt;
        &amp;lt;span class="slider"&amp;gt;&amp;lt;/span&amp;gt;
      &amp;lt;/label&amp;gt;

    &amp;lt;/div&amp;gt;
  &amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I’ve structured the content to include a switch container &lt;code&gt;.switch-container&lt;/code&gt; housing a pack box &lt;code&gt;.pack-box&lt;/code&gt; holding an image &lt;code&gt;.svg-icon&lt;/code&gt; and text "Notification Alert". A label &lt;code&gt;.switch&lt;/code&gt; is associated with a checkbox input &lt;code&gt;.switch-1&lt;/code&gt; and a span &lt;code&gt;.slider&lt;/code&gt; to create the appearance of a slider button.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpfxlesi8cpw5ji6abhfv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpfxlesi8cpw5ji6abhfv.png" alt="HTML Element visualization" width="800" height="471"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Styling the Switch Button with CSS&lt;/strong&gt;&lt;br&gt;
To style the switch button, create a file named styles.css and link it to your HTML file. Begin by ensuring proper alignment and centering of elements:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/* Centering body content */ 
body {
  height: 100dvh;
  width: 100dvw;
  display: flex;
  justify-content: center;
  align-items: center;
}

/* Styling container */ 
.switch-container {
  width: 300px;
  height: 150px;
  border: 3px solid #636363;
  border-radius: 25px 0;
  display: flex;
  align-items: center;
  justify-content: space-around;
}


.switch-container img {
  width: 25px;
}

/* Styling box with image and text */
.pack-box {
  display: flex;
  gap: 1rem;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Moving to the main logic for the switch button styling, we define its dimensions and behavior. The &lt;code&gt;.switch&lt;/code&gt; class sets the width and height of the label and positions it relatively:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/* Styling switch button */
.switch {
  width: 60px;
  height: 35px;
  display: inline-block;
  cursor: pointer;
  position: relative;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;.slider&lt;/code&gt; class positions the slider element absolutely within its container, ensuring it fills the entire area with flush positioning:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/* Styling slider button */ 
.switch .slider {
  position: absolute;
  inset: 0;
  background: rgb(145, 145, 145);
  border-radius: 25px;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To hide the checkbox, set its display property to &lt;code&gt;none&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/* Hiding checkbox input */ 
.switch input {
  display: none;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Using the &lt;code&gt;::before&lt;/code&gt; pseudo-class, add a blank content before the slider to center the toggle button and change its appearance to circular:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/* Styling slider appearance */
.switch .slider::before {
  content: "";
  position: absolute;
  height: 27px;
  width: 27px;
  left: 5px;
  top: 4px;
  background: white;
  transition: 0.5s all;
  border-radius: 50%;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwt5kp0sdippg0omqdf8i.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwt5kp0sdippg0omqdf8i.png" alt="Switch button without toggling effect" width="197" height="120"&gt;&lt;/a&gt;&lt;br&gt;
By leveraging the &lt;code&gt;:checked&lt;/code&gt; pseudo-class, we modify the background color of the slider to green when the switch is toggled:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/* Styling checked state */
.switch input:checked + .slider {
  background: rgb(12, 235, 12);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Lastly, to enable the slider button to move when toggled, use the &lt;code&gt;transform&lt;/code&gt; property to translate it horizontally:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; /* Styling slider movement */
.switch input:checked + .slider::before {
  transform: translateX(23px);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These CSS rules make sure the switch button works smoothly. It changes its look and moves as expected when you toggle it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmd04hdynvdtmnlkdbhqy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmd04hdynvdtmnlkdbhqy.gif" alt="fully functional iOS-style switch button" width="306" height="195"&gt;&lt;/a&gt;&lt;br&gt;
&lt;strong&gt;Adding JavaScript for Interactivity&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Create a file named &lt;code&gt;main.js&lt;/code&gt; and link it to your HTML file. Use the following JavaScript code to change the icon when toggling:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Main.js&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const svgIcon = document.querySelector("#svg-icon");

const handleChange = () =&amp;gt; {
  // Toggle icon based on switch state
  if (document.getElementById("switch-1").checked) {
    svgIcon.src = "./active.svg";
  } else {
    svgIcon.src = "./nonActive.svg";
  }
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;br&gt;
Creating an iOS-style switch button for your web applications is achievable with basic knowledge of HTML, CSS, and JavaScript. By following the simple steps outlined in this guide. Feel free to customize the styling and functionality to suit your specific needs and preferences.&lt;/p&gt;

&lt;p&gt;For the full code and resources used in this tutorial, you can find them in my GitHub repository: &lt;a href="https://github.com/Bishal-Pahari/iOS-Button" rel="noopener noreferrer"&gt;https://github.com/Bishal-Pahari/iOS-Button&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Simple Introduction to Higher Order Components in React</title>
      <dc:creator>Bishal Pahari</dc:creator>
      <pubDate>Sat, 16 Mar 2024 04:19:36 +0000</pubDate>
      <link>https://dev.to/bishalpahari/simple-introduction-to-higher-order-components-in-react-428</link>
      <guid>https://dev.to/bishalpahari/simple-introduction-to-higher-order-components-in-react-428</guid>
      <description>&lt;p&gt;&lt;strong&gt;Introduction&lt;/strong&gt;&lt;br&gt;
Have you ever heard of Higher Order Components (HOCs)? Don’t let the fancy name intimidate you! Think of them as powerful tools in React that can make your life easier when building complex applications. In this article, we’ll take a simple dive into what Higher Order Components are, why they’re useful, and how they can level up your React development skills without getting tangled in complex jargon.&lt;/p&gt;

&lt;p&gt;So, grab your favorite beverage, settle into your comfy chair, and let’s explore the world of Higher Order Components together!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why use Higher Order Components?&lt;/strong&gt;&lt;br&gt;
Higher Order Components offer several benefits in React development. Firstly, it helps in reusing logic across multiple components. They simplify handling common functionalities like authentication, logging, or data fetching. With HOCs, you can separate concerns and keep your components focused on their specific tasks. HOCs enable composing multiple behaviors or features into a single component effortlessly. Additionally, they make the code cleaner and easier to understand by moving complex logic out of components. This organization helps keep components focused on their main tasks, resulting in more organized and maintainable code overall&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;When to use Higher Order Components?&lt;/strong&gt;&lt;br&gt;
Imagine we have a dashboard, a profile page, and a settings page in our React application. Each of these pages requires the user to be logged in before accessing the content. Instead of writing authentication logic separately for each component, we can create a single Higher Order Component called &lt;code&gt;withAuth&lt;/code&gt; that wraps around these components.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;withAuth&lt;/code&gt; HOC can check if the user is authenticated. If the user is authenticated, it renders the component; otherwise, it redirects the user to the login page.&lt;/p&gt;

&lt;p&gt;This approach helps in keeping our code clean, modular, and easy to maintain. If the authentication logic needs to be updated or modified, we only need to do it in one place, inside the &lt;code&gt;withAuth&lt;/code&gt; HOC, rather than updating it in multiple components.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Implementing Higher Order Components in your React App&lt;/strong&gt;&lt;br&gt;
I will try to explain to you the concept of Higher Order Components (HOCs) using a simple analogy that involves cookies:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fy1b62hqd2kf8j8eqq7qo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fy1b62hqd2kf8j8eqq7qo.png" alt=" " width="800" height="464"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Imagine you have two cookies, and you want to add chocolate chips on top of each one. Now think if you only have two cookies, it’s easy to manually place the chocolate chips on each cookie. But what if you have a hundred cookies? Doing it manually for each cookie would take forever!&lt;/p&gt;

&lt;p&gt;This is where Higher Order Components (HOCs) come in handy. Think of the cookies as your components and the chocolate chips as some extra functionality or props you want to add to them. With a HOC, you can create a special function that automatically adds the chocolate chips to each cookie component without having to do it manually for each one. So, whether you have two cookies or a hundred, the HOC takes care of the job for you, making your life as a baker (or developer) much easier!&lt;/p&gt;

&lt;p&gt;First, let’s create a file called &lt;code&gt;App.js&lt;/code&gt;, which will be our entry point of our React application.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;App.js&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from "react";
import Cookie1 from "./components/Cookie1";
import Cookie2 from "./components/Cookie2";

const App = () =&amp;gt; {
  return (
    &amp;lt;div&amp;gt;
      &amp;lt;h1&amp;gt;Cookies with Chocolate Chips:&amp;lt;/h1&amp;gt;
      &amp;lt;Cookie1 /&amp;gt;
      &amp;lt;Cookie2 /&amp;gt;
    &amp;lt;/div&amp;gt;
  );
};

export default App;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, let’s create a file called &lt;code&gt;HOC.js&lt;/code&gt;, which will be our Higher Order Component (HOC) responsible for adding chocolate chips to our cookies.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;HOC.js&lt;/em&gt;&lt;br&gt;
We define our Higher Order Component called &lt;code&gt;WithComponentChips&lt;/code&gt;. This function takes a &lt;code&gt;WrappedComponent&lt;/code&gt; as input and returns an enhanced version of it. In this case, it adds functionality to track and display the number of chocolate chips added to the cookies.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { useState } from "react";

const WithComponentChips = (WrappedComponent) =&amp;gt; {
  const EnhancedComponent = () =&amp;gt; {
    const [addChocolateChips, setAddChocolateChips] = useState(0);
    const handleChange = () =&amp;gt; {
      setAddChocolateChips(addChocolateChips + 1);
    };
    return (
      &amp;lt;WrappedComponent
        addChocolateChips={addChocolateChips}
        handleChange={handleChange}
      /&amp;gt;
    );
  };
  return EnhancedComponent;
};

export default WithComponentChips;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, let’s create another file called &lt;code&gt;Cookie1.js&lt;/code&gt; &amp;amp; &lt;code&gt;Cookie2.js&lt;/code&gt;, which will represent our cookie component.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Cookie1.jsx&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from "react";
import WithComponentChips from "../HOC";

const Cookie1 = ({ addChocolateChips, handleChange }) =&amp;gt; {
  return (
    &amp;lt;div&amp;gt;
      &amp;lt;p&amp;gt; {addChocolateChips} 🍫 Chocolate Chips 🍫&amp;lt;/p&amp;gt;
      &amp;lt;button onClick={handleChange}&amp;gt;Add Chocolate Chip&amp;lt;/button&amp;gt;
    &amp;lt;/div&amp;gt;
  );
};

export default WithComponentChips(Cookie1);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Cookie2.jsx&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from "react";
import WithComponentChips from "../HOC";

const Cookie2 = ({ addChocolateChips, handleChange }) =&amp;gt; {
  return (
    &amp;lt;div&amp;gt;
      &amp;lt;p&amp;gt; {addChocolateChips} 🍫 Chocolate Chips 🍫&amp;lt;/p&amp;gt;
      &amp;lt;button onClick={handleChange}&amp;gt;Add Chocolate Chip&amp;lt;/button&amp;gt;
    &amp;lt;/div&amp;gt;
  );
};

export default WithComponentChips(Cookie2);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These files represent our individual cookie components. They receive props &lt;code&gt;addChocolateChips&lt;/code&gt; and &lt;code&gt;handleChange&lt;/code&gt; from the Higher Order Component. Each component displays the number of chocolate chips added and includes a button to add more chips when clicked.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7e3j6ne1s72fmndzkyqe.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7e3j6ne1s72fmndzkyqe.png" alt=" " width="550" height="287"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The magic happens when we export the &lt;code&gt;Cookie1.jsx&lt;/code&gt; and &lt;code&gt;Cookie2.jsx&lt;/code&gt; components after enhancing them with the &lt;code&gt;WithComponentChips&lt;/code&gt; HOC. By doing so, both cookies automatically gain the ability to track and add chocolate chips without us having to manually add this functionality to each one individually.&lt;/p&gt;

&lt;p&gt;This example demonstrates how Higher Order Components (HOCs) can make it easier to add extra features to lots of components in our React app. It’s like adding chocolate chips to cookies — instead of doing it one by one, we use a special method that does it automatically for all our cookies. So, whether we have just a couple of cookies or a whole bunch, the HOC does the work for us, making our code neater and simpler to manage.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;br&gt;
In this guide to Higher Order Components (HOCs) in React, we’ve learned how they make building apps easier. HOCs are like special helpers that let us add extra features to lots of components without repeating ourselves. We saw how they help keep our code organized, by separating different tasks into smaller pieces. Using a simple example with cookies and chocolate chips, we understood how HOCs work: just like adding chocolate chips to cookies, we can use HOCs to automatically add functionality to our components. Whether we have a few components or many, HOCs do the hard work for us, making our code cleaner and simpler to manage.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
