Hone logo
Hone
Problems

React Carousel Component with TypeScript

This challenge asks you to build a reusable carousel component in React using TypeScript. Carousels are a common UI element used to display a series of items in a horizontally scrollable fashion, often with navigation controls. Implementing this component will solidify your understanding of React state management, event handling, and component composition.

Problem Description

You are tasked with creating a Carousel component that displays a list of items horizontally. The carousel should have the following features:

  • Display Items: The component should render a list of items passed as a prop. Each item can be any React node (e.g., an image, a text block, a custom component).
  • Navigation: The carousel should include left and right arrow buttons for navigating between items.
  • Automatic Sliding (Optional): The carousel should optionally slide to the next item automatically after a specified interval.
  • Responsive Behavior: The carousel should adapt to different screen sizes. For simplicity, assume a fixed number of items visible at a time (e.g., 3 items on larger screens, 1 item on smaller screens). You don't need to implement complex responsive design, just a basic adjustment.
  • Infinite Loop: When the user reaches the end of the carousel, clicking the right arrow should loop back to the first item, and vice versa for the left arrow.

Key Requirements:

  • The component must be written in TypeScript.
  • Use React state to manage the current index of the displayed item.
  • Handle click events on the navigation buttons to update the current index.
  • Implement the infinite loop functionality.
  • Provide a mechanism to control the automatic sliding behavior (e.g., a prop to enable/disable it and a prop to set the interval).
  • The component should be reusable and accept a variety of item types.

Expected Behavior:

  • Clicking the left arrow should move to the previous item (wrapping around to the last item if at the beginning).
  • Clicking the right arrow should move to the next item (wrapping around to the first item if at the end).
  • If automatic sliding is enabled, the carousel should automatically transition to the next item at the specified interval.
  • The carousel should visually indicate the current item being displayed.

Edge Cases to Consider:

  • Empty item list: The carousel should gracefully handle an empty list of items (e.g., display a message or render nothing).
  • Single item: The carousel should still function correctly with only one item.
  • Very large number of items: Consider potential performance implications if the item list is extremely large. (Optimization is not required for this challenge, but awareness is good).

Examples

Example 1:

Input: items = [<div>Item 1</div>, <div>Item 2</div>, <div>Item 3</div>] , visibleItems = 1, autoSlide = false
Output: A carousel displaying one item at a time, with left and right arrow buttons. Clicking the arrows navigates between the items.
Explanation: The carousel renders the items horizontally, showing only one at a time.  The navigation buttons allow the user to cycle through the items.

Example 2:

Input: items = [<div>Image 1</div>, <div>Image 2</div>, <div>Image 3</div>, <div>Image 4</div>], visibleItems = 2, autoSlide = true, slideInterval = 3000
Output: A carousel displaying two items at a time, automatically sliding to the next item every 3 seconds, with left and right arrow buttons.
Explanation: The carousel renders two items side-by-side.  It automatically transitions to the next pair of items every 3 seconds.  The user can also manually navigate using the arrow buttons.

Example 3: (Edge Case)

Input: items = [], visibleItems = 3, autoSlide = true
Output: The carousel renders nothing or displays a message like "No items to display."
Explanation: The carousel handles the case where the item list is empty gracefully.

Constraints

  • The component should be implemented as a functional component using React Hooks.
  • The items prop should be an array of React nodes.
  • visibleItems prop should be an integer representing the number of items visible at a time (default: 1).
  • autoSlide prop should be a boolean indicating whether to enable automatic sliding (default: false).
  • slideInterval prop should be a number representing the interval in milliseconds for automatic sliding (default: 5000).
  • The carousel should be reasonably performant for lists of up to 100 items.

Notes

  • Consider using CSS for styling the carousel and its components. You can use inline styles, CSS modules, or a CSS-in-JS library.
  • Focus on the core functionality of the carousel. Advanced features like drag-and-drop or custom transitions are not required.
  • Think about how to make your component reusable and configurable.
  • Pay attention to accessibility considerations (e.g., providing appropriate ARIA attributes for the navigation buttons).
  • The visibleItems prop is a simplification. A more robust solution would dynamically calculate the number of visible items based on screen size. For this challenge, a fixed value is sufficient.
Loading editor...
typescript