Hone logo
Hone
Problems

Responsive Sidebar Component in React with TypeScript

This challenge focuses on building a reusable and responsive sidebar component in React using TypeScript. A responsive sidebar is a common UI element used in many applications to provide navigation, settings, or other contextual information, adapting seamlessly to different screen sizes. Successfully completing this challenge will demonstrate your understanding of React component creation, state management, responsive design principles, and TypeScript.

Problem Description

You are tasked with creating a Sidebar component in React that is responsive and can be toggled open and closed. The sidebar should initially be closed on smaller screens (e.g., mobile) and open on larger screens (e.g., desktop). The component should include a toggle button to manually open and close the sidebar on all screen sizes.

Key Requirements:

  • Responsive Behavior: The sidebar should be hidden (collapsed) by default on screens smaller than a specified breakpoint (e.g., 768px) and visible on screens larger than that breakpoint.
  • Toggle Functionality: A button should be present to manually toggle the sidebar's visibility, regardless of screen size.
  • TypeScript: The component must be written in TypeScript, including proper type annotations for props and state.
  • Styling: Use CSS (or a CSS-in-JS library like styled-components - though plain CSS is preferred for this challenge) to style the sidebar and toggle button. The styling should ensure the sidebar is visually appealing and responsive.
  • Content: The sidebar should contain a list of navigation links (at least 3). These links don't need to be functional, just visually present.

Expected Behavior:

  • On screens smaller than 768px, the sidebar should be initially closed (hidden). Clicking the toggle button should open the sidebar. Clicking it again should close it.
  • On screens larger than 768px, the sidebar should be initially open. Clicking the toggle button should close the sidebar. Clicking it again should open it.
  • The toggle button should be clearly visible and easily accessible.
  • The sidebar should smoothly transition between open and closed states.

Edge Cases to Consider:

  • What happens if the breakpoint is changed? The component should adapt accordingly.
  • Consider accessibility – ensure the toggle button is usable with keyboard navigation.

Examples

Example 1:

Input: Screen width < 768px, Sidebar initially closed. User clicks the toggle button.
Output: Sidebar opens, displaying navigation links.
Explanation: The toggle button triggers a state change, causing the sidebar to become visible.

Example 2:

Input: Screen width > 768px, Sidebar initially open. User clicks the toggle button.
Output: Sidebar closes, hiding the navigation links.
Explanation: The toggle button triggers a state change, causing the sidebar to become hidden.

Example 3: (Edge Case)

Input: Screen width = 768px.  Sidebar initially closed. User clicks the toggle button.
Output: Sidebar opens, displaying navigation links.
Explanation: The component should handle the breakpoint precisely, allowing the toggle button to override the default behavior.

Constraints

  • Breakpoint: The breakpoint for responsiveness is 768px.
  • Navigation Links: The sidebar must contain at least 3 navigation links.
  • Styling: Use standard CSS for styling. External CSS files are acceptable.
  • Performance: The component should render efficiently and avoid unnecessary re-renders.
  • Dependencies: You are allowed to use React and TypeScript. No other external libraries are required for this challenge.

Notes

  • Consider using React's useState hook to manage the sidebar's visibility state.
  • Use CSS media queries to control the sidebar's visibility based on screen size.
  • Think about how to make the toggle button visually indicate the sidebar's current state (open or closed).
  • Focus on creating a clean, well-structured, and reusable component. Good code readability is important.
  • The navigation links themselves do not need to be functional; they can be simple <li> elements with text.
Loading editor...
typescript