Skip to content
Home » All Blogs » Intractive Sidebar

Intractive Sidebar

sidebar image

Sidebar interaction refers to the user’s ability to engage with the content and features available on a website’s sidebar. This section is typically located to the left or right of the main content and provides quick access to navigation menus, search bars, and other useful tools.

When designing a sidebar, it’s essential to consider user experience and ensure that the layout is intuitive and easy to use. This may involve organizing content into logical categories, using clear labeling and icons, and minimizing clutter.

To optimize sidebar interaction, designers may also incorporate interactive elements such as collapsible menus, hover effects, and scrollable sections. These features help users navigate the website more efficiently and find the information they need quickly.

Overall, a well-designed sidebar can enhance the usability and functionality of a website, providing users with a seamless browsing experience

Demo

I would recommend you don’t just copy and paste the code, just look at the code and type by understanding it.

HTML Code

<!doctype html>
<html lang="en">

<head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!-- CSS -->
    <link rel="stylesheet" href="style.css">

    <title>Sidebar - Anoncodes</title>
</head>

<body>
     <div id="root"></div>
     <script src="script.js"></script>
</body>

</html>

CSS Code

Create a file style.css and paste the code below.

@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@400;500&display=swap');

* {
  box-sizing: border-box;
}

body {
  --main-bg: #f5f4fd;
  --item-hover: #f9f8fb;
  --secondary-bg: #251d33;
  --main-container-bg: #fff;
  --container-shadow: rgba(100, 100, 111, 0.2) 0px 7px 29px 0px;
  --text-color: #00071d;
}

