Frontend School

Interactive Color Picker Component

Machine Coding
medium

Interactive Color Picker Component

Medium
React
Frontend
UI
Color Picker
State Management
css-gradients

Overview

LinkedIn often allows users to customize their profiles, company pages, or even theme settings within the platform. A robust and intuitive color picker is essential for providing a rich customization experience. This problem challenges you to build a core color picker component, focusing on interactive UI, state management, and accurate color representation.

What you need to do / Task

Your task is to implement a reusable ColorPicker React component. It should allow users to select a color using a combination of a 2D saturation/value grid, a hue slider, and a hex code input field. The component should accept an initial color (hex string) and provide a callback for when the color changes.

  1. Implement the ColorArea (Saturation & Value Picker): Create a square area where users can pick saturation (horizontal axis) and value (vertical axis). The background of this area should dynamically update its hue based on the currently selected hue from the slider.
  2. Implement the HueSlider: Create a horizontal slider to control the hue of the color. Dragging the slider's handle should update the hue and, consequently, the background of the ColorArea.
  3. Implement a Hex Input Field: Display the currently selected color as a 6-digit hexadecimal string (e.g., #RRGGBB). The user should also be able to type a valid hex code into this input field to update the color picker's state and handles.
  4. Synchronization: Ensure all parts of the picker (ColorArea, HueSlider, and Hex input) are fully synchronized. Any interaction with one element should immediately reflect in the others.
  5. Controlled Component: The ColorPicker component should be a controlled component, accepting a value prop (hex string) and an onChange callback that is triggered with the new hex value whenever the color changes.

UI/UX expectations

  • Layout: Arrange the ColorArea, HueSlider, and Hex input field in a clear and intuitive vertical stack within a main container.
  • Interactions:
    • Dragging a handle within the ColorArea should smoothly update the selected color and the hex input.
    • Dragging a handle on the HueSlider should smoothly update the color area's background, the selected color, and the hex input.
    • Clicking anywhere within the ColorArea or HueSlider should set the color to that specific point.
    • Typing a valid 6-digit hex code (e.g., #FF0000 or FF0000) into the input field should update the picker's handles and the displayed color. Invalid input should not break the component and ideally revert to the last valid state or be ignored until valid.
  • Accessibility: Consider basic accessibility for interactive elements (e.g., ARIA roles for sliders, focus management). While full keyboard navigation for all elements is not strictly required within the time limit, awareness is a plus.

Acceptance criteria

  • The ColorPicker component renders correctly with an initial hex value provided via props.
  • Dragging the handle in the ColorArea accurately updates the selected color and the hex input field.
  • Dragging the handle on the HueSlider accurately updates the selected color and the hex input field.
  • Typing a valid hex code into the input field correctly updates the picker's internal state and all visual elements.
  • The onChange callback is reliably triggered with the new hex value whenever the color is changed through any interaction.
  • The ColorArea's background gradient correctly reflects the current hue, providing a smooth visual experience.
  • The overall user interface is functional, responsive to user interactions, and visually coherent.

Constraints

Constraints & assumptions

  • You must use React functional components and hooks.
  • Do not use external color picker libraries (e.g., react-colorful). Implement the core logic yourself.
  • Focus on modern browser capabilities (CSS gradients, MouseEvent APIs).
  • The internal color model should be HSV (Hue, Saturation, Value), with conversions to and from Hex for the public API. Helper conversion functions are provided in the starter code.
  • The problem focuses on core logic and UI interaction; extensive input validation for all possible malformed hex strings is not the primary focus (assume valid 6-digit hex codes for input).

Out of scope

  • Alpha channel (transparency) support.
  • Pre-defined color palettes or custom color swatches.
  • Full, comprehensive accessibility implementation for all edge cases.
  • Advanced error handling beyond basic input validation.

What interviewers evaluate

  • Component Design: Modularity, separation of concerns, and reusability of the ColorPicker component.
  • State Management: Effective use of React hooks (useState, useRef, useCallback, useEffect) to manage component state, ensure synchronization, and minimize unnecessary re-renders.
  • Interactive UI: Correct implementation of mouse event handling (mousedown, mousemove, mouseup) for dragging, ensuring a smooth and responsive user experience.
  • Color Logic: Accurate HSV to RGB to Hex color conversions and vice-versa, ensuring all UI elements synchronize correctly.
  • Code Quality: Readability, maintainability, and clarity of the code, reflecting a production-grade component.

Loading editor...