<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>Coder Legion - Recent posts tagged webcomponents</title>
<link>https://coderlegion.com/tag/webcomponents</link>
<description>Powered by Question2Answer</description>
<item>
<title>Build A Responsive Sidebar with React and Styled Components</title>
<link>https://coderlegion.com/25230/build-a-responsive-sidebar-with-react-and-styled-components</link>
<description>&lt;h3 id=&quot;why-does-sidebar-navbar-exist-&quot;&gt;Why does Sidebar / Navbar exist?&lt;/h3&gt;
&lt;p&gt;Navigation is the most easiest way to go through the website. The navigation links can be displayed either in Sidebar or Navbar. Both services are widely used by most of the websites. &lt;/p&gt;
&lt;h3 id=&quot;what-will-we-be-building-&quot;&gt;What will we be building?&lt;/h3&gt;
&lt;p&gt;In this article, we will be building a simple yet &lt;em&gt;responsive sidebar&lt;/em&gt;, step-by-step using &lt;a rel=&quot;nofollow&quot; href=&quot;https://reactjs.org&quot;&gt;ReactJS&lt;/a&gt;, &lt;a rel=&quot;nofollow&quot; href=&quot;https://styled-components.com&quot;&gt;styled-components&lt;/a&gt;, and &lt;a rel=&quot;nofollow&quot; href=&quot;https://reactrouter.com&quot;&gt;React Router DOM&lt;/a&gt; as our major dependencies. This sidebar will be fully responsive on all devices, a demo of which is &lt;a rel=&quot;nofollow&quot; href=&quot;https://jealousgx-sidebar.vercel.app&quot;&gt;present here&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Pro: We will also be learning one of the folder structures in this react app.&lt;/em&gt;&lt;/p&gt;
&lt;h3 id=&quot;prerequisites&quot;&gt;Prerequisites&lt;/h3&gt;
&lt;p&gt;Before diving into coding this component, you need to make sure you have good knowledge of:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;HTML, CSS, JavaScript&lt;/li&gt;
&lt;li&gt;ReactJS&lt;/li&gt;
&lt;li&gt;Styled-components&lt;/li&gt;
&lt;li&gt;React Router DOM&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Moreover, you also need have:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;NodeJS (Stable version) &lt;/li&gt;
&lt;li&gt;NPM and /  or Yarn&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;building-the-component&quot;&gt;Building the component&lt;/h3&gt;
&lt;p&gt;In &lt;em&gt;Command Prompt&lt;/em&gt;, navigate to the directory where you would like to create the project and type:&lt;/p&gt;
&lt;h4 id=&quot;1-install-the-react-app&quot;&gt;1. Install the React App&lt;/h4&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;# With npm
npx create-react-app react-sidebar

# With yarn
yarn create react-app react-sidebar
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt; where &lt;strong&gt;&lt;em&gt;react-sidebar&lt;/em&gt;&lt;/strong&gt; is the project directory name. Now open this project directory in your favorite code editor. I will be using &lt;a rel=&quot;nofollow&quot; href=&quot;https://code.visualstudio.com&quot;&gt;Visual Studio Code&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Now, keep &lt;code&gt;index.js&lt;/code&gt;, &lt;code&gt;App.js&lt;/code&gt; and &lt;code&gt;App.css&lt;/code&gt; and delete the other files / folders inside &lt;code&gt;src&lt;/code&gt; folder. This will clean up most of the react app.&lt;/p&gt;
&lt;p&gt;Inside &lt;code&gt;public&lt;/code&gt; folder, keep &lt;code&gt;index.html&lt;/code&gt; file and delete all other files / folders.&lt;/p&gt;
&lt;h4 id=&quot;2-add-the-packages-to-the-react-app&quot;&gt;2. Add the packages to the react app&lt;/h4&gt;
&lt;p&gt;Install Material UI Icons, React Router DOM, styled-components. Run the following command to get them installed on our react app:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;# With npm
npm install @mui/icons-material @mui/material @emotion/styled @emotion/react react-router-dom styled-components

# With yarn
yarn add @mui/material @emotion/react @emotion/styled react-router-dom styled-components
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Let's connect the whole app with &lt;code&gt;react-router-dom&lt;/code&gt; so that its functions / components can be used everywhere. Replace the code in &lt;code&gt;src/index.js&lt;/code&gt; with the following:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;// src/index.js
import React from &quot;react&quot;;
import { BrowserRouter } from &quot;react-router-dom&quot;;
import ReactDOM from &quot;react-dom/client&quot;;
import App from &quot;./App&quot;;

