Code A Program
Responsive Sidebar with React js and tailwind css
Published on August 19, 2025

Responsive Sidebar with React js and tailwind css

Creating an intuitive and visually appealing navigation sidebar is crucial for any modern web application. In this tutorial, I'll walk you through building a fully functional, animated sidebar component using React and Tailwind CSS that your users will love.

What We're Building

Our sidebar component features:

  • Smooth expand/collapse animation with a toggle button

  • Responsive design that adapts to different screen sizes

  • Clean, modern styling using Tailwind CSS

  • Hover effects for better user interaction

  • Icon-based navigation with text labels

  • Visual grouping with spacing between menu sections

Youtube Video:

The Complete Code

Let's start by examining the full component structure:

JSX
import { useState } from "react";

const App = () => {
  const [open, setOpen] = useState(true);
  
  const Menus = [
    { title: "Dashboard", src: "Chart_fill" },
    { title: "Inbox", src: "Chat" },
    { title: "Accounts", src: "User", gap: true },
    { title: "Schedule", src: "Calendar" },
    { title: "Search", src: "Search" },
    { title: "Analytics", src: "Chart" },
    { title: "Files", src: "Folder", gap: true },
    { title: "Setting", src: "Setting" },
  ];

  return (
    <div className="flex">
      {/* Sidebar */}
      <div className={`${open ? "w-72" : "w-20"} bg-dark-purple h-screen p-5 pt-8 relative duration-300`}>
        {/* Toggle Button */}
        <img
          src="./src/assets/control.png"
          className={`absolute cursor-pointer -right-3 top-9 w-7 border-dark-purple border-2 rounded-full ${!open && "rotate-180"}`}
          onClick={() => setOpen(!open)}
        />
        
        {/* Logo Section */}
        <div className="flex gap-x-4 items-center">
          <img
            src="./src/assets/logo.png"
            className={`cursor-pointer duration-500 ${open && "rotate-[360deg]"}`}
          />
          <h1 className={`text-white origin-left font-medium text-xl duration-200 ${!open && "scale-0"}`}>
            Designer
          </h1>
        </div>
        
        {/* Navigation Menu */}
        <ul className="pt-6">
          {Menus.map((Menu, index) => (
            <li
              key={index}
              className={`flex rounded-md p-2 cursor-pointer hover:bg-light-white text-gray-300 text-sm items-center gap-x-4 
              ${Menu.gap ? "mt-9" : "mt-2"} ${index === 0 && "bg-light-white"}`}
            >
              <img src={`./src/assets/${Menu.src}.png`} />
              <span className={`${!open && "hidden"} origin-left duration-200`}>
                {Menu.title}
              </span>
            </li>
          ))}
        </ul>
      </div>
      
      {/* Main Content */}
      <div className="h-screen flex-1 p-7">
        <h1 className="text-2xl font-semibold">Home Page</h1>
      </div>
    </div>
  );
};

export default App;

Breaking Down the Key Features

1. State Management with useState Hook

JSX
const [open, setOpen] = useState(true);

We use React's useState hook to manage the sidebar's open/closed state. The sidebar starts in an open state by default.

2. Dynamic Menu Configuration

JSX
const Menus = [
  { title: "Dashboard", src: "Chart_fill" },
  { title: "Inbox", src: "Chat" },
  { title: "Accounts", src: "User", gap: true },
  // ... more menu items
];

The menu items are stored in an array of objects, making it easy to add, remove, or modify navigation items. The gap property allows for visual grouping of related menu items.

3. Responsive Width Animation

JSX
className={`${open ? "w-72" : "w-20"} bg-dark-purple h-screen p-5 pt-8 relative duration-300`}

The sidebar smoothly transitions between two widths:

  • Expanded: 288px (w-72)

  • Collapsed: 80px (w-20)

The duration-300 class provides a smooth 300ms transition animation.

4. Interactive Toggle Button

