Frontend School

Interactive Star Rating Component

Machine Coding
medium

Interactive Star Rating Component

Medium
Star Rating
UI Components
React
TypeScript
Accessibility

Overview

In modern web applications, user feedback is paramount. A ubiquitous way for users to express their satisfaction with products or services is through star ratings. This problem challenges you to build a robust, reusable, and accessible star rating component that accurately reflects user input and provides a smooth interactive experience.

What you need to do / Task

Your primary task is to create a React component called StarRating that allows users to select a rating using a series of stars.

  1. Render Stars: Display a configurable number of stars (e.g., 5, 7, or 10) based on a maxRating prop. Each star should visually represent its state (filled or empty).
  2. Initial Rating: The component should accept an initialRating prop to pre-select a rating when it first renders. This rating should be reflected visually.
  3. Interactive Selection:
    • Hover Effect: When a user hovers over a star, all stars up to and including the hovered star should visually indicate a "hovered" state (e.g., filled). This is a preview of the rating.
    • Click to Select: Clicking a star should set the permanent rating to the value corresponding to that star. The visual state should update to reflect this selected rating.
    • Mouse Leave: When the mouse leaves the component, the stars should revert to displaying the currently selected rating (if any).
  4. Read-Only Mode: Implement a readOnly prop. When true, the component should display the initialRating but not allow any user interaction (hover or click). The cursor should also reflect this non-interactive state.
  5. Callback Function: Provide an onChange prop (a function) that is called with the new rating value whenever a user successfully selects a rating.
  6. Accessibility: Ensure the component is usable and understandable by users relying on assistive technologies. This includes keyboard navigation (e.g., using arrow keys to adjust the rating) and appropriate ARIA attributes.

UI/UX expectations

  • Each star should be represented by a clear visual element (e.g., an SVG star icon or a Unicode character like '⭐').
  • Filled stars should have a distinct color (e.g., gold/yellow). Empty stars should have a muted color (e.g., grey). Hovered stars should also have a distinct visual cue.
  • The component should clearly display the currently selected rating.
  • The cursor should change to a pointer when hovering over interactive stars, and to default in readOnly mode.
  • Keyboard users should be able to navigate to the component and adjust the rating using arrow keys (e.g., left/right to decrease/increase rating). The component should have a visible focus indicator.

Acceptance criteria

  • The StarRating component renders correctly with maxRating and initialRating props.
  • Hovering over a star correctly fills all stars up to the hovered position, and reverts on mouse leave.
  • Clicking a star sets the rating permanently, and the visual state updates accordingly.
  • When readOnly is true, no interactive effects (hover, click) are possible, and the cursor is default.
  • The onChange callback is invoked with the correct rating value upon selection.
  • The component is navigable via keyboard, and rating can be adjusted using arrow keys (left/right).
  • Appropriate ARIA attributes (e.g., role="slider", aria-valuenow, aria-valuemax, aria-label) are used for screen reader compatibility.

Constraints

Constraints & assumptions

  • You should use React (functional components and hooks) and TypeScript.
  • No external UI component libraries (e.g., Material-UI, Ant Design) are allowed; build the component from scratch.
  • The maxRating prop will be an integer between 1 and 10.
  • The initialRating prop will be an integer between 0 and maxRating.
  • Focus on client-side rendering; no server-side concerns.

Out of scope

  • Fetching or persisting ratings from an API.
  • Complex animations or transitions beyond basic CSS hover effects.
  • Support for half-star ratings.
  • Form submission integration beyond the onChange callback.

What interviewers evaluate

  • Component Design & Reusability: How well the StarRating component is designed, its API (props), and its ability to be reused in different contexts.
  • State Management & Event Handling: Effective use of React state and event handlers to manage user interactions (hover, click) and component state.
  • UI/UX Implementation: Accuracy of visual feedback (filled/empty/hovered stars) and responsiveness to user input.
  • Accessibility: Implementation of keyboard navigation and ARIA attributes for an inclusive user experience.
  • Code Quality & TypeScript Usage: Readability, maintainability, error handling, and correct application of TypeScript for type safety.

Loading editor...