const root = ReactDOM.createRoot(document.getElementById(&quot;root&quot;));
root.render(
  &amp;lt;React.StrictMode&amp;gt;
    &amp;lt;BrowserRouter&amp;gt;
      &amp;lt;App /&amp;gt;
    &amp;lt;/BrowserRouter&amp;gt;
  &amp;lt;/React.StrictMode&amp;gt;
);
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;First and foremost, we need to use &lt;code&gt;react-router-dom&lt;/code&gt; to create links to different pages within our app. So, we need to connect the whole app with its parent component which is &lt;code&gt;BrowserRouter&lt;/code&gt;. This will give us access to use almost all of the components, the said package has to offer.&lt;/p&gt;
&lt;p&gt;To do so, at first, we import the parent component and wrap the &lt;code&gt;App&lt;/code&gt; component inside the parent component. This &lt;code&gt;App&lt;/code&gt; component covers our whole app and we can use React Router anywhere within our app.&lt;/p&gt;
&lt;p&gt;Run the app in your browser using &lt;code&gt;npm start&lt;/code&gt; or &lt;code&gt;yarn start&lt;/code&gt; and head to &lt;strong&gt;localhost:3000&lt;/strong&gt; to view the changes.&lt;/p&gt;
&lt;p&gt;Now, let's create the routes / pages using &lt;code&gt;react-router-dom&lt;/code&gt;. Replace the code in &lt;code&gt;src/App.js&lt;/code&gt; with the following code:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;// src/App.js
import { Routes, Route } from &quot;react-router-dom&quot;;
import { DynamicItem, Sidebar, dummyData } from &quot;./components&quot;;
import &quot;./App.css&quot;;

function App() {
  return (
    &amp;lt;div id=&quot;main&quot;&amp;gt;
      &amp;lt;Sidebar&amp;gt;
        &amp;lt;Routes&amp;gt;
          &amp;lt;Route path=&quot;/&quot; element={&amp;lt;DynamicItem page=&quot;homepage&quot; /&amp;gt;} /&amp;gt;
          {dummyData &amp;amp;&amp;amp;
            dummyData.map((item, index) =&amp;gt; (
              &amp;lt;Route
                key={index}
                path={item.path}
                element={&amp;lt;DynamicItem page={item.name} /&amp;gt;}
              /&amp;gt;
            ))}
        &amp;lt;/Routes&amp;gt;
      &amp;lt;/Sidebar&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}

export default App;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Here, we are importing &lt;code&gt;Routes, Route&lt;/code&gt; from &lt;code&gt;react-router-dom&lt;/code&gt; which will help us create pages within our app. We are also importing the &lt;code&gt;Sidebar&lt;/code&gt; component, which we will be creating after sometime, &lt;code&gt;dummyData&lt;/code&gt;, some random data which includes path to our pages, &lt;code&gt;DynamicItem&lt;/code&gt;, a dummy page which displays the page name as we navigate to it.&lt;/p&gt;
&lt;p&gt;Next thing, we need to do is use the &lt;code&gt;Sidebar&lt;/code&gt; component. This component will be such that it will accept children as props so that it is visible everywhere as we navigate between the pages. Right after that, we need to add &lt;code&gt;Routes&lt;/code&gt; component, a container which covers our pages / routes as we create them so that the app knows this is a routes container and it contains pages.&lt;/p&gt;
&lt;p&gt;Now, the only thing we need to do is add the routes we want. We know that &lt;code&gt;dummyData&lt;/code&gt; contains the paths to the pages, we can map through the data to get them, and use &lt;code&gt;Route&lt;/code&gt; component for each of the path. The &lt;code&gt;Route&lt;/code&gt; component accepts two properties, &lt;code&gt;path&lt;/code&gt;, where the route will be navigating to, and &lt;code&gt;element&lt;/code&gt;, which is a component that will be rendered in that page / route. &lt;/p&gt;
&lt;p&gt;Now, we need to add the basic styling to our app. These styles only define the layout of our app. Replace the code in &lt;code&gt;src/App.css&lt;/code&gt; with the following code:&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Note: We can also create some of the styling using styled-components. You can do the styling however you like, but here, I have used css for basic styling.&lt;/em&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-css&quot;&gt;/* src/App.css */
* {
  margin: 0;
  padding: 0;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  outline: none;
  border: none;
  text-decoration: none;

  font-family: &quot;IBM Plex Sans&quot;, -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;,
    &quot;Oxygen&quot;, &quot;Ubuntu&quot;, &quot;Cantarell&quot;, &quot;Fira Sans&quot;, &quot;Droid Sans&quot;, &quot;Helvetica Neue&quot;,
    sans-serif;
}

#main {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: row;
}

