Frontend School

Interactive Star Rating Component

Machine Coding
medium

Interactive Star Rating Component

Medium
React
Frontend
UI Components
Accessibility
State Management
SVG

Overview

At Nykaa, product reviews and ratings are central to our user experience, helping customers make informed purchasing decisions and boosting product discoverability. A highly interactive and visually appealing star rating component is crucial for empowering users to easily provide feedback and for displaying product sentiment effectively.

What you need to do / Task

Your task is to build a reusable StarRating React component that supports displaying and interacting with star ratings, including half-star precision.

  1. Display Rating: The component should accept a rating prop (a number, e.g., 3.7) and visually represent it using a set of stars.
  2. Interactive Selection: When not in read-only mode, users should be able to:
    • Hover: Preview a rating by hovering their mouse over the stars. This should highlight stars up to the hovered point, supporting half-star increments.
    • Click: Select a rating by clicking on a star or a half-star area. This action should trigger an onChange callback with the newly selected rating.
  3. Half-Star Support: Both display and interactive selection should accurately handle half-star values (e.g., 3.5 stars).
  4. Read-Only Mode: The component should accept a readOnly boolean prop. When true, it should display the rating without any interactive capabilities.
  5. Accessibility: Ensure basic keyboard navigation and ARIA attributes for screen readers are implemented.

UI/UX expectations

  • The component should display a fixed set of 5 stars.
  • Stars should visually fill based on the rating prop. A half-filled star should be clearly distinguishable.
  • On hover in interactive mode, stars should visually highlight to indicate the potential selection.
  • Clicking a star or a half-star area should visually confirm the selection.
  • Basic styling should be applied to make the stars visually appealing (e.g., using SVG icons or a unicode character).
  • Keyboard navigation: Users should be able to use left/right arrow keys to increment/decrement the rating by 0.5, and Enter/Space to confirm the selection (in interactive mode).

Acceptance criteria

  • The StarRating component renders correctly, displaying 5 stars, with the fill accurately representing the rating prop (e.g., 0, 2.5, 4.7, 5).
  • In interactive mode (readOnly={false}), hovering over different parts of the stars correctly updates the visual preview for potential selection, including half-star increments.
  • Clicking on a star or a half-star region in interactive mode updates the component's internal state and invokes the onChange callback with the new rating.
  • When readOnly={true}, the component displays the rating prop without any hover effects, click interactions, or keyboard navigation.
  • The component supports displaying and selecting ratings with half-star precision (e.g., 2.5, 3.0, 3.5).
  • Keyboard navigation (arrow keys for adjustment, Enter/Space for selection) is functional and aria-label attributes are used for accessibility.

Constraints

Constraints & assumptions

  • The maximum rating is fixed at 5 stars.
  • The rating prop can be a floating-point number.
  • The component should be designed as a controlled component, meaning its state (rating) is managed by its parent via props.
  • You should use React function components and hooks.
  • No external UI libraries (e.g., Material UI, Ant Design) are allowed; build the component from scratch.
  • Focus on functional correctness and clean code.

Out of scope

  • Complex animations for star transitions.
  • Persisting ratings to a backend.
  • Advanced accessibility features beyond basic keyboard navigation and ARIA attributes (e.g., full screen reader voice-over testing).
  • Implementing a rating count or average display.

What interviewers evaluate

  • Component Design: How well the component is structured, its reusability, and adherence to React best practices (e.g., controlled component pattern).
  • Logic Implementation: Correctness in handling rating display, interactive hover/click events, and half-star logic.
  • Accessibility: Thoughtful inclusion of keyboard navigation and ARIA attributes for an inclusive user experience.
  • Code Quality: Readability, maintainability, and efficiency of the implemented solution.

Loading editor...