body.dark {
  --main-bg: #00071d;
  --item-hover: #1c2339;
  --secondary-bg: #36415e;
  --main-container-bg: #131c31;
  --text-color: #fff;
  --tooltip-bg: #251d33;
  --container-shadow: rgba( #131c31, 0.2) 0px 7px 29px 0px;
}

html, body {
  width: 100%;
  margin: 0;
  height: 100%;
  font-family: 'Poppins', sans-serif;
}

body {
  background-color: var(--main-bg);
  transition: .3s ease-in-out;
  display: flex;
  justify-content: center;
  align-items: center;
}

.sidebar {
  &-wrapper {
    display: flex;
    flex-direction: column;
    height: 100%;
    
    @media screen and (max-height: 520px) {
       overflow-y: auto;
       overflow-x: hidden;
    }
  }
  &-container {
    background-color: var(--main-container-bg);
    box-shadow: var(--container-shadow);
    border-radius: 10px;
    padding: 16px;
    transition: width .3s ease-in-out;
    position: relative;
    width: 240px;
    height: 100vh;
    max-height: 600px;
  
    &.shrink {
      width: 92px;
    }
  }
  
  &-themeInput {
    opacity: 0;
    width: 0;
    height: 0;
    position: absolute;
  }
  
  &-themeInputText {
    font-size: 14px;
    opacity: 1;
    transform: translatex(0);
    transition: .2s ease-in-out;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    .shrink & {
      width: 0;
      opacity: 0;
      transform: translatex(-8px);
    }
  }
  
  &-themeType {
    width: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 12px 8px;
    text-align: center;
    z-index: 1;
    flex-shrink: 0;
    line-height: 20px;
    
    .shrink & {
      width: 100%;
    }
  }
  
  &-themeContainer {
    padding: 4px;
    border-radius: 4px;
    color: var(--text-color);
    overflow: hidden;
    background-color: var(--main-bg);
    overflow: hidden;
    flex-shrink: 0;
  }
  
  &-themeLabel {
    display: flex;
    justify-content: space-between;
    position: relative;
    cursor: pointer;
    
    svg { width: 18px; height: 18px; }
    &.switched {
      .shrink & .light { display: none; }
      &:before {
        left: 50%;
        .shrink & { left: 2px; }
      }
    }

    &:not(.switched) .shrink & .dark { display: none; }
    &:before {
      content: "";
      position: absolute;
      z-index: 0;
      display: inline-block;
      width: calc(50% - 2px);
      height: calc(100% - 4px);
      border-radius: 4px;
      background-color: var(--main-container-bg);
      left: 2px;
      top: 50%;
      transform: translatey(-50%);
      transition: left .3s ease;
      
      .shrink & { width: calc(100% - 4px); }
    }
  }
  
  &-viewButton {
    position: absolute;
    width: 20px;
    height: 20px;
    border-radius: 50%;
    border: none;
    right: -10px;
    top: 32px;
    display: flex;
    align-items: center;
    padding: 0;
    cursor: pointer;
    transition: .3s ease-in-out;
    color: #fff;
    box-shadow: var(--container-shadow);
    background-color: var(--secondary-bg);
    display: flex;
    align-items: center;
    justify-content: center;
    
    &:hover { opacity: 0.8; }
    svg { width: 16px; }
    .shrink & { transform: rotatey(-180deg); }
  }
  
  &-list {
    list-style: none;
    padding: 0;
    margin: 12px 0 0;
  }
  
  &-listItem {
    color: var(--text-color);
    display: flex;
    align-items: center;
    animation: fadeInItem .6s forwards;
    transition: background .2s ease-in;
    transform: translatex(-16px);
    opacity: 0;
    padding: 8px 0;
    position: relative;
 
    .shrink & span { visibility: hidden; }
    &.active a { background-color: var(--main-bg); }
    &:not(.active) a:hover {
      background-color: var(--item-hover);
    }
    &:hover .sidebar-listItemText {
      display: inline-block;
      opacity: 1;
      left: calc(100% + 8px);
      visibility: visible;
    }
    + li {
      border-top: 1px solid var(--item-hover);
    } 
    a {
      width: 100%;
      padding: 20px;
      border-radius: 4px;
      display: flex;
      align-items: center;
      cursor: pointer;
    }
  }
  
  &-listIcon {
    width: 20px;
    height: 20px;
    display: inline-block;
    margin-right: 8px;
    flex-shrink: 0;
    
    .shrink & { margin-right: 0; }
  }
  
  &-profileSection {
    display: flex;
    align-items: center;
    margin-top: auto;
    border: 1px solid var(--item-hover);
    padding: 8px 10px;
    border-radius: 28px;
    overflow: hidden;
    height: 60px;
    flex-shrink: 0;
    transition: background .3s ease-in-out;
    cursor: pointer;
    
    &:hover { background-color: var(--item-hover); }
    
    img {
      width: 40px;
      height: 40px;
      border-radius: 50%;
      object-fit: cover;
      margin-right: 8px;
      flex-shrink: 0;
    }
    
    span {
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
      color: var(--text-color);
    }
    
    .shrink & span { display: none; }
    .shrink & { border-radius: 50%; }
  }
  
  &-listItemText {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    line-height: 20px;
  }
}

.shrink .sidebar-listItemText {
  position: absolute;
  padding: 8px;
  left: 100%;
  opacity: 0;
  background-color: var(--secondary-bg);
  color: #fff;
  font-size: 12px;
  border-radius: 4px;
  transition: left .3s ease-in-out, opacity .3s ease-in-out;
}

@for $i from 0 through 5 {
  .sidebar-listItem:nth-child(n + #{$i}) {
    animation-delay: $i * .2s;
  }
}

@keyframes fadeInItem {
  100% {
    transform: translatex(0px);
    opacity: 1;
  }
}

Javascript Code

Create a file style.css and paste the code below.

const { useState } = React;

const Sidebar = () => {
  const [isShrinkView, setIsShrinkView] = React.useState(false);
  const [isDarkMode, setIsDarkMode] = React.useState(false);

  const handleSidebarView = () => {
    setIsShrinkView(!isShrinkView);
  };

  const handleThemeChange = () => {
    setIsDarkMode(!isDarkMode);
    document.body.classList.toggle("dark");
  };

  return (
    <div className={`sidebar-container${isShrinkView ? " shrink" : ""}`}>
      <button
        className="sidebar-viewButton"
        type="button"
        aria-label={isShrinkView ? "Expand Sidebar" : "Shrink Sidebar"}
        title={isShrinkView ? "Expand" : "Shrink"}
        onClick={handleSidebarView}
      >
        <svg
          xmlns="http://www.w3.org/2000/svg"
          width="24"
          height="24"
          viewBox="0 0 24 24"
          fill="none"
          stroke="currentColor"
          stroke-width="2"
          stroke-linecap="round"
          stroke-linejoin="round"
          class="feather feather-chevron-left"
        >
          <polyline points="15 18 9 12 15 6" />
        </svg>
      </button>
      <div className="sidebar-wrapper">
        <div className="sidebar-themeContainer">
          <label
            labelFor="theme-toggle"
            className={`sidebar-themeLabel${isDarkMode ? " switched" : ""}`}
          >
            <input
              className="sidebar-themeInput"
              type="checkbox"
              id="theme-toggle"
              onChange={handleThemeChange}
            />
            <div className="sidebar-themeType light">
              <svg
                xmlns="http://www.w3.org/2000/svg"
                width="24"
                height="24"
                viewBox="0 0 24 24"
                fill="none"
                stroke="currentColor"
                stroke-width="2"
                stroke-linecap="round"
                stroke-linejoin="round"
                className="sidebar-listIcon"
              >
                <circle cx="12" cy="12" r="5" />
                <path d="M12 1v2M12 21v2M4.22 4.22l1.42 1.42M18.36 18.36l1.42 1.42M1 12h2M21 12h2M4.22 19.78l1.42-1.42M18.36 5.64l1.42-1.42" />
              </svg>
              <span className="sidebar-themeInputText">Light</span>
            </div>
            <div className="sidebar-themeType dark">
              <svg
                xmlns="http://www.w3.org/2000/svg"
                width="24"
                height="24"
                viewBox="0 0 24 24"
                fill="none"
                stroke="currentColor"
                stroke-width="2"
                stroke-linecap="round"
                stroke-linejoin="round"
                className="sidebar-listIcon"
              >
                <path d="M21 12.79A9 9 0 1 1 11.21 3 7 7 0 0 0 21 12.79z" />
              </svg>
              <span className="sidebar-themeInputText">Dark</span>
            </div>
          </label>
        </div>
        <ul className="sidebar-list">
          <li className="sidebar-listItem active">
            <a>
              <svg
                xmlns="http://www.w3.org/2000/svg"
                fill="none"
                stroke="currentColor"
                viewBox="0 0 24 24"
                stroke-width="2"
                stroke-linecap="round"
                stroke-linejoin="round"
                className="sidebar-listIcon"
              >
                <rect x="3" y="3" rx="2" ry="2" className="sidebar-listIcon" />
                <path d="M3 9h18M9 21V9" />
              </svg>
              <span className="sidebar-listItemText">Dashboard</span>
            </a>
          </li>
          <li className="sidebar-listItem">
            <a>
              <svg
                xmlns="http://www.w3.org/2000/svg"
                viewBox="0 0 24 24"
                fill="none"
                stroke="currentColor"
                stroke-width="2"
                stroke-linecap="round"
                stroke-linejoin="round"
                className="sidebar-listIcon"
              >
                <polyline points="22 12 16 12 14 15 10 15 8 12 2 12" />
                <path d="M5.45 5.11L2 12v6a2 2 0 0 0 2 2h16a2 2 0 0 0 2-2v-6l-3.45-6.89A2 2 0 0 0 16.76 4H7.24a2 2 0 0 0-1.79 1.11z" />
              </svg>
              <span className="sidebar-listItemText">Inbox</span>
            </a>
          </li>
          <li className="sidebar-listItem">
            <a>
              <svg
                xmlns="http://www.w3.org/2000/svg"
                viewBox="0 0 24 24"
                fill="none"
                stroke="currentColor"
                stroke-width="2"
                stroke-linecap="round"
                stroke-linejoin="round"
                className="sidebar-listIcon"
              >
                <rect x="3" y="4" width="18" height="18" rx="2" ry="2" />
                <line x1="16" y1="2" x2="16" y2="6" />
                <line x1="8" y1="2" x2="8" y2="6" />
                <line x1="3" y1="10" x2="21" y2="10" />
              </svg>
              <span className="sidebar-listItemText">Calendar</span>
            </a>
          </li>
          <li className="sidebar-listItem">
            <a>
              <svg
                xmlns="http://www.w3.org/2000/svg"
                viewBox="0 0 24 24"
                fill="none"
                stroke="currentColor"
                stroke-width="2"
                stroke-linecap="round"
                stroke-linejoin="round"
                className="sidebar-listIcon"
              >
                <polyline points="22 12 18 12 15 21 9 3 6 12 2 12" />
              </svg>
              <span className="sidebar-listItemText">Activity</span>
            </a>
          </li>
          <li className="sidebar-listItem">
            <a>
              <svg
                xmlns="http://www.w3.org/2000/svg"
                viewBox="0 0 24 24"
                fill="none"
                stroke="currentColor"
                stroke-width="2"
                stroke-linecap="round"
                stroke-linejoin="round"
                className="sidebar-listIcon"
              >
                <circle cx="12" cy="12" r="3" />
                <path d="M19.4 15a1.65 1.65 0 00.33 1.82l.06.06a2 2 0 010 2.83 2 2 0 01-2.83 0l-.06-.06a1.65 1.65 0 00-1.82-.33 1.65 1.65 0 00-1 1.51V21a2 2 0 01-2 2 2 2 0 01-2-2v-.09A1.65 1.65 0 009 19.4a1.65 1.65 0 00-1.82.33l-.06.06a2 2 0 01-2.83 0 2 2 0 010-2.83l.06-.06a1.65 1.65 0 00.33-1.82 1.65 1.65 0 00-1.51-1H3a2 2 0 01-2-2 2 2 0 012-2h.09A1.65 1.65 0 004.6 9a1.65 1.65 0 00-.33-1.82l-.06-.06a2 2 0 010-2.83 2 2 0 012.83 0l.06.06a1.65 1.65 0 001.82.33H9a1.65 1.65 0 001-1.51V3a2 2 0 012-2 2 2 0 012 2v.09a1.65 1.65 0 001 1.51 1.65 1.65 0 001.82-.33l.06-.06a2 2 0 012.83 0 2 2 0 010 2.83l-.06.06a1.65 1.65 0 00-.33 1.82V9a1.65 1.65 0 001.51 1H21a2 2 0 012 2 2 2 0 01-2 2h-.09a1.65 1.65 0 00-1.51 1z" />
              </svg>
              <span className="sidebar-listItemText">Settings</span>
            </a>
          </li>
        </ul>
        <div className="sidebar-profileSection">
          <img
            src="https://assets.codepen.io/3306515/i-know.jpg"
            width="40"
            height="40"
            alt="Monica Geller"
          />
          <span>Monica Geller</span>
        </div>
      </div>
    </div>
  );
};

ReactDOM.render(<Sidebar />, document.getElementById("root"));

Written By : @anoncodes 👀

Code Credit : @aybukeceylan

If you found any mistakes or have any doubts please feel free to Contact Us.🙏💖

Hope you find this post helpful 💖


Related Post

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *

www.000webhost.com