The toggle button features:

  • Positioned absolutely outside the sidebar (-right-3)

  • Rotates 180 degrees when sidebar is collapsed

  • Smooth click interaction to toggle state

5. Logo Animation

JSX
className={`cursor-pointer duration-500 ${open && "rotate-[360deg]"}`}

When the sidebar expands, the logo performs a full 360-degree rotation over 500ms, adding a delightful touch of personality.

6. Text Label Animations

JSX
className={`${!open && "hidden"} origin-left duration-200`}

Menu labels smoothly scale down from their left origin point when the sidebar collapses, creating a polished animation effect.

Styling with Tailwind CSS

This component leverages several Tailwind CSS features:

  • Custom Colors: Uses bg-dark-purple and bg-light-white (requires custom Tailwind configuration)

  • Flexbox Layout: flex, items-center, gap-x-4 for clean alignment

  • Hover Effects: hover:bg-light-white for interactive feedback

  • Transitions: duration-300, duration-200, duration-500 for smooth animations

  • Transform Utilities: rotate-180, rotate-[360deg], scale-0 for animations

Required Assets

To implement this sidebar, you'll need these image assets in your src/assets/ folder:

  • control.png - Toggle button icon

  • logo.png - Your application logo

  • Chart_fill.png, Chat.png, User.png, etc. - Menu item icons

Customization Tips

1. Adding New Menu Items

JSX
const Menus = [
  // ... existing items
  { title: "Reports", src: "Document", gap: true },
  { title: "Help", src: "Question" },
];

2. Custom Colors

Add these to your tailwind.config.js:

JavaScript
module.exports = {
  theme: {
    extend: {
      colors: {
        'dark-purple': '#081A51',
        'light-white': 'rgba(255,255,255,0.17)',
      }
    }
  }
}

3. Active State Management

You can enhance the component by tracking the active menu item:

JSX
const [activeIndex, setActiveIndex] = useState(0);

// In the menu item className:
${activeIndex === index && "bg-light-white"}

Mobile Responsiveness

For mobile devices, consider adding:

JSX
// Add this to your sidebar className
${open ? "w-72" : "w-20"} md:relative absolute md:h-screen h-full z-50

This makes the sidebar overlay on mobile when open, preventing content overlap.

Accessibility Considerations

To make your sidebar more accessible:

  1. Add ARIA labels:

jsx

JSX
<button aria-label="Toggle sidebar" onClick={() => setOpen(!open)}>
  1. Keyboard navigation:

jsx

JSX
onKeyDown={(e) => e.key === 'Enter' && setOpen(!open)}
  1. Screen reader support:

jsx

JSX
<span className="sr-only">Navigation menu</span>

Performance Optimization

For better performance with larger menu lists:

jsx

JSX
const menuItems = useMemo(() => Menus.map((menu, index) => (
  // menu item JSX
)), [Menus, open]);

Conclusion

This animated sidebar component provides a solid foundation for modern web application navigation. The combination of React hooks and Tailwind CSS creates a smooth, responsive user experience that's both functional and visually appealing.

The key to this component's success lies in its attention to detail—from the smooth animations to the thoughtful spacing and hover effects. These small touches add up to create a professional, polished interface that users will enjoy interacting with.

Next Steps

Consider extending this component with:

  • Sub-menu support for nested navigation

  • Theme switching for dark/light modes

  • Badge notifications for menu items like "Inbox"

  • Search functionality within the sidebar

  • Drag-and-drop reordering of menu items

Ready to implement this in your project? Copy the code above and start customizing it to match your application's design system and requirements!

Share:
Download Source Code

Get the complete source code for this tutorial. Includes all files, components, and documentation to help you follow along.

View on GitHub

📦What's Included:

  • • Complete source code files
  • • All assets and resources used
  • • README with setup instructions
  • • Package.json with dependencies
💡Free to use for personal and commercial projects