Frontend School

Interactive Star Rating Component

Machine Coding
easy

Interactive Star Rating Component

Easy
React
TypeScript
UI Components
Event Handling
Accessibility

Overview

Pinterest users constantly discover new ideas, products, and content. Providing a clear and intuitive way for users to rate these "Pins" or products is crucial for improving recommendations and helping others discover high-quality content. This problem challenges you to build a reusable and accessible Star Rating component, similar to what you might see on an e-commerce product detail page or a user profile.

What you need to do / Task

Your goal is to create a React component called StarRating that allows users to provide a rating by interacting with a set of stars.

  1. Component Structure: Create a functional React component named StarRating that accepts the following props:
    • maxRating: number (e.g., 5 for a 5-star system).
    • initialRating?: number (optional, the default selected rating).
    • onRatingChange: (rating: number | null) => void (callback fired when the rating changes, null if cleared).
  2. Visual Representation: Display maxRating stars. Each star should visually indicate its state (empty, filled, or hovered).
  3. Hover Interaction:
    • When a user hovers over a star, all stars up to and including the hovered star should visually "fill".
    • When the mouse leaves the component, the stars should revert to displaying the currently selected rating (or all empty if no rating is selected).
  4. Click Interaction:
    • Clicking a star should select that rating. All stars up to and including the clicked star should remain "filled" to represent the selected rating.
    • If a rating is already selected, clicking the same selected star again should clear the rating (set it to null).
    • The onRatingChange callback should be invoked with the new rating (or null).
  5. Accessibility:
    • Ensure the component is navigable and usable via keyboard (e.g., arrow keys to change rating, Enter/Space to select).
    • Use appropriate ARIA attributes to convey the component's purpose and state to assistive technologies.

UI/UX expectations

  • The stars should be visually distinct (e.g., different colors or icons) for empty, hovered, and selected states.
  • A clear visual indication for the currently selected rating should persist.
  • The component should be responsive to both mouse and keyboard interactions.

Acceptance criteria

  • The StarRating component renders maxRating distinct star elements.
  • Hovering over the Nth star highlights stars 1 through N, and un-highlights them when the mouse leaves, reverting to the selected rating or all empty.
  • Clicking the Nth star sets the permanent rating to N, and visually displays N filled stars.
  • Clicking an already selected Nth star clears the rating (0 stars filled).
  • The onRatingChange callback is correctly invoked with the new rating (or null) on selection/clear.
  • The component is fully navigable using keyboard (e.g., arrow keys to change preview, Enter/Space to select/clear).
  • Appropriate ARIA attributes (e.g., role, aria-valuenow, aria-valuemax, aria-label) are used for accessibility.

Constraints

Constraints & assumptions

  • You must use React with TypeScript.
  • Do not use any external UI component libraries (e.g., Material UI, Ant Design). You are building this from scratch.
  • The maxRating prop will always be a positive integer between 1 and 10.
  • Assume a modern browser environment.
  • No backend integration or data persistence beyond the component's internal state is required.

Out of scope

  • Complex animations or transition effects on star changes.
  • Integration with a global state management solution (e.g., Redux, Zustand).
  • Server-side rendering considerations.

What interviewers evaluate

  • Component Design: How well the StarRating component is designed for reusability, clear props, and internal state management.
  • Event Handling: Correct and efficient implementation of mouse (hover, click) and keyboard events.
  • Accessibility: Proper use of ARIA attributes and keyboard navigation for an inclusive user experience.
  • Code Quality: Readability, maintainability, and effective use of TypeScript.

Loading editor...