.btn {
  margin: 1rem 1rem 0 0;
  padding: 0.25rem 0.5rem;
  display: flex;
  gap: 0.25rem;
  align-items: center;
  justify-content: center;
  background: transparent;
  outline: none;
  border: 1px solid #808080;
  border-radius: 3px;
  -webkit-border-radius: 3px;
  -moz-border-radius: 3px;
  -ms-border-radius: 3px;
  -o-border-radius: 3px;
  cursor: pointer;
  transition: all 0.2s ease-in-out;
  -webkit-transition: all 0.2s ease-in-out;
  -moz-transition: all 0.2s ease-in-out;
  -ms-transition: all 0.2s ease-in-out;
  -o-transition: all 0.2s ease-in-out;
}

.btn:hover {
  background-color: #e4e3e34d;
}

#page {
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  text-transform: capitalize;
  font-size: 1rem;
  overflow: hidden;
}

@media screen and (min-width: 468px) {
  #page {
    font-size: 3rem;
  }

  .btn {
    padding: 0.5rem 0.75rem;
    gap: 0.5rem;
  }
}

.app__brand__text {
  font-size: 2rem;
  font-weight: 700;
  color: #5a8dee;
  margin-left: 0.5rem;
}

/* Sidebar toggle button starts */
.outer__circle {
  position: relative;
  width: 1.5rem;
  height: 1.5rem;
  border-radius: 50%;
  background-color: #5f97ef;
  display: flex;
  align-items: center;
  justify-content: center;
}

.outer__circle::after {
  position: absolute;
  top: 0.225rem;
  left: 0.215rem;
  content: &quot;&quot;;
  width: 1.1rem;
  height: 1.1rem;
  border-radius: 50%;
  background-color: #fff;
}

.inner__circle {
  position: relative;
  width: 0.75rem;
  height: 0.75rem;
  border-radius: 50%;
  background-color: #5f97ef;
  z-index: 100;
}

.inner__circle::after {
  position: absolute;
  top: 0.125rem;
  left: 0.15rem;
  content: &quot;&quot;;
  width: 0.5rem;
  height: 0.5rem;
  border-radius: 50%;
  background-color: #fff;
}
/* Sidebar toggle button ends */
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Here, we are resetting every style in the react app using &lt;code&gt;*&lt;/code&gt; pseudo selector to configure the whole app the way we want it. Moreover, we are also defining the styles for parent &lt;code&gt;div&lt;/code&gt; container of app with the class name &lt;code&gt;main&lt;/code&gt;. We are also defining the styles for a button which will be used later in &lt;code&gt;DynamicItem&lt;/code&gt; component. &lt;/p&gt;
&lt;p&gt;In this global styles file, we are manually making &lt;strong&gt;two circles&lt;/strong&gt; button instead of using some library. This button toggles whether or not to display the sidebar as a whole. This could done in many ways, this is just one of them.&lt;/p&gt;
&lt;p&gt;Let's create a file which will store the icons which will be used in our react app. &lt;/p&gt;
&lt;p&gt;Head to &lt;code&gt;src&lt;/code&gt; folder and create a new folder inside it under &lt;code&gt;components&lt;/code&gt; name. Inside &lt;code&gt;components&lt;/code&gt; folder, create a new file with &lt;code&gt;Icons.js&lt;/code&gt; name and add the following code to it:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;// src/components/Icons.js
import HomeOutlinedIcon from &quot;@mui/icons-material/HomeOutlined&quot;;
import WebOutlinedIcon from &quot;@mui/icons-material/WebOutlined&quot;;
import CalendarTodayOutlinedIcon from &quot;@mui/icons-material/CalendarTodayOutlined&quot;;
import CalendarMonthOutlinedIcon from &quot;@mui/icons-material/CalendarMonthOutlined&quot;;
import PersonOutlineOutlinedIcon from &quot;@mui/icons-material/PersonOutlineOutlined&quot;;
import SubjectOutlinedIcon from &quot;@mui/icons-material/SubjectOutlined&quot;;
import GppGoodOutlinedIcon from &quot;@mui/icons-material/GppGoodOutlined&quot;;
import AdminPanelSettingsOutlinedIcon from &quot;@mui/icons-material/AdminPanelSettingsOutlined&quot;;
import ListAltOutlinedIcon from &quot;@mui/icons-material/ListAltOutlined&quot;;
import InputOutlinedIcon from &quot;@mui/icons-material/InputOutlined&quot;;

