Frontend School

Configurable Accessible Star Rating Component

Machine Coding
hard

Configurable Accessible Star Rating Component

Hard
React
Component Design
Accessibility
Event Handling
State Management
UI

Overview

Many modern applications rely on user feedback to drive product improvements and enhance user experience. A common way to gather such feedback is through star ratings, allowing users to quickly express their sentiment or evaluate a product/service. This problem challenges you to build a robust, accessible, and highly configurable Star Rating component that can be used across various parts of a web application, from product review forms to service feedback modules.

What you need to do / Task

Your primary goal is to implement a reusable StarRating React component with the following features:

  1. Display Stars: Render a configurable number of stars (e.g., 5 stars by default).
  2. Initial Rating: The component should accept an initialRating prop to pre-select a rating.
  3. Interactive Selection:
    • Hover: When a user hovers over the stars, highlight the stars up to the hovered position, indicating a preview of the rating.
    • Click: Clicking a star should select that rating. The component should visually represent the selected rating persistently.
    • Half-Star Support: The component must allow for selecting and displaying half-star ratings (e.g., 3.5 stars). Hovering should precisely highlight full or half stars based on mouse position.
  4. Clear Rating: Clicking an already selected full star should clear the rating (set it to 0). If a half-star is selected (e.g., 3.5), clicking it should round down to the full star (3), and a subsequent click on that full star should clear it (0).
  5. Callback: When the rating changes (due to click or keyboard interaction), an onChange callback prop should be invoked with the new rating value.
  6. Keyboard Navigation:
    • Users should be able to navigate the stars using the Left and Right arrow keys to adjust the rating by 0.5 increments (if allowHalfStars is true) or 1 increment (if allowHalfStars is false).
    • Pressing Enter or Space should confirm the currently previewed rating, similar to a click.
    • Pressing Escape should clear the rating (set it to 0).
  7. Accessibility: Ensure the component is accessible for screen readers and keyboard users by using appropriate ARIA attributes.

UI/UX expectations

  • Visual Representation: Stars should clearly indicate their state: empty, half-filled, and fully-filled. Use distinct colors or icons (e.g., Unicode characters, SVGs).
  • Hover Feedback: A smooth visual change should occur when hovering over stars to indicate the previewed rating.
  • Click Feedback: The selected rating should remain visually distinct from unselected stars.
  • Layout: Stars should be arranged horizontally.
  • Focus Indicator: Ensure a visible focus indicator for keyboard navigation on the component container.

Acceptance criteria

  • The StarRating component renders correctly with a default number of stars.
  • The initialRating prop correctly sets the initial visual state.
  • Hovering over stars accurately previews full and half-star ratings.
  • Clicking selects the rating, including half-stars, and updates the visual display.
  • Clicking an already selected full star clears the rating; half-star clearing logic follows the specification.
  • The onChange callback is triggered with the correct rating value on selection or keyboard interaction.
  • Keyboard navigation (Left/Right arrows for increments, Enter/Space for selection, Escape for clear) functions as specified.
  • The component uses appropriate ARIA attributes (role, aria-valuenow, aria-valuemin, aria-valuemax, aria-label) for accessibility.
  • The component is reusable and its internal state management is clean.

Constraints

Constraints & assumptions

  • You should use React for component development.
  • No external UI libraries (e.g., Material UI, Ant Design) are allowed; all UI components must be built from scratch. Icon libraries are acceptable for star visuals if desired, but simple SVG or character-based stars are fine.
  • The component should be purely client-side; no backend integration is required.
  • The totalStars prop will be an integer between 1 and 10.
  • The rating value should be a number (float for half-stars).
  • Assume a modern browser environment.

Out of scope

  • Complex animations beyond basic CSS transitions.
  • Server-side rendering considerations.
  • Persistence of ratings (e.g., saving to a database).
  • Validation of initialRating beyond basic type checking.

What interviewers evaluate

  • Component Design & Modularity: How well the StarRating component is structured, its reusability, and separation of concerns.
  • State Management & Event Handling: Correct and efficient management of component state (selected rating, hover state) and handling of various user interactions (mouse events, keyboard events).
  • Accessibility: Implementation of appropriate ARIA roles and attributes, and robust keyboard navigation.
  • Code Quality: Readability, maintainability, and adherence to React best practices.

Loading editor...