Hone logo
Hone
Problems

Building a Responsive Sidebar Component in React

This challenge involves creating a reusable and responsive sidebar component for a React application. A well-designed sidebar is crucial for navigation and providing quick access to features, and making it responsive ensures a seamless user experience across various devices. You will implement the logic and styling to handle its appearance and behavior on different screen sizes.

Problem Description

Your task is to develop a Sidebar React component in TypeScript that dynamically adjusts its presentation based on the screen width.

Key Requirements:

  1. Basic Structure: The sidebar should accept an array of navigation items. Each item should have at least a label (string) and a path (string).
  2. Desktop View (Large Screens): On larger screens (e.g., desktop), the sidebar should be permanently visible and occupy a fixed width (e.g., 250px). It should display all provided navigation items.
  3. Mobile View (Small Screens): On smaller screens (e.g., mobile), the sidebar should be hidden by default and accessible via a "hamburger" menu icon. Clicking the icon should reveal the sidebar, typically as an overlay or sliding from the side. Clicking outside the sidebar or on a navigation item should close it.
  4. Responsiveness: The transition between desktop and mobile views should be handled smoothly using CSS media queries.
  5. State Management: You will need to manage the open/closed state of the sidebar on mobile devices.
  6. Reusability: The Sidebar component should be designed to be easily integrated into different parts of an application with different sets of navigation links.

Expected Behavior:

  • When the screen is large enough, the sidebar is always visible.
  • When the screen is small, a toggle button (e.g., hamburger icon) appears.
  • Clicking the toggle button on small screens opens the sidebar.
  • Clicking outside the open sidebar on small screens closes it.
  • Clicking a navigation link on small screens should typically close the sidebar and navigate to the specified path.

Edge Cases to Consider:

  • What happens if no navigation items are provided?
  • How does the sidebar behave if the screen is resized dynamically?
  • Ensure accessibility considerations (e.g., keyboard navigation, ARIA attributes) are kept in mind, though full implementation may be outside the scope of this core challenge.

Examples

Example 1: Desktop View (Conceptual)

  • Input:

    • navigationItems = [{ label: 'Dashboard', path: '/dashboard' }, { label: 'Settings', path: '/settings' }]
    • screenWidth >= 768px (hypothetical breakpoint)
  • Output: A sidebar component occupying a fixed width on the left side of the screen, displaying "Dashboard" and "Settings" as clickable links. The sidebar is permanently visible.

Example 2: Mobile View (Conceptual - Closed)

  • Input:

    • navigationItems = [{ label: 'Dashboard', path: '/dashboard' }, { label: 'Settings', path: '/settings' }]
    • screenWidth < 768px
  • Output: No sidebar is visible by default. A hamburger menu icon is displayed.

Example 3: Mobile View (Conceptual - Open)

  • Input:

    • navigationItems = [{ label: 'Dashboard', path: '/dashboard' }, { label: 'Settings', path: '/settings' }]
    • screenWidth < 768px
    • User clicks the hamburger menu icon.
  • Output: A sidebar appears (e.g., slides in from the left or overlays the content), displaying "Dashboard" and "Settings" as clickable links. Clicking outside this sidebar or on a link closes it.

Constraints

  • The component must be implemented in TypeScript.
  • Styling should be handled using CSS (e.g., CSS Modules, Styled Components, or inline styles). Focus on the logic and responsiveness.
  • The core functionality should work without relying on complex state management libraries (e.g., Redux, Zustand) for managing the sidebar's open/closed state on mobile.
  • Avoid using third-party UI component libraries (e.g., Material UI, Ant Design) for the sidebar itself. You can use them for routing if you prefer, but the sidebar structure and logic should be your own.

Notes

  • Consider using React's useState hook for managing the mobile sidebar's visibility.
  • Media queries in CSS will be essential for handling the responsive behavior.
  • Think about how you'll handle closing the sidebar when a navigation item is clicked on mobile. This might involve passing a callback function down to the Sidebar component.
  • For routing, you might consider using react-router-dom, but the primary focus is on building the Sidebar component itself. You can simulate navigation by calling a prop function if you don't want to set up a full routing system.
Loading editor...
typescript