import ArrowRightOutlinedIcon from &quot;@mui/icons-material/ArrowRightOutlined&quot;;
import ArrowBackIcon from &quot;@mui/icons-material/ArrowBack&quot;;

export {
  HomeOutlinedIcon as HomeIcon,
  WebOutlinedIcon as LayoutIcon,
  CalendarMonthOutlinedIcon as CalendarIcon,
  PersonOutlineOutlinedIcon as UserIcon,
  SubjectOutlinedIcon as InvoiceIcon,
  GppGoodOutlinedIcon as RolesIcon,
  CalendarTodayOutlinedIcon as PagesIcon,
  AdminPanelSettingsOutlinedIcon as AuthIcon,
  ListAltOutlinedIcon as WizardIcon,
  InputOutlinedIcon as ModalIcon,
  ArrowBackIcon,
  ArrowRightOutlinedIcon as ArrowIcon,
};
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In this file, we are importing every icon, that we will be using inside our app, and exporting it from a single file. This will help us to import our icons from a single file instead of using multiple lines to import our icons.&lt;/p&gt;
&lt;p&gt;Similarly, we will be creating a new file inside &lt;code&gt;src/components&lt;/code&gt; folder under the name &lt;code&gt;Data.js&lt;/code&gt;. This file will contain our dummy data that we will be using in our app. Open &lt;code&gt;Data.js&lt;/code&gt; file and add the following code to it:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;// src/components/Data.js
import {
  HomeIcon,
  LayoutIcon,
  CalendarIcon,
  InvoiceIcon,
  UserIcon,
  RolesIcon,
  PagesIcon,
  AuthIcon,
  WizardIcon,
  ModalIcon,
} from &quot;./Icons&quot;;

export const SIDEBAR_DATA = [
  {
    id: 1,
    name: &quot;dashboards&quot;,
    path: &quot;dashboards&quot;,
    icon: &amp;lt;HomeIcon /&amp;gt;,
  },
  {
    id: 2,
    name: &quot;layouts&quot;,
    path: &quot;layouts&quot;,
    icon: &amp;lt;LayoutIcon /&amp;gt;,
  },
  {
    id: 3,
    name: &quot;calendar&quot;,
    path: &quot;calendar&quot;,
    icon: &amp;lt;CalendarIcon /&amp;gt;,
  },
  {
    id: 4,
    name: &quot;invoice&quot;,
    path: &quot;invoice&quot;,
    icon: &amp;lt;InvoiceIcon /&amp;gt;,
  },
  {
    id: 5,
    name: &quot;users&quot;,
    path: &quot;users&quot;,
    icon: &amp;lt;UserIcon /&amp;gt;,
  },
  {
    id: 6,
    name: &quot;roles &amp;amp; permissions&quot;,
    path: &quot;roles&quot;,
    icon: &amp;lt;RolesIcon /&amp;gt;,
  },
  {
    id: 7,
    name: &quot;pages&quot;,
    path: &quot;pages&quot;,
    icon: &amp;lt;PagesIcon /&amp;gt;,
  },
  {
    id: 8,
    name: &quot;authentication&quot;,
    path: &quot;authentication&quot;,
    icon: &amp;lt;AuthIcon /&amp;gt;,
  },
  {
    id: 9,
    name: &quot;wizard examples&quot;,
    path: &quot;wizard&quot;,
    icon: &amp;lt;WizardIcon /&amp;gt;,
  },
  {
    id: 10,
    name: &quot;modal examples&quot;,
    path: &quot;modal&quot;,
    icon: &amp;lt;ModalIcon /&amp;gt;,
  },
];
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Note that we are importing the icons from a single file instead of using multiple lines to import. This way, we can also avoid redundancy. &lt;/p&gt;
&lt;p&gt;In this file, we are defining an array of objects each of which contains data for our pages, i.e. an id, name, path, icon. This is the whole data that will be used throughout our app. &lt;em&gt;Feel free to extend it as much as you want.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Another thing we need to do is create a centralized file which will export all our files, just like &lt;code&gt;Icons.js&lt;/code&gt; file. Create &lt;code&gt;index.js&lt;/code&gt; file inside &lt;code&gt;src/components&lt;/code&gt; folder and add the following code to it:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;// src/components/index.js
export { default as Sidebar } from &quot;./Sidebar&quot;;
export { default as SidebarItems } from &quot;./Sidebar/SidebarItems&quot;;
export { default as DynamicItem } from &quot;./Routes/[item]&quot;;

