Frontend School

Build an Accessible Star Rating Component

Machine Coding
easy

Build an Accessible Star Rating Component

Easy
React
Frontend
Component
UI
State Management
Accessibility

Overview

Imagine you're contributing to a new product review system for an e-commerce platform. A critical part of user feedback is the ability to rate products easily and intuitively. This problem focuses on building a robust and accessible star rating component that can be integrated across various product pages, allowing users to provide feedback with a simple click.

What you need to do / Task

Your task is to create a reusable StarRating React functional component that meets the following requirements:

  1. Component Structure: Create a StarRating functional component that accepts the following props:
    • totalStars: A number indicating the maximum number of stars (e.g., 5). Defaults to 5.
    • initialRating: A number representing the pre-selected rating. Defaults to 0.
    • onRatingChange: An optional callback function (rating: number) => void that is invoked when a new rating is selected.
  2. Display Stars: Render totalStars visually (e.g., using a star character like ★).
  3. Interactive Selection: Allow users to click on a star to set a permanent rating. When a star is clicked, all stars up to and including the clicked star should remain highlighted.
  4. Hover Preview: Implement a hover effect where moving the mouse over a star highlights all stars up to that star. When the mouse leaves the component, the display should revert to the currently selected permanent rating.

UI/UX expectations

  • Stars should visually indicate their 'filled' state (e.g., change color) when hovered or clicked.
  • On hover, stars up to the hovered star should light up. When the mouse leaves the StarRating component, it should revert to showing the currently selected permanent rating.
  • On click, the selected rating should persist, and the onRatingChange callback should be fired with the new rating.
  • The component should be keyboard navigable (e.g., using arrow keys to change rating, Enter/Space to select) and screen-reader friendly (e.g., using appropriate ARIA attributes like role and aria-label).

Acceptance criteria

  • The StarRating component renders the correct number of stars based on totalStars prop.
  • Initially, the component displays stars according to initialRating (or 0 if not provided).
  • Hovering over the Nth star highlights stars 1 to N, and moving the mouse out restores the previously selected rating.
  • Clicking the Nth star sets the permanent rating to N, and stars 1 to N remain filled.
  • The onRatingChange callback is triggered with the correct rating when a star is clicked.
  • The component is accessible via keyboard, allowing users to navigate and select ratings without a mouse.
  • Appropriate ARIA attributes are used to convey the component's state and purpose to assistive technologies.

Constraints

Constraints & assumptions

  • Build the component using React (functional components and hooks).
  • Styling should be done using plain CSS in styles.css. No external styling libraries or CSS-in-JS solutions are allowed.
  • The totalStars prop will always be a positive integer, typically between 1 and 10.
  • The component should manage its own internal state for the selected rating and hover preview.
  • No need for data persistence (e.g., saving to local storage or a backend).
  • Assume modern browser environment.

Out of scope

  • Complex animations or transitions beyond basic color changes.
  • Half-star ratings or decimal ratings.
  • Displaying average ratings or fetching ratings from an API.

What interviewers evaluate

  • Correct implementation of component state and lifecycle for managing rating and hover states.
  • Clear separation of concerns and component reusability.
  • Effective handling of user interactions (mouse hover, mouse click, keyboard navigation).
  • Attention to basic accessibility standards (keyboard navigation, ARIA roles, semantic HTML).
  • Clean, readable, and well-structured React code.

Loading editor...