Frontend School

Build a Customizable Hex Color Picker Component

Machine Coding
medium

Build a Customizable Hex Color Picker Component

Medium
UI
React
Component
State Management
Color Picker
Frontend

Overview

At Lenskart, precise color management is crucial for our digital product experiences, from customizing frame colors in our virtual try-on to defining thematic elements across our applications. We need a robust, reusable ColorPicker component that allows designers and product managers to easily select and input HEX color codes. This component will be integrated into various configuration interfaces, ensuring brand consistency and enabling dynamic visual adjustments.

What you need to do / Task

Your primary task is to develop a ColorPicker React component with the following capabilities:

  1. Display Current Color: Show a visual swatch and the HEX code of the currently selected color.
  2. Predefined Color Palette: Render a set of predefined color swatches that users can click to select a color.
  3. Custom HEX Input: Provide an input field where users can directly type a custom HEX color code (e.g., #RRGGBB). This input should update the displayed color.
  4. Component Reusability: The component should accept defaultColor as a prop for initial state and onColorChange as a callback prop to inform the parent component of color changes.

UI/UX expectations

  • Layout: The component should clearly display the selected color at the top, followed by the predefined color palette, and then the custom HEX input field. A simple vertical stack or a clean horizontal arrangement is acceptable.
  • Selected Color Display: A prominent square or circle swatch displaying the backgroundColor of the selected color, along with its full HEX code (e.g., #FF0000).
  • Palette Interaction: Each predefined color swatch should be clickable. Upon selection, it should update the main display and provide visual feedback (e.g., a border or highlight) indicating it's the currently active color.
  • HEX Input: The input field should display the current HEX code. It should accept valid HEX codes and update the displayed color instantly. Basic visual feedback for invalid input (e.g., red border) is a plus but not strictly required.
  • Accessibility: Ensure basic keyboard navigation for the color palette (e.g., tab focus) and clear focus states for interactive elements.

Acceptance criteria

  • The ColorPicker component renders correctly with a defaultColor if provided, or a fallback if not.
  • Clicking any color in the predefined palette updates the main display swatch and HEX code.
  • Typing a valid 6-digit HEX code (with or without # prefix) into the input field updates the main display swatch and HEX code.
  • The onColorChange prop is called with the new HEX color whenever the color is changed via palette selection or custom input.
  • The currently selected color from the palette (if any) is visually highlighted.
  • The component's overall layout is clean and intuitive.

Constraints

Constraints & assumptions

  • Technology Stack: Use React (functional components and hooks) and TypeScript.
  • Color Format: Only HEX color codes (e.g., #RRGGBB or RRGGBB) need to be handled. No RGB, HSL, or other formats.
  • Palette Data: The predefined color palette can be a static array within the component or passed as a prop (static array is sufficient for this exercise).
  • Styling: Use plain CSS or CSS Modules. No styled-components or TailwindCSS is required.
  • Time Budget: This problem is designed to be completable within approximately 45 minutes.

Out of scope

  • Advanced color pickers (e.g., HSL/HSV color wheels, alpha channel support, gradients).
  • Extensive error handling for malformed or invalid HEX inputs beyond basic validation (e.g., checking length and valid characters).
  • Persistence of selected colors across sessions or complex state management libraries (Redux, Zustand).
  • Backend integration or API calls.

What interviewers evaluate

  • Component Design & Reusability: How well the ColorPicker component is structured, its API (props), and its reusability.
  • React State Management: Effective and idiomatic use of useState and useEffect for managing component state and side effects.
  • UI/UX Implementation: Accuracy of the visual output, responsiveness to user interactions, and adherence to basic UI/UX expectations.
  • Code Quality & Readability: Cleanliness, maintainability, and clarity of the code, including appropriate TypeScript usage.

Loading editor...