export { SIDEBAR_DATA as dummyData } from &quot;./Data&quot;;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In this file, we are following the similar procedure as that of &lt;code&gt;Icons.js&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Note that the files exported as default need to be imported as &lt;code&gt;default&lt;/code&gt; as well but the ones without &lt;code&gt;default&lt;/code&gt; need to be imported without it.&lt;/p&gt;
&lt;p&gt;Now let's create a file that will render the items of a page. You guessed it right! We will be creating &lt;code&gt;DynamicItem&lt;/code&gt; component. Create a folder inside &lt;code&gt;src&lt;/code&gt; under the name &lt;code&gt;Routes&lt;/code&gt; and inside that folder, create a file with &lt;code&gt;[item].jsx&lt;/code&gt; and add the following code to it:&lt;/p&gt;
&lt;p&gt;&lt;em&gt;If you have worked with NextJS, you know why we are using square brackets. For those who do not know, you can name it anything you want, even without the square brackets.&lt;/em&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;// src/components/Routes/[item].jsx
import { Link } from &quot;react-router-dom&quot;;
import { ArrowBackIcon } from &quot;../Icons&quot;;

const Item = (props) =&amp;gt; {
  const { page } = props;
  if (page === &quot;homepage&quot;) {
    return &amp;lt;div id=&quot;page&quot;&amp;gt;{page}&amp;lt;/div&amp;gt;;
  } else {
    return (
      &amp;lt;div id=&quot;page&quot;&amp;gt;
        &amp;lt;Link to=&quot;/&quot;&amp;gt;
          &amp;lt;button className=&quot;btn&quot;&amp;gt;
            &amp;lt;ArrowBackIcon /&amp;gt; Back to Home
          &amp;lt;/button&amp;gt;
        &amp;lt;/Link&amp;gt;
        {page}
      &amp;lt;/div&amp;gt;
    );
  }
};

export default Item;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We know we have created the routes for the pages that we want. Now we need to make pages that will be rendered. &lt;/p&gt;
&lt;p&gt;Here we are importing &lt;code&gt;Link&lt;/code&gt; component from &lt;code&gt;react-router-dom&lt;/code&gt;, a back icon from &lt;code&gt;Icons.js&lt;/code&gt; file. We know there is no other page / route behind it but the homepage contains other pages / routes. So, if the route is &lt;code&gt;/&lt;/code&gt;, we just need to render the component, else, we also need to render a back button that will take us back to homepage. &lt;/p&gt;
&lt;p&gt;We are using &lt;code&gt;Link&lt;/code&gt; component to navigate back to &lt;code&gt;homepage&lt;/code&gt; as the page already exists. &lt;em&gt;Recall that we created routes inside &lt;code&gt;src/App.js&lt;/code&gt;.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Now comes the main item, the sidebar component. Head to &lt;code&gt;src&lt;/code&gt; folder and create a new folder inside it under the name &lt;code&gt;Sidebar&lt;/code&gt; and create a new file inside it with &lt;code&gt;index.jsx&lt;/code&gt; name. This will be the main file that will consume almost all the files. Add the following code to it:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;// src/components/Sidebar/index.jsx
import React, { useState } from &quot;react&quot;;

import {
  Children,
  SidebarContainer,
  SidebarWrapper,
  SidebarLogoWrapper,
  SidebarLogo,
  SidebarBrand,
  SidebarToggler,
} from &quot;./SidebarStyles&quot;;
import BrandLogo from &quot;./BrandLogo.svg&quot;;

import { SidebarItems } from &quot;..&quot;;

const MOBILE_VIEW = window.innerWidth &amp;lt; 468;

export default function Sidebar({ children }) {
  const [displaySidebar, setDisplaySidebar] = useState(!MOBILE_VIEW);

  const handleSidebarDisplay = (e) =&amp;gt; {
    e.preventDefault();
    if (window.innerWidth &amp;gt; 468) {
      setDisplaySidebar(!displaySidebar);
    } else {
      setDisplaySidebar(false);
    }
  };

  return (
    &amp;lt;React.Fragment&amp;gt;
      &amp;lt;SidebarContainer displaySidebar={displaySidebar}&amp;gt;
        &amp;lt;SidebarWrapper&amp;gt;
          &amp;lt;SidebarLogoWrapper displaySidebar={displaySidebar}&amp;gt;
            {/* Logo wrapper starts */}
            &amp;lt;SidebarLogo href=&quot;#&quot;&amp;gt;
              &amp;lt;span className=&quot;app-brand-logo demo&quot;&amp;gt;
                &amp;lt;img src={BrandLogo} alt=&quot;Brand logo&quot; /&amp;gt;
              &amp;lt;/span&amp;gt;
              &amp;lt;SidebarBrand
                displaySidebar={displaySidebar}
                className=&quot;app__brand__text&quot;
              &amp;gt;
                Frest
              &amp;lt;/SidebarBrand&amp;gt;
            &amp;lt;/SidebarLogo&amp;gt;
            {/* Logo wrapper ends */}
            {/* Toggle button */}
            &amp;lt;SidebarToggler
              displaySidebar={displaySidebar}
              onClick={handleSidebarDisplay}
            &amp;gt;
              &amp;lt;div className=&quot;outer__circle&quot;&amp;gt;
                &amp;lt;div className=&quot;inner__circle&quot; /&amp;gt;
              &amp;lt;/div&amp;gt;
            &amp;lt;/SidebarToggler&amp;gt;
          &amp;lt;/SidebarLogoWrapper&amp;gt;
            {/* Render the SidebarItems component */}
          &amp;lt;SidebarItems displaySidebar={displaySidebar} /&amp;gt;
        &amp;lt;/SidebarWrapper&amp;gt;
      &amp;lt;/SidebarContainer&amp;gt;
            {/* Render the children */}
      &amp;lt;Children displaySidebar={displaySidebar}&amp;gt;{children}&amp;lt;/Children&amp;gt;
    &amp;lt;/React.Fragment&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This is the file where we will be building &lt;code&gt;Sidebar&lt;/code&gt;. We are importing state manager &lt;code&gt;useState&lt;/code&gt; from React to control the view of the sidebar, styles from another file, it will be created within the same directory, a &lt;a rel=&quot;nofollow&quot; href=&quot;https://github.com/JealousGx/snippets/blob/sidebar/src/components/Sidebar/BrandLogo.svg&quot;&gt;Brand Logo&lt;/a&gt; &lt;em&gt;Feel free to use whatever logo you want&lt;/em&gt;, &lt;code&gt;SidebarItems&lt;/code&gt; file that will render our items from the data.&lt;/p&gt;
&lt;p&gt;Another thing we are doing here is creating a &lt;em&gt;global&lt;/em&gt; variable that will store whether the viewpoint is mobile or not. If the viewpoint is mobile, display a portion of the sidebar otherwise, make the sidebar togglable, using &lt;code&gt;useState&lt;/code&gt;. Then we are creating an arrow function that will handle whether or not to display the full sidebar.&lt;/p&gt;
&lt;p&gt;In the end, we are returning a &lt;a rel=&quot;nofollow&quot; href=&quot;https://reactjs.org/docs/fragments.html&quot;&gt;React Fragment&lt;/a&gt; and displaying the brand logo, toggle button, the sidebar items, and the children.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Note that we are creating the styles using &lt;code&gt;styled-components&lt;/code&gt; that can accept parameters and will help us in displaying the sidebar.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Now let's create a file that will apply all the necessary styling to the sidebar. Head to &lt;code&gt;src/components/Sidebar&lt;/code&gt; and create a new file under the name &lt;code&gt;SidebarStyles.js&lt;/code&gt; and add the following code to it:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;// src/components/Sidebar/SidebarStyles.js
import styled from &quot;styled-components&quot;;

// Children Component
export const Children = styled.div`
  width: 100%;
  height: 100%;
  margin-left: ${({ displaySidebar }) =&amp;gt; (displaySidebar ? &quot;15rem&quot; : &quot;5rem&quot;)};
  @media (max-width: 468px) {
    margin-left: 5rem;
  }
`;

export const SidebarWrapper = styled.div`
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  font-size: 0.9rem;
`;

export const SidebarLogoWrapper = styled.div`
  padding: 0.5rem 1rem;
  margin-bottom: 1rem;
  display: flex;
  justify-content: ${({ displaySidebar }) =&amp;gt;
    displaySidebar ? &quot;space-between&quot; : &quot;center&quot;};
  align-items: center;
  @media (max-width: 468px) {
    justify-content: center;
  }
`;

export const SidebarLogo = styled.a`
  display: flex;
  align-items: center;
  justify-content: center;
  @media (max-width: 468px) {
    display: none;
  }
`;

export const SidebarBrand = styled.span`
  display: ${({ displaySidebar }) =&amp;gt; (displaySidebar ? &quot;block&quot; : &quot;none&quot;)};
`;

export const SidebarToggler = styled.button`
  cursor: pointer;
  display: ${({ displaySidebar }) =&amp;gt; (displaySidebar ? &quot;block&quot; : &quot;none&quot;)};
  @media (max-width: 468px) {
    display: block;
  }
`;

// SidebarItem styles
export const ItemsList = styled.ul`
  list-style: none;
`;

export const ItemContainer = styled.li`
  margin-top: 0.5rem;
  width: 100%;
  padding: 0.5rem 0.25rem;
  border-radius: 0.2rem;
  cursor: pointer;
  &amp;amp;:hover {
    background: #eaeced;
  }
  &amp;amp;.active {
    background-color: #dbe4f3;
  }
`;

export const ItemWrapper = styled.div`
  display: flex;
  align-items: center;
  color: #7c7788;
`;

export const ItemName = styled.span`
  margin-left: ${({ displaySidebar }) =&amp;gt; (displaySidebar ? &quot;0.5rem&quot; : &quot;0&quot;)};
  display: ${({ displaySidebar }) =&amp;gt; (displaySidebar ? &quot;block&quot; : &quot;none&quot;)};
  text-transform: capitalize;
`;

// Sidebar Container
export const SidebarContainer = styled.div`
  position: absolute;
  left: 0;
  width: ${({ displaySidebar }) =&amp;gt; (displaySidebar ? &quot;15rem&quot; : &quot;5rem&quot;)};
  height: 100vh;
  padding: 0.75rem;
  background: #f3f4f4;
  transition: width 350ms ease;
  border-right: 1px solid #d4d8dd;
  overflow-x: hidden;
  ${({ displaySidebar }) =&amp;gt;
    displaySidebar &amp;amp;&amp;amp; &quot;box-shadow: 8px 0px 12px 0px rgba(0,0,0,0.1)&quot;};
  ${ItemWrapper} {
    justify-content: ${({ displaySidebar }) =&amp;gt; !displaySidebar &amp;amp;&amp;amp; &quot;center&quot;};
  }
  &amp;amp;:hover {
    ${({ displaySidebar }) =&amp;gt;
      !displaySidebar &amp;amp;&amp;amp; &quot;box-shadow: 8px 0px 12px 0px rgba(0,0,0,0.1)&quot;};
    @media (min-width: 468px) {
      width: ${({ displaySidebar }) =&amp;gt; !displaySidebar &amp;amp;&amp;amp; &quot;15rem&quot;};
      ${SidebarLogoWrapper} {
        justify-content: ${({ displaySidebar }) =&amp;gt;
          !displaySidebar &amp;amp;&amp;amp; &quot;space-between&quot;};
      }
      ${SidebarBrand} {
        display: ${({ displaySidebar }) =&amp;gt; !displaySidebar &amp;amp;&amp;amp; &quot;block&quot;};
      }
      ${SidebarToggler} {
        display: ${({ displaySidebar }) =&amp;gt; !displaySidebar &amp;amp;&amp;amp; &quot;block&quot;};
      }
      ${ItemWrapper} {
        justify-content: ${({ displaySidebar }) =&amp;gt;
          !displaySidebar &amp;amp;&amp;amp; &quot;flex-start&quot;};
      }
      ${ItemName} {
        display: ${({ displaySidebar }) =&amp;gt; !displaySidebar &amp;amp;&amp;amp; &quot;block&quot;};
        margin-left: ${({ displaySidebar }) =&amp;gt; !displaySidebar &amp;amp;&amp;amp; &quot;0.5rem&quot;};
      }
    }
  }
  ::-webkit-scrollbar {
    width: 4px;
    height: 3px;
  }
  ::-webkit-scrollbar-track {
    border-radius: 10px;
    background-color: transparent;
  }
  ::-webkit-scrollbar-thumb {
    border-radius: 10px;
    background: #eaeced;
    &amp;amp;:hover {
      background: #d5e0f3;
    }
  }
  @media (max-width: 468px) {
    width: 5rem;
  }
`;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Here we are making the styles according to the state that we created inside &lt;code&gt;Sidebar.jsx&lt;/code&gt;. Recall that we passed the parameters to these components. We can use those params to display and hide whatever we want. &lt;/p&gt;
&lt;p&gt;&lt;em&gt;Note the hierarchy. In order to control a child component from a parent component, the child component need to be declared before the parent component.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Now then, let's create a file that will render all the items of Sidebar. Inside the same directory, create a new file under the name &lt;code&gt;SidebarItems.jsx&lt;/code&gt; and add the following code to it:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;// src/components/Sidebar/SidebarItems.jsx
import React, { useState } from &quot;react&quot;;
import { Link } from &quot;react-router-dom&quot;;
import {
  ItemsList,
  ItemContainer,
  ItemWrapper,
  ItemName,
} from &quot;./SidebarStyles&quot;;

import { dummyData } from &quot;..&quot;;

const SidebarItems = ({ displaySidebar }) =&amp;gt; {
  const [activeItem, setActiveItem] = useState(0);

  return (
    &amp;lt;ItemsList&amp;gt;
      {dummyData.map((itemData, index) =&amp;gt; (
        &amp;lt;ItemContainer
          key={index}
          onClick={() =&amp;gt; setActiveItem(itemData.id)}
          {/* Adding active class when the user clicks */}
          className={itemData.id === activeItem ? &quot;active&quot; : &quot;&quot;}
        &amp;gt;
          &amp;lt;Link to={itemData.path}&amp;gt;
            &amp;lt;ItemWrapper&amp;gt;
              {itemData.icon}
              &amp;lt;ItemName displaySidebar={displaySidebar}&amp;gt;
                {itemData.name}
              &amp;lt;/ItemName&amp;gt;
            &amp;lt;/ItemWrapper&amp;gt;
          &amp;lt;/Link&amp;gt;
        &amp;lt;/ItemContainer&amp;gt;
      ))}
    &amp;lt;/ItemsList&amp;gt;
  );
};

export default SidebarItems;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In this file, we are using &lt;code&gt;useState&lt;/code&gt; to manage the active item of the sidebar, &lt;code&gt;Link&lt;/code&gt; from React Router to redirect the user to the page, the dummy data from &lt;code&gt;src/components/index.js&lt;/code&gt;, and the styles from &lt;code&gt;src/components/Sidebar/SidebarStyles.js&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Inside the main function, we are creating a list and inside the list, we are mapping through the dummy data and rendering it using the styled components that we have imported. Note that we also created an active pseudo-selector inside &lt;code&gt;SidebarStyles.js&lt;/code&gt;, that will style the item that is active. The active class is added to the item only if the user clicks it.&lt;/p&gt;
&lt;p&gt;Here, we are also using the &lt;code&gt;Link&lt;/code&gt; component of React Router, for each item, to navigate to the item that the user clicks.&lt;/p&gt;
&lt;p&gt;Finally, we have the following output. &lt;em&gt;Play with the code however you wish and see the changes.&lt;/em&gt;&lt;br&gt;
&lt;img src=&quot;https://cdn.hashnode.com/res/hashnode/image/upload/v1661236257044/s8sdKS2zK.png?auto=compress,format&amp;amp;format=webp&quot; alt=&quot;image.png&quot;&gt;&lt;/p&gt;
&lt;p&gt;This is a simple Sidebar component. You can, however, extend it and make it much more amazing however you demand it. &lt;/p&gt;
&lt;h3 id=&quot;resources-&quot;&gt;Resources:&lt;/h3&gt;
&lt;ol&gt;
&lt;li&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;https://jealousgx-sidebar.vercel.app&quot;&gt;Live demo&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;https://github.com/JealousGx/snippets/tree/sidebar&quot;&gt;Source Code&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;https://styled-components.com&quot;&gt;styled-components&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;https://reactrouter.com&quot;&gt;React Router DOM&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Stay tuned with more of my blogs on &lt;a rel=&quot;nofollow&quot; href=&quot;https://jealous.dev&quot;&gt;my site&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;This is my first blog, so, there will be some mistakes in the script, however, the code works perfect. Kindly share tips that can help me make the articles more organized.&lt;/em&gt;&lt;/p&gt;
</description>
<category>Articles</category>
<guid isPermaLink="true">https://coderlegion.com/25230/build-a-responsive-sidebar-with-react-and-styled-components</guid>
<pubDate>Mon, 24 Aug 2026 07:34:57 +0000</pubDate>
</item>
</channel>
</rss>